输出标签Y train shape keras tensorflow 1.4

时间:2017-11-26 05:35:25

标签: python tensorflow keras

我在svhn数据库上运行VGG16网络进行图像分类。 我将图像保存为形状(无,64,64,3)和形状标签(无,10)。标签是1d大小为10的数组。

以下是我的代码部分。

import pandas as pd
import numpy as np
import cv2
import tensorflow as tf
import os
import scipy
from skimage import data, io, filters
import scipy.io as sio
from utils import *
import h5py

vgg = tf.keras.applications.vgg16.VGG16 (include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=(64,64,3),
pooling='avg',
classes=10)

vgg.compile(loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

vgg.fit(train_data, labels_data, epochs=5, batch_size=32)

因此我收到错误:

    ValueError: Error when checking target: expected block5_pool to have shape (None, 512) but got array with shape (None, 10)

我应该做些什么改变?

1 个答案:

答案 0 :(得分:1)

根据Keras docs,当您将include_top设置为False时,省略3个完全连接的图层,如果将其设置为True,则需要有1000个类imagenet预训练重量。

所以你需要的是在vgg网络的顶部连接完全连接的层:

model = Sequential([vgg, Dense(10), Activation('softmax')])
model.compile(loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

# now check the input/output shapes
print(model.input_shape)
print(model.output_shape)