簡體   English   中英

Keras轉換神經網絡問題:ValueError:檢查輸入時出錯:預期conv2d_1_input具有4維

[英]Keras conv-neural-network issue: ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions

我是Keras的新手,並嘗試構建卷積神經網絡。 對於預處理,我在樣本中(批次大小)對每個圖片取了32個補丁,大小為200 x 200(補丁大小)。 我生成了它的numpy數組並保存了它們。 我嘗試用這些批次來填充模型。

pg = PatchGenerator(image_folder, list_path, sheet_name, patch_size, 
batch_size, split_distribution)

# Load batches and labels
X_train = []
Y_train = []
c = 0
for x in range(2):
     X_train.append(np.load(train_batch_load_path + train_batch_name + 
     str(c+1) + '.npy'))
     Y_train.append(pg.hd_train_list[c])
     c = c + 1
X_train = np.array(X_train)

print(X_train.shape)  # shape: (2, 32, 200, 200, 3)

# Model
model.add(Convolution2D(filters=16, kernel_size=3, strides=(1, 1), 
bias_initializer='zeros', padding='same', data_format='channels_last', 
activation=None, batch_size=32, input_shape=(patch_size, patch_size, 3))) 

print(model.output_shape) # shape: (32, 200, 200, 16)

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),  padding='same', 
data_format='channels_last'))

print(model.output_shape) #shape: (32, 100, 100, 16)

model.add(Convolution2D(filters=32, kernel_size=3, strides=(2, 2), 
bias_initializer='zeros', padding='same', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 50, 50, 32)

model.add(AveragePooling2D(pool_size=(2, 2), strides=(5, 5),  
padding='same', data_format='channels_last'))

print(model.output_shape) # shape: (32, 10, 10, 32)

model.add(Convolution2D(filters=16, kernel_size=5, strides=(1, 1), 
bias_initializer='zeros', padding='valid', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 6, 6, 16)

model.add(MaxPooling2D(pool_size=(4, 4), strides=(1, 1),  padding='valid', 
data_format='channels_last'))

print(model.output_shape) # shape: (32, 3, 3, 16)

model.add(Convolution2D(filters=1, kernel_size=3, strides=(1, 1), 
bias_initializer='zeros', padding='valid', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 1, 1, 1)

model.add(Flatten())

print(model.output_shape) # shape: (32, 1)

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

model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)

當我嘗試運行代碼時,出現以下錯誤:

ValueError:檢查輸入時出錯:預期conv2d_1_input具有4個維,但數組的形狀為(2,32,200,200,3)

我知道它們的數量多於4個Dimenson,但是當我只對形狀為4個Dimenson的一批批次執行相同操作時,我得到一個錯誤,即我的尺寸太小。 我認為輸出形狀是運行模型所需的正確形狀。

希望您能夠幫助我。

不,錯誤所提到的,您的數據應為4D。 您給定的輸入形狀等於(200,200,3),這意味着輸入X的形狀應為(samples,200,200,3)。 樣本可以是任意數量,但其他兩個維度是固定的。

如果在使用4D陣列時遇到其他錯誤,則還應將其包括在問題中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM