简体   繁体   English

使用Keras VGG16的工作-故障输出形状

[英]Working on using Keras VGG16 — Trouble output shapes

I am trying to train the VGG16 model (from keras.applications) from scratch and I get a weird error 我正在尝试从头训练VGG16模型(来自keras.applications),但出现奇怪的错误

X_train shape is (73257, 48, 48, 3) X_train的形状是(73257,48,48,3)

Y_train shape is (73257, 10) Y_train形状为(73257,10)

I have no idea what's going on... I assume it has to do with the conv layer before it but since I'm importing the model directly from keras I'm having issues figuring out where I went wrong. 我不知道发生了什么...我认为它与转换层有关,但是由于我是直接从keras导入模型,所以我在弄清楚我出了问题的地方遇到了问题。

My dataset is comprised of 73,257 images which have (48,48,3) shape. 我的数据集包含73,257张具有(48,48,3)形状的图像。 I am essentially trying to do character recognition (think mnist flavor) but I am getting hung up on feeding this through the model (with weights set to 0). 我本质上是在尝试进行字符识别(想想mnmn风格),但是我很想通过模型(权重设置为0)进行输入。

model = keras.applications.vgg16.VGG16(include_top=False,
                                       weights=None,
                                       input_shape=(48, 48, 3),
                                       input_tensor=None, pooling='avg', classes=10)
sgd = SGD(lr=.1)

model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
print('X_train shape is {}'.format(X_train.shape))
print('Y_train shape is {}'.format(y_train.shape))

model.fit(X_train, y_train,
          epochs=20,
          batch_size=128)

score = model.evaluate(X_test, y_test, batch_size=128)

This is the error I am getting 这是我得到的错误

File "/home/codebrotherone/PycharmProjects/Computer Vision/deep_neural/dnn.py", line 169, in VGG16 batch_size=128) 文件“ / home / codebrotherone / PycharmProjects / Computer Vision / deep_neural / dnn.py”,行169,在VGG16 batch_size = 128中)

File "/home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py", line 1574, in fit batch_size=batch_size) 文件“ /home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py”,行1574,适合batch_size = batch_size)

File "/home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py", line 1411, in _standardize_user_data exception_prefix='target') 文件“ /home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py”,第1411行,位于_standardize_user_data exception_prefix ='target')

File "/home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py", line 141, in _standardize_input_data str(array.shape)) 文件“ /home/codebrotherone/anaconda2/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py”第141行,位于_standardize_input_data str(array.shape)中

ValueError: Error when checking target: expected block5_pool to have 4 dimensions, but got array with shape (73257, 10) ValueError:检查目标时出错:预期block5_pool具有4维,但数组的形状为(73257,10)

Ideally, for classification problems, you should have include_top=True and classes=10 . 理想情况下,对于分类问题,您应该具有include_top=Trueclasses=10

This is enough. 这就够了。 Since you're not including top, and are using a global pooling, you should end up with something like (73257,512). 由于您不包括top,并且正在使用全局池,因此应该以类似(73257,512)的形式结束。 But the message you're getting suggests that you have not used the pooling in this attempt. 但是,您收到的消息表明您没有在此尝试中使用池。 Something is not quite matching. 不太匹配。

Anyway, go with this: 无论如何,用这个:

model = keras.applications.vgg16.VGG16(include_top=True,
                                   input_shape=(48, 48, 3),
                                   classes=10)

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

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