Last active
June 22, 2020 06:14
-
-
Save maskaravivek/722560f451c7870abe302aa84f78dd67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow import keras | |
import keras.backend as K | |
class YoloReshape(tf.keras.layers.Layer): | |
def __init__(self, target_shape): | |
super(YoloReshape, self).__init__() | |
self.target_shape = tuple(target_shape) | |
def get_config(self): | |
config = super().get_config().copy() | |
config.update({ | |
'target_shape': self.target_shape | |
}) | |
return config | |
def call(self, input): | |
# grids 7x7 | |
S = [self.target_shape[0], self.target_shape[1]] | |
# classes | |
C = 20 | |
# no of bounding boxes per grid | |
B = 2 | |
idx1 = S[0] * S[1] * C | |
idx2 = idx1 + S[0] * S[1] * B | |
# class probabilities | |
class_probs = K.reshape(input[:, :idx1], (K.shape(input)[0],) + tuple([S[0], S[1], C])) | |
class_probs = K.softmax(class_probs) | |
#confidence | |
confs = K.reshape(input[:, idx1:idx2], (K.shape(input)[0],) + tuple([S[0], S[1], B])) | |
confs = K.sigmoid(confs) | |
# boxes | |
boxes = K.reshape(input[:, idx2:], (K.shape(input)[0],) + tuple([S[0], S[1], B * 4])) | |
boxes = K.sigmoid(boxes) | |
outputs = K.concatenate([class_probs, confs, boxes]) | |
return outputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment