ValueError:检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(454,512,512)

时间:2018-10-21 20:14:19

标签: python tensorflow keras

我使用此代码在keras中生成了我的数据集。但是当我实现我的代码时,会产生此错误:

  

ValueError:检查输入时出错:预期的conv2d_1_input为   有4个维度,但数组的形状为(454,512,512)

,我无法解决。你能告诉我是什么问题吗?在网络中使用之前,我先扩大尺寸,但不起作用!您能不能请我快速回答,因为我搜索了几天,但找不到解决方案,而且没有足够的时间:

coordinatorLayout

我的带有生成器的新代码在这里,您看到任何问题吗?我的数据集和以前一样。

import os,cv2
import numpy as np
import matplotlib.pyplot as plt

from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split

from keras import backend as K
#K.set_image_dim_ordering('th')

from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam

#%%

PATH = os.getcwd()
# Define data path
data_path = r"E:\PhD\thesis\deepwatermark\databasetest\train"
data_dir_list = os.listdir(data_path)

img_rows=512
img_cols=512
num_channel=1
num_epoch=20

# Define the number of classes
num_classes = 7

labels_name={'CRP':0,'GF':1,'GN':2,'JPG':3,'MED':4,'ROT':5,'SP':6}

img_data_list=[]
labels_list = []

for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)
    print ('Loading the images of dataset-'+'{}\n'.format(dataset))
    label = labels_name[dataset]
    for img in img_list:
        input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
        input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
        input_img_resize=cv2.resize(input_img,(512,512))
        img_data_list.append(input_img_resize)
        labels_list.append(label)

img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
print (img_data.shape)

labels = np.array(labels_list)
# print the count of number of samples for different classes
print(np.unique(labels,return_counts=True))
# convert class labels to on-hot encoding
Y = np_utils.to_categorical(labels, num_classes)

#Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
img_data= np.expand_dims(img_data, axis=4)** 
print (img_data.shape)


# Defining the model
input_shape=img_data[0].shape

model = Sequential()

model.add(Convolution2D(32, 3,3,border_mode='same',input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
#model.add(Convolution2D(64, 3, 3))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=["accuracy"])
model.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=["accuracy"])

# Viewing model_configuration

model.summary()
model.get_config()
model.layers[0].get_config()
model.layers[0].input_shape         
model.layers[0].output_shape            
model.layers[0].get_weights()
np.shape(model.layers[0].get_weights()[0])
model.layers[0].trainable

#%%
# Training
hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test))

但是当我实现它时,我再次收到此错误:

ResourceExhaustedError:分配带有形状[32,32,512,512]的张量并在/ job:localhost / replica:0 / task:0 / device:GPU:0上通过分配器GPU_0_bfc键​​入float      [[节点:conv2d_1 / convolution = Conv2D [T = DT_FLOAT,data_format =“ NCHW”,膨胀= [1,1,1,1],padding =“ SAME”,步幅= [1,1,1,1], use_cudnn_on_gpu = true,_device =“ / job:localhost /副本:0 / task:0 / device:GPU:0”](conv2d_1 / convolution-0-TransposeNHWCToNCHW-LayoutOptimizer,conv2d_1 / kernel / read)]]

1 个答案:

答案 0 :(得分:0)

    for img in img_list:
        input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
        input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
        input_img_resize=cv2.resize(input_img,(512,512))
    --->input_img_resize = np.expand_dims(input_img_resize, axis=-1)
        img_data_list.append(input_img_resize)
        labels_list.append(label)

这将使您的所有数组均为512x512x1,这应该可以解决问题,并最终将形状数组变成(454、512、512、1)。您确定要使用灰度图像吗?

另一件事是这段代码

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, `random_state=2)`
img_data= np.expand_dims(img_data, axis=4)**
print (img_data.shape)

在声明了x_train等之后,将另一个维度应用于img_data。最后,您输入未扩展的x_train,因此出现错误。如果您从一开始就做,然后在结尾处删除扩展,那么您的代码应该可以工作。

编辑OOM

我建议为OOM问题创建一个单独的问题,以便更多的人看到它。可能的问题是图像的大小和批处理大小。将图像尺寸减小为64 x 64,然后将批处理尺寸更改为5。如果仍然引发错误,请尝试也踢掉该密集层。

model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))

如果这些减少仍然导致错误,那么我有以下问题:您在GPU / CPU上运行,是哪一个?

只是重复一遍:代码很好,也许只需要进行一些更改。