【问题标题】:Customising transfer learning model tensorflow-Keras自定义迁移学习模型 tensorflow-Keras
【发布时间】:2021-06-03 01:01:59
【问题描述】:

我正在尝试将添加卷积层添加到下面提到的迁移学习代码中。但不确定如何进行。我想添加 conv, max-pooling, 3x3 filter and stride 3 and activation mode ReLUconv, max-pooling, 3x3 filter and stride 3 and activation mode LReLU下面提到的迁移学习代码中的这一层。让我知道这是否可能,如果可以,如何?

 CLASSES = 2
 
# setup model
base_model = MobileNet(weights='imagenet', include_top=False)
 
x = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dropout(0.4)(x)
predictions = Dense(CLASSES, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
 
# transfer learning
for layer in base_model.layers:
 layer.trainable = False
 
model.compile(optimizer='rmsprop',
 loss='categorical_crossentropy',
 metrics=['accuracy'])
 
"""##Data augmentation"""
 
# data prep

"""
## Transfer learning
"""
 
from tensorflow.keras.callbacks import ModelCheckpoint
filepath="mobilenet/my_model.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
 
EPOCHS = 1
BATCH_SIZE = 32
STEPS_PER_EPOCH = 5
VALIDATION_STEPS = 32
 
MODEL_FILE = 'mobilenet/filename.model'
 
history = model.fit_generator(
 train_generator,
 epochs=EPOCHS,
 steps_per_epoch=STEPS_PER_EPOCH,
 validation_data=validation_generator,
 validation_steps=VALIDATION_STEPS,
 callbacks=callbacks_list)
 
model.save(MODEL_FILE)
backup_model = model
model.summary()

【问题讨论】:

    标签: python tensorflow keras deep-learning transfer-learning


    【解决方案1】:

    我想这就是你所追求的

    CLASSES=2
    new_filters=256 # specify the number of filter you want in the added convolutional layer
    img_shape=(224,224,3)
    base_model=tf.keras.applications.mobilenet.MobileNet( include_top=False, input_shape=img_shape,  weights='imagenet',dropout=.4) 
    x=base_model.output
    x= Conv2D(new_filters, 3, padding='same', strides= (3,3), activation='relu', name='added')(x)
    x= GlobalAveragePooling2D(name='avg_pool')(x)
    x= Dropout(0.4)(x)
    predictions= Dense(CLASSES, activation='softmax', name='output')(x)
    
    model=Model(inputs=base_model.input, outputs=predictions)
    model.summary()
    

    【讨论】:

    • 我试过这个,我能生成模型,如果你能解释一下sn-p,那就太好了。
    【解决方案2】:

    您可以通过多种方式做到这一点,其中之一是:

    model = Sequential([
        base_model,
        GlobalAveragePooling2D(name='avg_pool'),
        Dropout(0.4),
        Conv(...), # the layers you would like to add for the base model
        MaxPool(...),
        ...
    ])
    
    model.compile(...)
    

    【讨论】:

    • 这应该加在下面提到的代码之前?``` x = base_model.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.4)(x) predictions =密集(类,激活='softmax')(x)模型=模型(输入=base_model.input,输出=预测)```
    • 没有。我已经添加了原始答案,看看。
    猜你喜欢
    • 2023-03-19
    • 2022-11-27
    • 1970-01-01
    • 2022-11-10
    • 2020-05-13
    • 2023-03-27
    • 1970-01-01
    • 2020-10-31
    • 2020-02-11
    相关资源
    最近更新 更多