【问题标题】:Conv2d input parameter mismatchConv2d 输入参数不匹配
【发布时间】:2018-09-24 00:47:55
【问题描述】:

我正在向我的 cnn 模型提供可变大小的图像(每个类别的 139 个不同大小的所有 278 个图像)输入。事实上,cnn 需要固定大小的图像,所以从here 我得到的解决方案是使 input_shape=(None,Nonen,1) (用于 tensorflow 后端和灰度)。但是这个解决方案不适用于扁平层,所以我从他们那里得到了使用 GlobleMaxpooling 或 Globalaveragepooling 的解决方案。因此,通过使用这些因素,我在 keras 中制作了一个 cnn 模型,以使用以下代码训练我的网络:

import os,cv2
import numpy as np
from sklearn.utils import shuffle
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential
from keras.optimizers import SGD,RMSprop,adam
from keras.layers import Conv2D, MaxPooling2D,BatchNormalization,GlobalAveragePooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import regularizers
from keras import initializers 
from skimage.io import imread_collection
from keras.preprocessing import image
from keras import Input
import keras
from keras import backend as K

#%%

PATH = os.getcwd()
# Define data path
data_path = PATH+'/current_exp'
data_dir_list = os.listdir(data_path)

img_rows=None
img_cols=None
num_channel=1

# Define the number of classes
num_classes = 2

img_data_list=[]

for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)
    print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
    for img in img_list:
        input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img,0)
        img_data_list.append(input_img)

img_data = np.array(img_data_list)

if num_channel==1:
    if K.image_dim_ordering()=='th':
        img_data= np.expand_dims(img_data, axis=1) 
        print (img_data.shape)
    else:
        img_data= np.expand_dims(img_data, axis=4) 
        print (img_data.shape)

else:
    if K.image_dim_ordering()=='th':
        img_data=np.rollaxis(img_data,3,1)
        print (img_data.shape)


#%%
num_classes = 2

#Total 278 sample, 139 for 0 category and 139 for category 1
num_of_samples = img_data.shape[0]

labels = np.ones((num_of_samples,),dtype='int64')

labels[0:138]=0
labels[138:]=1

x,y = shuffle(img_data,labels, random_state=2)
y = keras.utils.to_categorical(y, 2)

model = Sequential()
model.add(Conv2D(32,(2,2),input_shape=(None,None,1),activation='tanh',kernel_initializer=initializers.glorot_uniform(seed=100)))
model.add(Conv2D(32, (2,2),activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (2,2),activation='tanh'))
model.add(Conv2D(64, (2,2),activation='tanh'))
model.add(MaxPooling2D())
model.add(Dropout(0.25))
#model.add(Flatten())
model.add(GlobalAveragePooling2D())
model.add(Dense(256,activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(2,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
model.fit(x, y,batch_size=1,epochs=5,verbose=1)

但我收到以下错误:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (278, 1)

如何解决。

【问题讨论】:

    标签: keras keras-layer keras-2


    【解决方案1】:

    docs for Conv2D 中它说输入张量必须采用这种格式:

    (样本、通道、行、列)

    我相信除非你的网络是fully convolutional,否则你不能有可变的输入大小。

    也许您想要做的是将其保持为固定的输入大小,并在将图像输入网络之前将其调整为该大小?

    【讨论】:

      【解决方案2】:

      您的输入数据数组不能有可变维度(这是一个 numpy 限制)。

      因此,数组,而不是常规的 4 维数字数组,被创建为数组数组。

      由于此限制,您应该单独调整每张图片。

      for epoch in range(epochs):    
          for img,class in zip(x,y):
      
              #expand the first dimension of the image to have a batch size 
              img = img.reshape((1,) + img.shape)) #print and check there are 4 dimensions, like (1, width, height, 1).    
              class = class.reshape((1,) + class.shape)) #print and check there are two dimensions, like (1, classes).
      
              model.train_on_batch(img,class,....)
      

      【讨论】:

        猜你喜欢
        • 2018-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        相关资源
        最近更新 更多