如何使用转移学习Vgg拟合我的模型

时间:2019-06-02 09:01:13

标签: tensorflow machine-learning keras deep-learning vgg-net

我是机器学习的新手,我得到了1个猫对狗图像分类的示例 这是它的链接

https://pythonprogramming.net/convolutional-neural-network-kats-vs-dogs-machine-learning-tutorial/

它工作得很好,但是现在当我想使用VGG16对其进行迁移学习时,它就不起作用了

from keras.models import Sequential, Model, load_model
from keras.applications.vgg16 import VGG16

from keras import optimizers
from keras.layers import Dropout, Flatten, Dense, Activation

from keras.models import Sequential
from keras import utils

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
test_y = np.array([i[1] for i in test])

from keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential


modelvgg = VGG16(weights='imagenet', include_top=False, input_shape=(50,50,3))
type(modelvgg)
modelvgg.layers.pop()
model = Sequential()
for layer in modelvgg.layers:
    model.add(layer)

for layer in model.layers:
    layer.trainable = False 

model.add(Dense(1, activation= 'sigmoid'))



model.compile(optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model.summary()


model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

这是我总是得到的错误

我想我在安装模块时遇到问题,所以我需要帮助

Unrecognized keyword arguments: {'n_epoch': 10, 'validation_set': ({'input': array([[[[ 41,  40,  36],
         [ 43,  42,  38],
         [ 43,  42,  38],

1 个答案:

答案 0 :(得分:1)

您链接的教程不使用keras,而是使用tflearn,难怪fit调用不起作用。与keras的正确通话应该是:

model.fit(X, Y, epochs=10, validation_data=(test_x, test_y))