I am training an encoder neural network to encode/decode images of shape None,100,100,3 with tensorflow (batch,height,width,channels). I want to output both the reconstructed image and the coded image (output of the encoder part of the nn) but when i fit the model an exception occurs. Stating:
- ValueError: Shapes (None, 100, 100, 3) and (None, 25, 25, 3) are incompatible File "/tmp/autograph_generated_fileh4snkmtb.py", line 15, in tf__train_function retval = ag_.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) ValueError: Shapes (None, 100, 100, 3) and (None, 25, 25, 3) are incompatible*
This is my NN:
def custom_architecture(input_shape):
inputs = input_shape
# Encoder
conv1 = Conv2D(64, (13, 13), activation='relu', padding='same')(inputs)
conv1 = Conv2D(64, (13, 13), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D((2, 2), padding='same')(conv1)
conv2 = Conv2D(128, (13, 13), activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, (13, 13), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D((2, 2), padding='same')(conv2)
# Bottleneck
conv3 = Conv2D(256, (13, 13), activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, (13, 13), activation='relu', padding='same')(conv3)
# Decoder
upconv2 = UpSampling2D((2, 2))(conv3)
conv2_upsampled = Conv2D(128, (1, 1), activation='relu', padding='same')(conv2)
concat2 = Concatenate(axis=-1)([upconv2, conv2_upsampled])
conv4 = Conv2D(128, (3, 3), activation='relu', padding='same')(concat2)
conv4 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv4)
upconv1 = UpSampling2D((2, 2))(conv4)
conv1_upsampled = Conv2D(64, (1, 1), activation='relu', padding='same')(conv1)
concat1 = Concatenate(axis=-1)([upconv1, conv1_upsampled])
conv5 = Conv2D(64, (3, 3), activation='relu', padding='same')(concat1)
conv5 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv5)
# Output layer
#outputs = Conv2D(1, (1, 1), activation='sigmoid', padding='same')(repeat_gap)
conv_output = layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same')(conv5)
outputs = conv_output#layers.Flatten()(conv_output)
# Create the model
model = Model(inputs, [outputs,conv3])
return model
this is the part where i call the compile and fit methods:
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
model.compile(optimizer='adam',loss=['binary_crossentropy',None], metrics=['accuracy'],loss_weights=[1.0,0.0])
history = model.fit(train_ds, epochs = 1001, batch_size=32, shuffle=True,callbacks = [tensorboard_callback])
I do not understand the problem, if i understand correctly you are supposed to be able to return more than one output, and this output should not be mandatory to match the input size. I mean the problem is that it is trying to compare it to compute the loss and metrics but i have stated ‘None’ so it doesnt do it for the second output (output of the encoder).
I have tried to change the loss function, adding None as function for the second output, and also return the encoder ouput with the same size of the input and treat it afterwards but this last option is not desirable as it outputs a very big chunk of information and i need to reduce it (the nn treats chunks of 100x100 called patches, for an image of size X,X)