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

时间:2019-04-25 20:59:11

标签: python machine-learning deep-learning

当我尝试使用VGG-16模型拟合包含图像的ndarrays的数组时,错误:“ ValueError:检查输入时出错:期望conv2d_14_input具有4个尺寸,但形状为数组(16,1) ”。知道如何解决此问题吗?

我已经看到很多与此相似的问题,但是遵循这些问题的答案对我来说并不起作用。例如,我曾尝试通过以下方式来调整X_train的形状大小:X_train.values.reshape([-1,266,472,3]),但它回答了一个错误,即无法将大小为16的数组重塑为形状(266,472, 3)。

信息:

X形状=(20,)

图像形状=(472,266,3)

CNN中的input_shape =(472,266,3)

以下是print(X)

0     [[[19, 17, 20], [19, 17, 20], [19, 17, 20], [2...
1     [[[15, 13, 16], [15, 13, 16], [15, 13, 16], [1...
2     [[[16, 14, 17], [15, 13, 16], [15, 13, 16], [1...
3     [[[13, 11, 14], [13, 11, 14], [12, 10, 13], [1...
4     [[[11, 9, 12], [11, 9, 12], [11, 9, 12], [11, ...
5     [[[14, 12, 15], [14, 12, 15], [14, 12, 15], [1...
6     [[[11, 9, 12], [11, 9, 12], [11, 9, 12], [11, ...
7     [[[13, 11, 14], [13, 11, 14], [12, 10, 13], [1...
8     [[[13, 11, 14], [12, 10, 13], [11, 9, 12], [11...
9     [[[12, 10, 13], [12, 10, 13], [12, 10, 13], [1...
10    [[[12, 10, 13], [12, 10, 13], [12, 10, 13], [1...
11    [[[12, 10, 13], [12, 10, 13], [12, 10, 13], [1...
12    [[[18, 16, 19], [18, 16, 19], [17, 15, 18], [1...
13    [[[18, 16, 19], [17, 15, 18], [16, 14, 17], [1...
14    [[[9, 7, 10], [10, 8, 11], [10, 8, 11], [11, 9...
15    [[[24, 22, 25], [23, 21, 24], [23, 21, 24], [2...
16    [[[21, 19, 22], [21, 19, 22], [20, 18, 21], [2...
17    [[[20, 18, 21], [20, 18, 21], [21, 19, 22], [2...
18    [[[12, 10, 13], [12, 10, 13], [12, 10, 13], [1...
19    [[[11, 9, 12], [11, 9, 12], [11, 9, 12], [11, ...
Name: Labeled Data, dtype: object

这是我的代码:

#Import Data
dataset = pd.read_csv('/content/gdrive/My Drive/CNN-VGG16/lowres.csv')

#Category Label
dataset["Label"] = dataset["Label"].astype("category")
dataset["Label_category"] = dataset["Label"].cat.codes

#Use specific columns for data
X = dataset['Labeled Data']
y = dataset["Label_category"]

#URL images to ndarray function
def url_to_ndarray(url):
    ndarray = io.imread(url)
    return ndarray

#Replaces url with ndarray
for i, data in enumerate(X):
  X[i] = url_to_ndarray(data)

#Splitting data into training and validation
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)

#VGG-16 Archiitecture
def NewVGG16():
  model = Sequential()
  model.add(Conv2D(64,(3,3),input_shape=input_shape,padding='same',activation='relu'))
  model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  model.add(Conv2D(128,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(128,(3,3),activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  model.add(Conv2D(56,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(Conv2D(512,(3,3),activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  model.add(Flatten())
  model.add(Dense((4096),activation = 'relu'))
  model.add(Dense((4096),activation = 'relu'))
  model.add(Dense((1000),activation='softmax'))
  return model

#Create and Compile model
model = NewVGG16()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#Fit Model
model.fit(X_train,y_train,batch_size = 32, verbose=1)

0 个答案:

没有答案