def __build_nework(self, input_data): """ It is better look at this function with the Yolo_v3_Structure diagram made by Levio One link is here: https://github.com/wizyoung/YOLOv3_TensorFlow """ # by YZ, these are the three feature extractions from darknet53, with difference dimension reductions (by 8, 16, 32) route_1, route_2, input_data = backbone.darknet53(input_data, self.trainable) # input_data is -1*13*13*1024 input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') # upsampling input data (1/32) to match route_2 (1/16), -1*26*16*512 input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): # route_2 is -1*26*26*256, so input_data is -1*26*26*768 input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch' ) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) # dimensions are: -1*13*13*255, -1*26*26*255, -1*52*52*255 return conv_lbbox, conv_mbbox, conv_sbbox
def __build_nework(self, input_layer, training): route_1, route_2, conv = backbone.darknet53(input_layer, training) conv = common.convolutional(conv, (1, 1, 1024, 512), training) conv = common.convolutional(conv, (3, 3, 512, 1024), training) conv = common.convolutional(conv, (1, 1, 1024, 512), training) conv = common.convolutional(conv, (3, 3, 512, 1024), training) conv = common.convolutional(conv, (1, 1, 1024, 512), training) conv_lobj_branch = common.convolutional(conv, (3, 3, 512, 1024), training) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (80 + 5)), training, activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 512, 256), training) conv = common.upsample(conv) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 768, 256), training) conv = common.convolutional(conv, (3, 3, 256, 512), training) conv = common.convolutional(conv, (1, 1, 512, 256), training) conv = common.convolutional(conv, (3, 3, 256, 512), training) conv = common.convolutional(conv, (1, 1, 512, 256), training) conv_mobj_branch = common.convolutional(conv, (3, 3, 256, 512), training) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (80 + 5)), training, activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 256, 128), training) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) conv = common.convolutional(conv, (1, 1, 384, 128), training) conv = common.convolutional(conv, (3, 3, 128, 256), training) conv = common.convolutional(conv, (1, 1, 256, 128), training) conv = common.convolutional(conv, (3, 3, 128, 256), training) conv = common.convolutional(conv, (1, 1, 256, 128), training) conv_sobj_branch = common.convolutional(conv, (3, 3, 128, 256), training) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (80 + 5)), training, activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def YOLOv4(input_layer, NUM_CLASS): route_1, route_2, conv = backbone.cspdarknet53(input_layer) route = conv conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.upsample(conv) route_2 = common.convolutional(route_2, (1, 1, 512, 256)) conv = tf.concat([route_2, conv], axis=-1) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) route_2 = conv conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) route_1 = common.convolutional(route_1, (1, 1, 256, 128)) conv = tf.concat([route_1, conv], axis=-1) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) route_1 = conv conv = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(route_1, (3, 3, 128, 256), downsample=True) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) route_2 = conv conv = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(route_2, (3, 3, 256, 512), downsample=True) conv = tf.concat([conv, route], axis=-1) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) return [conv_sbbox, conv_mbbox, conv_lbbox]
def YOLOv3(for_annother_use=False): input_layer = tf.keras.layers.Input(shape=[416,416,3]) route_1, route_2, conv = backbone.darknet53(input_layer) conv = common.convolutional(conv, (1, 1, 1024, 512)) sconv1 = conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) sconv2 = conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) sconv3 = conv_lobj_branch = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.upsample(conv) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 768, 256)) mconv1 = conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) mconv2 = conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) mconv3 = conv_mobj_branch = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) conv = common.convolutional(conv, (1, 1, 384, 128)) lconv1 = conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) lconv2 = conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) lconv3 = conv_sobj_branch = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) if for_annother_use: convs_sbbox = tf.concat([lconv1, lconv2, lconv3], axis=-1, name="lfeature") convs_mbbox = tf.concat([mconv1, mconv2, mconv3], axis=-1, name="mfeature") convs_lbbox = tf.concat([sconv1, sconv2, sconv3], axis=-1, name="sfeature") yolo = Model(input_layer,[conv_sbbox, conv_mbbox, conv_lbbox, convs_sbbox,convs_mbbox,convs_lbbox]) # 52,26,13 52,26,13 yolo.load_weights(filepath=tf.keras.utils.get_file('yolo3.h5','')) yolo.trainable = False else: # classic YOLOV3 model trained for coco datasets yolo = Model(input_layer, [conv_sbbox, conv_mbbox, conv_lbbox]) yolo.load_weights(filepath=tf.keras.utils.get_file('yolo3.h5','')) yolo.trainable =False return yolo
def __build_nework(self, input_data): route_1, route_2, input_data = backbone.darknet53(input_data, self.trainable) # input_data is -1*13*13*1024 input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') # upsampling input data (1/32) to match route_2 (1/16), -1*26*16*512 input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): # route_2 is -1*26*26*256, so input_data is -1*26*26*768 input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch' ) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3*(self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) # dimensions are: -1*13*13*255, -1*26*26*255, -1*52*52*255 return conv_lbbox, conv_mbbox, conv_sbbox
def YOLOv3(input_layer): """ build the whole network of YOLOv3 :param input_layer: [batch, 416, 416, 3] :return: 3 scale predictions [[b, 52, 52, anchors*(5+classes)], [b, 26, 26, anchors*(5+classes)], [b, 13, 13, anchors*(5+classes)]] """ route_1, route_2, conv = backbone.darknet53( input_layer ) # call darknet to get the basic network, [52,52,256], [26,26,512], [13,13,1024] """large scale output [batch, 13, 13, 3*(5+c)]""" conv = common.convolutional(conv, (1, 1, 1024, 512), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 512, 1024), conv_trainable=False) conv = common.convolutional(conv, (1, 1, 1024, 512), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv_lobj_branch = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) """the medium scale output, [batch, 26, 26, 3*(NUM_CLASS+5)]""" conv = common.convolutional(conv, (1, 1, 512, 256), conv_trainable=False) conv = common.upsample(conv) conv = tf.concat([conv, route_2], axis=-1) # concat in channel dimension conv = common.convolutional(conv, (1, 1, 768, 256), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 256, 512), conv_trainable=False) conv = common.convolutional(conv, (1, 1, 512, 256), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv_mobj_branch = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) """the small scale output, [batch, 52, 52, 3*(NUM_CLASS+5)]""" conv = common.convolutional(conv, (1, 1, 256, 128), conv_trainable=False) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) # concat in channel dimension conv = common.convolutional(conv, (1, 1, 384, 128), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 128, 256), conv_trainable=False) conv = common.convolutional(conv, (1, 1, 256, 128), conv_trainable=False) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv_sobj_branch = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) return [conv_sbbox, conv_mbbox, conv_lbbox]
def YOLOv3(input_layer): route_1, route_2, conv = backbone.darknet53(input_layer) original_conv = conv conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv_lobj_branch = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.upsample(conv) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 768, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv_mobj_branch = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) conv = common.convolutional(conv, (1, 1, 384, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv_sobj_branch = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) return [conv_sbbox, conv_mbbox, conv_lbbox], (route_1, route_2, original_conv)
def __build_nework(self, input_data): route_1, route_2, input_data = backbone.darknet53(input_data) input_data = common.convolutional(input_data, (1, 1, 1024, 512)) input_data = common.convolutional(input_data, (3, 3, 512, 1024)) input_data = common.convolutional(input_data, (1, 1, 1024, 512)) input_data = common.convolutional(input_data, (3, 3, 512, 1024)) input_data = common.convolutional(input_data, (1, 1, 1024, 512)) conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (80 + 5)), activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256)) input_data = common.upsample(input_data) input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256)) input_data = common.convolutional(input_data, (3, 3, 256, 512)) input_data = common.convolutional(input_data, (1, 1, 512, 256)) input_data = common.convolutional(input_data, (3, 3, 256, 512)) input_data = common.convolutional(input_data, (1, 1, 512, 256)) conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (80 + 5)), activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128)) input_data = common.upsample(input_data) input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128)) input_data = common.convolutional(input_data, (3, 3, 128, 256)) input_data = common.convolutional(input_data, (1, 1, 256, 128)) input_data = common.convolutional(input_data, (3, 3, 128, 256)) input_data = common.convolutional(input_data, (1, 1, 256, 128)) conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (80 + 5)), activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def __build_nework(self, backbone): route_1 = backbone.od_layer1 route_2 = backbone.od_layer2 input_data = backbone.od_layer4 input_data = common.convolutional(input_data, (1, 1, 512, 512), self.trainable, 'od_conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'od_conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'od_conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'od_conv55') input_data = common.convolutional(input_data, (3, 3, 1024, 512), self.trainable, 'od_conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='od_conv_lobj_branch') conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3*(self.num_class + 5)), trainable=self.trainable, name='od_conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'od_conv57') input_data = common.upsample(input_data, name='od_upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'od_conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'od_conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'od_conv60') input_data = common.convolutional(input_data, (1, 1, 256, 512), self.trainable, 'od_conv61') input_data = common.convolutional(input_data, (3, 3, 512, 256), self.trainable, 'od_conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='od_conv_mobj_branch' ) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3*(self.num_class + 5)), trainable=self.trainable, name='od_conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'od_conv63') input_data = common.upsample(input_data, name='od_upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'od_conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'od_conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'od_conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'od_conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'od_conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='od_conv_sobj_branch') conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3*(self.num_class + 5)), trainable=self.trainable, name='od_conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def __build_nework(self, input_data): route_1, route_2, input_data = backbone.darknet53(input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, self.anchor_per_scale*(self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch' ) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, self.anchor_per_scale*(self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, self.anchor_per_scale*(self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def upsample_2_wrapper(route_1, input_data): input_data = common.upsample(input_data, name="upsample1", method=self.upsample_method, precision=self.precision) with tf.variable_scope("route_2"): input_data = tf.concat([input_data, route_1], axis=-1) return {"input_data": input_data}
def __build_nework(self, input_data): route_8_layer, route_13_layer, input_data = backbone.darknet19( input_data, self.trainable) with tf.variable_scope('yolo1'): conv_lobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) with tf.variable_scope('route_1'): input_data = route_13_layer input_data = common.convolutional(input_data, filters_shape=(3, 3, 256, 128), trainable=self.trainable, name='conv18') input_data = common.upsample(input_data, method=self.upsample_method, name='upsample19') with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_8_layer], axis=-1, name='route20') with tf.variable_scope('yolo2'): conv_mobj_branch = common.convolutional(input_data, filters_shape=(1, 1, 384, 256), trainable=self.trainable, name='conv21') conv_mbbox = common.convolutional( conv_mobj_branch, filters_shape=(1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv22', activate=False, bn=False) return conv_lbbox, conv_mbbox
def YOLOv3_tiny(input_layer, NUM_CLASS): route_1, conv = backbone.darknet53_tiny(input_layer) conv = common.convolutional(conv, (1, 1, 1024, 256)) conv_lobj_branch = common.convolutional(conv, (3, 3, 256, 512)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) conv_mobj_branch = common.convolutional(conv, (3, 3, 128, 256)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) return [conv_mbbox, conv_lbbox]
def __build_nework(self, input_data): # backbone input_data = common.convolutional(input_data, filters_shape=(3, 3, 3, 16), self.trainable, name='conv0') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 16, 32), self.trainable, name='conv1') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 32, 64), self.trainable, name='conv2') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 64, 128), self.trainable, name='conv3') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 128, 256), self.trainable, name='conv4') route = input_data input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 256, 512), self.trainable, name='conv5') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, filters_shape=(3, 3, 512, 1024), self.trainable, name='conv6') # end input_data = common.convolutional(input_data, filters_shape=(1, 1, 1024, 256), self.trainable, name='conv7') conv_lobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv8') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route'): input_data = tf.concat([input_data, route], axis=-1) conv_mobj_branch = common.convolutional(input_data, (3, 3, 384, 256), self.trainable, name='conv_mobj_branch' ) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox
def YOLOV3_TINY(input_layer): #call darknet_tiny route_1, route_2 = backbone.darknet_tiny(input_data=input_layer) #call yolov3_tiny_block route_3, mobj_branch = YOLOV3_TINY_block(route_2, 256) #get feature map for 13x13x512 tensor conv_mbbox = common.conv2d(mobj_branch, filter_shape=(1, 1, 512, 3 * (NUM_CLASS + 5)), activativation=False, Bn=False) route_3 = common.conv2d(input_layer=route_3, filter_shape=(1, 1, 256, 128)) route_3 = common.upsample(input_layer=route_3) sobj_branch = tf.concat([route_3, route_1], axis=-1) conv_sbbox = common.conv2d(sobj_branch, filter_shape=(1, 1, 256, 3 * (NUM_CLASS + 5)), activativation=False, Bn=False) return conv_sbbox, conv_mbbox
def test_upsample(): input_value = np.random.random((2, 224, 224, 3)).astype(np.float16) # nearest_neighborhood function that run on cpu: input_data = tf.constant(input_value) input_shape = tf.shape(input_data) upsample_cpu = tf.image.resize_nearest_neighbor( input_data, (input_shape[1] * 2, input_shape[2] * 2)) with tf.Session() as sess: cpu_output = sess.run(upsample_cpu) tf.reset_default_graph() # upsample that run on IPU: config = ipu.config.IPUConfig() config.auto_select_ipus = 1 config.configure_ipu_system() with ipu.scopes.ipu_scope('/device:IPU:0'): input_data = tf.constant(input_value) upsample_ipu = upsample(input_data, "test_upsmaple", method="resize") with tf.Session(config=tf.ConfigProto()) as sess: ipu_output = sess.run(upsample_ipu) assert np.max(cpu_output - ipu_output) <= 0.0001
def __build_nework(self, input_data): """ :param input_data: tf.placeholder(dtype=tf.float32, name='input_data') :return: conv_lbbox [batch, 52, 52, 3*(self.num_class + 5)], conv_mbbox [batch, 26, 26, 3*(self.num_class + 5)], conv_sbbox [batch, 13, 13, 3*(self.num_class + 5)] """ route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox # 13,26,52
def __build_nework(self, input_data): # route1 2 3 input data 分别为用于后面concat的值 即 128残差后用于最大特征图concat的 route1 以此类推 route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) # 经过全部残差的 output 先经过五次DBL操作 input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) # 返回三种大小的检测框 13*13*255 26*26 52*52 return conv_lbbox, conv_mbbox, conv_sbbox
def __build_nework(self, input_data): # mobilenet v1 backbone # input_data = tf.layers.conv2d(input_data, # filters=32, # kernel_size=(3, 3), # strides=(2,2), # padding = 'same', # activation = tf.nn.relu, # name = 'conv1' # ) # input_data = tf.layers.batch_normalization(input_data, beta_initializer=tf.zeros_initializer(), # gamma_initializer=tf.ones_initializer(), # moving_mean_initializer=tf.zeros_initializer(), # moving_variance_initializer=tf.ones_initializer(), training=self.trainable, # name='bn') ''' input_data = common.convolutional(input_data, (3, 3, 3, 32), self.trainable, 'conv0') input_data = self.separable_conv_block(input=input_data, dw_filter=(3,3,32,1),output_channel=64, strides=(1,1,1,1), name="spearable_1") input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 64, 1), output_channel=128, strides=(1, 2, 2, 1), name="spearable_2") input_data = self.separable_conv_block(input=input_data, dw_filter=(3,3,128,1),output_channel=128, strides=(1,1,1,1), name="spearable_3") input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 128, 1), output_channel=256, strides=(1, 2, 2, 1), name="spearable_4") input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 256, 1), output_channel=256, strides=(1, 1, 1, 1), name="spearable_5") with tf.variable_scope("spearable_6"): dw_weight = tf.get_variable(name='dw_filter', dtype=tf.float32, trainable=True, shape=(3, 3, 256, 1), initializer=tf.random_normal_initializer(stddev=0.01)) input_data = tf.nn.depthwise_conv2d(input=input_data, filter=dw_weight, strides=(1, 2, 2, 1), padding="SAME", name='Conv/dw') input_data = tf.layers.batch_normalization(input_data, beta_initializer=tf.zeros_initializer(), gamma_initializer=tf.ones_initializer(), moving_mean_initializer=tf.zeros_initializer(), moving_variance_initializer=tf.ones_initializer(), training=self.trainable, name='dw/bn') route = tf.nn.leaky_relu(input_data,0.1) weight = tf.get_variable(name='weight', dtype=tf.float32, trainable=True, shape=(1, 1, 256, 512), initializer=tf.random_normal_initializer(stddev=0.01)) input_data = tf.nn.conv2d(input=route, filter=weight, strides=[1, 1, 1, 1], padding="SAME",name="conv/s1") input_data = tf.layers.batch_normalization(input_data, beta_initializer=tf.zeros_initializer(), gamma_initializer=tf.ones_initializer(), moving_mean_initializer=tf.zeros_initializer(), moving_variance_initializer=tf.ones_initializer(), training=self.trainable, name='pt/bn') input_data = tf.nn.leaky_relu(input_data,0.1) for i in range(5): input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 512, 1), output_channel=512, strides=(1, 1, 1, 1), name="spearable_%d" % (i+ 7)) input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 512, 1), output_channel=1024, strides=(1, 2, 2, 1), name="spearable_12") input_data = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 1024, 1), output_channel=1024, strides=(1, 1, 1, 1), name="spearable_13") ''' # mobilenet backbone end # yolo tiny backbone input_data = common.convolutional(input_data, (3, 3, 3, 16), self.trainable, 'conv0') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 16, 32), self.trainable, 'conv1') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 32, 64), self.trainable, 'conv2') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 64, 128), self.trainable, 'conv3') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv4') route = input_data input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv5') input_data = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv6') # end # yolo detect layer input_data = common.convolutional(input_data, (1, 1, 1024, 256), self.trainable, 'conv7') conv_lobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), self.trainable, 'conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv8') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route], axis=-1) conv_mobj_branch = common.convolutional(input_data, (3, 3, 384, 256), self.trainable, 'conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), self.trainable, 'conv_mbbox', activate=False, bn=False) # yolo detect layer end # yolo detect layer DSC ''' input_data = self.separable_conv_block(input=input_data, dw_filter=(1, 1, 1024, 1),output_channel=256, strides=(1,1,1,1), name="conv7") conv_lobj_branch = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 256, 1),output_channel=512, strides=(1,1,1,1), name="conv_lobj_branch") conv_lbbox = self.separable_conv_block(input=conv_lobj_branch, dw_filter=(1, 1, 512, 1),output_channel=3 * (self.num_class + 5), strides=(1,1,1,1), name="conv_lbbox") input_data = self.separable_conv_block(input=input_data, dw_filter=(1, 1, 256, 1),output_channel=128, strides=(1,1,1,1), name="conv8") input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route], axis=-1) conv_mobj_branch = self.separable_conv_block(input=input_data, dw_filter=(3, 3, 384, 1),output_channel=256, strides=(1,1,1,1), name="conv_mobj_branch") conv_mbbox = self.separable_conv_block(input=conv_mobj_branch, dw_filter=(1, 1, 256, 1),output_channel=3 * (self.num_class + 5), strides=(1,1,1,1), name="conv_mbbox") ''' return conv_lbbox, conv_mbbox
def __build_nework(self, input_data): route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) conv_lobj_branch = common.EP(input_data, 189, 462, name='EP5', stride=1) conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 462, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 189, 105), self.trainable, 'conv5') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.PEP(input_data, 430, 325, 113, name='PEP17', stride=1) input_data = common.PEP(input_data, 325, 207, 99, name='PEP18', stride=1) input_data = common.convolutional(input_data, (1, 1, 207, 98), self.trainable, 'conv6') conv_mobj_branch = common.EP(input_data, 98, 183, name='EP6', stride=1) conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 183, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 98, 47), self.trainable, 'conv7') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.PEP(input_data, 197, 122, 58, name='PEP19', stride=1) input_data = common.PEP(input_data, 122, 87, 52, name='PEP20', stride=1) conv_sobj_branch = common.PEP(input_data, 87, 93, 47, name='PEP21', stride=1) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 93, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def __build_tiny_3l(self, input_data): input_data = input_data / 255. input_data = common.convolutional(input_data, filters_shape=(3, 3, 3, 16), trainable=self.trainable, name='conv0') input_data = tf.nn.max_pool(input_data, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME', name='max_pool0') for i in range(5): input_data = common.convolutional( input_data, filters_shape=(3, 3, 16 * pow(2, i), 16 * pow(2, i + 1)), trainable=self.trainable, name='conv{}'.format(i + 1)) if i == 2: route_0 = input_data if i == 3: route_1 = input_data if i == 4: input_data = tf.nn.max_pool(input_data, ksize=(1, 2, 2, 1), strides=(1, 1, 1, 1), padding='SAME', name='max_pool{}'.format(i + 1)) else: input_data = tf.nn.max_pool(input_data, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME', name='max_pool{}'.format(i + 1)) input_data = common.convolutional(input_data, filters_shape=(3, 3, 512, 1024), trainable=self.trainable, name='conv6') input_data = common.convolutional(input_data, filters_shape=(1, 1, 1024, 256), trainable=self.trainable, name='conv7') conv_lobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, filters_shape=(1, 1, 256, 128), trainable=self.trainable, name='conv8') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_1], axis=-1) conv_mobj_branch = common.convolutional(input_data, (3, 3, 384, 256), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(conv_mobj_branch, filters_shape=(1, 1, 256, 128), trainable=self.trainable, name='conv9') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_0'): input_data = tf.concat([input_data, route_0], axis=-1) conv_sobj_branch = common.convolutional(input_data, (3, 3, 256, 128), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 128, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox
def YOLOv5(input_layer, NUM_CLASS, scale_v5): V5_STR = utils.get_V5STR(scale_v5) print("YOLOv5 structure parameters:",V5_STR) N_start_next, route_1, route_2, route_3, G1, G2 = backbone.Focus_cspdarknet53(input_layer,V5_STR) ###Detection branch### G3, G4 = route_1, route_2 route_3 = common.convolutional(route_3, (1, 1, V5_STR[9][0], V5_STR[10][0]),activate_type="SiLU",name=[0,N_start_next,N_start_next]) R1 = route_3 route_3 = common.upsample(route_3,activate_type="SiLU",name=[0,N_start_next+1,N_start_next+1]) route_3 = tf.concat([route_2, route_3], axis=-1) route_3 = common.C3(route_3, V5_STR[10][0]*2, V5_STR[11][0], V5_STR[11][1], cut1=False,C3_name=[0,N_start_next+2]) N_start_next = N_start_next+5+2*V5_STR[11][1] route_3 = common.convolutional(route_3, (1, 1, V5_STR[11][0], V5_STR[12][0]),activate_type="SiLU", name=[0,N_start_next,N_start_next]) R2 = route_3 route_3 = common.upsample(route_3,activate_type="SiLU", name=[0,N_start_next+1,N_start_next+1]) route_3 = tf.concat([route_1, route_3], axis=-1) route_3 = common.C3(route_3, V5_STR[12][0]*2, V5_STR[13][0], V5_STR[13][1], cut1=False,C3_name=[0,N_start_next+2]) N_start_next = N_start_next+5+2*V5_STR[13][1] conv_sbbox = common.convolutional(route_3, (1, 1, V5_STR[13][0], 3 * (NUM_CLASS + 5)), activate=False, bn=False, name=[0,N_start_next,N_start_next]) route_3 = common.convolutional(route_3, (3, 3, V5_STR[13][0], V5_STR[14][0]), downsample=True, activate_type="SiLU", name=[0,N_start_next+1,N_start_next+1]) route_3 = tf.concat([R2, route_3], axis=-1) route_3 = common.C3(route_3, V5_STR[14][0]*2, V5_STR[15][0], V5_STR[15][1], cut1=False,C3_name=[0,N_start_next+2]) N_start_next = N_start_next+5+2*V5_STR[15][1] conv_mbbox = common.convolutional(route_3, (1, 1, V5_STR[15][0], 3 * (NUM_CLASS + 5)), activate=False, bn=False, name=[0,N_start_next,N_start_next]) route_3 = common.convolutional(route_3, (3, 3, V5_STR[15][0], V5_STR[16][0]), downsample=True, activate_type="SiLU", name=[0,N_start_next+1,N_start_next+1]) route_3 = tf.concat([R1, route_3], axis=-1) route_3 = common.C3(route_3, V5_STR[16][0]*2, V5_STR[17][0],V5_STR[17][1], cut1=False, C3_name=[0,N_start_next+2]) N_start_next = N_start_next+5+2*V5_STR[17][1] conv_lbbox = common.convolutional(route_3, (1, 1, V5_STR[17][0], 3 * (NUM_CLASS + 5)), activate=False, bn=False,name=[0,N_start_next,N_start_next]) ###Deblurring branch### out2 = common.convolutional(G2, (1, 1, 128, 128), activate=False, bn=False, name=[1,N_start_next+1,N_start_next+1]) out2 = common.CSP_CBAM(out2, activate_type1="SiLU", CB_name=[1,N_start_next+2]) N_start_next = N_start_next+12 out3 = common.convolutional(G3, (1, 1, 256, 128), activate=False, bn=False, name=[1,N_start_next,N_start_next]) out3 = common.CSP_CBAM(out3, activate_type1="SiLU", CB_name=[1,N_start_next+1]) N_start_next = N_start_next+11 out2 = tf.concat([common.upsample(out3,activate_type="SiLU",name=[1,N_start_next,N_start_next]), out2], axis=-1) out2 = common.convolutional(common.upsample(out2,activate_type="SiLU",name=[1,N_start_next+1,N_start_next+1]), (3, 3, 256, 64),activate_type="SiLU",name=[1,N_start_next+2,N_start_next+2]) N_start_next = N_start_next+3 out4 = common.convolutional(G4, (1, 1, 512, 128), activate=False, bn=False,name=[1,N_start_next,N_start_next]) out4 = common.CSP_CBAM(out4, activate_type1="SiLU", CB_name=[1,N_start_next+1]) N_start_next = N_start_next+11 out3 = tf.concat([common.upsample(out4,activate_type="SiLU",name=[1,N_start_next,N_start_next]), out3], axis=-1) out3 = common.convolutional(common.upsample(out3,activate_type="SiLU",name=[1,N_start_next+1,N_start_next+1]), (3, 3, 256, 128),activate_type="SiLU",name=[1,N_start_next+2,N_start_next+2]) out3 = common.convolutional(common.upsample(out3,activate_type="SiLU",name=[1,N_start_next+3,N_start_next+3]), (3, 3, 128, 64),activate_type="SiLU",name=[1,N_start_next+4,N_start_next+4]) N_start_next = N_start_next+5 out4 = common.convolutional(common.upsample(out4,activate_type="SiLU",name=[1,N_start_next,N_start_next]), (3, 3, 128, 128),activate_type="SiLU",name=[1,N_start_next+1,N_start_next+1]) out4 = common.convolutional(common.upsample(out4,activate_type="SiLU",name=[1,N_start_next+2,N_start_next+2]), (3, 3, 128, 64),activate_type="SiLU",name=[1,N_start_next+3,N_start_next+3]) out4 = common.convolutional(common.upsample(out4,activate_type="SiLU",name=[1,N_start_next+4,N_start_next+4]), (3, 3, 64, 64),activate_type="SiLU",name=[1,N_start_next+5,N_start_next+5]) N_start_next = N_start_next+6 concat = tf.concat([out2, out3, out4], axis=-1) concat = common.convolutional(concat, (3, 3, 64*3, 128),activate_type="SiLU",name=[1,N_start_next,N_start_next]) out1 = common.convolutional(G1, (1, 1, 64, 128),activate=False, bn=False,name=[1,N_start_next+1,N_start_next+1]) out1 = common.CSP_CBAM(out1, activate_type1="SiLU", CB_name=[1,N_start_next+2]) N_start_next = N_start_next+12 out1 = tf.concat([out1, concat], axis=-1) out1 = common.convolutional(common.upsample(out1,activate_type="SiLU",name=[1,N_start_next,N_start_next]), (3, 3, 256, 32),activate_type="SiLU",name=[1,N_start_next+1,N_start_next+1]) out1 = common.convolutional(out1, (1, 1, 32, 3), activate=False, bn=False,name=[1,N_start_next+2,N_start_next+2]) out1 = tf.math.sigmoid(out1) + input_layer result = tf.clip_by_value(out1, clip_value_min=0, clip_value_max=1) return [conv_sbbox, conv_mbbox, conv_lbbox, result]
def YOLOv4(input_layer, NUM_CLASS): print('[INFO][core.yolov4.YOLOv4] Initializing YOLOv4 architecture') route_1, route_2, conv = backbone.cspdarknet53(input_layer) print('[INFO][core.yolov4.YOLOv4] cspdarknet53 backbone initialized') route = conv conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.upsample(conv) route_2 = common.convolutional(route_2, (1, 1, 512, 256)) conv = tf.concat([route_2, conv], axis=-1) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) route_2 = conv conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) route_1 = common.convolutional(route_1, (1, 1, 256, 128)) conv = tf.concat([route_1, conv], axis=-1) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) route_1 = conv conv = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) print(f'[DEBUG][core.yolov4.YOLOv4] conv_ssbox : {conv_sbbox}') conv = common.convolutional(route_1, (3, 3, 128, 256), downsample=True) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) route_2 = conv conv = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) print(f'[DEBUG][core.yolov4.YOLOv4] conv_mbbox : {conv_mbbox}') conv = common.convolutional(route_2, (3, 3, 256, 512), downsample=True) conv = tf.concat([conv, route], axis=-1) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) print(f'[DEBUG][core.yolov4.YOLOv4] conv_lbbox : {conv_lbbox}') print(f'[INFO][core.yolov4.YOLOv4] YOLOv4 architecture initialized') return [conv_sbbox, conv_mbbox, conv_lbbox]
def __build_nework(self, input_data): net_name = "YOLOV3" with tf.variable_scope(net_name): route_1, route_2, input_data = backbone.MobilenetV2( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 1280, 512), self.trainable, 'conv52') input_data = separable_conv(name="conv53", input_data=input_data, input_c=512, output_c=1024, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = separable_conv(name="conv55", input_data=input_data, input_c=512, output_c=1024, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = separable_conv(name="conv_lobj_branch", input_data=input_data, input_c=512, output_c=1024, training=self.trainable) conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 352, 256), self.trainable, 'conv58') input_data = separable_conv(name="conv59", input_data=input_data, input_c=256, output_c=512, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = separable_conv(name="conv61", input_data=input_data, input_c=256, output_c=512, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = separable_conv(name="conv_mobj_branch", input_data=input_data, input_c=256, output_c=512, training=self.trainable) conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 160, 128), self.trainable, 'conv64') input_data = separable_conv(name="conv_65", input_data=input_data, input_c=128, output_c=256, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = separable_conv(name="conv_67", input_data=input_data, input_c=128, output_c=256, training=self.trainable) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = separable_conv(name="conv_sobj_branch", input_data=input_data, input_c=128, output_c=256, training=self.trainable) conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) for var in tf.global_variables(net_name): tf.add_to_collection(net_name, var) return conv_lbbox, conv_mbbox, conv_sbbox
def __build_nework(self, input_data): input_data = common.convolutional(input_data, (3, 3, 1, 16), self.trainable, name='conv0', downsample=True) input_data = common.convolutional(input_data, (3, 3, 16, 32), self.trainable, name='conv1', downsample=True) input_data = common.convolutional(input_data, (3, 3, 32, 32), self.trainable, name='conv2', downsample=True) input_data = common.convolutional(input_data, (3, 3, 32, 32), self.trainable, name='conv3', downsample=True) input_data = common.convolutional(input_data, (3, 3, 32, 64), self.trainable, name='conv4', downsample=False) #16 route_1 = input_data input_data = common.convolutional(input_data, (3, 3, 64, 64), self.trainable, name='conv5', downsample=True) #32 input_data = common.convolutional(input_data, (3, 3, 64, 128), self.trainable, name='conv6', downsample=False) input_data = common.convolutional(input_data, (1, 1, 128, 64), self.trainable, name='conv7', downsample=False) conv_lobj_branch = common.convolutional(input_data, (3, 3, 64, 128), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 128, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 64, 32), self.trainable, name='conv8', downsample=False) input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) #16 route_2 = input_data input_data = tf.concat([route_1, route_2], axis=-1) conv_mobj_branch = common.convolutional(input_data, (3, 3, 96, 32), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 32, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox
def __build_nework(self, input_data): route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) # input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') # input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') # input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') # input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') # input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') # # conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') # conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), # trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) # # input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) # 1024+512=1536 # input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') # input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') # input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') # input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') # input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') # # conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch') # conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), # trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) # # input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) # 1536+256=1792 # input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') # input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') # input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') # input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') # input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') # conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') # conv_s = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), # trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) input_data = common.convolutional(input_data, filters_shape=(3, 3, 1792, 256), trainable=True, name='conv_s_conv', downsample=True) # 26*26*256 input_data = common.convolutional(input_data, filters_shape=(1, 1, 256, self.num_class), trainable=True, name='conv_s_conv1', downsample=True) # 13*13*19 conv_s = tf.layers.flatten(input_data) conv_s = tf.reshape(conv_s, (-1, 13 * 13 * self.num_class)) conv_s = tf.layers.dense( conv_s, units=1024, activation=tf.nn.leaky_relu, kernel_regularizer=tf.contrib.layers.l2_regularizer(0.1)) conv_s = tf.layers.dropout(conv_s, rate=0.5) conv_s = tf.layers.dense( conv_s, units=512, activation=tf.nn.leaky_relu, kernel_regularizer=tf.contrib.layers.l2_regularizer(0.1)) conv_s = tf.layers.dropout(conv_s, rate=0.5) conv_s = tf.layers.dense(conv_s, units=self.num_class, name='conv_l', activation=tf.nn.softmax) return conv_s
def __build_nework(self, input_data): print('__build_nework self.net_type=', self.net_type) assert self.net_type in [ 'darknet53', 'mobilenetv2', 'mobilenetv3', 'mobilenetv3_small' ] if self.net_type == 'darknet53': route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv52') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv53') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv54') input_data = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, 'conv55') input_data = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv56') conv_lobj_branch = common.convolutional(input_data, (3, 3, 512, 1024), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv57') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 768, 256), self.trainable, 'conv58') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv59') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv60') input_data = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, 'conv61') input_data = common.convolutional(input_data, (1, 1, 512, 256), self.trainable, 'conv62') conv_mobj_branch = common.convolutional(input_data, (3, 3, 256, 512), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv63') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 384, 128), self.trainable, 'conv64') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv65') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv66') input_data = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, 'conv67') input_data = common.convolutional(input_data, (1, 1, 256, 128), self.trainable, 'conv68') conv_sobj_branch = common.convolutional(input_data, (3, 3, 128, 256), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox elif self.net_type == 'mobilenetv2': route_1, route_2, input_data = backbone.mobilenetv2( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 320, 160), self.trainable, 'conv18') input_data = common.convolutional(input_data, (3, 3, 160, 320), self.trainable, 'conv19') input_data = common.convolutional(input_data, (1, 1, 320, 160), self.trainable, 'conv20') conv_lobj_branch = common.convolutional(input_data, (3, 3, 160, 320), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 320, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 160, 80), self.trainable, 'conv21') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 176, 80), self.trainable, 'conv22') input_data = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, 'conv23') input_data = common.convolutional(input_data, (1, 1, 160, 80), self.trainable, 'conv24') conv_mobj_branch = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 160, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv25') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 72, 40), self.trainable, 'conv26') input_data = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, 'conv27') input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv28') conv_sobj_branch = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 80, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox elif self.net_type == 'mobilenetv3': route_1, route_2, input_data = backbone.mobilenetv3( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 160, 80), self.trainable, 'conv8') input_data = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, 'conv9') input_data = common.convolutional(input_data, (1, 1, 160, 80), self.trainable, 'conv10') conv_lobj_branch = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 160, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv11') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 152, 40), self.trainable, 'conv12') input_data = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, 'conv13') input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv14') conv_mobj_branch = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 80, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 40, 20), self.trainable, 'conv15') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 60, 20), self.trainable, 'conv16') input_data = common.convolutional(input_data, (3, 3, 20, 40), self.trainable, 'conv17') input_data = common.convolutional(input_data, (1, 1, 40, 20), self.trainable, 'conv18') conv_sobj_branch = common.convolutional(input_data, (3, 3, 20, 40), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 40, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox elif self.net_type == 'mobilenetv3_small': route_1, route_2, input_data = backbone.mobilenetv3_small( input_data, self.trainable) input_data = common.convolutional(input_data, (1, 1, 96, 80), self.trainable, 'conv7') input_data = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, 'conv8') input_data = common.convolutional(input_data, (1, 1, 160, 80), self.trainable, 'conv9') conv_lobj_branch = common.convolutional(input_data, (3, 3, 80, 160), self.trainable, name='conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 160, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_lbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv10') input_data = common.upsample(input_data, name='upsample0', method=self.upsample_method) with tf.variable_scope('route_1'): input_data = tf.concat([input_data, route_2], axis=-1) input_data = common.convolutional(input_data, (1, 1, 88, 40), self.trainable, 'conv11') input_data = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, 'conv12') input_data = common.convolutional(input_data, (1, 1, 80, 40), self.trainable, 'conv13') conv_mobj_branch = common.convolutional(input_data, (3, 3, 40, 80), self.trainable, name='conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 80, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_mbbox', activate=False, bn=False) input_data = common.convolutional(input_data, (1, 1, 40, 20), self.trainable, 'conv14') input_data = common.upsample(input_data, name='upsample1', method=self.upsample_method) with tf.variable_scope('route_2'): input_data = tf.concat([input_data, route_1], axis=-1) input_data = common.convolutional(input_data, (1, 1, 44, 20), self.trainable, 'conv15') input_data = common.convolutional(input_data, (3, 3, 20, 40), self.trainable, 'conv16') input_data = common.convolutional(input_data, (1, 1, 40, 20), self.trainable, 'conv17') conv_sobj_branch = common.convolutional(input_data, (3, 3, 20, 40), self.trainable, name='conv_sobj_branch') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 40, 3 * (self.num_class + 5)), trainable=self.trainable, name='conv_sbbox', activate=False, bn=False) return conv_lbbox, conv_mbbox, conv_sbbox else: print('self.net_type=%s error' % self.net_type)
def object_detection_network(self, input_data): route_1, route_2, input_data = backbone.darknet53( input_data, self.trainable) conv_output = common.convolutional(input_data, (1, 1, 1024, 512), self.trainable, 'conv53') conv_output = common.convolutional(conv_output, (3, 3, 512, 1024), self.trainable, 'conv54') conv_output = common.convolutional(conv_output, (1, 1, 1024, 512), self.trainable, 'conv55') conv_output = common.convolutional(conv_output, (3, 3, 512, 1024), self.trainable, 'conv56') conv_output = common.convolutional(conv_output, (1, 1, 1024, 512), self.trainable, 'conv57') conv_lobj_branch = common.convolutional(conv_output, (3, 3, 512, 1024), self.trainable, 'conv_lobj_branch') conv_lbbox = common.convolutional( conv_lobj_branch, (1, 1, 1024, 3 * (self.num_class + 5)), trainable=self.trainable, activate=False, bn=False, name='conv_lbbox') conv_output = common.convolutional(conv_output, (1, 1, 512, 256), self.trainable, 'conv58') conv_upsampled_1 = common.upsample(name='upsample1', input_data=conv_output, method=self.upsample_method) with tf.variable_scope('route_1'): conv_output = tf.concat([conv_upsampled_1, route_2], axis=-1) conv_output = common.convolutional(conv_output, (1, 1, 768, 256), self.trainable, 'conv59') conv_output = common.convolutional(conv_output, (3, 3, 256, 512), self.trainable, 'conv60') conv_output = common.convolutional(conv_output, (1, 1, 512, 256), self.trainable, 'conv61') conv_output = common.convolutional(conv_output, (3, 3, 256, 512), self.trainable, 'conv62') conv_output = common.convolutional(conv_output, (1, 1, 512, 256), self.trainable, 'conv63') conv_mobj_branch = common.convolutional(conv_output, (3, 3, 256, 512), self.trainable, 'conv_mobj_branch') conv_mbbox = common.convolutional( conv_mobj_branch, (1, 1, 512, 3 * (self.num_class + 5)), self.trainable, activate=False, bn=False, name='conv_mbbox') conv_output = common.convolutional(conv_output, (1, 1, 256, 128), self.trainable, 'conv64') conv_upsampled_2 = common.upsample(name='upsample2', input_data=conv_output, method=self.upsample_method) with tf.variable_scope('route_2'): conv_output = tf.concat([conv_upsampled_2, route_1], axis=-1) conv_output = common.convolutional(conv_output, (1, 1, 384, 128), self.trainable, 'conv65') conv_output = common.convolutional(conv_output, (3, 3, 128, 256), self.trainable, 'conv66') conv_output = common.convolutional(conv_output, (1, 1, 256, 128), self.trainable, 'conv67') conv_output = common.convolutional(conv_output, (3, 3, 128, 256), self.trainable, 'conv68') conv_output = common.convolutional(conv_output, (1, 1, 256, 128), self.trainable, 'conv69') conv_sobj_branch = common.convolutional(conv_output, (3, 3, 128, 256), self.trainable, 'conv70') conv_sbbox = common.convolutional( conv_sobj_branch, (1, 1, 256, 3 * (self.num_class + 5)), self.trainable, activate=False, bn=False, name='conv_sbbox') return conv_lbbox, conv_mbbox, conv_sbbox
def YOLOv3(input_layer): if BACK_BONE == 'resnet18': route_1, route_2, conv = backbone.resnet(input_layer, 18) if BACK_BONE == 'resnet34': route_1, route_2, conv = backbone.resnet(input_layer, 34) if BACK_BONE == 'resnet50': route_1, route_2, conv = backbone.resnet(input_layer, 50) if BACK_BONE == 'resnet101': route_1, route_2, conv = backbone.resnet(input_layer, 101) if BACK_BONE == 'resnet152': route_1, route_2, conv = backbone.resnet(input_layer, 152) if BACK_BONE == 'darknet53': route_1, route_2, conv = backbone.darknet53(input_layer) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv = common.convolutional(conv, (3, 3, 512, 1024)) conv = common.convolutional(conv, (1, 1, 1024, 512)) conv_lobj_branch = common.convolutional(conv, (3, 3, 512, 1024)) conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.upsample(conv) conv = tf.concat([conv, route_2], axis=-1) conv = common.convolutional(conv, (1, 1, 768, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv = common.convolutional(conv, (3, 3, 256, 512)) conv = common.convolutional(conv, (1, 1, 512, 256)) conv_mobj_branch = common.convolutional(conv, (3, 3, 256, 512)) conv_mbbox = common.convolutional(conv_mobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.upsample(conv) conv = tf.concat([conv, route_1], axis=-1) conv = common.convolutional(conv, (1, 1, 384, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv = common.convolutional(conv, (3, 3, 128, 256)) conv = common.convolutional(conv, (1, 1, 256, 128)) conv_sobj_branch = common.convolutional(conv, (3, 3, 128, 256)) conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False) return [conv_sbbox, conv_mbbox, conv_lbbox]
def yolo_v3_network(self, input_layer): # 输入层进入 Darknet-53 网络后,得到了三个分支 middle_data_1, middle_data_2, conv = darknet53.darknet53(input_layer) # 五次DBL(卷积,BN[批量规范化],激活函数) conv = common.convolutional_Layer(conv, (1, 1, 1024, 512), name='conv52_five_1') conv = common.convolutional_Layer(conv, (3, 3, 512, 1024), name='conv53_five_1') conv = common.convolutional_Layer(conv, (1, 1, 1024, 512), name='conv54_five_1') conv = common.convolutional_Layer(conv, (3, 3, 512, 1024), name='conv55_five_1') conv = common.convolutional_Layer(conv, (1, 1, 1024, 512), name='conv56_five_1') # 第一尺度 ----------->conv_BIG_BOX 用于预测大尺寸物体,shape = [None, 13, 13, 255] conv_first_obj_branch = common.convolutional_Layer( conv, (3, 3, 512, 1024), name='conv_big_obj_branch') conv_big_BOX = common.convolutional_Layer( conv_first_obj_branch, (1, 1, 1024, 3 * (self.class_num + 5)), activate=False, bn=False, name='conv_big_BOX') # -------------------------------- conv = common.convolutional_Layer(conv, (1, 1, 512, 256), name='conv57') conv = common.upsample(conv, 'middle_box_upsample') conv = tf.concat([conv, middle_data_2], axis=-1) # axis,拼接方向,1y轴,2x轴,3...-1-->最后一维度 # 此刻的输入维度(512+256) conv = common.convolutional_Layer(conv, (1, 1, 768, 256), name='conv58_five_2') conv = common.convolutional_Layer(conv, (3, 3, 256, 512), name='conv59_five_2') conv = common.convolutional_Layer(conv, (1, 1, 512, 256), name='conv60_five_2') conv = common.convolutional_Layer(conv, (3, 3, 256, 512), name='conv61_five_2') conv = common.convolutional_Layer(conv, (1, 1, 512, 256), name='conv62_five_2') # 第二尺度 ----------->conv_middle_BOX 用于预测中等尺寸物体,shape = [None, 26, 26, 255] conv_second_obj_branch = common.convolutional_Layer( conv, (3, 3, 256, 512), name='conv_mid_obj_branch') conv_middle_BOX = common.convolutional_Layer( conv_second_obj_branch, (1, 1, 512, 3 * (self.class_num + 5)), activate=False, bn=False, name='conv_mid_BOX') # -------------------------------- conv = common.convolutional_Layer(conv, (1, 1, 256, 128), name='conv63') conv = common.upsample(conv, 'smart_box_upsample') conv = tf.concat([conv, middle_data_1], axis=-1) # 此刻的输入维度(256+128) conv = common.convolutional_Layer(conv, (1, 1, 384, 128), name='conv64_five_3') conv = common.convolutional_Layer(conv, (3, 3, 128, 256), name='conv65_five_3') conv = common.convolutional_Layer(conv, (1, 1, 256, 128), name='conv66_five_3') conv = common.convolutional_Layer(conv, (3, 3, 128, 256), name='conv67_five_3') conv = common.convolutional_Layer(conv, (1, 1, 256, 128), name='conv68_five_3') # 第三尺度 ----------->conv_smart_BOX 用于预测小尺寸物体,shape = [None, 52, 52, 255] conv_third_obj_branch = common.convolutional_Layer( conv, (3, 3, 128, 256), name='conv_smt_obj_branch') conv_smart_BOX = common.convolutional_Layer( conv_third_obj_branch, (1, 1, 256, 3 * (self.class_num + 5)), activate=False, bn=False, name='conv_smt_BOX') return conv_smart_BOX, conv_middle_BOX, conv_big_BOX