Q
Q
qqqZXzz1232021-10-23 14:47:56
Python
qqqZXzz123, 2021-10-23 14:47:56

How to train a neural network to display different images?

i wrote the code

import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from tensorflow import keras
from tensorflow.keras import layers
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

im = Image.open("im.png")
im = im.convert('RGB')
im = im.resize((128, 128))
im = np.array(im) / 255.0
im = im.flatten()
print(len(im))

im2 = Image.open("im2.png")
im2 = im2.convert('RGB')
im2 = im2.resize((128, 128))
im2 = np.array(im2) / 255.0
im2 = im2.flatten()
print(len(im2))

c = np.array()
f = np.array()

model = tf.keras.Sequential()
model.add(keras.Input(shape=(2,)))
model.add(layers.Dense(2, activation='sigmoid'))
model.add(layers.Dense(49152, use_bias=False, activation=None))

model.compile(loss='mean_squared_error', optimizer=keras.optimizers.Adam(0.2))
model.summary()

history = model.fit(c, f, epochs=500)
print("Обучение завершено")
model.save("my_model")

im3 = model.predict()
im3 = np.reshape(im3, (128, 128, 3 ))
plt.imshow(im3, interpolation='nearest')
plt.show()

im3 = model.predict()
im3 = np.reshape(im3, (128, 128, 3 ))
plt.imshow(im3, interpolation='nearest')
plt.show()

plt.plot(history.history['loss'])
plt.grid(True)
plt.show()


after training, the pictures just overlap each other.
how can i make it so that the neural network can produce different images?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-10-23
@dmshar

And what about the neural network? Matplotlib is responsible for the "picture output", and what you write there is up to you.
In order to create several pictures, the plt.subplots(.....) method is used, for example, we create three pictures: Now you have an ax object, and you are free to write to the "picture" you need: First: Third : In the second: In random order and arbitrary types of graphs / pictures. The principle, I hope, is clear. But in general, I recommend that before "training neural networks" to familiarize yourself with at least the basics of Python and the main libraries.
fig, ax = plt.subplots(1, 3)
ax[0].imshow(....)
ax[2].imshow(...)
ax[1].plot()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question