output label Y train shape keras tensorflow 1.4

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I am running VGG16 network for image classification on svhn database. I am saving images into shape (None,64,64,3) and labels of shape (None,10).Labels are 1d array of size 10.

Below is the part of my code.

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) 

and so I get error:

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

What changes should I do?

回答1:

According to Keras docs, when you set include_top to False, you omit 3 fully connected layers and if you set it to True, you need to have 1000 classes with imagenet pretrained weights.

So what you need is to attach fully connected layers on top of the vgg network:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!