def yolo3lite_spp_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create YOLO_v3 Lite SPP MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1: 13 x 13 x (1024*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f3: 52 x 52 x (256*alpha) f3 = mobilenet.get_layer('conv_pw_5_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) f3_channel_num = int(256 * alpha) y1, y2, y3 = yolo3lite_predictions( (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes, use_spp=True) return Model(inputs=inputs, outputs=[y1, y2, y3])
def yolo4_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V4 MobileNet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenet.layers))) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1: 13 x 13 x (1024*alpha) for 416 input f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) for 416 input f2 = mobilenet.get_layer('conv_pw_11_relu').output # f3: 52 x 52 x (256*alpha) for 416 input f3 = mobilenet.get_layer('conv_pw_5_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) f3_channel_num = int(256 * alpha) y1, y2, y3 = yolo4_predictions( (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes) return Model(inputs, [y1, y2, y3])
def tiny_yolo4_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0, use_spp=True): '''Create Tiny YOLO_v4 MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenet.layers))) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1 :13 x 13 x (1024*alpha) for 416 input f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) for 416 input f2 = mobilenet.get_layer('conv_pw_11_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) y1, y2 = tiny_yolo4_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes, use_spp) return Model(inputs, [y1, y2])
def yolo5lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V5 Lite MobileNet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenet.layers))) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1: 13 x 13 x (1024*alpha) for 416 input f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) for 416 input f2 = mobilenet.get_layer('conv_pw_11_relu').output # f3: 52 x 52 x (256*alpha) for 416 input f3 = mobilenet.get_layer('conv_pw_5_relu').output # add SPP neck with original channel number f1 = yolo5_spp_neck(f1, int(1024*alpha)) # use yolo5_small depth_multiple and width_multiple for head depth_multiple = 0.33 width_multiple = 0.5 f1_channel_num = int(1024*width_multiple) f2_channel_num = int(512*width_multiple) f3_channel_num = int(256*width_multiple) y1, y2, y3 = yolo5lite_predictions((f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes, depth_multiple, width_multiple, with_spp=False) return Model(inputs, [y1, y2, y3])
def tiny_yolo3lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create Tiny YOLO_v3 Lite MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) x1 = mobilenet.get_layer('conv_pw_11_relu').output x2 = mobilenet.get_layer('conv_pw_13_relu').output x2 = DarknetConv2D_BN_Leaky(int(512 * alpha), (1, 1))(x2) y1 = compose( #DarknetConv2D_BN_Leaky(int(1024*alpha), (3,3)), Depthwise_Separable_Conv2D_BN_Leaky(filters=int(1024 * alpha), kernel_size=(3, 3), block_id_str='14'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) x2 = compose(DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1)), UpSampling2D(2))(x2) y2 = compose( Concatenate(), #DarknetConv2D_BN_Leaky(int(512*alpha), (3,3)), Depthwise_Separable_Conv2D_BN_Leaky(filters=int(512 * alpha), kernel_size=(3, 3), block_id_str='15'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1]) return Model(inputs, [y1, y2])
def tiny_yolo4lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0, use_spp=True): '''Create Tiny YOLO_v3 Lite MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1 :13 x 13 x (1024*alpha) for 416 input f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) for 416 input f2 = mobilenet.get_layer('conv_pw_11_relu').output #feature map 1 head (13 x 13 x (512*alpha) for 416 input) x1 = DarknetConv2D_BN_Leaky(int(512 * alpha), (1, 1))(f1) if use_spp: x1 = Spp_Conv2D_BN_Leaky(x1, int(512 * alpha)) #upsample fpn merge for feature map 1 & 2 x1_upsample = compose(DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1)), UpSampling2D(2))(x1) x2 = compose( Concatenate(), #DarknetConv2D_BN_Leaky(int(512*alpha), (3,3)), Depthwise_Separable_Conv2D_BN_Leaky(filters=int(512 * alpha), kernel_size=(3, 3), block_id_str='15'))( [x1_upsample, f2]) #feature map 2 output (26 x 26 x (512*alpha) for 416 input) y2 = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x2) #downsample fpn merge for feature map 2 & 1 x2_downsample = compose( ZeroPadding2D(((1, 0), (1, 0))), #DarknetConv2D_BN_Leaky(int(512*alpha), (3,3), strides=(2,2)), Darknet_Depthwise_Separable_Conv2D_BN_Leaky(int(512 * alpha), (3, 3), strides=(2, 2), block_id_str='16'))(x2) x1 = compose( Concatenate(), #DarknetConv2D_BN_Leaky(int(1024*alpha), (3,3)), Depthwise_Separable_Conv2D_BN_Leaky(filters=int(1024 * alpha), kernel_size=(3, 3), block_id_str='17'))( [x2_downsample, x1]) #feature map 1 output (13 x 13 x (1024*alpha) for 416 input) y1 = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x1) return Model(inputs, [y1, y2])
def deep_encoder_model(): base_model = MobileNet(include_top=True, weights='imagenet') model = Model( inputs=base_model.input, outputs=base_model.get_layer('conv_pw_13').output) ##hamper with this print(model.summary()) return model
def yolo3lite_spp_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create YOLO_v3 Lite SPP MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x (1024*alpha) #x, y1 = make_depthwise_separable_last_layers(f1, int(512*alpha), num_anchors * (num_classes + 5), block_id_str='14') x, y1 = make_spp_depthwise_separable_last_layers(f1, int(512 * alpha), num_anchors * (num_classes + 5), block_id_str='14') x = compose(DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1)), UpSampling2D(2))(x) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x (512*alpha) x = Concatenate()([x, f2]) x, y2 = make_depthwise_separable_last_layers(x, int(256 * alpha), num_anchors * (num_classes + 5), block_id_str='15') x = compose(DarknetConv2D_BN_Leaky(int(128 * alpha), (1, 1)), UpSampling2D(2))(x) f3 = mobilenet.get_layer('conv_pw_5_relu').output # f3 : 52 x 52 x (256*alpha) x = Concatenate()([x, f3]) x, y3 = make_depthwise_separable_last_layers(x, int(128 * alpha), num_anchors * (num_classes + 5), block_id_str='16') return Model(inputs=inputs, outputs=[y1, y2, y3])
def mobile_yolo_body(inputs, num_anchors, num_classes): mobile_net = MobileNet(input_tensor=inputs, weights=None) # conv_pw_13_relu: 13 x 13 x 1024 # conv_pw_11_relu: 26 x 26 x 512 # conv_pw_5_relu : 52 x 52 x 256 f1 = mobile_net.get_layer("conv_pw_13_relu").output x, y1 = make_last_layers(f1, 512, num_anchors * (num_classes + 5)) x = compose(DarkNetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) f2 = mobile_net.get_layer("conv_pw_11_relu").output x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) x = compose(DarkNetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(x) f3 = mobile_net.get_layer("conv_pw_5_relu").output x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def tiny_yolo3_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create Tiny YOLO_v3 MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1: 13 x 13 x (1024*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) f2 = mobilenet.get_layer('conv_pw_11_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) y1, y2 = tiny_yolo3_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, [y1, y2])
def create_model(trainable=True): model = MobileNet(input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, 3), include_top=False, alpha=1.0, weights="imagenet") for layer in model.layers: layer.trainable = trainable # Add all the UNET layers here block1 = model.get_layer("conv_pw_5_relu").output block2 = model.get_layer("conv_pw_11_relu").output block3 = model.get_layer("conv_pw_13_relu").output block4 = model.get_layer("conv_pw_3_relu").output block5 = model.get_layer("conv_pw_1_relu").output block6 = model.get_layer("conv1_relu").output x = Concatenate()([UpSampling2D()(block3), block2]) x = Concatenate()([UpSampling2D()(x), block1]) x = Concatenate()([UpSampling2D()(x), block4]) x = Concatenate()([UpSampling2D()(x), block5]) x = Concatenate()([UpSampling2D()(x), UpSampling2D()(block5)]) x = Conv2D(1, kernel_size=1, activation="sigmoid")(x) x = Reshape((IMAGE_HEIGHT, IMAGE_WIDTH))(x) return Model(inputs=model.input, outputs=x)
def yolo2lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 Lite MobileNet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenet.layers))) # input: 416 x 416 x 3 # mobilenet.output : 13 x 13 x (1024*alpha) # conv_pw_11_relu(layers[73]) : 26 x 26 x (512*alpha) # f1: 13 x 13 x (1024*alpha) f1 = mobilenet.output # f2: 26 x 26 x (512*alpha) f2 = mobilenet.get_layer('conv_pw_11_relu').output f1_channel_num = int(1024*alpha) f2_channel_num = int(512*alpha) y = yolo2lite_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def __init__(self, weights=None): """Either load pretrained from imagenet, or load our saved weights from our own training.""" self.weights = weights # so we can check elsewhere which model if weights is None: # Get model with pretrained weights. #base_model = MobileNetV2( #weights='imagenet', #include_top=True #) ## We'll extract features at the final pool layer. #self.model = Model( #inputs=base_model.input, #outputs=base_model.get_layer('global_average_pooling2d').output #) # create the base pre-trained model base_model = MobileNet(input_shape=(224,224,3), weights='imagenet', pooling='avg', include_top=False, alpha=0.5) # add a global spatial average pooling layer features = base_model.get_layer('conv_pw_11_relu').output features = GlobalAveragePooling2D()(features) # this is the model we will train self.model = Model(inputs=base_model.input, outputs=features) #self.model.summary() else: # Load the model first. self.model = load_model(weights) # Then remove the top so we get features not predictions. # From: https://github.com/fchollet/keras/issues/2371 self.model.layers.pop() self.model.layers.pop() # two pops to get to pool layer self.model.outputs = [self.model.layers[-1].output] self.model.output_layers = [self.model.layers[-1]] self.model.layers[-1].outbound_nodes = []
def get_model(weights='imagenet'): # create the base pre-trained model base_model = MobileNet(input_shape=(224, 224, 3), weights=weights, pooling='avg', include_top=False, alpha=0.5) # add a global spatial average pooling layer x = base_model.get_layer('conv_pw_11_relu').output #x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer #x = Dense(128, activation='relu')(x) x = Dropout(0.2)(x) # and a logistic layer predictions = Dense(len(data.classes), activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) model.summary() return model
def yolo3_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V3 MobileNet model CNN body in Keras.""" ''' Layer Name: input_1 Output: Tensor("input_1:0", shape=(?, 416, 416, 3), dtype=float32) Layer Name: conv1_pad Output: Tensor("conv1_pad/Pad:0", shape=(?, 417, 417, 3), dtype=float32) Layer Name: conv1 Output: Tensor("conv1/convolution:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv1_bn Output: Tensor("conv1_bn/cond/Merge:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv1_relu Output: Tensor("conv1_relu/Minimum:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv_pad_1 Output: Tensor("conv_pad_1/Pad:0", shape=(?, 210, 210, 32), dtype=float32) Layer Name: conv_dw_1 Output: Tensor("conv_dw_1/depthwise:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv_dw_1_bn Output: Tensor("conv_dw_1_bn/cond/Merge:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv_dw_1_relu Output: Tensor("conv_dw_1_relu/Minimum:0", shape=(?, 208, 208, 32), dtype=float32) Layer Name: conv_pw_1 Output: Tensor("conv_pw_1/convolution:0", shape=(?, 208, 208, 64), dtype=float32) Layer Name: conv_pw_1_bn Output: Tensor("conv_pw_1_bn/cond/Merge:0", shape=(?, 208, 208, 64), dtype=float32) Layer Name: conv_pw_1_relu Output: Tensor("conv_pw_1_relu/Minimum:0", shape=(?, 208, 208, 64), dtype=float32) Layer Name: conv_pad_2 Output: Tensor("conv_pad_2/Pad:0", shape=(?, 210, 210, 64), dtype=float32) Layer Name: conv_dw_2 Output: Tensor("conv_dw_2/depthwise:0", shape=(?, 104, 104, 64), dtype=float32) Layer Name: conv_dw_2_bn Output: Tensor("conv_dw_2_bn/cond/Merge:0", shape=(?, 104, 104, 64), dtype=float32) Layer Name: conv_dw_2_relu Output: Tensor("conv_dw_2_relu/Minimum:0", shape=(?, 104, 104, 64), dtype=float32) Layer Name: conv_pw_2 Output: Tensor("conv_pw_2/convolution:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pw_2_bn Output: Tensor("conv_pw_2_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pw_2_relu Output: Tensor("conv_pw_2_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pad_3 Output: Tensor("conv_pad_3/Pad:0", shape=(?, 106, 106, 128), dtype=float32) Layer Name: conv_dw_3 Output: Tensor("conv_dw_3/depthwise:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_dw_3_bn Output: Tensor("conv_dw_3_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_dw_3_relu Output: Tensor("conv_dw_3_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pw_3 Output: Tensor("conv_pw_3/convolution:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pw_3_bn Output: Tensor("conv_pw_3_bn/cond/Merge:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pw_3_relu Output: Tensor("conv_pw_3_relu/Minimum:0", shape=(?, 104, 104, 128), dtype=float32) Layer Name: conv_pad_4 Output: Tensor("conv_pad_4/Pad:0", shape=(?, 106, 106, 128), dtype=float32) Layer Name: conv_dw_4 Output: Tensor("conv_dw_4/depthwise:0", shape=(?, 52, 52, 128), dtype=float32) Layer Name: conv_dw_4_bn Output: Tensor("conv_dw_4_bn/cond/Merge:0", shape=(?, 52, 52, 128), dtype=float32) Layer Name: conv_dw_4_relu Output: Tensor("conv_dw_4_relu/Minimum:0", shape=(?, 52, 52, 128), dtype=float32) Layer Name: conv_pw_4 Output: Tensor("conv_pw_4/convolution:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pw_4_bn Output: Tensor("conv_pw_4_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pw_4_relu Output: Tensor("conv_pw_4_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pad_5 Output: Tensor("conv_pad_5/Pad:0", shape=(?, 54, 54, 256), dtype=float32) Layer Name: conv_dw_5 Output: Tensor("conv_dw_5/depthwise:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_dw_5_bn Output: Tensor("conv_dw_5_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_dw_5_relu Output: Tensor("conv_dw_5_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pw_5 Output: Tensor("conv_pw_5/convolution:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pw_5_bn Output: Tensor("conv_pw_5_bn/cond/Merge:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pw_5_relu Output: Tensor("conv_pw_5_relu/Minimum:0", shape=(?, 52, 52, 256), dtype=float32) Layer Name: conv_pad_6 Output: Tensor("conv_pad_6/Pad:0", shape=(?, 54, 54, 256), dtype=float32) Layer Name: conv_dw_6 Output: Tensor("conv_dw_6/depthwise:0", shape=(?, 26, 26, 256), dtype=float32) Layer Name: conv_dw_6_bn Output: Tensor("conv_dw_6_bn/cond/Merge:0", shape=(?, 26, 26, 256), dtype=float32) Layer Name: conv_dw_6_relu Output: Tensor("conv_dw_6_relu/Minimum:0", shape=(?, 26, 26, 256), dtype=float32) Layer Name: conv_pw_6 Output: Tensor("conv_pw_6/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_6_bn Output: Tensor("conv_pw_6_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_6_relu Output: Tensor("conv_pw_6_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_7 Output: Tensor("conv_pad_7/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_7 Output: Tensor("conv_dw_7/depthwise:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_7_bn Output: Tensor("conv_dw_7_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_7_relu Output: Tensor("conv_dw_7_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_7 Output: Tensor("conv_pw_7/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_7_bn Output: Tensor("conv_pw_7_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_7_relu Output: Tensor("conv_pw_7_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_8 Output: Tensor("conv_pad_8/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_8 Output: Tensor("conv_dw_8/depthwise:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_8_bn Output: Tensor("conv_dw_8_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_8_relu Output: Tensor("conv_dw_8_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_8 Output: Tensor("conv_pw_8/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_8_bn Output: Tensor("conv_pw_8_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_8_relu Output: Tensor("conv_pw_8_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_9 Output: Tensor("conv_pad_9/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_9 Output: Tensor("conv_dw_9/depthwise:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_9_bn Output: Tensor("conv_dw_9_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_9_relu Output: Tensor("conv_dw_9_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_9 Output: Tensor("conv_pw_9/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_9_bn Output: Tensor("conv_pw_9_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_9_relu Output: Tensor("conv_pw_9_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_10 Output: Tensor("conv_pad_10/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_10 Output: Tensor("conv_dw_10/depthwise:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_10_bn Output: Tensor("conv_dw_10_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_10_relu Output: Tensor("conv_dw_10_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_10 Output: Tensor("conv_pw_10/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_10_bn Output: Tensor("conv_pw_10_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_10_relu Output: Tensor("conv_pw_10_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_11 Output: Tensor("conv_pad_11/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_11 Output: Tensor("conv_dw_11/depthwise:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_11_bn Output: Tensor("conv_dw_11_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_dw_11_relu Output: Tensor("conv_dw_11_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_11 Output: Tensor("conv_pw_11/convolution:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_11_bn Output: Tensor("conv_pw_11_bn/cond/Merge:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pw_11_relu Output: Tensor("conv_pw_11_relu/Minimum:0", shape=(?, 26, 26, 512), dtype=float32) Layer Name: conv_pad_12 Output: Tensor("conv_pad_12/Pad:0", shape=(?, 28, 28, 512), dtype=float32) Layer Name: conv_dw_12 Output: Tensor("conv_dw_12/depthwise:0", shape=(?, 13, 13, 512), dtype=float32) Layer Name: conv_dw_12_bn Output: Tensor("conv_dw_12_bn/cond/Merge:0", shape=(?, 13, 13, 512), dtype=float32) Layer Name: conv_dw_12_relu Output: Tensor("conv_dw_12_relu/Minimum:0", shape=(?, 13, 13, 512), dtype=float32) Layer Name: conv_pw_12 Output: Tensor("conv_pw_12/convolution:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pw_12_bn Output: Tensor("conv_pw_12_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pw_12_relu Output: Tensor("conv_pw_12_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pad_13 Output: Tensor("conv_pad_13/Pad:0", shape=(?, 15, 15, 1024), dtype=float32) Layer Name: conv_dw_13 Output: Tensor("conv_dw_13/depthwise:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_dw_13_bn Output: Tensor("conv_dw_13_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_dw_13_relu Output: Tensor("conv_dw_13_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pw_13 Output: Tensor("conv_pw_13/convolution:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pw_13_bn Output: Tensor("conv_pw_13_bn/cond/Merge:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: conv_pw_13_relu Output: Tensor("conv_pw_13_relu/Minimum:0", shape=(?, 13, 13, 1024), dtype=float32) Layer Name: global_average_pooling2d_1 Output: Tensor("global_average_pooling2d_1/Mean:0", shape=(?, 1024), dtype=float32) Layer Name: reshape_1 Output: Tensor("reshape_1/Reshape:0", shape=(?, 1, 1, 1024), dtype=float32) Layer Name: dropout Output: Tensor("dropout/cond/Merge:0", shape=(?, 1, 1, 1024), dtype=float32) Layer Name: conv_preds Output: Tensor("conv_preds/BiasAdd:0", shape=(?, 1, 1, 1000), dtype=float32) Layer Name: act_softmax Output: Tensor("act_softmax/truediv:0", shape=(?, 1, 1, 1000), dtype=float32) Layer Name: reshape_2 Output: Tensor("reshape_2/Reshape:0", shape=(?, 1000), dtype=float32) ''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x (1024*alpha) x, y1 = make_last_layers(f1, int(512 * alpha), num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1)), UpSampling2D(2))(x) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x (512*alpha) x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, int(256 * alpha), num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(int(128 * alpha), (1, 1)), UpSampling2D(2))(x) f3 = mobilenet.get_layer('conv_pw_5_relu').output # f3 : 52 x 52 x (256*alpha) x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, int(128 * alpha), num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
os.environ["CUDA_VISIBLE_DEVICES"] = "6" #gpu_options = tf.GPUOptions(allow_growth=True) #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) from keras.utils import plot_model from matplotlib import pyplot as plt #【0】MobileNet模型,加载预训练权重 base_model = MobileNet(weights='imagenet') #print(base_model.summary()) #【1】创建一个新model, 使得它的输出(outputs)是 MobileNet 中任意层的输出(output) #定义层的时候可以指定name参数,如:x=Dense(784,activation='relu',name="my_lay")(x) model = Model(inputs=base_model.input, outputs=base_model.get_layer('dropout').output) print(model.summary()) # 打印模型概况 #【2】从网上下载一张图片,保存在当前路径下 img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) # 加载图片并resize成224x224 #【3】将图片转化为4d tensor形式 x = image.img_to_array(img) x = np.expand_dims(x, axis=0) #【4】数据预处理 x = preprocess_input(x) #去均值中心化,preprocess_input函数详细功能见注释 #【5】提取特征 block4_pool_features = model.predict(x)
outputs=base_model.get_layer('flatten').output) image_size = (299, 299) elif model_name == "inceptionresnetv2": base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299, 299, 3))) model = Model(inputs=base_model.input, outputs=base_model.get_layer('conv_7b').output) image_size = (299, 299) elif model_name == "mobilenet": base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224, 224, 3)), input_shape=(224, 224, 3)) model = Model(inputs=base_model.input, outputs=base_model.get_layer('conv1_relu').output) image_size = (224, 224) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None # check if the output directory exists, if not, create it. existing_directory("results/deep_features/" + dataset[ds] + "/" + model_name) print("[INFO] successfully loaded base model and model...")
def yolo4_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V4 MobileNet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) # f1: 13 x 13 x (1024*alpha) for 416 input f1 = mobilenet.get_layer('conv_pw_13_relu').output # f2: 26 x 26 x (512*alpha) for 416 input f2 = mobilenet.get_layer('conv_pw_11_relu').output # f3: 52 x 52 x (256*alpha) for 416 input f3 = mobilenet.get_layer('conv_pw_5_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) f3_channel_num = int(256 * alpha) #feature map 1 head (13 x 13 x (512*alpha) for 416 input) x1 = make_yolo_spp_head(f1, f1_channel_num // 2) #upsample fpn merge for feature map 1 & 2 x1_upsample = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)), UpSampling2D(2))(x1) x2 = DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1))(f2) x2 = Concatenate()([x2, x1_upsample]) #feature map 2 head (26 x 26 x (256*alpha) for 416 input) x2 = make_yolo_head(x2, f2_channel_num // 2) #upsample fpn merge for feature map 2 & 3 x2_upsample = compose(DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1)), UpSampling2D(2))(x2) x3 = DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1))(f3) x3 = Concatenate()([x3, x2_upsample]) #feature map 3 head & output (52 x 52 x (256*alpha) for 416 input) #x3, y3 = make_last_layers(x3, f3_channel_num//2, num_anchors*(num_classes+5)) x3 = make_yolo_head(x3, f3_channel_num // 2) y3 = compose(DarknetConv2D_BN_Leaky(f3_channel_num, (3, 3)), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x3) #downsample fpn merge for feature map 3 & 2 x3_downsample = compose( ZeroPadding2D(((1, 0), (1, 0))), DarknetConv2D_BN_Leaky(f2_channel_num // 2, (3, 3), strides=(2, 2)))(x3) x2 = Concatenate()([x3_downsample, x2]) #feature map 2 output (26 x 26 x (512*alpha) for 416 input) #x2, y2 = make_last_layers(x2, f2_channel_num//2, num_anchors*(num_classes+5)) x2 = make_yolo_head(x2, f2_channel_num // 2) y2 = compose(DarknetConv2D_BN_Leaky(f2_channel_num, (3, 3)), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) #downsample fpn merge for feature map 2 & 1 x2_downsample = compose( ZeroPadding2D(((1, 0), (1, 0))), DarknetConv2D_BN_Leaky(f1_channel_num // 2, (3, 3), strides=(2, 2)))(x2) x1 = Concatenate()([x2_downsample, x1]) #feature map 1 output (13 x 13 x (1024*alpha) for 416 input) #x1, y1 = make_last_layers(x1, f1_channel_num//2, num_anchors*(num_classes+5)) x1 = make_yolo_head(x1, f1_channel_num // 2) y1 = compose(DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x1) return Model(inputs, [y1, y2, y3])
def _get_ImageNet_trained_model(self, model): """Initialize an ImageNet pre-trained model After the chosen convolution neural network (see `model` for options) is loaded with the ImageNet pre-trained weights from `tensorflow.keras.applications`, the last fully connected layer(s) is removed to turn the classification model into a feature extractor. Parameters ---------- model : str Name of the pre-trained deep learning model to be used for feature extraction. Can be one of "ResNet50", "DenseNet121", "DenseNet201", "VGG16", "VGG19", "MobileNet", "MobileNetV2", "Xception", "InceptionV3". Returns ------- feature_extractor A Keras model object. image_size : tuple Image size as (height, width). preprocess_input A preprocessing function - the `preprocess_input` function from `tensorflow.keras.applications` that corresponds to the chosen `model`. """ if model == "ResNet50": from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input image_size = (224, 224) res = ResNet50(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=res.input, outputs=res.get_layer("avg_pool").output) elif model == "DenseNet121": from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input image_size = (224, 224) dense121 = DenseNet121(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=dense121.input, outputs=dense121.get_layer("avg_pool").output) elif model == "DenseNet201": from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input image_size = (224, 224) dense201 = DenseNet201(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=dense201.input, outputs=dense201.get_layer("avg_pool").output) elif model == "VGG16": from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input image_size = (224, 224) vgg16 = VGG16(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=vgg16.input, outputs=vgg16.get_layer("fc2").output) elif model == "VGG19": from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input image_size = (224, 224) vgg19 = VGG19(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=vgg19.input, outputs=vgg19.get_layer("fc2").output) elif model == "MobileNet": from tensorflow.keras.applications.mobilenet import MobileNet, preprocess_input image_size = (224, 224) mobile = MobileNet(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=mobile.input, outputs=mobile.get_layer(index=-6).output) # the output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow elif model == "MobileNetV2": from tensorflow.keras.applications.mobilenet_v2 import ( MobileNetV2, preprocess_input, ) image_size = (224, 224) mobilev2 = MobileNetV2(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=mobilev2.input, outputs=mobilev2.get_layer(index=-2).output) # output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow elif model == "Xception": from tensorflow.keras.applications.xception import Xception, preprocess_input image_size = (299, 299) xception = Xception(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=xception.input, outputs=xception.get_layer("avg_pool").output) elif model == "InceptionV3": from tensorflow.keras.applications.inception_v3 import ( InceptionV3, preprocess_input, ) image_size = (299, 299) inception = InceptionV3(input_shape=image_size + (3, ), weights="imagenet", include_top=True) feature_extractor = tensorflow.keras.models.Model( inputs=inception.input, outputs=inception.get_layer("avg_pool").output) else: raise NotImplementedError("please specify a model!") return feature_extractor, image_size, preprocess_input
def yolo4lite_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): '''Create YOLO_v4 Lite MobileNet model CNN body in keras.''' mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x (1024*alpha) # conv_pw_11_relu :26 x 26 x (512*alpha) # conv_pw_5_relu : 52 x 52 x (256*alpha) f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x (1024*alpha) for 416 input #feature map 1 head (13 x 13 x (512*alpha) for 416 input) x1 = make_yolo_spp_depthwise_separable_head(f1, int(512 * alpha), block_id_str='14') #upsample fpn merge for feature map 1 & 2 x1_upsample = compose(DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1)), UpSampling2D(2))(x1) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x (512*alpha) for 416 input x2 = DarknetConv2D_BN_Leaky(int(256 * alpha), (1, 1))(f2) x2 = Concatenate()([x2, x1_upsample]) #feature map 2 head (26 x 26 x (256*alpha) for 416 input) x2 = make_yolo_depthwise_separable_head(x2, int(256 * alpha), block_id_str='15') #upsample fpn merge for feature map 2 & 3 x2_upsample = compose(DarknetConv2D_BN_Leaky(int(128 * alpha), (1, 1)), UpSampling2D(2))(x2) f3 = mobilenet.get_layer('conv_pw_5_relu').output # f3 : 52 x 52 x (256*alpha) for 416 input x3 = DarknetConv2D_BN_Leaky(int(128 * alpha), (1, 1))(f3) x3 = Concatenate()([x3, x2_upsample]) #feature map 3 head & output (52 x 52 x (256*alpha) for 416 input) #x3, y3 = make_depthwise_separable_last_layers(x3, int(128*alpha), num_anchors*(num_classes+5), block_id_str='16') x3 = make_yolo_depthwise_separable_head(x3, int(128 * alpha), block_id_str='16') y3 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(256 * alpha), (3, 3), block_id_str='16_3'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x3) #downsample fpn merge for feature map 3 & 2 x3_downsample = compose( ZeroPadding2D(((1, 0), (1, 0))), Darknet_Depthwise_Separable_Conv2D_BN_Leaky(int(256 * alpha), (3, 3), strides=(2, 2), block_id_str='16_4'))(x3) x2 = Concatenate()([x3_downsample, x2]) #feature map 2 output (26 x 26 x (512*alpha) for 416 input) #x2, y2 = make_depthwise_separable_last_layers(x2, int(256*alpha), num_anchors*(num_classes+5), block_id_str='17') x2 = make_yolo_depthwise_separable_head(x2, int(256 * alpha), block_id_str='17') y2 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(512 * alpha), (3, 3), block_id_str='17_3'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) #downsample fpn merge for feature map 2 & 1 x2_downsample = compose( ZeroPadding2D(((1, 0), (1, 0))), Darknet_Depthwise_Separable_Conv2D_BN_Leaky(int(512 * alpha), (3, 3), strides=(2, 2), block_id_str='17_4'))(x2) x1 = Concatenate()([x2_downsample, x1]) #feature map 1 output (13 x 13 x (1024*alpha) for 416 input) #x1, y1 = make_depthwise_separable_last_layers(x1, int(512*alpha), num_anchors*(num_classes+5), block_id_str='18') x1 = make_yolo_depthwise_separable_head(x1, int(512 * alpha), block_id_str='18') y1 = compose( Depthwise_Separable_Conv2D_BN_Leaky(int(1024 * alpha), (3, 3), block_id_str='18_3'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x1) return Model(inputs, [y1, y2, y3])
def _mobilenet_v1(): net = MobileNet(include_top=False) layer_names = ['conv_pw_5_relu', 'conv_pw_11_relu', 'conv_pw_13_relu'] output = [net.get_layer(n).output for n in layer_names] backbone = tf.keras.Model(net.input, output, name=name) return backbone