0
I set up a dataset with my image data. I want to do convolutional neural network training but I ended up stopping at the exit layer of the network. The idea after training the network is to generate an image from this 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 Hape 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)
Grateful for the attention
To generate new images you should search about Generative Adversarial Networks (Gans) here are some examples
– Bernardo Lopes
I believed a CNN could generate it faster. But I understood its placement. Thank you.
– Rodrigo Ferraz