Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YOLOv8] Step 0/? Addition of SPP and SPPF Layer #1479

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions keras_cv/models/object_detection/yolov8/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
13 changes: 13 additions & 0 deletions keras_cv/models/object_detection/yolov8/__internal__/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 The KerasCV Authors
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from keras_cv.models.object_detection.yolov8.__internal__.layers.sppf import SPP
from keras_cv.models.object_detection.yolov8.__internal__.layers.sppf import (
SPPF,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
from tensorflow.keras import layers


@tf.keras.utils.register_keras_serializable(package="keras_cv")
class SPP(layers.Layer):
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
"""
Performs Spatial Pyramid Pooling.
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self, filters, kernels=(5, 9, 13), **kwargs):
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(**kwargs)
self.filters = filters
self.kernels = kernels
self.num_filters = filters // 2
self.conv1 = layers.Conv2D(
filters=self.num_filters, kernel_size=1, strides=1
)
self.conv2 = layers.Conv2D(
filters=self.filters, kernel_size=1, strides=1
)
self.modules = [
layers.MaxPool2D(pool_size=x, strides=1, padding="SAME")
for x in self.kernels
]

def call(self, x):
x = self.conv1(x)
return self.conv2(
tf.concat([x] + [module(x) for module in self.modules], axis=-1)
)

def get_config(self):
config = {
"filters": self.filters,
"kernels": self.kernels,
"num_filters": self.num_filters,
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))


@tf.keras.utils.register_keras_serializable(package="keras_cv")
class SPPF(layers.Layer):
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
"""
Performs Spatial Pyramid Pooling.
"""
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
def __init__(self, filters, kernel=5, **kwargs):
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(**kwargs)
self.filters = filters
self.kernel = kernel
self.num_filters = filters // 2
self.conv1 = layers.Conv2D(
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
filters=self.num_filters, kernel_size=1, strides=1
)
self.conv2 = layers.Conv2D(
filters=self.filters, kernel_size=1, strides=1
)
self.module = layers.MaxPool2D(
IMvision12 marked this conversation as resolved.
Show resolved Hide resolved
pool_size=self.kernel, strides=1, padding="SAME"
)

def call(self, x):
x = self.conv1(x)
x1 = self.module(x)
x2 = self.module(x1)
return self.conv2(tf.concat([x, x1, x2, self.module(x2)], axis=-1))

def get_config(self):
config = {
"filters": self.filters,
"kernel": self.kernel,
"num_filters": self.num_filters,
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
13 changes: 13 additions & 0 deletions keras_cv/models/object_detection/yolov8/yolov8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.