【问题标题】:Question about implementing Transfer Learning Correctly in Python Keras关于在 Python Keras 中正确实现迁移学习的问题
【发布时间】:2020-06-25 15:50:29
【问题描述】:

我有一个作业,我必须在虹膜图像上使用迁移学习(数据集非常小。这是一个简单的作业)。任务是建立一个正则化的神经网络,将图像分类到各自的类别中。

我的整个代码如下:

import keras
from keras.preprocessing.image import ImageDataGenerator

#importing images trhrough ImageDataGenerator

train_gen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
test_gen = ImageDataGenerator(rescale = 1./255)

#Generating training and test sets

training_set = train_gen.flow_from_directory(r"Iris_Imgs",
target_size = (224, 224), shuffle=True, batch_size = 15, class_mode = 'categorical')
train_imgs, train_labels = next(training_set)
test_set = test_gen.flow_from_directory(r"Iris_Imgs",
target_size = (224, 224), shuffle=True, class_mode = 'categorical')
test_imgs, test_labels = next(test_set)

from keras.applications import VGG16
from keras.models import Model

#Importing VGG16 and getting weights

vgg_conv = VGG16(weights='imagenet', input_shape=(224, 224, 3))

vgg_conv = VGG16(weights='imagenet', include_top=False,
input_shape=(224, 224, 3))
part_model = Model(inputs = vgg_conv.input,
outputs = vgg_conv.get_layer('block4_pool').output)

#These are the features I would like to connect to my NN

block4_pool_features = part_model.predict(training_set)
vgg_conv.layers.pop()

这是我最困惑的部分 - 当我构建我的 NN(如下)时,我想确保我将层从 vgg_conv.output 层连接到我构建的 NN。

from tensorflow import keras
from tensorflow.keras import layers 
from keras.layers.convolutional import Conv2D
from keras.models import Sequential
from keras.layers.convolutional import MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout  


imageSize=224
classifier=Sequential() 

classifier.add(Conv2D(3, (3, 3), input_shape = (imageSize, imageSize, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (1, 1)))

classifier.add(BatchNormalization())


classifier.add(Conv2D(3, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (1, 1)))

classifier.add(BatchNormalization())

classifier.add(Flatten())

classifier.add(Dense(128, activation='relu'))

classifier.add(Dropout(1e-3))

classifier.add(Dense(3, activation='softmax'))

opt = keras.optimizers.Adam(learning_rate=3e-2)

#Connecting last layer - Am I doing this correctly? 

last = vgg_conv.output
x = Flatten()(last) ## functional API
x2 = Dense(1024, activation='relu')(x) # Fully Connect
my_preds = Dense(200, activation='softmax')(x2)


for layer in classifier.layers[:10000]: 
    layer.trainable = False

classifier.compile(loss='categorical_crossentropy', optimizer='adam', metrics = ['acc'])

classifier.fit(train_imgs, train_labels, batch_size = 10, epochs = 30)

我还注意到我构建的模型性能很差,因此非常欢迎任何建设性的批评(我是新手)。

谢谢!

【问题讨论】:

    标签: python keras artificial-intelligence transfer-learning


    【解决方案1】:

    我看到的一个问题是模型没有使用正确的优化器。该代码创建了 Adam 优化器,但没有使用它。更改代码以使用它。

    classifier.compile(loss='categorical_crossentropy', optimizer=opt, metrics = ['acc'])
    

    另外,为了性能尝试:

    1. 优化器中的学习率较低(如 1e-5、1e-4)。
    2. 使用较小的批量大小是好的,但也可以尝试将大小设置为 32、64。

    这也可能有用:https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-train-a-neural-network

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 2020-05-13
      • 2019-09-19
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多