简体   繁体   中英

ValueError: Error when checking target: expected conv2d_120 to have shape (256, 256, 16) but got array with shape (256, 256, 3)

my input is 256x256 rgb image , and my autoencoder output wants to be 256x256 rgb(but in only black and white color),input and ouput as below for example input image outputimage here's my code

train_data = np.empty((train_N,256,256,3))
train_labels = np.empty((trainL_N,256,256,3))
test_data = np.empty((test_N,256,256,3))
test_labels = np.empty((testL_N,256,256,3))
def loadIMG(imagePath , number, Array):
while number >0:
    img = cv2.imread(imagePath[number-1])
    img = cv2.resize(img,(256,256),interpolation=cv2.INTER_AREA)
    img_ndarray=np.asarray(img,dtype='float64')
    Array[number-1] = img_ndarray
    number = number - 1

loadIMG(imagePath1,train_N,train_data)
loadIMG(imagePath2,trainL_N,train_labels)
loadIMG(imagePath3,test_N,test_data)
loadIMG(imagePath4,testL_N,test_labels)
def train_model():

global history

input_img= Input(shape=(256, 256, 3))
#大小 = 256*256
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 127*127
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 62*62
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 30*30
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)    
#大小 = 14*14
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)   
#大小 = 6*6
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 2*2
#這邊直接再次maxpooling 來達到1*1
encoded = MaxPooling2D((2, 2), padding='same', name='encoder')(x)   #大小 = 1*1 

x = UpSampling2D((2, 2))(encoded)
#大小2*2
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小6*6
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)    
#大小14*14
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)    
#大小30*30
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小62*62
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小127*127
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(16, (3, 3), activation='softmax', padding='same')(x)
    
autoencoder = Model(input_img, decoded)  

print(autoencoder.summary())
autoencoder.compile(optimizer='adam', loss='categorical_crossentropy',metrics=[tf.keras.metrics.CategoricalAccuracy()])

history = autoencoder.fit(train_data, train_labels,
                epochs=20,
                batch_size=24,
                shuffle=True,
                validation_data=(test_data, test_labels),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder', histogram_freq=0, write_graph=False)])

autoencoder.save('autoencoder.h5')

here's the model summary the model summary

how to make (256,256,16) back to (256,256,3)? I have read other similiar problems but didn't find solutin about my situation

If you want your output to be a grayscale image you need to change the last layer in your model like this:

decoded = Conv2D(1, (3, 3), activation='softmax', padding='same')(x)

If you want to have your output shape as (256,256,3) then just use 3 as number of filters.

Not sure what you're trying to solve here though. Missing the context here..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM