Пример #1
0
    def build_model(self, input_dim):

        input1 = Input(shape=(input_dim, ))
        input2 = Input((config.img_x, config.img_y, 1))
        input3 = Input((config.img_x, config.img_y, 1))

        # 一维特征深度网络
        #x1 = Dense(32, activation='relu')(input1)
        #x1 = Dropout(0.15)(x1)
        #x1 = Dense(8, activation='relu')(x1)

        ## s/n 矩阵特征卷积网络
        #x2 = Conv2D(2, (3, 3), padding='same')(input2) # 62*62*8
        #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 31*31*8
        #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 29*29*16
        #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 15*15*16
        #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 13*13*32
        #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 7*7*32
        #x2 = Conv2D(16, (3, 3), padding='same')(x2) # 5*5*32
        #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 3*3*32
        #x2 = Flatten()(x2)
        #x2 = Dense(32, activation='relu')(x2)
        #x2 = Dropout(0.15)(x2)
        #x2 = Dense(8, activation='relu')(x2)

        ## s/n 矩阵特征卷积网络
        #x3 = Conv2D(2, (3, 3), padding='same')(input3) # 62*62*8
        #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 31*31*8
        #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 29*29*16
        #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 15*15*16
        #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 13*13*32
        #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 7*7*32
        #x3 = Conv2D(16, (3, 3), padding='same')(x3) # 5*5*32
        #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 3*3*16
        #x3 = Flatten()(x3)
        #x3 = Dense(32, activation='relu')(x3)
        #x3 = Dropout(0.15)(x3)
        #x3 = Dense(8, activation='relu')(x3)

        #top_tensor = concatenate([x1,x2], axis=1)
        #top_tensor = Dense(8, activation='relu')(top_tensor)
        #top_tensor = Dropout(0.15)(top_tensor)
        #top_tensor = Dense(2, activation='softmax')(top_tensor)

        self.model = ResnetBuilder.build_resnet_50(
            (config.img_x, config.img_y, 1), 2)
        #self.model = Model(inputs=[input2], outputs=dense)
        # self.model = Model(inputs=[ input1], outputs=top_tensor)
        sgd = keras.optimizers.SGD(lr=0.001,
                                   momentum=0.85,
                                   decay=1e-4,
                                   nesterov=False)
        adam = keras.optimizers.Adam(lr=0.0001,
                                     beta_1=0.9,
                                     beta_2=0.999,
                                     epsilon=1e-08)
        self.model.compile(loss="categorical_crossentropy",
                           optimizer=adam,
                           metrics=["acc"])
def my_assessor_model(mouth_nn='cnn', mouth_features_dim=512, lstm_units_1=32, dense_fc_1=128, dense_fc_2=64,
                      conv_f_1=32, conv_f_2=64, conv_f_3=128, dropout_p=0.2):

    mouth_input_shape = (MOUTH_H, MOUTH_W, MOUTH_CHANNELS)
    my_input_mouth_images = Input(shape=(TIME_STEPS, *mouth_input_shape))
    my_input_head_poses = Input(shape=(TIME_STEPS, 3))
    my_input_n_of_frames = Input(shape=(1,))
    my_input_lipreader_dense = Input(shape=(1024,))
    my_input_lipreader_softmax = Input(shape=(500,))

    if mouth_nn == 'cnn':
        mouth_feature_model = my_timedistributed_cnn_model((TIME_STEPS, *mouth_input_shape), conv_f_1, conv_f_2, conv_f_3, mouth_features_dim)
    elif mouth_nn == 'resnet18':
        mouth_feature_model = ResnetBuilder.build_resnet_18((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet34':
        mouth_feature_model = ResnetBuilder.build_resnet_34((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet50':
        mouth_feature_model = ResnetBuilder.build_resnet_50((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet101':
        mouth_feature_model = ResnetBuilder.build_resnet_101((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet152':
        mouth_feature_model = ResnetBuilder.build_resnet_152((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'flatten':
        mouth_feature_model = Reshape((TIME_STEPS, -1), input_shape=(TIME_STEPS, *mouth_input_shape))

    cnn_features = mouth_feature_model(my_input_mouth_images)

    lstm_input = Concatenate()([cnn_features, my_input_head_poses])

    lstm_output = LSTM(lstm_units_1, activation='tanh', kernel_regularizer=l2(1.e-4), return_sequences=False)(lstm_input)

    concatenated_features = Concatenate()([lstm_output, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax])

    fc1 = Dense(dense_fc_1, activation='relu', kernel_regularizer=l2(1.e-4))(concatenated_features)

    bn1 = BatchNormalization()(fc1)

    dp1 = Dropout(dropout_p)(bn1)

    fc2 = Dense(dense_fc_2, activation='relu', kernel_regularizer=l2(1.e-4))(dp1)

    bn2 = BatchNormalization()(fc2)

    dp2 = Dropout(dropout_p)(bn2)

    assessor_output = Dense(1, activation='sigmoid')(dp2)

    assessor = Model(inputs=[my_input_mouth_images, my_input_head_poses, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax],
                     outputs=assessor_output)

    assessor.summary()

    return assessor
Пример #3
0
def get_model(model, ishape, nb_classes):
    "Return proper ResNet model"
    if model.endswith('18'):
        model = ResnetBuilder.build_resnet_18(ishape, nb_classes)
    elif model.endswith('34'):
        model = ResnetBuilder.build_resnet_34(ishape, nb_classes)
    elif model.endswith('50'):
        model = ResnetBuilder.build_resnet_50(ishape, nb_classes)
    elif model.endswith('101'):
        model = ResnetBuilder.build_resnet_101(ishape, nb_classes)
    elif model.endswith('152'):
        model = ResnetBuilder.build_resnet_152(ishape, nb_classes)
    else:
        return NotImplemented()
    return model
Пример #4
0
    def build_resnet(self, model_name, lr):
        input_shape = (128, 128, 3)
        num_outputs = 100
        model = None
        if model_name == "resnet18":
            model = ResnetBuilder.build_resnet_18(input_shape, num_outputs)
        elif model_name == "resnet34":
            model = ResnetBuilder.build_resnet_34(input_shape, num_outputs)
        elif model_name == "resnet50":
            model = ResnetBuilder.build_resnet_50(input_shape, num_outputs)
        elif model_name == "resnet50_keras":
            inp = Input((224, 224, 3))
            model = ResNet50(input_tensor=inp, weights=None, classes=100)
        else:
            raise

        # opt = SGD(lr=lr, momentum=0.9, nesterov=True)
        opt = Adam()
        model.compile(optimizer=opt,
                      loss="categorical_crossentropy",
                      metrics=['accuracy'])
        return model
    def model_confirm(self, choosed_model):
        if choosed_model == 'AlexNet':
            model = alexnet(self.config)
        elif choosed_model == 'VGG16':
            model = vgg16(self.config)
        elif choosed_model == 'VGG19':
            model = vgg19(self.config)
        elif choosed_model == 'InceptionV3':
            model = inception_v3(self.config)
        elif choosed_model == 'InceptionV4':
            model = inception_v4(self.config)
        elif choosed_model == 'InceptionV4_ResNetV1':
            model = inception_resnet_v1(self.config)
        elif choosed_model == 'InceptionV4_ResNetV2':
            model = inception_resnet_v2(self.config)
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder.build_resnet_18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder.build_resnet_34(self.config)
        elif choosed_model == 'ResNet50':
            model = ResnetBuilder.build_resnet_50(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder.build_resnet_101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder.build_resnet_152(self.config)
        elif choosed_model == 'DenseNet121':
            model = densenet121(self.config)
        elif choosed_model == 'DenseNet169':
            model = densenet169(self.config)
        elif choosed_model == 'DenseNet201':
            model = densenet201(self.config)
        elif choosed_model == 'DenseNet264':
            model = densenet264(self.config)
        else:
            model = -1

        return model
Пример #6
0
    def create_model(self, model_type='xception', load_weights=None):
        if (model_type == 'inceptionv3' or model_type == 1):
            base = InceptionV3(include_top=False,
                               weights='imagenet',
                               input_tensor=self.input_tensor,
                               classes=self.output_size,
                               pooling='avg')
            model_name = 'inceptionv3'
            pred = base.output
        elif (model_type == 'resnet50' or model_type == 2):
            base = ResNet50(include_top=False,
                            weights='imagenet',
                            input_tensor=self.input_tensor,
                            classes=self.output_size,
                            pooling='avg')
            model_name = 'resnet50'
            pred = base.output
        elif (model_type == 'vgg19' or model_type == 3):
            base = VGG19(include_top=False,
                         weights='imagenet',
                         input_tensor=self.input_tensor,
                         classes=self.output_size,
                         pooling='avg')
            model_name = 'vgg19'
            pred = base.output
        elif (model_type == 'vgg16' or model_type == 4):
            base = VGG16(include_top=False,
                         weights='imagenet',
                         input_tensor=self.input_tensor,
                         classes=self.output_size,
                         pooling='avg')
            model_name = 'vgg16'
            pred = base.output
        elif (model_type == 'resnet152' or model_type == 5):
            resbuild = ResnetBuilder()
            base = resbuild.build_resnet_152(self.input_shape,
                                             self.output_size)
            model_name = 'resnet152'
            pred = base.output
        elif (model_type == 'resnet50MOD' or model_type == 6):
            resbuild = ResnetBuilder()
            base = resbuild.build_resnet_50(self.input_shape, self.output_size)
            model_name = 'resnet50MOD'
            pred = base.output
        elif (model_type == 'inceptionv3MOD' or model_type == 7):
            base = InceptionV3MOD(include_top=False,
                                  weights='imagenet',
                                  input_tensor=self.input_tensor,
                                  classes=self.output_size,
                                  pooling='avg')
            model_name = 'inceptionv3MOD'
            pred = base.output
        else:
            base = Xception(include_top=False,
                            weights='imagenet',
                            input_tensor=self.input_tensor,
                            classes=self.output_size,
                            pooling='avg')
            model_name = 'xception'
            pred = base.output
        pred = Dense(self.output_size,
                     activation='sigmoid',
                     name='predictions')(pred)
        self.model = Model(base.input, pred, name=model_name)

        if load_weights != None:
            self.model.load_weights(load_weights)

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

        self.model.compile(loss=losses.binary_crossentropy,
                           optimizer='adam',
                           metrics=[FScore2])
Пример #7
0
train_data_dir = './train_data/'
validation_data_dir = './val_data/'
log_filepath = './log/'

num_train_samples = 100000
num_val_samples = 20000
num_epoch = 100

result_dir = './result/'
if not os.path.exists(result_dir):
    os.mkdir(result_dir)

if __name__ == '__main__':
    # Resnet50モデルを構築
    input_shape = [3, 30, 30]
    resnet50 = ResnetBuilder.build_resnet_50(input_shape, num_classes)
    model = Model(input=resnet50.input, output=resnet50.output)

    # SGD+momentumがいいらしい
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.SGD(lr=1e-2,
                                           decay=1e-6,
                                           momentum=0.9,
                                           nesterov=True),
                  metrics=['accuracy'])

    datagen = ImageDataGenerator(data_format="channels_first")

    train_generator = datagen.flow_from_directory(train_data_dir,
                                                  target_size=(img_rows,
                                                               img_cols),
Пример #8
0
def test_resnet50():
    model = ResnetBuilder.build_resnet_50((3, 224, 224), 100)
    _test_model_compile(model)
Пример #9
0
def test_resnet50():
    for ordering in DIM_ORDERING:
        K.set_image_dim_ordering(ordering)
        model = ResnetBuilder.build_resnet_50((3, 224, 224), 100)
        model.compile(loss="categorical_crossentropy", optimizer="sgd")
        assert True, "Failed to build with '{}' dim ordering".format(ordering)
Пример #10
0
N_CLASS = 28
DATA_DIR = 'dataset224/'
n_valid = 1000

Ids, labels = loadData('all.csv', N_CLASS, DATA_DIR)

index = random.sample(range(len(Ids)), 1000)
train, _ = loadData('train.csv', N_CLASS, DATA_DIR)
validation, _ = loadData('validation.csv', N_CLASS, DATA_DIR)

params = {
    'dim': (224, 224),
    'batch_size': 32,
    'n_classes': 28,
    'n_channels': 3,
    'shuffle': True
}

training_generator = DataGenerator(train, labels, **params)
validation_generator = DataGenerator(validation, labels, **params)
histories = Histories()
model = ResnetBuilder.build_resnet_50((3, 224, 224), 28)
model.compile(loss="categorical_crossentropy",
              optimizer="sgd",
              metrics=[metrics.categorical_accuracy])
print(len(Ids) - len(train) - len(validation))
model.fit_generator(generator=training_generator,
                    validation_data=validation_generator,
                    callbacks=[histories],
                    epochs=1)
Пример #11
0
    def build_resnet_50(self, shape, num_classes):
        self.model = ResnetBuilder.build_resnet_50(shape, num_classes)

        print('[+] ResNet-50 model built')
        return self.model
Пример #12
0
    def create_model(self, model_type='xception', load_weights=None):
        if (model_type == 'inceptionv3' or model_type == 1):
            base = InceptionV3(include_top=False,
                               weights='imagenet',
                               input_tensor=self.input_tensor,
                               classes=self.output_size,
                               pooling='avg')
            model_name = 'inceptionv3'
            pred = base.output
        elif (model_type == 'resnet50' or model_type == 2):
            base = ResNet50(include_top=False,
                            weights='imagenet',
                            input_tensor=self.input_tensor,
                            classes=self.output_size,
                            pooling='avg')
            model_name = 'resnet50'
            pred = base.output
        elif (model_type == 'vgg19' or model_type == 3):
            base = VGG19(include_top=False,
                         weights='imagenet',
                         input_tensor=self.input_tensor,
                         classes=self.output_size,
                         pooling='avg')
            model_name = 'vgg19'
            pred = base.output
        elif (model_type == 'vgg16' or model_type == 4):
            base = VGG16(include_top=False,
                         weights='imagenet',
                         input_tensor=self.input_tensor,
                         classes=self.output_size,
                         pooling='avg')
            model_name = 'vgg16'
            pred = base.output
        elif (model_type == 'resnet152' or model_type == 5):
            sys.path.append(os.path.join(PATH, "resnet", "keras-resnet"))
            from resnet import ResnetBuilder
            resbuild = ResnetBuilder()
            base = resbuild.build_resnet_152(self.input_shape,
                                             self.output_size)
            model_name = 'resnet152'
            pred = base.output
        elif (model_type == 'resnet50MOD' or model_type == 6):
            sys.path.append(os.path.join(PATH, "resnet", "keras-resnet"))
            from resnet import ResnetBuilder
            resbuild = ResnetBuilder()
            base = resbuild.build_resnet_50(self.input_shape, self.output_size)
            model_name = 'resnet50MOD'
            pred = base.output
        elif (model_type == 'inceptionv3MOD' or model_type == 7):
            from keras.applications.inception_v3_mod import InceptionV3MOD
            base = InceptionV3MOD(include_top=False,
                                  weights='imagenet',
                                  input_tensor=self.input_tensor,
                                  classes=self.output_size,
                                  pooling='avg')
            model_name = 'inceptionv3MOD'
            pred = base.output
        else:
            base = Xception(include_top=False,
                            weights='imagenet',
                            input_tensor=self.input_tensor,
                            classes=self.output_size,
                            pooling='avg')
            model_name = 'xception'
            pred = base.output
        pred = Dense(self.output_size,
                     activation='sigmoid',
                     name='predictions')(pred)
        self.model = Model(base.input, pred, name=model_name)

        if load_weights != None:
            self.model.load_weights(load_weights)

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

        self.compile()
Пример #13
0
def test_resnet50():
    model = ResnetBuilder.build_resnet_50((3, 224, 224), 100)
    _test_model_compile(model)