ValueError:检查目标时出错:预期activation_17有2个维度,但得到的数组有形状(1,256,256,3)

时间:2017-12-20 18:18:26

标签: python-2.7 machine-learning keras anaconda image-recognition

我正在构建一个基于VGG-16预测人类年龄的CNN,并提供两个RGB图像。 (filetype:jpg)。我在使用Python 2.7的anaconda环境中使用tensorflow后端。

但是,它总是会引发错误:

  

追踪(最近一次呼叫最后一次):

     

文件" train2.py",第167行,in       洗牌= TRUE)

     

File" /Users/name/anaconda/lib/python2.7/sitepackages/keras/models.py" ;,第973行,in fit validation_steps = validation_steps)

     

File" /Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py" ;,第1581行,in fit batch_size = batch_size)

     

File" /Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py" ;,第1418行,在_standardize_user_data exception_prefix =' target')

     

File" /Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py" ;,第141行,_standardize_input_data str(array.shape))

     

ValueError:检查目标时出错:预期activation_17为

     

有2个维度,但是有形状的数组(1,256,256,3)

如何解决此错误?这是代码:

import keras

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.layers import Dense

from keras.layers import Dropout

from keras.layers import Activation

from keras.layers import Flatten

from keras.layers import Conv2D

from keras.layers import MaxPooling2D

from keras.layers import BatchNormalization

from keras.utils import np_utils

import cv2

from PIL import Image

import numpy as np

from sklearn.cross_validation import train_test_split

from sklearn.model_selection import train_test_split

name_path = ["pathname"]

new_age_list=[45,52]

img_rows=256

img_cols=256

img_array = 
np.array([np.array(Image.open(i).resize((img_rows,img_cols),Image.BILINEAR)) for i in name_path[0:2]],"f")

(X, y) = (img_array[0:2],new_age_list[0:2])

y=np.asarray(y)

X=X.reshape(2,256,256,3)

# STEP 1: split X and y into training and testing sets

train_data, train_label,test_data, test_label= train_test_split(X, y, 
test_size=0.5, random_state=4)

train_data = train_data.astype('float32')

test_data = test_data.astype('float32')

train_data = train_data / 255

test_data = test_data / 255

""" Model """

model = Sequential()

""" Block 1 """

model.add(Conv2D(64, (3,3), padding='same', 
border_mode='valid',input_shape=(256,256,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(64, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))

""" Block 2 """

model.add(Conv2D(128, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(128, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))

""" Block 3 """

model.add(Conv2D(256, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(256, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(256, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))

""" Block 4 """
model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))

""" Block 5 """

model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Conv2D(512, (3,3)))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Dropout(0.25))

""" Flatten """

model.add(Flatten())

model.add(Dense(512))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Dense(128))

model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Dense(32))

model.add(BatchNormalization())

model.add(Activation('relu'))

model.add(Dense(1))

model.add(BatchNormalization())

model.add(Activation('softmax'))

""" Optimizer """

opt = keras.optimizers.rmsprop(lr=config.learning_rate, 

decay=config.decay)

print model.summary()

model.compile(loss='mean_squared_error', optimizer=opt, metrics=
['accuracy'])

""" Fit Data """

batch_size = 512

epoch = 1000

learning_rate = 1e-4

decay = 1e-7

for i in range(epoch):

    model.fit(train_data, train_label,

              batch_size=batch_size,

              epochs=int(epoch/epoch),

              validation_data=(test_data, test_label),

              shuffle=True)

1 个答案:

答案 0 :(得分:2)

我假设“activation_17”指的是最后一个激活层,即“softmax”层。当您的图像没有问题地在图层中传播时,我认为标签的尺寸存在问题。

检查sklearn.model_selection.train_test_split的{​​{3}}后,您似乎错误地指定了train_test_split()的返回值。

尝试如下:

train_data, test_data, train_label, test_label= train_test_split(X, y, test_size=0.5, random_state=4)