简体   繁体   English

迁移学习,错误的密集层形状

[英]Transfer learning, wrong dense layer's shape

I am trying to apply transfer learning to my ANN for image classification.我正在尝试将迁移学习应用于我的ANN以进行图像分类。 I have found an example of it, and I would personalize the network.我找到了一个例子,我会个性化网络。

Here there are the main blocks of code:这里有主要的代码块:

model = VGG19(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3))
batch_size = 16

for layer in model.layers[:5]:
    layer.trainable = False

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(16, activation="sigmoid")(x)

model_final = Model(input = model.input, output = predictions)

model_final.fit_generator(
train_generator,
samples_per_epoch = nb_train_samples,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples,
callbacks = [checkpoint, early])

When I run the code above I get this error:当我运行上面的代码时,我收到此错误:

ValueError: Error when checking target: expected dense_3 to have shape (16,) but got array with shape (1,) . ValueError: Error when checking target: expected dense_3 to have shape (16,) but got array with shape (1,)

I suppose that the problem is about the dimensions' order in the dense layer, I have tried to transpose it, but I get the same error.我想问题在于dense层中的维度顺序,我试图转置它,但我得到了同样的错误。

Maybe this simple example can help:也许这个简单的例子可以帮助:

import numpy as np

test = np.array([1,2,3])
print(test.shape) # (3,)

test = test[np.newaxis]
print(test.shape) # (1, 3)  

Try apply [np.newaxis] in your train_generator output.尝试在您的train_generator输出中应用[np.newaxis]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM