Example #1
0
    def _extract_backbone(self):
        """extract feature map from backbone.
        """
        if self.backbone == 'large':
            from model.mobilenet_v3_large import MobileNetV3_Large

            model = MobileNetV3_Large(self.shape, self.n_class).build()
            layer_name8 = 'batch_normalization_13'
            layer_name16 = 'add_5'
        elif self.backbone == 'small':
            from model.mobilenet_v3_small import MobileNetV3_Small

            model = MobileNetV3_Small(self.shape, self.n_class).build()
            layer_name8 = 'batch_normalization_7'
            layer_name16 = 'add_2'
        else:
            raise Exception('Invalid backbone: {}'.format(self.backbone))

        if self.weights is not None:
            model.load_weights(self.weights)

        inputs= model.input
        # 1/8 feature map.
        out_feature8 = model.get_layer(layer_name8).output
        # 1/16 feature map.
        out_feature16 = model.get_layer(layer_name16).output

        return inputs, out_feature8, out_feature16
def pre(img, save_model = False):

    global model
    global model_shape
    if model is None:
        with open('config/config.json', 'r') as f:
            cfg = json.load(f)

        save_dir = cfg['save_dir']
        model_shape = (int(cfg['height']), int(cfg['width']), 3)
        n_class = int(cfg['class_number'])
        batch = int(cfg['batch'])

        if not os.path.exists(save_dir):
            os.mkdir(save_dir)

        if cfg['model'] == 'large':
            from model.mobilenet_v3_large import MobileNetV3_Large
            model = MobileNetV3_Large(model_shape, n_class).build()
        if cfg['model'] == 'small':
            from model.mobilenet_v3_small import MobileNetV3_Small
            model = MobileNetV3_Small(model_shape, n_class).build()

        if cfg['model'] == 'mymodel':
            from model.my_model import MyModel
            model = MyModel(model_shape, n_class).build()

        if cfg['model'] == 'v2':
            from model.mobilenet_v2 import MyModel
            model = MyModel(model_shape, n_class).build()


        pre_weights = "save/v3_weights0.87-0.87.h5"#cfg['weights']
        if pre_weights and os.path.exists(pre_weights):
            model.load_weights(pre_weights, by_name=True)
            print("------------------load pre model!!!!!")

    if(save_model):
        print("Finish save.")
        model.save('save/model_all.h5')

    # 预处理
    img = cv2.resize(img, model_shape[:2])
    img = img*1. / 255



    pre = model.predict(np.array([img]))
    #print("pre: ",pre,np.argmax(pre[0]))

    #pre_cate = np.argmax(pre[0])
    return pre
Example #3
0
def build(epoch = default_values["epoch"],\
          learning_rate = default_values["learning_rate"], \
          batch_size = default_values["batch_size"],\
          optimizer = default_values["optimizer"],\
          initial_weights = default_values["initial_weights"],\
          cnn_class = default_values["cnn_class"],\
          pre_weights = default_values["pre_weights"], \
          lstm_conf = default_values["lstm_conf"], \
          cnn_train_type=default_values["cnn_train_type"]):

    model = 0
    shape = (224, 224, 3)
    n_class = 2
    #Create CNN
    if (cnn_train_type != 'train'):
        logger.info("CNN Created with Pre-weights:{}".format(pre_weights))
        # base_model = cnn_class(weights=pre_weights,include_top=False)
        base_model = MobileNetV3_Large(shape, n_class,
                                       include_top=False).build()
    else:
        logger.info("CNN Created with no Pre-weights")
        base_model = cnn_class()

    #control Train_able of CNNN
    if (cnn_train_type == 'static'):
        logger.info("CNN set to NOT-Train")
        for layer in base_model.layers:
            layer.trainable = False
    if (cnn_train_type == 'retrain'):
        logger.info("CNN set to retrain")
        for layer in base_model.layers:
            layer.trainable = True

    # print(base_model.summary())
    # add a global spatial average pooling layer
    x = base_model.output
    logger.info("base_model.output: {}".format(base_model.output))
    x = GlobalAveragePooling2D()(x)
    # let's add a fully-connected layer
    x = Dense(1024, activation='relu')(x)
    # and a logistic layer -- let's say we have 200 classes
    predictions = Dense(100, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=predictions)
    model.compile(optimizer=optimizer, loss='categorical_crossentropy')

    model.summary()
    print("Commit update2")
    print("Commit update3")
    return model
Example #4
0
def train():
    with open('config/config.json', 'r') as f:
        cfg = json.load(f)

    save_dir = cfg['save_dir']
    shape = (int(cfg['height']), int(cfg['width']), 3)
    n_class = int(cfg['class_number'])
    batch = int(cfg['batch'])

    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    if cfg['model'] == 'large':
        from model.mobilenet_v3_large import MobileNetV3_Large
        model = MobileNetV3_Large(shape, n_class).build()
    if cfg['model'] == 'small':
        from model.mobilenet_v3_small import MobileNetV3_Small
        model = MobileNetV3_Small(shape, n_class).build()

    pre_weights = cfg['weights']
    if pre_weights and os.path.exists(pre_weights):
        model.load_weights(pre_weights, by_name=True)

    opt = Adam(lr=float(cfg['learning_rate']))
    earlystop = EarlyStopping(monitor='val_acc',
                              patience=5,
                              verbose=0,
                              mode='auto')
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    train_generator, validation_generator, count1, count2 = generate(
        batch, shape[:2], cfg['train_dir'], cfg['eval_dir'])

    hist = model.fit_generator(train_generator,
                               validation_data=validation_generator,
                               steps_per_epoch=count1 // batch,
                               validation_steps=count2 // batch,
                               epochs=cfg['epochs'],
                               callbacks=[earlystop])

    df = pd.DataFrame.from_dict(hist.history)
    df.to_csv(os.path.join(save_dir, 'hist.csv'),
              encoding='utf-8',
              index=False)
    model.save_weights(
        os.path.join(save_dir, '{}_weights.h5'.format(cfg['model'])))
Example #5
0
def train():
    with open('config/config.json', 'r') as f:
        cfg = json.load(f)

    save_dir = cfg['save_dir']
    shape = (int(cfg['height']), int(cfg['width']))
    n_class = int(cfg['class_number'])
    batch = int(cfg['batch'])

    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    train_images, train_labels, test_images, test_labels = load_images()

    if cfg['model'] == 'large':
        from model.mobilenet_v3_large import MobileNetV3_Large
        model = MobileNetV3_Large(train_images[0].shape,
                                  n_class).build(shape=shape)
    if cfg['model'] == 'small':
        from model.mobilenet_v3_small import MobileNetV3_Small
        model = MobileNetV3_Small(train_images[0].shape,
                                  n_class).build(shape=shape)

    optimizer = build_optimizer(learning_rate=float(cfg['learning_rate']),
                                momentum=0.9)
    # earlystop = EarlyStopping(monitor='val_accuracy', patience=5, verbose=0, mode='auto')
    checkpoint = ModelCheckpoint(filepath=os.path.join(
        save_dir, '{}_weights.h5'.format(cfg['model'])),
                                 monitor='val_acc',
                                 save_best_only=True,
                                 save_weights_only=True)

    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    model.summary()

    # data augmentation
    datagen1 = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=
        False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=
        15,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=
        0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=
        0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images

    datagen1.fit(train_images)

    hist = model.fit_generator(datagen1.flow(train_images,
                                             train_labels,
                                             batch_size=batch),
                               validation_data=(test_images, test_labels),
                               steps_per_epoch=train_images.shape[0] // batch,
                               epochs=cfg['epochs'],
                               callbacks=[checkpoint])

    df = pd.DataFrame.from_dict(hist.history)
    df.to_csv(os.path.join(save_dir, 'hist.csv'),
              encoding='utf-8',
              index=False)