去噪自动编码器的创建是为了消除噪音从嘈杂的手写数字。
接下来,我想把我准备好的有噪声的手写数字输入到我创建的模型中,并去除噪声。我已经写了代码来做这件事,但它不会产生任何错误,也不会显示去噪后的图像。
创建以下去噪自动编码器以从嘈杂的手写数字中去除噪声。
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import layers
from keras.callbacks import TensorBoard
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
input_img = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train_noisy, x_train,
epochs=25,
batch_size=128,
shuffle=True,
validation_data=(x_test_noisy, x_test),
callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])
autoencoder.save("model_number.h5")
字符串
接下来,我想将我自己准备的有噪声的手写数字“noise_number1.png”输入到创建的模型中以去除噪声。我为此编写了以下代码,但没有发生错误,也没有显示去噪图像。我该如何解决这个问题?具体的代码会有帮助。
from PIL import Image
from keras.models import load_model
img = Image.open('/content/noise_number1.png').convert('L')
img=img.resize((28,28))
img = np.array(img)
img=img.reshape(28,28,1)
autoencoder=load_model("model_number.h5")
pred = autoencoder.predict(img[np.newaxis])
型
1条答案
按热度按时间nue99wik1#
这是因为您没有打印要显示的去噪图像。
字符串
请使用下面的代码与其他部分代码沿着显示图像:
型
的数据
型
的