Denoising autoencoder

i have this code

import sys
import tensorflow as tf
import keras, keras.layers as L, keras.backend as K
import numpy as np
from sklearn.model_selection import train_test_split
from numpy import asarray
from tensorflow import keras
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras.layers import (Input, Conv2D, MaxPooling2D, UpSampling2D, LeakyReLU)
import cv2

import matplotlib.pyplot as plt





from keras.preprocessing import image



# load images

img = cv2.imread('image1.png')
img = cv2.resize(img, (500, 500))
print(img.shape)
plt.imshow(img)
plt.show()


#plt.imshow(img)
#img = np.asarray(img, dtype="float32")




# convert list to numpy array
X_train = img
Y_train = img
X_train = X_train.astype('float32') / 255

Y_train = Y_train.astype('float32') / 255
X_train = X_train.reshape(-1, 500, 500, 1)

Y_train = Y_train.reshape(-1, 500, 500, 1)





print(X_train.shape)




#X_train, Y_train= train_test_split(X_train, Y_train, test_size=0.15)
X_train_size = len(X_train) 
Y_train_size = len(Y_train) 
 
print(X_train_size) 
print(Y_train_size) 

#x_train, x_test = train_test_split(X)



#X_train[0].shape

#print(X_train, X_val)



noise_factor = 0.9
x_train_noisy = X_train + noise_factor * np.random.normal(loc=0.0, scale=1.0,
size=X_train.shape)
x_test_noisy = Y_train + noise_factor * np.random.normal(loc=0.0, scale=1.0,
size=Y_train.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
input_img = (500,500, 1)  

def DAE_CNN(features_shape, act='relu'):



    # Input

    x = Input(name='inputs', shape=features_shape, dtype='float32')

    o = x

    

    # Encoder

    o = Conv2D(32, (3, 3), activation=act, padding='same', strides=(1,1), name='en_conv1')(o)

    o = MaxPooling2D((2, 2), strides=(2,2), padding='same', name='en_pool1')(o)

    o = Conv2D(32, (3, 3), activation=act, padding='same', strides=(1,1), name='en_conv2')(o)

    enc = MaxPooling2D((2, 2), strides=(2,2), padding='same', name='en_pool2')(o)

    

    # Decoder

    o = Conv2D(32, (3, 3), activation=act, padding='same', strides=(1,1), name='de_conv1')(enc)

    o = UpSampling2D((2, 2), name='upsampling1')(o)

    o = Conv2D(32, (3, 3), activation=act, padding='same', strides=(1,1), name='de_conv2')(o)

    o = UpSampling2D((2, 2), name='upsampling2')(o)

    dec = Conv2D(1, (3, 3), activation='sigmoid', padding='same', strides=(1,1), name='de_conv3')(o)

    

    # Print network summary

    Model(inputs=x, outputs=dec).summary()

    

    return Model(inputs=x, outputs=dec)



batch_size = 128

epochs = 100




autoenc = DAE_CNN(input_img, act=LeakyReLU(alpha=0.1))

autoenc.compile(optimizer='adadelta', loss='binary_crossentropy')



autoenc.fit(x_train_noisy, X_train, epochs=epochs, batch_size=batch_size )



decoded_imgs = autoenc.predict(x_train_noisy)

plt.figure()

ax = plt.subplot()
plt.imshow(x_test_noisy[1].reshape(500,500))


plt.gray()
ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

plt.show()






ax = plt.subplot()
plt.imshow(decoded_imgs[1].reshape(500,500))
plt.gray()



ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

plt.show()

i try to enter a noisy image and the autoencoder give a denoising image but the image dont appear can someone help me

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.