custom loss function with multiple output and using add_loss - python

I have an autoencoder in keras and I need a loss function which is the combination of mse, a binary_crossentropy and third part that tries to make minimum the number of pixels of output that have a value different from 0 or 1. the final loss should be like this: amse+bbinary_crossentropy+c*L. I used the below code for this but it produces this error:
Traceback (most recent call last): File
"", line 134, in if
(pred_w(i,j)>=0 & pred_w(i,j)<0.1)|(pred_w(i,j)<=1 &
pred_w(i,j)>=0.9): TypeError: 'Tensor' object is not callable
could you please tell me what should I do to solve this problem? I appreciate your help.
wtm=Input((4,4,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e')(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)
#-----------------------adding w---------------------------------------
wpad=Kr.layers.Lambda(lambda xy: xy[0] + Kr.backend.spatial_2d_padding(xy[1], padding=((0, 24), (0, 24))))
encoded_merged=wpad([encoded,wtm])
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl3d')(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl4d')(deconv3)
BNd=BatchNormalization()(deconv4)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd)
model=Model(inputs=[image,wtm],outputs=decoded)
decoded_noise = GaussianNoise(0.5)(decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (5,5), activation='relu', name='conl1w')(decoded_noise)#24
convw2 = Conv2D(64, (5,5), activation='relu', name='convl2w')(convw1)#20
convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
convw7 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl7w',dilation_rate=(2,2))(convw6)#4
convw8 = Conv2D(64, (5,5), activation='relu', padding='same',name='conl8w',dilation_rate=(2,2))(convw7)#4
convw9 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl9w',dilation_rate=(2,2))(convw8)#4
convw10 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl10w',dilation_rate=(2,2))(convw9)#4
BNed=BatchNormalization()(convw10)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(BNed)
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
count=0
for i in range(28):
for j in range(28):
if (pred_w(i,j)>=0 & pred_w(i,j)<0.1)|(pred_w(i,j)<=1 & pred_w(i,j)>=0.9):
count+=1
loss = K.sum(0.7*mse(decoded, image),binary_crossentropy(pred_w,wtm))+count
w_extraction.add_loss(loss)

Technically the error just tells you that pred_w(i,j) should be pred_w[i,j] if you want to refer to an entry of the matrix. However, to make this code as you intend it to run requires quite a bit more rewrite.
To actually optimize a loss the main thing would be that it needs to be differentiable with regard to the weights in the networks. Examples you could use in this case could be:
(x-0.5)^N for a relatively high N
or log-barriers, i.e. -log(x)-log(1-x)
actually counting the numbers (which will not help for optimization) could be achieved with something like
count = tf.sum(pred_w<=0.1) + tf.sum(predictions_w>=0.9)
Maybe this is helpful to output how many numbers are in that range during training or something like this.

Related

TypeError: Inputs to a layer should be tensors. unet model

X_train = np.array(X_train)
X_test = np.array(X_test)
y_train = np.array(y_train)
y_test = np.array(y_test)
def unet():
# First block Going down
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same',input_shape=(s,s,3))
d1_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(d1_2)
# Second block Going down
d2_1 = MaxPooling2D()(d1_3)
d2_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(d2_1)
d2_3 = Conv2D(32, (3, 3), activation='relu', padding='same')(d2_2)
# Third block Going down
d3_1 = MaxPooling2D()(d2_3)
d3_2 = Conv2D(64, (3, 3), activation='relu', padding='same')(d3_1)
d3_3 = Conv2D(64, (3, 3), activation='relu', padding='same')(d3_2)
# Fourth block Going down
d4_1 = MaxPooling2D()(d3_3)
d4_2 = Conv2D(128, (3, 3), activation='relu', padding='same')(d4_1)
d4_3 = Conv2D(128, (3, 3), activation='relu', padding='same')(d4_2)
# Fifth block
d5_1 = MaxPooling2D()(d4_3)
d5_2 = Conv2D(256, (3, 3), activation='relu', padding='same')(d5_1)
d5_3 = Conv2D(256, (3, 3), activation='relu', padding='same')(d5_2)
# Fourth block going up, concatenated with Fourth block going down
up4_0 = UpSampling2D((2, 2))(d5_3)
up4_1 = tf.keras.layers.concatenate([d4_3, up4_0])
up4_2 = Conv2D(128, (3, 3), activation='relu', padding='same')(up4_1)
up4_3 = Conv2D(128, (3, 3), activation='relu', padding='same')(up4_2)
# Third block going up, concatenated with Third block going down
up3_0 = UpSampling2D((2, 2))(up4_3)
up3_1 = tf.keras.layers.concatenate([d3_3, up3_0])
up3_2 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3_1)
up3_3 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3_2)
# Second block going up, concatenated with Second block going down
up2_0 = UpSampling2D((2, 2))(up3_3)
up2_1 = tf.keras.layers.concatenate([d2_3, up2_0])
up2_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(up2_1)
up2_3 = Conv2D(32, (3, 3), activation='relu', padding='same')(up2_2)
# First block going up, concatenated with First block going down
up1_0 = UpSampling2D((2, 2))(up2_3)
up1_1 = tf.keras.layers.concatenate([d1_3, up1_0])
up1_2 = Conv2D(16, (3, 3), activation='relu', padding='same')(up1_1)
up1_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(up1_2)
# Output
out = Conv2D(1, (1, 1), activation='sigmoid', padding='same')(up1_3)
return out
some answer to a similar question suggested using as_numpy_iterator.next() so I'm passing one batch at a time instead of the whole dataset
from keras.layers import Input,Conv2D,MaxPooling2D,UpSampling2D, BatchNormalization
from keras.models import Model
unet = Model(unet(),input_shape = (s,s,3))
unet.compile(loss='mean_squared_error', optimizer = optimizers.rmsprop_v2.RMSprop())
unet_train = unet.fit(X_train.as_numpy_iterator.next(), y_train.as_numpy_iterator.next(), batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test, y_test))
still run into the same error
TypeError: Inputs to a layer should be tensors. Got: <keras.layers.convolutional.conv2d.Conv2D object
I think the issue at the beginning of 'unet' function:
# First block Going down
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same',input_shape=(s,s,3))
Please try instead:
# First block Going down
inputs = Input(shape=(s,s,3))
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs)
For more details please check:
https://keras.io/guides/functional_api/
"...To build this model using the functional API, start by creating an input node."

wrong input of image in tensorflow for training

I am writing my thesis in machine learning and am trying to build a unet to perform it. The code is as follows:
First i create the dataloader to create the datasets for input:
def dataloader(filepath, subset):
# Initiliaze return arrays - input of shape = HYPERPARAMETER
global size
if subset=="train":
size = 129
elif subset=="test":
size = 18
input_data=np.zeros((size,1024,1024,1))
output_data=np.zeros((size,1024,1024,1))
# Open file and create loop
with open(filepath+"annotation_"+subset+".txt", "r") as input_file:
# Count to pass through the file
count=0
for line in input_file:
line=line.split(" ")
data=cv2.imread(filepath+str(line[0])+".jpg",cv2.IMREAD_GRAYSCALE)
input_data[count,:,:,0]=data
# Case of benevolent
if line[3]=="B":
x=int(line[4])
y=1024-int(line[5])
radius=int(line[6])
for i in range(1024):
for j in range(1024):
if ((radius*radius-(i-x)*(i-x)-(j-y)*(j-y))>0):
# Setting 80 as th value of the benevolent mask
output_data[count,i,j,0]=80
# Case of malevolent
elif line[3]=="M":
x=int(line[4])
y=1024-int(line[5])
radius=int(line[6])
for i in range(1024):
for j in range(1024):
if ((radius*radius-(i-x)*(i-x)-(j-y)*(j-y))>0):
# Setting 160 as th value of the benevolent mask
output_data[count,i,j,0]=160
if count==0:
print(type(data))
print(type(input_data))
cv2.imshow('test',data)
cv2.waitKey(0)
cv2.imshow('image',input_data[count,:,:,0])
cv2.waitKey(0)
cv2.imshow('mask',output_data[count,:,:,0])
cv2.waitKey(0)
cv2.destroyAllWindows()
count=count+1
#input_data=K.zeros_like(input_data)
#output_data=K.zeros_like(output_data)
return input_data, output_data
and then the model and the commands to run it:
def unet_model(optimizer, loss_metric, metrics, sample_width, sample_height, lr=1e-3):
inputs = Input((sample_width, sample_height, 1))
print(inputs.shape)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
drop1 = Dropout(0.5)(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(drop1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
drop2 = Dropout(0.5)(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(drop2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
drop3 = Dropout(0.3)(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(drop3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
drop4 = Dropout(0.3)(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(drop4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='softmax')(conv9)
model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=optimizer(lr=lr), loss=loss_metric, metrics=metrics)
return model
#Filepath of datasets
filepath = "/home/tzikos/Downloads/train/"
# Load datasets
train_input, train_output = dataloader(filepath, "train")
test_input, test_output = dataloader(filepath, "test")
train_input = normalize(train_input)
test_input = normalize(test_input)
train_output = normalize(train_output)
test_output = normalize(test_output)
print(train_input.shape)
print(train_output.shape)
print(test_input.shape)
print(test_output.shape)
# Load model
model = unet_model(optimizer=Adam, loss_metric=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"], sample_width=train_input.shape[1], sample_height=train_input.shape[2],lr=1e-3)
model.compile(optimizer="Adam", loss=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"])
history = model.fit(x=train_input, y=train_output, batch_size=1, epochs=30)
# Save weights
model_filepath = '/home/tzikos/Desktop/thesis_DENSE-IN-UNET/unet_weights.h5'
model.save(model_filepath)
# Check results
results = model.evaluate(test_input, test_output)
print(results)
So the problem is the following:
When I train my model i get 0 accuracy and no change in the loss function. So I went and dived into the images.
When I imshow the data variable i get the photo as should be. However when I input it into the numpy array it is transcribed into a binary one where there is black and white and idk why.
So I think that is the problem but i cant see why that is since the data variable is allright
I am not sure what this network is supposed to do, so I will list some mistakes that in your code.
In your function you already compile the model:
model.compile(optimizer=optimizer(lr=lr), loss=loss_metric, metrics=metrics)
return model
After outside the function you do it again:
model.compile(optimizer="Adam", loss=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"])
One issue is about metric and loss. tf.keras.losses.MeanSquaredError() is a regression loss and 'accuracy' is a classification metric.
The most obvious one:
conv10 = Conv2D(1, (1, 1), activation='softmax')(conv9)
Softmax activation will be applied to your last axis. If you check your model.summary() your last axis consist of size 1 which means you have a single element. So you are just returning(outputting) a vector of ones everytime.

Input and output layers of Keras autoencoder don't match, can't run model

I am trying to work on building an autoencoder in Keras, with an input shape of (470,470,3) but the output never seems to match, even when I try to switch around padding. This is my code, can you please help? The way it is currently written my model summary shows an output of (472, 472, 3).
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras import Input, Model
input_image = Input(shape=(470, 470, 3))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_image)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded_image = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_image, decoded_image)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
Thank you!
Change your last padding to 'valid':
decoded_image = Conv2D(3, (3, 3), activation='sigmoid', padding='valid')(x)

expected conv2d_7 to have shape (4, 268, 1) but got array with shape (1, 270, 480)

I'm having trouble with this autoencoder I'm building using Keras. The input's shape is dependent on the screen size, and the output is going to be a prediction of the next screen size... However there seems to be an error that I cannot figure out... Please excuse my awful formatting on this website...
Code:
def model_build():
input_img = InputLayer(shape=(1, env_size()[1], env_size()[0]))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (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(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
model = Model(input_img, decoded)
return model
if __name__ == '__main__':
model = model_build()
model.compile('adam', 'mean_squared_error')
y = np.array([env()])
print(y.shape)
print(y.ndim)
debug = model.fit(np.array([[env()]]), np.array([[env()]]))
Error:
Traceback (most recent call last):
File "/home/ai/Desktop/algernon-test/rewarders.py", line 46, in
debug = model.fit(np.array([[env()]]), np.array([[env()]]))
File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
batch_size=batch_size)
File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
exception_prefix='target')
File "/home/ai/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking target: expected conv2d_7 to have shape (4, 268, 1) but got array with shape (1, 270, 480)
EDIT:
Code for get_screen imported as env():
def get_screen():
img = screen.grab()
img = img.resize(screen_size())
img = img.convert('L')
img = np.array(img)
return img
You have three 2x downsampling steps, and three x2 upsampling steps. These steps have no knowledge of the original image size, so they will round out the size to the nearest multiple of 8 = 2^3.
cropX = 7 - ((size[0]+7) % 8)
cropY = 7 - ((size[1]+7) % 8)
cropX = 7 - ((npix+7) % 8)
cropY = 7 - ((nlin+7) % 8)
It ought to work if you add a new final layer...
decoded = layers.Cropping2D(((0,cropY),(0,cropX)))(x)
Looks like env_size() and env() mess image dimensions somehow. Consider this example:
image1 = np.random.rand(1, 1, 270, 480) #First dimension is batch size for test purpose
image2 = np.random.rand(1, 4, 268, 1) #Or any other arbitrary dimensions
input_img = layers.Input(shape=image1[0].shape)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
model = tf.keras.Model(input_img, decoded)
model.compile('adam', 'mean_squared_error')
model.summary()
This line will work:
model.fit(image1, nb_epoch=1, batch_size=1)
But this doesn't
model.fit(image2, nb_epoch=1, batch_size=1)
Edit:
In order to get output of the same size as input you need to calculate convolution kernel size carefully.
image1 = np.random.rand(1, 1920, 1080, 1)
input_img = layers.Input(shape=image1[0].shape)
x = layers.Conv2D(32, 3, activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(16, 3, activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, 3, activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, 3, activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, 3, activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, 1, activation='relu')(x) # set kernel size to 1 for example
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, 3, activation='sigmoid', padding='same')(x)
model = tf.keras.Model(input_img, decoded)
model.compile('adam', 'mean_squared_error')
model.summary()
This will output same dimensions.
As per this guide http://cs231n.github.io/convolutional-networks/
We can compute the spatial size of the output volume as a function of
the input volume size (W), the receptive field size of the Conv Layer
neurons (F), the stride with which they are applied (S), and the
amount of zero padding used (P) on the border. You can convince
yourself that the correct formula for calculating how many neurons
“fit” is given by (W−F+2P)/S+1. For example for a 7x7 input and a 3x3
filter with stride 1 and pad 0 we would get a 5x5 output. With stride
2 we would get a 3x3 output.

How do I go from a dense layer to a conv2D layer using Keras?

I'm simply trying to do what the title says. Here's my code:
def ConvAutoEncoder(train_data,test_data,n_epochs = 50,batchSize = 128,data_shape=(IMAGE_SIZE,IMAGE_SIZE,3)):
print('Training Neural Network')
input_img = Input(shape=data_shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
print(encoded.shape)
# at this point the representation is (6, 6, 4 i.e. 128-dimensional
encoded = Flatten()(encoded)
encoded = Dense( 6*6*4,activation='relu')(encoded)
print(encoded.shape)
endoded = Reshape((6,6,4))(encoded)
print(encoded.shape)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(train_data, train_data,
epochs=n_epochs,
batch_size=batchSize,
shuffle=True,
verbose=2,
validation_data=(test_data, test_data),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
return autoencoder
However when I run it the Reshape layer doesn't do anything at all, the shape of the output before the reshape is (?,144) and the shape after is also (?,144). Am I using reshape wrong or is there some other way to connect a dense layer to a conv2D layer?

Categories

Resources