[英]ValueError: Error when checking target: expected conv2d_3 to have 4 dimensions, but got array with shape (66256, 58)
[英]ValueError: Error when checking target: expected conv2d_21 to have 4 dimensions, but got array with shape (26, 1)
我有形狀為(3600, 3600, 3)
。 我想對它們使用自動編碼器。 我的代碼是:
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
input_img = Input(shape=(3600, 3600, 3))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
batch_size=2
datagen = ImageDataGenerator(rescale=1. / 255)
# dimensions of our images.
img_width, img_height = 3600, 3600
train_data_dir = 'train'
validation_data_dir = validation
generator_train = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
)
generator_valid = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
autoencoder.fit_generator(generator=generator_train,
validation_data = generator_valid,
)
當我運行代碼時,出現以下錯誤消息:
ValueError: Error when checking target: expected conv2d_21 to have 4 dimensions, but got array with shape (26, 1)
我知道問題出在層的形狀中,但是我找不到它。 有人可以幫我解釋一下解決方案嗎?
您的代碼中存在以下問題:
將class_mode='input'
傳遞給flow_from_directory
方法以將輸入圖像也作為標簽提供(因為您正在創建自動編碼器)。
將padding='same'
傳遞到解碼器中的第三個Conv2D層:
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
由於您的圖像是RGB,請在最后一層使用三個濾鏡:
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.