Exit of a recurrent Neural network with Tensorflow and Keras

I set up a dataset with my image data. I want to do the training of a convolutional neural network but ended up stopping at the exit layer of the network. The idea after training the network is to generate an image from that trained network (regression problem). However, at the time of training the network I found the following error msg:

ValueError: a target array with shape (1501, 32, 32, 1) was passed for an output of shape (None, 1) while using as loss mean_squared_error. This loss expects targets to have the same shape as the output.

How can I fix this?

Below follows My Code:

import os
import PIL
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import cv2
import glob
import tensorflow as tf
import matplotlib.pyplot as plt

x = []
files = glob.glob ("E:/NN_SRTM/Teste_Split_imagem/srtm_treino/*.tif")

y = []
files1 = glob.glob ("E:/NN_SRTM/Teste_Split_imagem/gnsstreino/*.tif")

IMG_SIZE = 32

for myFile in files:
    print(myFile)
    image = cv2.imread (myFile, cv2.IMREAD_GRAYSCALE)
    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
    x.append (image)

for myFile1 in files1:
    print(myFile1)
    image1 = cv2.imread (myFile1, cv2.IMREAD_GRAYSCALE)
    image1 = cv2.resize(image1, (IMG_SIZE, IMG_SIZE))
    y.append (image1)

x = np.array(x)
x = x.astype('float32')

y = np.array(y)
y = y.astype('float32')

x = x.reshape(x.shape[0],32,32,1)
y = y.reshape(y.shape[0],32,32,1)

X_treinamento, X_teste, y_treinamento, y_teste = train_test_split(x,y, 
                                                                  test_size=0.30,random_state=0)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", 
                                 activation="relu", input_shape=[32, 32, 1]))

model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, 
                                 padding="same", activation="relu"))

model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))

model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))

model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))

model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(units=128, activation='relu'))

model.add(tf.keras.layers.Dense(units=1, activation='relu'))

model.compile(loss="mean_squared_error", optimizer="Adam", 
              metrics=["accuracy"])

model.summary()

model.fit(X_treinamento, y_treinamento, epochs=5)

insert the description of the image here

Grateful for your attention

Author: Rodrigo Ferraz, 2020-05-24

1 answers

The problem of generating artificial images is not a regression problem. It is known in the literature as generative models, among them we have the Generative Adversarial Networks (GANs) which is a class of generative models, among this class were developed several works with great results in the generation of artificial images, follows a demonstration.

 1
Author: Emanuel Huber, 2020-08-24 17:00:27