def mobilenet_16s(train_encoder=True,
                  final_layer_activation='sigmoid',
                  prep=True):
    '''
        This script creates a model object and loads pretrained weights 
    '''
    net = MobileNet(include_top=False, weights=None)
    if prep == True:
        net.load_weights(os.path.join('.', 'keras_preprocessing_weights.h5'),
                         by_name=True)
    else:
        net.load_weights(os.path.join('.', 'wences_preprocessing_weights.h5'),
                         by_name=True)

    for layer in net.layers:
        layer.trainable = train_encoder

    #build decoder
    predict = Conv2D(filters=1, kernel_size=1, strides=1)(net.output)
    deconv2 = Conv2DTranspose(filters=1,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              use_bias=False)(predict)
    pred_conv_pw_11_relu = Conv2D(filters=1, kernel_size=1, strides=1)(
        net.get_layer('conv_pw_11_relu').output)
    fuse1 = Add()([deconv2, pred_conv_pw_11_relu])
    deconv16 = Conv2DTranspose(filters=1,
                               kernel_size=32,
                               strides=16,
                               padding='same',
                               use_bias=False,
                               activation=final_layer_activation)(fuse1)

    return Model(inputs=net.input, outputs=deconv16)
Exemplo n.º 2
0
def load_mobilenet():
    """Loads the MobileNet model"""
    print("Loading the MobileNet model...")
    mobilenet = MobileNet(alpha=0.25)
    print("Model Loaded.")
    layer = mobilenet.get_layer('conv_pw_13_relu')
    return keras.Model(inputs=mobilenet.inputs, outputs=layer.output)
def mobilenet_8s(train_encoder=True,
                 final_layer_activation="sigmoid",
                 prep=True):
    """
    This script creates a model object and loads pretrained weights
    """

    net = MobileNet(include_top=False, weights=None)
    if prep == True:
        net.load_weights(os.path.join(".", "mn_classification_weights.h5"),
                         by_name=True)
    else:
        net.load_weights(os.path.join(".", "test_preprocessing_weights.h5"),
                         by_name=True)

    for layer in net.layers:
        layer.trainable = train_encoder

        # build decoder
        predict = Conv2D(filters=1, kernel_size=1, strides=1)(net.output)
        deconv2 = Conv2DTranspose(filters=1,
                                  kernel_size=4,
                                  strides=2,
                                  padding="same",
                                  use_bias=False)(predict)
        pred_conv_pw_11_relu = Conv2D(filters=1, kernel_size=1, strides=1)(
            net.get_layer("conv_pw_11_relu").output)
        fuse1 = Add()([deconv2, pred_conv_pw_11_relu])
        pred_conv_pw_5_relu = Conv2D(filters=1, kernel_size=1, strides=1)(
            net.get_layer("conv_pw_5_relu").output)
        deconv2fuse1 = Conv2DTranspose(filters=1,
                                       kernel_size=4,
                                       strides=2,
                                       padding="same",
                                       use_bias=False)(fuse1)
        fuse2 = Add()([deconv2fuse1, pred_conv_pw_5_relu])
        deconv8 = Conv2DTranspose(
            filters=1,
            kernel_size=16,
            strides=8,
            padding="same",
            use_bias=False,
            activation=final_layer_activation,
        )(fuse2)

        return Model(inputs=net.input, outputs=deconv8)
Exemplo n.º 4
0
def get_microclassifier(mc_model_fns, mc_intermediate_layers,
                        mobilenet_input_shape):
    """ Build a microclassifier (mobilenet -> microclassifier) """
    # Load pre-trained mobilenet and set all layers to not be trainable
    print("Loading weights from 224x224 MobileNet...", end=" ", flush=True)
    mobilenet_base_model = MobileNet(input_shape=(224, 224, 3),
                                     include_top=False,
                                     weights='imagenet',
                                     input_tensor=None,
                                     pooling=None)
    print("Done.")
    print("Initializing {}x{} MobileNet...".format(*mobilenet_input_shape),
          end=" ",
          flush=True)
    mobilenet_reshaped_model = MobileNet(input_shape=mobilenet_input_shape,
                                         include_top=False,
                                         weights=None,
                                         input_tensor=None,
                                         pooling=None)
    print("Copying weights from 224x224 MobileNet...", end=" ", flush=True)
    for reshaped_layer, layer in zip(mobilenet_reshaped_model.layers,
                                     mobilenet_base_model.layers):
        reshaped_layer.set_weights(layer.get_weights())
    print("Setting all MobileNet layers to be non-trainable...", flush=True)
    for layer in mobilenet_reshaped_model.layers:
        layer.trainable = False
    print("Done", flush=True)
    # Infer the input shape from the reshaped model
    full_mc_models = []
    for mc_model_fn, mc_intermediate_layer in zip(mc_model_fns,
                                                  mc_intermediate_layers):
        mc_input_shape = mobilenet_reshaped_model.get_layer(
            mc_intermediate_layer).output.shape[1:]
        mc_input_shape = tuple([int(dim) for dim in mc_input_shape])
        full_mc_models.append(
            mc_model_fn(mc_input_shape)(mobilenet_reshaped_model.get_layer(
                mc_intermediate_layer).output))
    full_model = Model(inputs=mobilenet_reshaped_model.input,
                       outputs=full_mc_models[0])
    full_model.summary()
    for layer in full_model.layers:
        if layer.trainable:
            print("Training: {}".format(layer.name))
    return full_model
Exemplo n.º 5
0
def example_feature_extract_Keras():

    #CNN = Xception()
    CNN = MobileNet()

    img = cv2.imread('data/ex09-natural/dog/dog_0000.jpg')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (224, 224)).astype(numpy.float32)
    model = Model(inputs=CNN.input, outputs=CNN.output)
    prob = model.predict(preprocess_input(numpy.array([img])))

    #model = Model(inputs=CNN.input, outputs=CNN.get_layer('avg_pool').output)
    model = Model(inputs=CNN.input,
                  outputs=CNN.get_layer('global_average_pooling2d_1').output)

    feature = model.predict(preprocess_input(numpy.array([img])))

    return
Exemplo n.º 6
0
def feature_obtainer_mb(augment_images_info, pre_train_model_type, y_dict):
    print("base model of MobileNet is loading!")
    base_model = MobileNet(weights='imagenet',
                           include_top=True)  # 加载VGG16模型及参数
    model = Model(inputs=base_model.input,
                  outputs=base_model.get_layer('reshape_2').output)
    print("loading finished!")
    X_list_train = []
    y_train = []
    X_list_test = []
    y_test = []
    for augment_image_info in augment_images_info:
        i = 0
        for item in augment_image_info["images_name"]:
            file_path = os.path.join(BASE_DIR, "aug_images",
                                     augment_image_info["model_belong"],
                                     augment_image_info["label_belong"], item)
            img = image.load_img(file_path, target_size=(224, 224))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            fc = model.predict(x)  # 获取VGG16/19全连接层特征
            if i <= 0.8 * len(augment_image_info["images_name"]):
                X_list_train.append(fc.tolist()[0])
                y_train.append(y_dict[augment_image_info["label_belong"]])
            else:
                X_list_test.append(fc.tolist()[0])
                y_test.append(y_dict[augment_image_info["label_belong"]])
            i += 1
    X_train = np.array(X_list_train)
    X_test = np.array(X_list_test)
    print("Features has been obtained !")
    K.clear_session()
    tf.reset_default_graph()
    print("memory has been cleared !")
    return X_train, y_train, X_test, y_test
Exemplo n.º 7
0
def SSD(input_shape, num_classes):
    """SSD300 architecture.

    # Arguments
        input_shape: Shape of the input image,
            expected to be either (300, 300, 3) or (3, 300, 300)(not tested).
        num_classes: Number of classes including background.

    # References
        https://arxiv.org/abs/1512.02325
    """
    img_size=(input_shape[1],input_shape[0])
    input_shape=(input_shape[1],input_shape[0],3)
    mobilenet_input_shape=(224,224,3)
    net={}
    net['input']=Input(input_shape)
    mobilenet=MobileNet(input_shape=mobilenet_input_shape,include_top=False,weights='imagenet')
    FeatureExtractor=Model(inputs=mobilenet.input,outputs=mobilenet.get_layer('conv_dw_11_relu').output)
    conv11=FeatureExtractor(net['input'])

    net['conv11'] = Conv2D(512, (1, 1),  padding='same', name='conv11')(conv11)
    net['conv11'] = BatchNormalization( momentum=0.99, name='bn11')(net['conv11'])
    net['conv11'] = Activation('relu')(net['conv11'])
    # Block
    #(19,19)
    net['conv12dw'] = SeparableConv2D(512, (3, 3),strides=(2, 2),  padding='same', name='conv12dw')(net['conv11'])
    net['conv12dw'] = BatchNormalization( momentum=0.99, name='bn12dw')(net['conv12dw'])
    net['conv12dw'] = Activation('relu')(net['conv12dw'])
    net['conv12'] = Conv2D(1024, (1, 1), padding='same',name='conv12')(net['conv12dw'])
    net['conv12'] = BatchNormalization( momentum=0.99, name='bn12')(net['conv12'])
    net['conv12'] = Activation('relu')(net['conv12'])
    net['conv13dw'] = SeparableConv2D(1024, (3, 3), padding='same',name='conv13dw')(net['conv12'])
    net['conv13dw'] = BatchNormalization( momentum=0.99, name='bn13dw')(net['conv13dw'])
    net['conv13dw'] = Activation('relu')(net['conv13dw'])
    net['conv13'] = Conv2D(1024, (1, 1), padding='same',name='conv13')(net['conv13dw'])
    net['conv13'] = BatchNormalization( momentum=0.99, name='bn13')(net['conv13'])
    net['conv13'] = Activation('relu')(net['conv13'])
    net['conv14_1'] = Conv2D(256, (1, 1),  padding='same', name='conv14_1')(net['conv13'])
    net['conv14_1'] = BatchNormalization( momentum=0.99, name='bn14_1')(net['conv14_1'])
    net['conv14_1'] = Activation('relu')(net['conv14_1'])
    net['conv14_2'] = Conv2D(512, (3, 3), strides=(2, 2),  padding='same', name='conv14_2')(net['conv14_1'])
    net['conv14_2'] = BatchNormalization( momentum=0.99, name='bn14_2')(net['conv14_2'])
    net['conv14_2'] = Activation('relu')(net['conv14_2'])
    net['conv15_1'] = Conv2D(128, (1, 1), padding='same',name='conv15_1')(net['conv14_2'])
    net['conv15_1'] = BatchNormalization( momentum=0.99, name='bn15_1')(net['conv15_1'])
    net['conv15_1'] = Activation('relu')(net['conv15_1'])
    net['conv15_2'] = Conv2D(256, (3, 3), strides=(2, 2), padding='same',name='conv15_2')(net['conv15_1'])
    net['conv15_2'] = BatchNormalization( momentum=0.99, name='bn15_2')(net['conv15_2'])
    net['conv15_2'] = Activation('relu')(net['conv15_2'])
    net['conv16_1'] = Conv2D(128, (1, 1),  padding='same', name='conv16_1')(net['conv15_2'])
    net['conv16_1'] = BatchNormalization( momentum=0.99, name='bn16_1')(net['conv16_1'])
    net['conv16_1'] = Activation('relu')(net['conv16_1'])
    net['conv16_2'] = Conv2D(256, (3, 3), strides=(2, 2),  padding='same', name='conv16_2')(net['conv16_1'])
    net['conv16_2'] = BatchNormalization( momentum=0.99, name='bn16_2')(net['conv16_2'])
    net['conv16_2'] = Activation('relu')(net['conv16_2'])
    net['conv17_1'] = Conv2D(64, (1, 1),  padding='same', name='conv17_1')(net['conv16_2'])
    net['conv17_1'] = BatchNormalization( momentum=0.99, name='bn17_1')(net['conv17_1'])
    net['conv17_1'] = Activation('relu')(net['conv17_1'])
    net['conv17_2'] = Conv2D(128, (3, 3), strides=(2, 2),  padding='same', name='conv17_2')(net['conv17_1'])
    net['conv17_2'] = BatchNormalization( momentum=0.99, name='bn17_2')(net['conv17_2'])
    net['conv17_2'] = Activation('relu')(net['conv17_2'])

    #Prediction from conv11
    num_priors = 3
    x = Conv2D(num_priors * 4, (1,1), padding='same',name='conv11_mbox_loc')(net['conv11'])
    net['conv11_mbox_loc'] = x
    flatten = Flatten(name='conv11_mbox_loc_flat')
    net['conv11_mbox_loc_flat'] = flatten(net['conv11_mbox_loc'])
    name = 'conv11_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Conv2D(num_priors * num_classes, (1,1), padding='same',name=name)(net['conv11'])
    net['conv11_mbox_conf'] = x
    flatten = Flatten(name='conv11_mbox_conf_flat')
    net['conv11_mbox_conf_flat'] = flatten(net['conv11_mbox_conf'])
    priorbox = PriorBox(img_size,60,max_size=None, aspect_ratios=[2],variances=[0.1, 0.1, 0.2, 0.2],name='conv11_mbox_priorbox')
    net['conv11_mbox_priorbox'] = priorbox(net['conv11'])
    # Prediction from conv13
    num_priors = 6
    net['conv13_mbox_loc'] = Conv2D(num_priors * 4, (1,1),padding='same',name='conv13_mbox_loc')(net['conv13'])
    flatten = Flatten(name='conv13_mbox_loc_flat')
    net['conv13_mbox_loc_flat'] = flatten(net['conv13_mbox_loc'])
    name = 'conv13_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    net['conv13_mbox_conf'] = Conv2D(num_priors * num_classes, (1,1),padding='same',name=name)(net['conv13'])
    flatten = Flatten(name='conv13_mbox_conf_flat')
    net['conv13_mbox_conf_flat'] = flatten(net['conv13_mbox_conf'])
    priorbox = PriorBox(img_size, 105.0, max_size=150.0, aspect_ratios=[2, 3],variances=[0.1, 0.1, 0.2, 0.2],name='conv13_mbox_priorbox')
    net['conv13_mbox_priorbox'] = priorbox(net['conv13'])
    # Prediction from conv12
    num_priors = 6
    x = Conv2D(num_priors * 4, (1,1), padding='same',name='conv14_2_mbox_loc')(net['conv14_2'])
    net['conv14_2_mbox_loc'] = x
    flatten = Flatten(name='conv14_2_mbox_loc_flat')
    net['conv14_2_mbox_loc_flat'] = flatten(net['conv14_2_mbox_loc'])
    name = 'conv14_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Conv2D(num_priors * num_classes, (1,1), padding='same',name=name)(net['conv14_2'])
    net['conv14_2_mbox_conf'] = x
    flatten = Flatten(name='conv14_2_mbox_conf_flat')
    net['conv14_2_mbox_conf_flat'] = flatten(net['conv14_2_mbox_conf'])
    priorbox = PriorBox(img_size, 150, max_size=195.0, aspect_ratios=[2, 3],variances=[0.1, 0.1, 0.2, 0.2],name='conv14_2_mbox_priorbox')
    net['conv14_2_mbox_priorbox'] = priorbox(net['conv14_2'])
    # Prediction from conv15_2_mbox
    num_priors = 6
    x = Conv2D(num_priors * 4, (1,1), padding='same',name='conv15_2_mbox_loc')(net['conv15_2'])
    net['conv15_2_mbox_loc'] = x
    flatten = Flatten(name='conv15_2_mbox_loc_flat')
    net['conv15_2_mbox_loc_flat'] = flatten(net['conv15_2_mbox_loc'])
    name = 'conv15_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Conv2D(num_priors * num_classes, (1,1), padding='same',name=name)(net['conv15_2'])
    net['conv15_2_mbox_conf'] = x
    flatten = Flatten(name='conv15_2_mbox_conf_flat')
    net['conv15_2_mbox_conf_flat'] = flatten(net['conv15_2_mbox_conf'])
    priorbox = PriorBox(img_size, 195.0, max_size=240.0, aspect_ratios=[2, 3],variances=[0.1, 0.1, 0.2, 0.2],name='conv15_2_mbox_priorbox')
    net['conv15_2_mbox_priorbox'] = priorbox(net['conv15_2'])

    # Prediction from conv16_2
    num_priors = 6
    x = Conv2D(num_priors * 4, (1,1), padding='same',name='conv16_2_mbox_loc')(net['conv16_2'])
    net['conv16_2_mbox_loc'] = x
    flatten = Flatten(name='conv16_2_mbox_loc_flat')
    net['conv16_2_mbox_loc_flat'] = flatten(net['conv16_2_mbox_loc'])
    name = 'conv16_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Conv2D(num_priors * num_classes, (1,1), padding='same',name=name)(net['conv16_2'])
    net['conv16_2_mbox_conf'] = x
    flatten = Flatten(name='conv16_2_mbox_conf_flat')
    net['conv16_2_mbox_conf_flat'] = flatten(net['conv16_2_mbox_conf'])
    priorbox = PriorBox(img_size, 240.0, max_size=285.0, aspect_ratios=[2, 3],variances=[0.1, 0.1, 0.2, 0.2],name='conv16_2_mbox_priorbox')
    net['conv16_2_mbox_priorbox'] = priorbox(net['conv16_2'])

    # Prediction from conv17_2
    num_priors = 6
    x = Conv2D(num_priors * 4,(1, 1), padding='same', name='conv17_2_mbox_loc')(net['conv17_2'])
    net['conv17_2_mbox_loc'] = x
    flatten = Flatten(name='conv17_2_mbox_loc_flat')
    net['conv17_2_mbox_loc_flat'] = flatten(net['conv17_2_mbox_loc'])
    name = 'conv17_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Conv2D(num_priors * num_classes, (1,1), padding='same', name=name)(net['conv17_2'])
    net['conv17_2_mbox_conf'] = x
    flatten = Flatten(name='conv17_2_mbox_conf_flat')
    net['conv17_2_mbox_conf_flat'] = flatten(net['conv17_2_mbox_conf'])
    priorbox = PriorBox(img_size, 285.0, max_size=300.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2],name='conv17_2_mbox_priorbox')
    net['conv17_2_mbox_priorbox'] = priorbox(net['conv17_2'])

    # Gather all predictions
    net['mbox_loc'] = concatenate([net['conv11_mbox_loc_flat'],net['conv13_mbox_loc_flat'],net['conv14_2_mbox_loc_flat'],net['conv15_2_mbox_loc_flat'],net['conv16_2_mbox_loc_flat'],net['conv17_2_mbox_loc_flat']],axis=1, name='mbox_loc')
    net['mbox_conf'] = concatenate([net['conv11_mbox_conf_flat'],net['conv13_mbox_conf_flat'],net['conv14_2_mbox_conf_flat'],net['conv15_2_mbox_conf_flat'],net['conv16_2_mbox_conf_flat'],net['conv17_2_mbox_conf_flat']],axis=1, name='mbox_conf')
    net['mbox_priorbox'] = concatenate([net['conv11_mbox_priorbox'],net['conv13_mbox_priorbox'],net['conv14_2_mbox_priorbox'],net['conv15_2_mbox_priorbox'],net['conv16_2_mbox_priorbox'],net['conv17_2_mbox_priorbox']],axis=1,name='mbox_priorbox')
    if hasattr(net['mbox_loc'], '_keras_shape'):
        num_boxes = net['mbox_loc']._keras_shape[-1] // 4
    elif hasattr(net['mbox_loc'], 'int_shape'):
        num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4
    net['mbox_loc'] = Reshape((num_boxes, 4),name='mbox_loc_final')(net['mbox_loc'])
    net['mbox_conf'] = Reshape((num_boxes, num_classes),name='mbox_conf_logits')(net['mbox_conf'])
    net['mbox_conf'] = Activation('softmax',name='mbox_conf_final')(net['mbox_conf'])
    net['predictions'] = concatenate([net['mbox_loc'],net['mbox_conf'],net['mbox_priorbox']],axis=2,name='predictions')
    model = Model(net['input'], net['predictions'])
    return model
 def __init__(self):
     net = MobileNet()
     self.convnet = Model(net.input, net.get_layer('conv_preds').output)
Exemplo n.º 9
0
class CNN_App_Keras(object):
    def __init__(self):
        self.name = 'CNN_App_Keras'
        self.input_shape = (224, 224)
        #self.model = Xception()
        self.model = MobileNet()
        self.class_names = tools_CNN_view.class_names

        return
# ----------------------------------------------------------------------------------------------------------------------

    def generate_features(self,
                          path_input,
                          path_output,
                          mask='*.png',
                          limit=1000000):

        if not os.path.exists(path_output):
            os.makedirs(path_output)
        else:
            tools_IO.remove_files(path_output)
            tools_IO.remove_folders(path_output)

        patterns = numpy.sort(
            numpy.array([
                f.path[len(path_input):] for f in os.scandir(path_input)
                if f.is_dir()
            ]))

        for each in patterns:
            print(each)
            local_filenames = numpy.array(
                fnmatch.filter(listdir(path_input + each), mask))[:limit]
            feature_filename = path_output + each + '.txt'
            features = []

            if not os.path.isfile(feature_filename):
                for i in range(0, local_filenames.shape[0]):
                    img = cv2.imread(path_input + each + '/' +
                                     local_filenames[i])
                    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                    img = cv2.resize(img,
                                     self.input_shape).astype(numpy.float32)
                    model = Model(inputs=self.model.input,
                                  outputs=self.model.get_layer(
                                      'global_average_pooling2d_1').output)
                    feature = model.predict(
                        preprocess_input(numpy.array([img])))[0]
                    features.append(feature)

                features = numpy.array(features)

                mat = numpy.zeros((features.shape[0],
                                   features.shape[1] + 1)).astype(numpy.str)
                mat[:, 0] = local_filenames
                mat[:, 1:] = features
                tools_IO.save_mat(mat, feature_filename, fmt='%s', delim='\t')

        return
# ----------------------------------------------------------------------------------------------------------------------

    def predict_classes(self,
                        path_input,
                        filename_output,
                        limit=1000000,
                        mask='*.png'):

        patterns = numpy.sort(
            numpy.array([
                f.path[len(path_input):] for f in os.scandir(path_input)
                if f.is_dir()
            ]))

        for each in patterns:
            print(each)
            local_filenames = numpy.array(
                fnmatch.filter(listdir(path_input + each), mask))[:limit]
            for i in range(0, local_filenames.shape[0]):
                img = cv2.imread(path_input + each + '/' + local_filenames[i])
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                img = cv2.resize(img, self.input_shape).astype(numpy.float32)

                model = Model(inputs=self.model.input,
                              outputs=self.model.output)
                prob = model.predict(preprocess_input(numpy.array([img])))[0]

                idx = numpy.argsort(-prob)[0]
                label = self.class_names[idx]
                tools_IO.save_labels(path_input + each + '/' + filename_output,
                                     numpy.array([local_filenames[i]]),
                                     numpy.array([label]),
                                     append=i,
                                     delim=' ')

        return
Exemplo n.º 10
0
def ssd_300(image_size,
            n_classes,
            input_tensor = None,
            mode='training',
            alpha=1.0,
            depth_multiplier = 1,
            min_scale=None,
            max_scale=None,
            scales=None,
            aspect_ratios_global=None,
            aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                     [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                     [1.0, 2.0, 0.5, 3.0, 1.0/3.0],
                                     [1.0, 2.0, 0.5],
                                     [1.0, 2.0, 0.5]],
            two_boxes_for_ar1=True,
            steps=[8, 16, 32, 64, 100, 300],
            offsets=None,
            clip_boxes=False,
            variances=[0.1, 0.1, 0.2, 0.2],
            coords='centroids',
            normalize_coords=True,
            subtract_mean=[123, 117, 104],
            divide_by_stddev=None,
            swap_channels=[2, 1, 0],
            confidence_thresh=0.01,
            iou_threshold=0.45,
            top_k=200,
            nms_max_output_size=400,
            return_predictor_sizes=False):
    '''
    Build a Keras model with SSD300 architecture, see references.

    The base network is a reduced atrous VGG-16, extended by the SSD architecture,
    as described in the paper.

    Most of the arguments that this function takes are only needed for the anchor
    box layers. In case you're training the network, the parameters passed here must
    be the same as the ones used to set up `SSDBoxEncoder`. In case you're loading
    trained weights, the parameters passed here must be the same as the ones used
    to produce the trained weights.

    Some of these arguments are explained in more detail in the documentation of the
    `SSDBoxEncoder` class.

    Note: Requires Keras v2.0 or later. Currently works only with the
    TensorFlow backend (v1.0 or later).

    Arguments:
        image_size (tuple): The input image size in the format `(height, width, channels)`.
        input_tensor: Tensor with shape (batch, height, width, channels)
        n_classes (int): The number of positive classes, e.g. 20 for Pascal VOC, 80 for MS COCO.
        mode (str, optional): One of 'training', 'inference' and 'inference_fast'. In 'training' mode,
            the model outputs the raw prediction tensor, while in 'inference' and 'inference_fast' modes,
            the raw predictions are decoded into absolute coordinates and filtered via confidence thresholding,
            non-maximum suppression, and top-k filtering. The difference between latter two modes is that
            'inference' follows the exact procedure of the original Caffe implementation, while
            'inference_fast' uses a faster prediction decoding procedure.
        min_scale (float, optional): The smallest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images.
        max_scale (float, optional): The largest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images. All scaling factors between the smallest and the
            largest will be linearly interpolated. Note that the second to last of the linearly interpolated
            scaling factors will actually be the scaling factor for the last predictor layer, while the last
            scaling factor is used for the second box for aspect ratio 1 in the last predictor layer
            if `two_boxes_for_ar1` is `True`.
        scales (list, optional): A list of floats containing scaling factors per convolutional predictor layer.
            This list must be one element longer than the number of predictor layers. The first `k` elements are the
            scaling factors for the `k` predictor layers, while the last element is used for the second box
            for aspect ratio 1 in the last predictor layer if `two_boxes_for_ar1` is `True`. This additional
            last scaling factor must be passed either way, even if it is not being used. If a list is passed,
            this argument overrides `min_scale` and `max_scale`. All scaling factors must be greater than zero.
        aspect_ratios_global (list, optional): The list of aspect ratios for which anchor boxes are to be
            generated. This list is valid for all prediction layers.
        aspect_ratios_per_layer (list, optional): A list containing one aspect ratio list for each prediction layer.
            This allows you to set the aspect ratios for each predictor layer individually, which is the case for the
            original SSD300 implementation. If a list is passed, it overrides `aspect_ratios_global`.
        two_boxes_for_ar1 (bool, optional): Only relevant for aspect ratio lists that contain 1. Will be ignored otherwise.
            If `True`, two anchor boxes will be generated for aspect ratio 1. The first will be generated
            using the scaling factor for the respective layer, the second one will be generated using
            geometric mean of said scaling factor and next bigger scaling factor.
        steps (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either ints/floats or tuples of two ints/floats. These numbers represent for each predictor layer how many
            pixels apart the anchor box center points should be vertically and horizontally along the spatial grid over
            the image. If the list contains ints/floats, then that value will be used for both spatial dimensions.
            If the list contains tuples of two ints/floats, then they represent `(step_height, step_width)`.
            If no steps are provided, then they will be computed such that the anchor box center points will form an
            equidistant grid within the image dimensions.
        offsets (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either floats or tuples of two floats. These numbers represent for each predictor layer how many
            pixels from the top and left boarders of the image the top-most and left-most anchor box center points should be
            as a fraction of `steps`. The last bit is important: The offsets are not absolute pixel values, but fractions
            of the step size specified in the `steps` argument. If the list contains floats, then that value will
            be used for both spatial dimensions. If the list contains tuples of two floats, then they represent
            `(vertical_offset, horizontal_offset)`. If no offsets are provided, then they will default to 0.5 of the step size.
        clip_boxes (bool, optional): If `True`, clips the anchor box coordinates to stay within image boundaries.
        variances (list, optional): A list of 4 floats >0. The anchor box offset for each coordinate will be divided by
            its respective variance value.
        coords (str, optional): The box coordinate format to be used internally by the model (i.e. this is not the input format
            of the ground truth labels). Can be either 'centroids' for the format `(cx, cy, w, h)` (box center coordinates, width,
            and height), 'minmax' for the format `(xmin, xmax, ymin, ymax)`, or 'corners' for the format `(xmin, ymin, xmax, ymax)`.
        normalize_coords (bool, optional): Set to `True` if the model is supposed to use relative instead of absolute coordinates,
            i.e. if the model predicts box coordinates within [0,1] instead of absolute coordinates.
        subtract_mean (array-like, optional): `None` or an array-like object of integers or floating point values
            of any shape that is broadcast-compatible with the image shape. The elements of this array will be
            subtracted from the image pixel intensity values. For example, pass a list of three integers
            to perform per-channel mean normalization for color images.
        divide_by_stddev (array-like, optional): `None` or an array-like object of non-zero integers or
            floating point values of any shape that is broadcast-compatible with the image shape. The image pixel
            intensity values will be divided by the elements of this array. For example, pass a list
            of three integers to perform per-channel standard deviation normalization for color images.
        swap_channels (list, optional): Either `False` or a list of integers representing the desired order in which the input
            image channels should be swapped.
        confidence_thresh (float, optional): A float in [0,1), the minimum classification confidence in a specific
            positive class in order to be considered for the non-maximum suppression stage for the respective class.
            A lower value will result in a larger part of the selection process being done by the non-maximum suppression
            stage, while a larger value will result in a larger part of the selection process happening in the confidence
            thresholding stage.
        iou_threshold (float, optional): A float in [0,1]. All boxes that have a Jaccard similarity of greater than `iou_threshold`
            with a locally maximal box will be removed from the set of predictions for a given class, where 'maximal' refers
            to the box's confidence score.
        top_k (int, optional): The number of highest scoring predictions to be kept for each batch item after the
            non-maximum suppression stage.
        nms_max_output_size (int, optional): The maximal number of predictions that will be left over after the NMS stage.
        return_predictor_sizes (bool, optional): If `True`, this function not only returns the model, but also
            a list containing the spatial dimensions of the predictor layers. This isn't strictly necessary since
            you can always get their sizes easily via the Keras API, but it's convenient and less error-prone
            to get them this way. They are only relevant for training anyway (SSDBoxEncoder needs to know the
            spatial dimensions of the predictor layers), for inference you don't need them.

    Returns:
        model: The Keras SSD300 model.
        predictor_sizes (optional): A Numpy array containing the `(height, width)` portion
            of the output tensor shape for each convolutional predictor layer. During
            training, the generator function needs this in order to transform
            the ground truth labels into tensors of identical structure as the
            output tensors of the model, which is in turn needed for the cost
            function.

    References:
        https://arxiv.org/abs/1512.02325v5
    '''

    n_predictor_layers = 6 # The number of predictor conv layers in the network is 6 for the original SSD300.
    n_classes += 1 # Account for the background class.
    img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2]

    ############################################################################
    # Get a few exceptions out of the way.
    ############################################################################

    if aspect_ratios_global is None and aspect_ratios_per_layer is None:
        raise ValueError("`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified.")
    if aspect_ratios_per_layer:
        if len(aspect_ratios_per_layer) != n_predictor_layers:
            raise ValueError("It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}.".format(n_predictor_layers, len(aspect_ratios_per_layer)))

    if (min_scale is None or max_scale is None) and scales is None:
        raise ValueError("Either `min_scale` and `max_scale` or `scales` need to be specified.")
    if scales:
        if len(scales) != n_predictor_layers+1:
            raise ValueError("It must be either scales is None or len(scales) == {}, but len(scales) == {}.".format(n_predictor_layers+1, len(scales)))
    else: # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
        scales = np.linspace(min_scale, max_scale, n_predictor_layers+1)

    if len(variances) != 4:
        raise ValueError("4 variance values must be pased, but {} values were received.".format(len(variances)))
    variances = np.array(variances)
    if np.any(variances <= 0):
        raise ValueError("All variances must be >0, but the variances given are {}".format(variances))

    if (not (steps is None)) and (len(steps) != n_predictor_layers):
        raise ValueError("You must provide at least one step value per predictor layer.")

    if (not (offsets is None)) and (len(offsets) != n_predictor_layers):
        raise ValueError("You must provide at least one offset value per predictor layer.")

    ############################################################################
    # Compute the anchor box parameters.
    ############################################################################

    # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
    if aspect_ratios_per_layer:
        aspect_ratios = aspect_ratios_per_layer
    else:
        aspect_ratios = [aspect_ratios_global] * n_predictor_layers

    # Compute the number of boxes to be predicted per cell for each predictor layer.
    # We need this so that we know how many channels the predictor layers need to have.
    if aspect_ratios_per_layer:
        n_boxes = []
        for ar in aspect_ratios_per_layer:
            if (1 in ar) & two_boxes_for_ar1:
                n_boxes.append(len(ar) + 1) # +1 for the second box for aspect ratio 1
            else:
                n_boxes.append(len(ar))
    else: # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
        if (1 in aspect_ratios_global) & two_boxes_for_ar1:
            n_boxes = len(aspect_ratios_global) + 1
        else:
            n_boxes = len(aspect_ratios_global)
        n_boxes = [n_boxes] * n_predictor_layers

    if steps is None:
        steps = [None] * n_predictor_layers
    if offsets is None:
        offsets = [None] * n_predictor_layers

    ############################################################################
    # Define functions for the Lambda layers below.
    ############################################################################

    def identity_layer(tensor):
        return tensor

    def input_mean_normalization(tensor):
        return tensor - np.array(subtract_mean)

    def input_stddev_normalization(tensor):
        return tensor / np.array(divide_by_stddev)

    def input_channel_swap(tensor):
        if len(swap_channels) == 3:
            return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]]], axis=-1)
        elif len(swap_channels) == 4:
            return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]], tensor[...,swap_channels[3]]], axis=-1)

    ############################################################################# 
    # Functions for Mobilenet architeture
    #############################################################################

    def relu6(x):
        return K.relu(x, max_value=6)
    
    def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
    	channel_axis = -1
    	filters = int(filters * alpha)
    	x = ZeroPadding2D(padding=(1, 1), name='conv1_pad')(inputs)
    	x = Conv2D(filters, kernel,padding='valid',use_bias=False,strides=strides,name='conv1')(x)
    	x = BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
    	return Activation(relu6, name='conv1_relu')(x)


    def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1):
    	channel_axis = -1
    	pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    	x = ZeroPadding2D(padding=(1, 1), name='conv_pad_%d' % block_id)(inputs)
    	x = DepthwiseConv2D((3, 3),padding='valid',depth_multiplier=depth_multiplier,strides=strides,use_bias=False,name='conv_dw_%d' % block_id)(x)
    	x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x)
    	x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)
    	x = Conv2D(pointwise_conv_filters, (1, 1),padding='same',use_bias=False,strides=(1, 1),name='conv_pw_%d' % block_id)(x)
    	x = BatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x)
    	return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)

    def _depthwise_conv_block_f(inputs, depth_multiplier=1, strides=(1, 1), block_id=1):
    	channel_axis = -1
    	x = ZeroPadding2D(padding=(1, 1), name='conv_pad_%d'  % block_id)(inputs)
    	x = DepthwiseConv2D((3, 3),padding='valid',depth_multiplier=depth_multiplier,strides=strides,use_bias=False,name='conv_dw_%d' % block_id)(x)
    	x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x)
    	return Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)

    def _conv_blockSSD_f(inputs, filters, alpha, kernel, strides,block_id=11, apply_alpha=True):
    	if apply_alpha:
    		filters = int(filters * alpha)
    	channel_axis = -1
    	Conv = Conv2D(filters, kernel,padding='valid',use_bias=False,strides=strides,name='conv__%d' % block_id)(inputs)
    	x = BatchNormalization(axis=channel_axis, name='conv_%d_bn' % block_id)(Conv)
    	return Activation(relu6, name='conv_%d_relu' % block_id)(x), Conv

    def _conv_blockSSD(inputs, filters,block_id=11):
    	channel_axis = -1
    	x = ZeroPadding2D(padding=(1, 1), name='conv_pad_%d_1' % block_id)(inputs)
    	x = Conv2D(filters, (1,1),padding='valid',use_bias=False,strides=(1, 1),name='conv__%d_1'%block_id)(x)
    	x = BatchNormalization(axis=channel_axis, name='conv_%d_bn_1'% block_id)(x)
    	x = Activation(relu6, name='conv_%d_relu_1'% block_id)(x)
    	Conv = Conv2D(filters*2, (3,3), padding='valid', use_bias=False, strides=(2, 2), name='conv__%d_2' % block_id)(x)
    	x = BatchNormalization(axis=channel_axis, name='conv_%d_bn_2' % block_id)(Conv)
    	x = Activation(relu6, name='conv_%d_relu_2' % block_id)(x)
    	return x,Conv
    
    ############################################################################
    # Build the network.
    ############################################################################

    if input_tensor != None:
        x = Input(tensor=input_tensor, shape=(img_height, img_width, img_channels))
    else:
        x = Input(shape=(img_height, img_width, img_channels))

    # The following identity layer is only needed so that the subsequent lambda layers can be optional.
    x1 = Lambda(identity_layer, output_shape=(img_height, img_width, img_channels), name='identity_layer')(x)
    if not (divide_by_stddev is None):
        x1 = Lambda(input_stddev_normalization, output_shape=(img_height, img_width, img_channels), name='input_stddev_normalization')(x1)
    if not (subtract_mean is None):
        x1 = Lambda(input_mean_normalization, output_shape=(img_height, img_width, img_channels), name='input_mean_normalization')(x1)
    if swap_channels:
        x1 = Lambda(input_channel_swap, output_shape=(img_height, img_width, img_channels), name='input_channel_swap')(x1)

    # Get mobilenet architecture with imagenet weights
    mobilenet_input_shape = (224, 224, 3)
    mobilenet = MobileNet(input_shape=mobilenet_input_shape, include_top=False, weights=None, alpha=alpha)
    FeatureExtractor = Model(inputs=mobilenet.input, outputs=mobilenet.get_layer('conv_dw_11_relu').output)

    layer = FeatureExtractor(x1)
    layer, conv11 =_conv_blockSSD_f(layer, 512, alpha, kernel=(1, 1), strides=(1, 1),block_id=11)
    layer = _depthwise_conv_block(layer, 512, alpha, depth_multiplier,strides=(2, 2), block_id=12)
    layer = _depthwise_conv_block_f(layer, depth_multiplier,strides=(1, 1), block_id=13)
    layer, conv13 = _conv_blockSSD_f(layer, 1024, alpha, kernel=(1, 1), strides=(1, 1), block_id=13, apply_alpha=True)
    layer, conv14_2 = _conv_blockSSD(layer, 256, block_id=14)
    layer, conv15_2 = _conv_blockSSD(layer, 128, block_id=15)
    layer, conv16_2 = _conv_blockSSD(layer, 128, block_id=16)
    layer, conv17_2 = _conv_blockSSD(layer, 64, block_id=17)

    ### Build the convolutional predictor layers on top of the base network

    # We precidt `n_classes` confidence values for each box, hence the confidence predictors have depth `n_boxes * n_classes`
    # Output shape of the confidence layers: `(batch, height, width, n_boxes * n_classes)`
    conv11_mbox_conf = Conv2D(n_boxes[0] * n_classes, (3, 3), padding='same', name='conv11_mbox_conf')(conv11)
    conv13_mbox_conf = Conv2D(n_boxes[1] * n_classes, (3, 3), padding='same', name='conv13_mbox_conf')(conv13)
    conv14_2_mbox_conf = Conv2D(n_boxes[2] * n_classes, (3, 3), padding='same', name='conv14_2_mbox_conf')(conv14_2)
    conv15_2_mbox_conf = Conv2D(n_boxes[3] * n_classes, (3, 3), padding='same', name='conv15_2_mbox_conf')(conv15_2)
    conv16_2_mbox_conf = Conv2D(n_boxes[4] * n_classes, (3, 3), padding='same', name='conv16_2_mbox_conf')(conv16_2)
    conv17_2_mbox_conf = Conv2D(n_boxes[5] * n_classes, (3, 3), padding='same', name='conv17_2_mbox_conf')(conv17_2)

    # We predict 4 box coordinates for each box, hence the localization predictors have depth `n_boxes * 4`
    # Output shape of the localization layers: `(batch, height, width, n_boxes * 4)`
    conv11_mbox_loc = Conv2D(n_boxes[0] * 4, (3, 3), padding='same', name='conv11_mbox_loc')(conv11)
    conv13_mbox_loc = Conv2D(n_boxes[1] * 4, (3, 3), padding='same', name='conv13_mbox_loc')(conv13)
    conv14_2_mbox_loc = Conv2D(n_boxes[2] * 4, (3, 3), padding='same', name='conv14_2_mbox_loc')(conv14_2)
    conv15_2_mbox_loc = Conv2D(n_boxes[3] * 4, (3, 3), padding='same', name='conv15_2_mbox_loc')(conv15_2)
    conv16_2_mbox_loc = Conv2D(n_boxes[4] * 4, (3, 3), padding='same', name='conv16_2_mbox_loc')(conv16_2)
    conv17_2_mbox_loc = Conv2D(n_boxes[5] * 4, (3, 3), padding='same', name='conv17_2_mbox_loc')(conv17_2)

    ### Generate the anchor boxes (called "priors" in the original Caffe/C++ implementation, so I'll keep their layer names)

    # Output shape of anchors: `(batch, height, width, n_boxes, 8)`
    conv11_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios[0],
                                      two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[0], this_offsets=offsets[0], clip_boxes=clip_boxes,
                                      variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv11_mbox_priorbox')(conv11_mbox_loc)
    conv13_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios[1],
                                      two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[1], this_offsets=offsets[1], clip_boxes=clip_boxes,
                                      variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv13_mbox_priorbox')(conv13_mbox_loc)
    conv14_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios[2],
                                        two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[2], this_offsets=offsets[2], clip_boxes=clip_boxes,
                                        variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv14_2_mbox_priorbox')(conv14_2_mbox_loc)
    conv15_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios[3],
                                        two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[3], this_offsets=offsets[3], clip_boxes=clip_boxes,
                                        variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv15_2_mbox_priorbox')(conv15_2_mbox_loc)
    conv16_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[4], next_scale=scales[5], aspect_ratios=aspect_ratios[4],
                                        two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[4], this_offsets=offsets[4], clip_boxes=clip_boxes,
                                        variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv16_2_mbox_priorbox')(conv16_2_mbox_loc)
    conv17_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[5], next_scale=scales[6], aspect_ratios=aspect_ratios[5],
                                        two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[5], this_offsets=offsets[5], clip_boxes=clip_boxes,
                                        variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv17_2_mbox_priorbox')(conv17_2_mbox_loc)

    ### Reshape

    # Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
    # We want the classes isolated in the last axis to perform softmax on them
    conv11_mbox_conf_reshape = Reshape((-1, n_classes), name='conv11_mbox_conf_reshape')(conv11_mbox_conf)
    conv13_mbox_conf_reshape = Reshape((-1, n_classes), name='conv13_mbox_conf_reshape')(conv13_mbox_conf)
    conv14_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv14_2_mbox_conf_reshape')(conv14_2_mbox_conf)
    conv15_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv15_2_mbox_conf_reshape')(conv15_2_mbox_conf)
    conv16_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv16_2_mbox_conf_reshape')(conv16_2_mbox_conf)
    conv17_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv17_2_mbox_conf_reshape')(conv17_2_mbox_conf)
    
    # Reshape the box predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
    # We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
    conv11_mbox_loc_reshape = Reshape((-1, 4), name='conv11_mbox_loc_reshape')(conv11_mbox_loc)
    conv13_mbox_loc_reshape = Reshape((-1, 4), name='conv13_mbox_loc_reshape')(conv13_mbox_loc)
    conv14_2_mbox_loc_reshape = Reshape((-1, 4), name='conv14_2_mbox_loc_reshape')(conv14_2_mbox_loc)
    conv15_2_mbox_loc_reshape = Reshape((-1, 4), name='conv15_2_mbox_loc_reshape')(conv15_2_mbox_loc)
    conv16_2_mbox_loc_reshape = Reshape((-1, 4), name='conv16_2_mbox_loc_reshape')(conv16_2_mbox_loc)
    conv17_2_mbox_loc_reshape = Reshape((-1, 4), name='conv17_2_mbox_loc_reshape')(conv17_2_mbox_loc)
    
    # Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)`
    conv11_mbox_priorbox_reshape = Reshape((-1, 8), name='conv11_mbox_priorbox_reshape')(conv11_mbox_priorbox)
    conv13_mbox_priorbox_reshape = Reshape((-1, 8), name='conv13_mbox_priorbox_reshape')(conv13_mbox_priorbox)
    conv14_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv14_2_mbox_priorbox_reshape')(conv14_2_mbox_priorbox)
    conv15_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv15_2_mbox_priorbox_reshape')(conv15_2_mbox_priorbox)
    conv16_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv16_2_mbox_priorbox_reshape')(conv16_2_mbox_priorbox)
    conv17_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv17_2_mbox_priorbox_reshape')(conv17_2_mbox_priorbox)

    ### Concatenate the predictions from the different layers

    # Axis 0 (batch) and axis 2 (n_classes or 4, respectively) are identical for all layer predictions,
    # so we want to concatenate along axis 1, the number of boxes per layer
    # Output shape of `mbox_conf`: (batch, n_boxes_total, n_classes)
    mbox_conf = Concatenate(axis=1, name='mbox_conf')([conv11_mbox_conf_reshape,
                                                       conv13_mbox_conf_reshape,
                                                       conv14_2_mbox_conf_reshape,
                                                       conv15_2_mbox_conf_reshape,
                                                       conv16_2_mbox_conf_reshape,
                                                       conv17_2_mbox_conf_reshape])

    # Output shape of `mbox_loc`: (batch, n_boxes_total, 4)
    mbox_loc = Concatenate(axis=1, name='mbox_loc')([conv11_mbox_loc_reshape,
                                                     conv13_mbox_loc_reshape,
                                                     conv14_2_mbox_loc_reshape,
                                                     conv15_2_mbox_loc_reshape,
                                                     conv16_2_mbox_loc_reshape,
                                                     conv17_2_mbox_loc_reshape])

    # Output shape of `mbox_priorbox`: (batch, n_boxes_total, 8)
    mbox_priorbox = Concatenate(axis=1, name='mbox_priorbox')([conv11_mbox_priorbox_reshape,
                                                               conv13_mbox_priorbox_reshape,
                                                               conv14_2_mbox_priorbox_reshape,
                                                               conv15_2_mbox_priorbox_reshape,
                                                               conv16_2_mbox_priorbox_reshape,
                                                               conv17_2_mbox_priorbox_reshape])

    # The box coordinate predictions will go into the loss function just the way they are,
    # but for the class predictions, we'll apply a softmax activation layer first
    mbox_conf_softmax = Activation('softmax', name='mbox_conf_softmax')(mbox_conf)

    # Concatenate the class and box predictions and the anchors to one large predictions vector
    # Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8)
    predictions = Concatenate(axis=2, name='predictions')([mbox_conf_softmax, mbox_loc, mbox_priorbox])

    if mode == 'training':
        model = Model(inputs=x, outputs=predictions)
    elif mode == 'inference':
        decoded_predictions = DecodeDetections(confidence_thresh=confidence_thresh,
                                               iou_threshold=iou_threshold,
                                               top_k=top_k,
                                               nms_max_output_size=nms_max_output_size,
                                               coords=coords,
                                               normalize_coords=normalize_coords,
                                               img_height=img_height,
                                               img_width=img_width,
                                               name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    elif mode == 'inference_fast':
        decoded_predictions = DecodeDetectionsFast(confidence_thresh=confidence_thresh,
                                                   iou_threshold=iou_threshold,
                                                   top_k=top_k,
                                                   nms_max_output_size=nms_max_output_size,
                                                   coords=coords,
                                                   normalize_coords=normalize_coords,
                                                   img_height=img_height,
                                                   img_width=img_width,
                                                   name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    else:
        raise ValueError("`mode` must be one of 'training', 'inference' or 'inference_fast', but received '{}'.".format(mode))

    if return_predictor_sizes:
        predictor_sizes = np.array([conv4_3_norm_mbox_conf._keras_shape[1:3],
                                     fc7_mbox_conf._keras_shape[1:3],
                                     conv6_2_mbox_conf._keras_shape[1:3],
                                     conv7_2_mbox_conf._keras_shape[1:3],
                                     conv8_2_mbox_conf._keras_shape[1:3],
                                     conv9_2_mbox_conf._keras_shape[1:3]])
        return model, predictor_sizes
    else:
        return model
Exemplo n.º 11
0
def mobilenet_ssd_300(image_size,
                      n_classes,
                      mode='training',
                      l2_regularization=0.0005,
                      min_scale=None,
                      max_scale=None,
                      scales=None,
                      aspect_ratios_global=None,
                      aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                               [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                               [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                               [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                               [1.0, 2.0, 0.5],
                                               [1.0, 2.0, 0.5]],
                      two_boxes_for_ar1=True,
                      steps=[8, 16, 32, 64, 100, 300],
                      offsets=None,
                      clip_boxes=False,
                      variances=[0.1, 0.1, 0.2, 0.2],
                      coords='centroids',
                      normalize_coords=True,
                      subtract_mean=[123, 117, 104],
                      divide_by_stddev=None,
                      swap_channels=[2, 1, 0],
                      confidence_thresh=0.01,
                      iou_threshold=0.45,
                      top_k=200,
                      nms_max_output_size=400,
                      return_predictor_sizes=False):

    n_predictor_layers = 6  # The number of predictor conv layers in the network is 6 for the original SSD300.
    n_classes += 1  # Account for the background class.
    l2_reg = l2_regularization  # Make the internal name shorter.
    img_height, img_width, img_channels = image_size[0], image_size[
        1], image_size[2]

    ############################################################################
    # Get a few exceptions out of the way.
    ############################################################################

    if aspect_ratios_global is None and aspect_ratios_per_layer is None:
        raise ValueError(
            "`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified."
        )
    if aspect_ratios_per_layer:
        if len(aspect_ratios_per_layer) != n_predictor_layers:
            raise ValueError(
                "It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}."
                .format(n_predictor_layers, len(aspect_ratios_per_layer)))

    if (min_scale is None or max_scale is None) and scales is None:
        raise ValueError(
            "Either `min_scale` and `max_scale` or `scales` need to be specified."
        )
    if scales:
        if len(scales) != n_predictor_layers + 1:
            raise ValueError(
                "It must be either scales is None or len(scales) == {}, but len(scales) == {}."
                .format(n_predictor_layers + 1, len(scales)))
    else:  # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
        scales = np.linspace(min_scale, max_scale, n_predictor_layers + 1)

    if len(variances) != 4:
        raise ValueError(
            "4 variance values must be pased, but {} values were received.".
            format(len(variances)))
    variances = np.array(variances)
    if np.any(variances <= 0):
        raise ValueError(
            "All variances must be >0, but the variances given are {}".format(
                variances))

    if (not (steps is None)) and (len(steps) != n_predictor_layers):
        raise ValueError(
            "You must provide at least one step value per predictor layer.")

    if (not (offsets is None)) and (len(offsets) != n_predictor_layers):
        raise ValueError(
            "You must provide at least one offset value per predictor layer.")

    ############################################################################
    # Compute the anchor box parameters.
    ############################################################################

    # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
    if aspect_ratios_per_layer:
        aspect_ratios = aspect_ratios_per_layer
    else:
        aspect_ratios = [aspect_ratios_global] * n_predictor_layers

    # Compute the number of boxes to be predicted per cell for each predictor layer.
    # We need this so that we know how many channels the predictor layers need to have.
    if aspect_ratios_per_layer:
        n_boxes = []
        for ar in aspect_ratios_per_layer:
            if (1 in ar) & two_boxes_for_ar1:
                n_boxes.append(len(ar) +
                               1)  # +1 for the second box for aspect ratio 1
            else:
                n_boxes.append(len(ar))
    else:  # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
        if (1 in aspect_ratios_global) & two_boxes_for_ar1 & len(n_boxes) != 0:
            n_boxes = len(aspect_ratios_global) + 1
        else:
            n_boxes = len(aspect_ratios_global)
        n_boxes = [n_boxes] * n_predictor_layers

    if steps is None:
        steps = [None] * n_predictor_layers
    if offsets is None:
        offsets = [None] * n_predictor_layers

    # print("Boxes:{}".format(n_boxes))

    ############################################################################
    # Define functions for the Lambda layers below.
    ############################################################################

    def identity_layer(tensor):
        return tensor

    def input_mean_normalization(tensor):
        return tensor - np.array(subtract_mean)

    def input_stddev_normalization(tensor):
        return tensor / np.array(divide_by_stddev)

    def input_channel_swap(tensor):
        if len(swap_channels) == 3:
            return K.stack([
                tensor[..., swap_channels[0]], tensor[..., swap_channels[1]],
                tensor[..., swap_channels[2]]
            ],
                           axis=-1)
        elif len(swap_channels) == 4:
            return K.stack([
                tensor[..., swap_channels[0]], tensor[..., swap_channels[1]],
                tensor[..., swap_channels[2]], tensor[..., swap_channels[3]]
            ],
                           axis=-1)

    ############################################################################
    # Build the network.
    ############################################################################

    x = Input(shape=(img_height, img_width, img_channels))

    # The following identity layer is only needed so that the subsequent lambda layers can be optional.
    x1 = Lambda(identity_layer,
                output_shape=(img_height, img_width, img_channels),
                name='identity_layer')(x)
    if not (subtract_mean is None):
        x1 = Lambda(input_mean_normalization,
                    output_shape=(img_height, img_width, img_channels),
                    name='input_mean_normalization')(x1)
    if not (divide_by_stddev is None):
        x1 = Lambda(input_stddev_normalization,
                    output_shape=(img_height, img_width, img_channels),
                    name='input_stddev_normalization')(x1)
    if swap_channels:
        x1 = Lambda(input_channel_swap,
                    output_shape=(img_height, img_width, img_channels),
                    name='input_channel_swap')(x1)

    mobilenet = MobileNet(input_shape=(224, 224, 3),
                          include_top=False,
                          weights='imagenet')

    FeatureExtractor = Model(
        inputs=mobilenet.input,
        outputs=mobilenet.get_layer('conv_pw_5_relu').output)
    mobilenet_conv_pw_5_relu = FeatureExtractor(x1)

    conv6dw = SeparableConv2D(256, (3, 3),
                              padding='same',
                              strides=(2, 2),
                              name='conv_dw_6')(mobilenet_conv_pw_5_relu)
    conv6dw = BatchNormalization(momentum=0.99, name='conv_dw_6_bn')(conv6dw)
    conv6dw = ReLU(6., name='conv_dw_6_relu')(conv6dw)

    conv6pw = Conv2D(256, (1, 1), padding='same', name='conv_pw_6')(conv6dw)
    conv6pw = BatchNormalization(momentum=0.99, name='conv_pw_6_bn')(conv6pw)
    conv6pw = ReLU(6., name='conv_pw_6_relu')(conv6pw)

    conv7dw = SeparableConv2D(512, (3, 3), padding='same',
                              name='conv_dw_7')(conv6pw)
    conv7dw = BatchNormalization(momentum=0.99, name='conv_dw_7_bn')(conv7dw)
    conv7dw = ReLU(6., name='conv_dw_7_relu')(conv7dw)

    conv7pw = Conv2D(512, (1, 1), padding='same', name='conv_pw_7')(conv7dw)
    conv7pw = BatchNormalization(momentum=0.99, name='conv_pw_7_bn')(conv7pw)
    conv7pw = ReLU(6., name='conv_pw_7_relu')(conv7pw)

    conv8dw = SeparableConv2D(512, (3, 3), padding='same',
                              name='conv_dw_8')(conv7pw)
    conv8dw = BatchNormalization(momentum=0.99, name='conv_dw_8_bn')(conv8dw)
    conv8dw = ReLU(6., name='conv_dw_8_relu')(conv8dw)

    conv8pw = Conv2D(512, (1, 1), padding='same', name='conv_pw_8')(conv8dw)
    conv8pw = BatchNormalization(momentum=0.99, name='conv_pw_8_bn')(conv8pw)
    conv8pw = ReLU(6., name='conv_pw_8_relu')(conv8pw)

    conv9dw = SeparableConv2D(512, (3, 3), padding='same',
                              name='conv_dw_9')(conv8pw)
    conv9dw = BatchNormalization(momentum=0.99, name='conv_dw_9_bn')(conv9dw)
    conv9dw = ReLU(6., name='conv_dw_9_relu')(conv9dw)

    conv9pw = Conv2D(512, (1, 1), padding='same', name='conv_pw_9')(conv9dw)
    conv9pw = BatchNormalization(momentum=0.99, name='conv_pw_9_bn')(conv9pw)
    conv9pw = ReLU(6., name='conv_pw_9_relu')(conv9pw)

    conv10dw = SeparableConv2D(512, (3, 3), padding='same',
                               name='conv_dw_10')(conv9pw)
    conv10dw = BatchNormalization(momentum=0.99,
                                  name='conv_dw_10_bn')(conv10dw)
    conv10dw = ReLU(6., name='conv_dw_10_relu')(conv10dw)

    conv10pw = Conv2D(512, (1, 1), padding='same', name='conv_pw_10')(conv10dw)
    conv10pw = BatchNormalization(momentum=0.99,
                                  name='conv_pw_10_bn')(conv10pw)
    conv10pw = ReLU(6., name='conv_pw_10_relu')(conv10pw)

    conv11dw = SeparableConv2D(512, (3, 3), padding='same',
                               name='conv_dw_11')(conv10pw)
    conv11dw = BatchNormalization(momentum=0.99,
                                  name='conv_dw_11_bn')(conv11dw)
    conv11dw = ReLU(6., name='conv_dw_11_relu')(conv11dw)

    conv11pw = Conv2D(512, (1, 1), padding='same', name='conv_pw_11')(conv11dw)
    conv11pw = BatchNormalization(momentum=0.99,
                                  name='conv_pw_11_bn')(conv11pw)
    conv11pw = ReLU(6., name='conv_pw_11_relu')(conv11pw)

    conv12dw = SeparableConv2D(512, (3, 3),
                               strides=(2, 2),
                               padding='same',
                               name='conv_dw_12')(conv11pw)
    conv12dw = BatchNormalization(momentum=0.99,
                                  name='conv_dw_12_bn')(conv12dw)
    conv12dw = ReLU(6., name='conv_dw_12_relu')(conv12dw)

    conv12pw = Conv2D(1024, (1, 1), padding='same',
                      name='conv_pw_12')(conv12dw)
    conv12pw = BatchNormalization(momentum=0.99,
                                  name='conv_pw_12_bn')(conv12pw)
    conv12pw = ReLU(6., name='conv_pw_12_relu')(conv12pw)

    conv13dw = SeparableConv2D(1024, (3, 3), padding='same',
                               name='conv_dw_13')(conv12pw)
    conv13dw = BatchNormalization(momentum=0.99,
                                  name='conv_dw_13_bn')(conv13dw)
    conv13dw = ReLU(6., name='conv_dw_13_relu')(conv13dw)

    conv13pw = Conv2D(1024, (1, 1), padding='same',
                      name='conv_pw_13')(conv13dw)
    conv13pw = BatchNormalization(momentum=0.99,
                                  name='conv_pw_13_bn')(conv13pw)
    conv13pw = ReLU(6., name='conv_pw_13_relu')(conv13pw)

    conv14_1 = Conv2D(256, (1, 1), padding='same', name='conv14_1')(conv13pw)
    conv14_1 = BatchNormalization(momentum=0.99, name='bn14_1')(conv14_1)
    conv14_1 = ReLU(6., name='conv14_1_relu')(conv14_1)

    conv14_2dw = SeparableConv2D(512, (3, 3),
                                 strides=(2, 2),
                                 padding='same',
                                 name='conv_dw_14_2')(conv14_1)
    conv14_2dw = BatchNormalization(momentum=0.99,
                                    name='conv_dw_14_2_bn')(conv14_2dw)
    conv14_2dw = ReLU(6., name='conv_dw_14_2_relu')(conv14_2dw)

    conv14_2pw = Conv2D(512, (1, 1), padding='same',
                        name='conv_pw_14_2')(conv14_2dw)
    conv14_2pw = BatchNormalization(momentum=0.99,
                                    name='conv_pw_14_2_bn')(conv14_2pw)
    conv14_2pw = ReLU(6., name='conv_pw_14_2_relu')(conv14_2pw)

    conv15_1 = Conv2D(128, (1, 1), padding='same', name='conv15_1')(conv14_2pw)
    conv15_1 = BatchNormalization(momentum=0.99, name='bn15_1')(conv15_1)
    conv15_1 = ReLU(6., name='conv15_1_relu')(conv15_1)

    conv15_2dw = SeparableConv2D(256, (3, 3), name='conv_dw_15_2')(conv15_1)
    conv15_2dw = BatchNormalization(momentum=0.99,
                                    name='conv_dw_15_2_bn')(conv15_2dw)
    conv15_2dw = ReLU(6., name='conv_dw_15_2_relu')(conv15_2dw)

    conv15_2pw = Conv2D(256, (1, 1), padding='same',
                        name='conv_pw_15_2')(conv15_2dw)
    conv15_2pw = BatchNormalization(momentum=0.99,
                                    name='conv_pw_15_2_bn')(conv15_2pw)
    conv15_2pw = ReLU(6., name='conv_pw_15_2_relu')(conv15_2pw)

    conv16_1 = Conv2D(128, (1, 1), padding='same', name='conv16_1')(conv15_2pw)
    conv16_1 = BatchNormalization(momentum=0.99, name='bn16_1')(conv16_1)
    conv16_1 = ReLU(6., name='conv16_1_relu')(conv16_1)

    conv16_2 = SeparableConv2D(256, (3, 3), name='conv16_2_')(conv16_1)
    conv16_2 = BatchNormalization(momentum=0.99, name='bn16_2')(conv16_2)
    conv16_2 = ReLU(6., name='conv16_2_relu')(conv16_2)

    conv5_mbox_loc = Conv2D(n_boxes[0] * 4, (1, 1),
                            padding='same',
                            name='conv5_mbox_loc')(mobilenet_conv_pw_5_relu)
    conv11_mbox_loc = Conv2D(n_boxes[1] * 4, (1, 1),
                             padding='same',
                             name='conv11_mbox_loc_')(conv11pw)
    conv13_mbox_loc = Conv2D(n_boxes[2] * 4, (1, 1),
                             padding='same',
                             name='conv13_mbox_loc_')(conv13pw)
    conv14_mbox_loc = Conv2D(n_boxes[3] * 4, (1, 1),
                             padding='same',
                             name='conv14_mbox_loc_')(conv14_2pw)
    conv15_mbox_loc = Conv2D(n_boxes[4] * 4, (1, 1),
                             padding='same',
                             name='conv15_mbox_loc_')(conv15_2pw)
    conv16_mbox_loc = Conv2D(n_boxes[5] * 4, (1, 1),
                             padding='same',
                             name='conv16_mbox_loc_')(conv16_2)

    conv5_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv5_mbox_loc_reshape')(conv5_mbox_loc)
    conv11_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv11_mbox_loc_reshape')(conv11_mbox_loc)
    conv13_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv13_mbox_loc_reshape')(conv13_mbox_loc)
    conv14_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv14_mbox_loc_reshape')(conv14_mbox_loc)
    conv15_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv15_mbox_loc_reshape')(conv15_mbox_loc)
    conv16_mbox_loc_reshape = Reshape(
        (-1, 4), name='conv16_mbox_loc_reshape')(conv16_mbox_loc)

    conv5_mbox_conf = Conv2D(n_boxes[0] * n_classes, (1, 1),
                             padding='same',
                             name='conv5_mbox_conf')(mobilenet_conv_pw_5_relu)
    conv11_mbox_conf = Conv2D(n_boxes[1] * n_classes, (1, 1),
                              padding='same',
                              name='conv11_mbox_conf_')(conv11pw)
    conv13_mbox_conf = Conv2D(n_boxes[2] * n_classes, (1, 1),
                              padding='same',
                              name='conv13_mbox_conf_')(conv13pw)
    conv14_mbox_conf = Conv2D(n_boxes[3] * n_classes, (1, 1),
                              padding='same',
                              name='conv14_mbox_conf_')(conv14_2pw)
    conv15_mbox_conf = Conv2D(n_boxes[4] * n_classes, (1, 1),
                              padding='same',
                              name='conv15_mbox_conf_')(conv15_2pw)
    conv16_mbox_conf = Conv2D(n_boxes[5] * n_classes, (1, 1),
                              padding='same',
                              name='conv16_mbox_conf_')(conv16_2)

    conv5_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv5_mbox_conf_reshape')(conv5_mbox_conf)
    conv11_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv11_mbox_conf_reshape')(conv11_mbox_conf)
    conv13_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv13_mbox_conf_reshape')(conv13_mbox_conf)
    conv14_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv14_mbox_conf_reshape')(conv14_mbox_conf)
    conv15_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv15_mbox_conf_reshape')(conv15_mbox_conf)
    conv16_mbox_conf_reshape = Reshape(
        (-1, n_classes), name='conv16_mbox_conf_reshape')(conv16_mbox_conf)

    conv5_mbox_priorbox = AnchorBoxes(
        img_height,
        img_width,
        this_scale=scales[0],
        next_scale=scales[1],
        aspect_ratios=aspect_ratios[0],
        two_boxes_for_ar1=two_boxes_for_ar1,
        this_steps=steps[0],
        this_offsets=offsets[0],
        clip_boxes=clip_boxes,
        variances=variances,
        coords=coords,
        normalize_coords=normalize_coords,
        name='conv5_mbox_priorbox')(mobilenet_conv_pw_5_relu)
    conv11_mbox_priorbox = AnchorBoxes(img_height,
                                       img_width,
                                       this_scale=scales[1],
                                       next_scale=scales[2],
                                       aspect_ratios=aspect_ratios[1],
                                       two_boxes_for_ar1=two_boxes_for_ar1,
                                       this_steps=steps[1],
                                       this_offsets=offsets[1],
                                       clip_boxes=clip_boxes,
                                       variances=variances,
                                       coords=coords,
                                       normalize_coords=normalize_coords,
                                       name='conv11_mbox_priorbox')(conv11pw)
    conv13_mbox_priorbox = AnchorBoxes(img_height,
                                       img_width,
                                       this_scale=scales[2],
                                       next_scale=scales[3],
                                       aspect_ratios=aspect_ratios[2],
                                       two_boxes_for_ar1=two_boxes_for_ar1,
                                       this_steps=steps[2],
                                       this_offsets=offsets[2],
                                       clip_boxes=clip_boxes,
                                       variances=variances,
                                       coords=coords,
                                       normalize_coords=normalize_coords,
                                       name='conv13_mbox_priorbox')(conv13pw)
    conv14_mbox_priorbox = AnchorBoxes(img_height,
                                       img_width,
                                       this_scale=scales[3],
                                       next_scale=scales[4],
                                       aspect_ratios=aspect_ratios[3],
                                       two_boxes_for_ar1=two_boxes_for_ar1,
                                       this_steps=steps[3],
                                       this_offsets=offsets[3],
                                       clip_boxes=clip_boxes,
                                       variances=variances,
                                       coords=coords,
                                       normalize_coords=normalize_coords,
                                       name='conv14_mbox_priorbox')(conv14_2pw)
    conv15_mbox_priorbox = AnchorBoxes(img_height,
                                       img_width,
                                       this_scale=scales[4],
                                       next_scale=scales[5],
                                       aspect_ratios=aspect_ratios[4],
                                       two_boxes_for_ar1=two_boxes_for_ar1,
                                       this_steps=steps[4],
                                       this_offsets=offsets[4],
                                       clip_boxes=clip_boxes,
                                       variances=variances,
                                       coords=coords,
                                       normalize_coords=normalize_coords,
                                       name='conv15_mbox_priorbox')(conv15_2pw)
    conv16_mbox_priorbox = AnchorBoxes(img_height,
                                       img_width,
                                       this_scale=scales[5],
                                       next_scale=scales[6],
                                       aspect_ratios=aspect_ratios[5],
                                       two_boxes_for_ar1=two_boxes_for_ar1,
                                       this_steps=steps[5],
                                       this_offsets=offsets[5],
                                       clip_boxes=clip_boxes,
                                       variances=variances,
                                       coords=coords,
                                       normalize_coords=normalize_coords,
                                       name='conv16_mbox_priorbox')(conv16_2)

    conv5_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv5_mbox_priorbox_reshape')(conv5_mbox_priorbox)
    conv11_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv11_mbox_priorbox_reshape')(conv11_mbox_priorbox)
    conv13_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv13_mbox_priorbox_reshape')(conv13_mbox_priorbox)
    conv14_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv14_mbox_priorbox_reshape')(conv14_mbox_priorbox)
    conv15_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv15_mbox_priorbox_reshape')(conv15_mbox_priorbox)
    conv16_mbox_priorbox_reshape = Reshape(
        (-1, 8), name='conv16_mbox_priorbox_reshape')(conv16_mbox_priorbox)

    mbox_loc = concatenate([
        conv5_mbox_loc_reshape, conv11_mbox_loc_reshape,
        conv13_mbox_loc_reshape, conv14_mbox_loc_reshape,
        conv15_mbox_loc_reshape, conv16_mbox_loc_reshape
    ],
                           axis=1,
                           name='mbox_loc')

    mbox_conf = concatenate([
        conv5_mbox_conf_reshape, conv11_mbox_conf_reshape,
        conv13_mbox_conf_reshape, conv14_mbox_conf_reshape,
        conv15_mbox_conf_reshape, conv16_mbox_conf_reshape
    ],
                            axis=1,
                            name='mbox_conf')

    mbox_priorbox = concatenate([
        conv5_mbox_priorbox_reshape, conv11_mbox_priorbox_reshape,
        conv13_mbox_priorbox_reshape, conv14_mbox_priorbox_reshape,
        conv15_mbox_priorbox_reshape, conv16_mbox_priorbox_reshape
    ],
                                axis=1,
                                name='mbox_priorbox')

    mbox_conf_softmax = Activation('softmax',
                                   name='mbox_conf_softmax')(mbox_conf)

    predictions = concatenate([mbox_conf_softmax, mbox_loc, mbox_priorbox],
                              axis=2,
                              name='predictions')

    if mode == 'training':
        model = Model(inputs=x, outputs=predictions)
    elif mode == 'inference':
        decoded_predictions = DecodeDetections(
            confidence_thresh=confidence_thresh,
            iou_threshold=iou_threshold,
            top_k=top_k,
            nms_max_output_size=nms_max_output_size,
            coords=coords,
            normalize_coords=normalize_coords,
            img_height=img_height,
            img_width=img_width,
            name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    elif mode == 'inference_fast':
        decoded_predictions = DecodeDetectionsFast(
            confidence_thresh=confidence_thresh,
            iou_threshold=iou_threshold,
            top_k=top_k,
            nms_max_output_size=nms_max_output_size,
            coords=coords,
            normalize_coords=normalize_coords,
            img_height=img_height,
            img_width=img_width,
            name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    else:
        raise ValueError(
            "`mode` must be one of 'training', 'inference' or 'inference_fast', but received '{}'."
            .format(mode))

    if return_predictor_sizes:
        predictor_sizes = np.array([
            conv5_mbox_conf._keras_shape[1:3],
            conv11_mbox_conf._keras_shape[1:3],
            conv13_mbox_conf._keras_shape[1:3],
            conv14_mbox_conf._keras_shape[1:3],
            conv15_mbox_conf._keras_shape[1:3],
            conv16_mbox_conf._keras_shape[1:3]
        ])
        return model, predictor_sizes
    else:
        return model
Exemplo n.º 12
0
 def __init__(self):
     self.preprocess_mode = 'tf'
     model = MobileNet(weights='imagenet')
     self.model = Model(
         model.input, [model.output, model.get_layer('conv_preds').output])
Exemplo n.º 13
0
from keras.layers import Dense, GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.preprocessing import image
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, Sequential
from keras.optimizers import Adam
import cv2

# In[2]:

base_model = MobileNet(
    weights='imagenet', include_top=False
)  #imports the mobilenet model and discards the last 1000 neuron layer.

base_input = base_model.get_layer(index=0).input
base_output = base_model.get_layer(index=-2).output

base_output = GlobalAveragePooling2D()(
    base_output
)  # Important to set the expected shape = [?, height, width, 3 channels]

bottleneck_model = Model(inputs=base_input, outputs=base_output)
print(
    base_output
)  # the model has learned a 1024 dimensional representation of any image input
#specify the inputs
#specify the outputs
#now a model has been created based on our architecture

# In[4]:
Exemplo n.º 14
0
def run(args):
    lr = args.lr
    epochs = args.epochs
    decay = args.decay
    momentum = args.momentum
    h5file = args.model
    test_set_path = args.test
    hist = args.hist
    dataset = pd.read_csv(
        os.path.join('/home', 'wvillegas', 'dataset-mask', 'full_masks.csv'))

    from utils_fcn import DataGeneratorMobileNet
    from sklearn.model_selection import train_test_split
    X_train, X_test, Y_train, Y_test = train_test_split(dataset['orig'],
                                                        dataset['mask'],
                                                        test_size=0.2,
                                                        random_state=1)
    partition = {'train': list(X_train), 'test': list(X_test)}
    img_list = list(X_train) + list(X_test)
    mask_list = list(Y_train) + list(Y_test)
    labels = dict(zip(img_list, mask_list))

    img_path = os.path.join('/home', 'wvillegas', 'dataset-mask',
                            'dataset_resize', 'images_resize')
    masks_path = os.path.join('/home', 'wvillegas', 'dataset-mask',
                              'dataset_resize', 'masks_resize')

    batch_size = 4

    train_generator = DataGeneratorMobileNet(batch_size=batch_size,
                                             img_path=img_path,
                                             labels=labels,
                                             list_IDs=partition['train'],
                                             n_channels=3,
                                             n_channels_label=1,
                                             shuffle=True,
                                             mask_path=masks_path)
    from keras.applications import MobileNet
    from keras.layers import Conv2DTranspose, Conv2D, Add
    from keras import Model
    net = MobileNet(include_top=False, weights=None)
    net.load_weights(
        '/home/wvillegas/DLProjects/BudClassifier/cmdscripts/modelosV2/mobilenet_weights_detection.h5',
        by_name=True)

    for layer in net.layers:
        layer.trainable = True

    predict = Conv2D(filters=1, kernel_size=1, strides=1)(net.output)
    deconv2 = Conv2DTranspose(filters=1,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              use_bias=False)(predict)
    pred_conv_dw_11_relu = Conv2D(filters=1, kernel_size=1, strides=1)(
        net.get_layer('conv_dw_11_relu').output)
    fuse1 = Add()([deconv2, pred_conv_dw_11_relu])
    pred_conv_pw_5_relu = Conv2D(filters=1, kernel_size=1, strides=1)(
        net.get_layer('conv_pw_5_relu').output)
    deconv2fuse1 = Conv2DTranspose(filters=1,
                                   kernel_size=4,
                                   strides=2,
                                   padding='same',
                                   use_bias=False)(fuse1)
    fuse2 = Add()([deconv2fuse1, pred_conv_pw_5_relu])
    deconv8 = Conv2DTranspose(filters=1,
                              kernel_size=16,
                              strides=8,
                              padding='same',
                              use_bias=False)(fuse2)

    fcn = Model(inputs=net.input, outputs=deconv8)

    from keras.optimizers import SGD
    sgd = SGD(lr=lr, momentum=momentum, decay=decay)
    fcn.compile(loss='binary_crossentropy',
                optimizer=sgd,
                metrics=['accuracy'])

    history = fcn.fit_generator(generator=train_generator,
                                use_multiprocessing=True,
                                workers=6,
                                epochs=epochs)
    fcn.save(os.path.join(h5file))
    test_csv = pd.DataFrame({'x': X_test, 'y': Y_test})
    test_csv.to_csv(test_set_path, header=None)
    test_csv = pd.DataFrame(history.history)
    test_csv.to_csv(hist)
Exemplo n.º 15
0
if __name__ == '__main__':
    print('Loading VGG16 Weights ...')
    VGG16_notop = MobileNet(input_shape=None,
                            alpha=1.0,
                            depth_multiplier=1,
                            dropout=1e-3,
                            include_top=False,
                            weights='imagenet',
                            input_tensor=None,
                            pooling=None,
                            classes=1000)
    VGG16_notop.summary()

    print('Adding Average Pooling Layer and Softmax Output Layer ...')
    output = VGG16_notop.get_layer(index=-1).output  # Shape: (6, 6, 2048)
    output = AveragePooling2D((8, 8), strides=(8, 8), name='avg_pool')(output)
    output = Flatten(name='flatten')(output)
    output = Dense(n_classes, activation='softmax', name='predictions')(output)

    VGG16_model = Model(VGG16_notop.input, output)
    VGG16_model.summary()

    optimizer = SGD(lr=learning_rate, momentum=0.9, decay=0.001, nesterov=True)
    VGG16_model.compile(loss='categorical_crossentropy',
                        optimizer=optimizer,
                        metrics=['accuracy'])

    # autosave best Model
    # best_model_file = model_dir + "VGG16_UCM_weights.h5"
    best_model_file = model_dir + "VGG16_2015_4_classes_weights.h5"