示例#1
0
def create_new_model(input_shape):
    #     facenet_model = load_model(modelFile)
    inputs = Input(shape=input_shape)
    #i = facenet_model(inputs)
    #print(facenet_model.get_input_at(0).shape)
    model1 = Model(input=facenet_model.get_input_at(0),
                   output=facenet_model.get_layer('add_15').output)
    for layer in model1.layers:
        layer.trainable = False

    inter_shape = model1.output_shape[1:]
    print(inter_shape)
    #model2 = Model(input=model1.input1, outputs = inter_layer);
    #inter_shape =
    print(inter_shape)
    dense = DenseNet(input_shape=inter_shape,
                     nb_filter=600,
                     include_top=False,
                     depth=1,
                     nb_dense_block=2,
                     growth_rate=32)(model1.output)
    #d.layers.pop()
    #d.layers.pop()
    #d.layers.pop()
    #d = d.layers.pop()
    #dense.summary()
    #model = d
    #model.summary()
    x = AveragePooling2D(pool_size=(4, 4), name='cutome_avg_pool_1')(dense)
    x = Flatten(name='cutome_flatten_2')(x)
    x = Dense(512, activation='relu', name='custome_dense_dense_3')(x)
    x = BatchNormalization(name='cutome_batch_normalization_4')(x)
    x = Dense(20, activation='relu', name='custome_dense_5')(x)
    #x = Dense(1, activation='relu', name = 'custome_dense_output_1')(x)
    x = Lambda(lambda x: K.l2_normalize(x, axis=1),
               name='custome_l2_normalize_6')(x)
    #fin = d(inter_layer.output)

    model = Model(inputs=[model1.input], outputs=x)
    input_1 = Input(shape=input_shape)
    input_2 = Input(shape=input_shape)
    input_3 = Input(shape=input_shape)

    l1 = model(input_1)
    l2 = model(input_2)
    l3 = model(input_3)

    x = concatenate([l1, l2, l3])
    print(x.shape)
    final_model = Model(inputs=[input_1, input_2, input_3], outputs=x)

    final_model.compile(optimizer=optimizer.Adam(lr=0.0005),
                        loss=loss_fun,
                        metrics=[accuracy_c])
    return final_model
示例#2
0
 def _build_model(self, input_shape):
     #blks = [self.config['block_0'], self.config['block_1'], self.config['block_2'], self.config['block_3']]
     #print('--------BLOCK:',blks)
     #model = densenet.DenseNet(blocks=[6,6,6,6], input_shape=(32,32,3), include_top=True, weights=None, input_tensor=None, pooling=None, classes=10, backend=keras.backend, layers=keras.layers, models=keras.models)#, keras_utils= keras.utils )
     depth = 40
     nb_dense_block = 3
     growth_rate = self.config['growth_rate']
     nb_filter = 16  #TODO
     dropout_rate = self.config['dropout']
     weight_decay = self.config['weight_decay']
     model = DenseNet(depth=depth,
                      nb_dense_block=nb_dense_block,
                      growth_rate=growth_rate,
                      nb_filter=nb_filter,
                      dropout_rate=dropout_rate,
                      input_shape=(32, 32, 3),
                      weights=None)
     #model = densenet.DenseNet(blocks=[12,24,12,24], input_shape=(32,32,3), include_top=True, weights=None, input_tensor=None, pooling=None, classes=10, backend=keras.backend, layers=keras.layers, models=keras.models)#, keras_utils= keras.utils )
     model.summary()
     return model
def create_model(X_train, y_train, X_val, y_val, X_test, y_test):
    epochs = {{choice([25, 30, 35])}}
    es_patience = 5
    lr_patience = 3
    dropout = {{uniform(0.2, 0.8)}}
    depth = 25
    nb_dense_block = 3
    nb_filter = 16
    growth_rate = 18
    bn = True
    reduction_ = 0.5
    bs = 32
    weight_file = 'hyperas_dn_lr_optimizer_wt_5Oct_2210.h5'
    nb_classes = 1
    img_dim = (2, 96, 96)
    n_channels = 2

    model = DenseNet(depth=depth,
                     nb_dense_block=nb_dense_block,
                     growth_rate=growth_rate,
                     nb_filter=nb_filter,
                     dropout_rate=dropout,
                     activation='sigmoid',
                     input_shape=img_dim,
                     include_top=True,
                     bottleneck=bn,
                     reduction=reduction_,
                     classes=nb_classes,
                     pooling='avg',
                     weights=None)

    model.summary()
    model.compile(loss=binary_crossentropy,
                  optimizer=Nadam(),
                  metrics=['accuracy'])

    es = EarlyStopping(monitor='val_loss', patience=es_patience, verbose=1)
    checkpointer = ModelCheckpoint(filepath=weight_file,
                                   verbose=1,
                                   save_best_only=True)

    lr_reducer = ReduceLROnPlateau(monitor='val_loss',
                                   factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=lr_patience,
                                   min_lr=0.5e-6,
                                   verbose=1)

    model.fit(X_train,
              y_train,
              batch_size=bs,
              epochs=epochs,
              callbacks=[checkpointer, es],
              validation_data=(X_val, y_val),
              verbose=2)

    score, acc = model.evaluate(X_val, y_val)
    print("current val accuracy:%0.3f" % acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("current auc_score ------------------> %0.3f" % auc_score)

    model = load_model(weight_file)  #This is the best model
    score, acc = model.evaluate(X_val, y_val)
    print("Best saved model val accuracy:%0.3f" % acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("best saved model auc_score ------------------> %0.3f" % auc_score)

    return {'loss': -auc_score, 'status': STATUS_OK, 'model': model}
示例#4
0
# Parameters for the DenseNet model builder
img_dim = (img_channels, img_rows,
           img_cols) if K.image_data_format() == 'channels_first' else (
               img_rows, img_cols, img_channels)
depth = 40
nb_dense_block = 3
growth_rate = 12
nb_filter = 16
dropout_rate = 0.0  # 0.0 for data augmentation

# Create the model (without loading weights)
model = DenseNet(depth,
                 nb_dense_block,
                 growth_rate,
                 nb_filter,
                 dropout_rate=dropout_rate,
                 input_shape=img_dim,
                 weights=None)
print('Model created')

model.summary()

optimizer = Adam(lr=1e-3)  # Using Adam instead of SGD to speed up training
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=['acc'])
print('Finished compiling')

(trainX, trainY), (testX, testY) = cifar10.load_data()
示例#5
0
def fit_model(X_train, y_train, X_val, y_val):
    epochs = 30
    #input_shape = (1,96,96)
    es_patience = 5
    lr_patience = 5
    #dense_filter = 512
    #dropout = 0.76
    dropout1 = None
    depth = 13  #40
    nb_dense_block = 3
    nb_filter = 18
    growth_rate = 12
    weight_decay = 1E-4
    lr = 3E-4
    weight_file = 'keras_densenet_simple_wt_28Sept.h5'

    nb_classes = 1
    img_dim = (2, 96, 96)
    n_channels = 2

    model = DenseNet(depth=depth,
                     nb_dense_block=nb_dense_block,
                     growth_rate=growth_rate,
                     nb_filter=nb_filter,
                     dropout_rate=dropout1,
                     activation='sigmoid',
                     input_shape=img_dim,
                     include_top=True,
                     bottleneck=True,
                     reduction=0.5,
                     classes=nb_classes,
                     pooling='avg',
                     weights=None)

    model.summary()
    opt = Adam(lr=lr)
    model.compile(loss=binary_crossentropy,
                  optimizer=opt,
                  metrics=['accuracy'])

    es = EarlyStopping(monitor='val_loss', patience=es_patience, verbose=1)
    #es = EarlyStopping(monitor='val_acc', patience=es_patience,verbose=1,restore_best_weights=True)
    checkpointer = ModelCheckpoint(filepath=weight_file,
                                   verbose=1,
                                   save_best_only=True)

    lr_reducer = ReduceLROnPlateau(monitor='val_loss',
                                   factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=lr_patience,
                                   min_lr=0.5e-6,
                                   verbose=1)

    model.fit(X_train,
              y_train,
              batch_size=64,
              epochs=epochs,
              callbacks=[es, lr_reducer, checkpointer],
              validation_data=(X_val, y_val),
              verbose=2)

    score, acc = model.evaluate(X_val, y_val)
    print('current Test accuracy:', acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("current auc_score ------------------> ", auc_score)

    model = load_model(weight_file)  #This is the best model
    score, acc = model.evaluate(X_val, y_val)
    print('Best saved model Test accuracy:', acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("best saved model auc_score ------------------> ", auc_score)

    threshold = 0.6
    pred_scores2 = (pred > threshold).astype(int)
    test_acc2 = accuracy_score(y_val, pred_scores2)
    print('Test accuracy 0.6:', test_acc2)

    return auc_score, model
def create_model():
    epochs = 4
    es_patience = 7
    lr_patience = 5
    dropout = None
    #depth = {{choice([7,13,19,25,31])}}
    #nb_dense_block = {{choice([2,3])}}
    depth = 7
    nb_dense_block = 2
    nb_filter = 16
    #growth_rate = {{choice([6,10,14,18])}}
    growth_rate = 10
    bn = True
    reduction_ = 0.5
    bs = 32
    lr = 1E-3  #########################################################CHange file name##########################################
    nb_classes = 1
    img_dim = (2, 96, 96)
    n_channels = 2

    model = DenseNet(depth=depth,
                     nb_dense_block=nb_dense_block,
                     growth_rate=growth_rate,
                     nb_filter=nb_filter,
                     dropout_rate=dropout,
                     activation='sigmoid',
                     input_shape=img_dim,
                     include_top=True,
                     bottleneck=bn,
                     reduction=reduction_,
                     classes=nb_classes,
                     pooling='avg',
                     weights=None)

    model.summary()
    opt = Adam(lr=lr)
    model.compile(loss=binary_crossentropy,
                  optimizer=opt,
                  metrics=['accuracy'])
    """es = EarlyStopping(monitor='val_loss', patience=es_patience,verbose=1)
    #es = EarlyStopping(monitor='val_acc', patience=es_patience,verbose=1,restore_best_weights=True)
    checkpointer = ModelCheckpoint(filepath=weight_file,verbose=1, save_best_only=True)

    lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1), cooldown=0, patience=lr_patience, min_lr=0.5e-6,verbose=1)

    model.fit(X_train,y_train,
          batch_size=bs,
          epochs=epochs,
          callbacks=[es,lr_reducer,checkpointer],
          validation_data=(X_val,y_val),
          verbose=2)
    
    score, acc = model.evaluate(X_test, y_test)
    print('current Test accuracy:', acc)
    pred = model.predict(X_test)
    auc_score = roc_auc_score(y_test,pred)
    print("current auc_score ------------------> ",auc_score)"""
    """model = load_model(weight_file) #This is the best model
    score, acc = model.evaluate(X_test, y_test)
    print('Best saved model Test accuracy:', acc)
    pred = model.predict(X_test)
    auc_score = roc_auc_score(y_test,pred)
    print("best saved model auc_score ------------------> ",auc_score)"""

    return model
示例#7
0
def create_model(X_train, y_train, X_val, y_val, X_test, y_test):
    epochs = 1
    es_patience = 5
    lr_patience = 3
    dropout = None
    depth = {{choice([7, 16, 25, 34])}}
    nb_dense_block = {{choice([2, 3, 4])}}
    nb_filter = 16
    growth_rate = {{choice([6, 14, 22, 30])}}
    bn = True
    reduction_ = 0.5
    bs = 32
    lr = {{choice([1E-2, 5E-2, 1E-3, 5E-3, 1E-4, 5E-4, 1E-5, 5E-5])}}
    weight_file = 'keras_densenet_simple_wt_02Oct_1300.h5'
    nb_classes = 1
    img_dim = (2, 96, 96)
    n_channels = 2
    print("Depth: ", depth, " Growth_rate: ", growth_rate, " Nb_dense_block: ",
          nb_dense_block, " lr: ", lr)

    model = DenseNet(depth=depth,
                     nb_dense_block=nb_dense_block,
                     growth_rate=growth_rate,
                     nb_filter=nb_filter,
                     dropout_rate=dropout,
                     activation='sigmoid',
                     input_shape=img_dim,
                     include_top=True,
                     bottleneck=bn,
                     reduction=reduction_,
                     classes=nb_classes,
                     pooling='avg',
                     weights=None)

    model.summary()
    opt = Adam(lr=lr)
    model.compile(loss=binary_crossentropy,
                  optimizer=opt,
                  metrics=['accuracy'])

    es = EarlyStopping(monitor='val_loss', patience=es_patience, verbose=1)
    checkpointer = ModelCheckpoint(filepath=weight_file,
                                   verbose=1,
                                   save_best_only=True)

    lr_reducer = ReduceLROnPlateau(monitor='val_loss',
                                   factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=lr_patience,
                                   min_lr=0.5e-6,
                                   verbose=1)

    model.fit(X_train,
              y_train,
              batch_size=bs,
              epochs=epochs,
              callbacks=[lr_reducer, es],
              validation_data=(X_val, y_val),
              verbose=2)

    score, acc = model.evaluate(X_val, y_val)
    print('current val accuracy:', acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("current auc_score ------------------> ", auc_score)

    #model = load_model(weight_file) #This is the best model
    #score, acc = model.evaluate(X_val,y_val)
    #print('Best saved model val accuracy:', acc)
    #pred = model.predict(X_val)
    #auc_score = roc_auc_score(y_val,pred)
    #print("best saved model auc_score ------------------> ",auc_score)

    return {'loss': -auc_score, 'status': STATUS_OK, 'model': model}
示例#8
0
def fit_model(X_train, y_train, X_val, y_val, G):
    epochs = 5
    es_patience = 5
    lr_patience = 3
    dropout1 = None
    depth = 25  #40
    nb_dense_block = 3
    nb_filter = 18
    growth_rate = 18
    lr = 3E-1
    weight_file = 'keras_densenet_simple_wt_30Sept.h5'
    bn = True
    reduction_ = 0.5
    nb_classes = 1
    img_dim = (2, 96, 96)
    n_channels = 2

    model = DenseNet(depth=depth,
                     nb_dense_block=nb_dense_block,
                     growth_rate=growth_rate,
                     nb_filter=nb_filter,
                     dropout_rate=dropout1,
                     activation='sigmoid',
                     input_shape=img_dim,
                     include_top=True,
                     bottleneck=bn,
                     reduction=reduction_,
                     classes=nb_classes,
                     pooling='avg',
                     weights=None)

    model.summary()
    opt = Adam(lr=lr)
    parallel_model = multi_gpu_model(model, gpus=G)

    parallel_model.compile(loss=binary_crossentropy,
                           optimizer=Adadelta(),
                           metrics=['accuracy'])

    es = EarlyStopping(monitor='val_loss', patience=es_patience, verbose=1)
    #es = EarlyStopping(monitor='val_acc', patience=es_patience,verbose=1,restore_best_weights=True)
    checkpointer = ModelCheckpoint(filepath=weight_file,
                                   verbose=1,
                                   save_best_only=True)

    lr_reducer = ReduceLROnPlateau(monitor='val_loss',
                                   factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=lr_patience,
                                   min_lr=0.5e-6,
                                   verbose=1)

    parallel_model.fit(X_train,
                       y_train,
                       batch_size=64 * G,
                       epochs=epochs,
                       callbacks=[es, lr_reducer, checkpointer],
                       validation_data=(X_val, y_val),
                       verbose=2)

    score, acc = parallel_model.evaluate(X_val, y_val)
    print('current Test accuracy:', acc)
    pred = parallel_model.predict(X_val)
    auc_score = roc_auc_score(y_val, pred)
    print("current auc_score ------------------> ", auc_score)
    """model = load_model(weight_file) #This is the best model
    score, acc = model.evaluate(X_val, y_val)
    print('Best saved model Test accuracy:', acc)
    pred = model.predict(X_val)
    auc_score = roc_auc_score(y_val,pred)
    print("best saved model auc_score ------------------> ",auc_score)"""

    return auc_score, parallel_model