-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoiseReductionAutoEncoder.py
More file actions
110 lines (91 loc) · 3.29 KB
/
NoiseReductionAutoEncoder.py
File metadata and controls
110 lines (91 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import numpy as np
import os
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,MaxPooling2D,UpSampling2D,InputLayer,Reshape
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
import matplotlib.pyplot as plt
curr_path=os.path.dirname(__file__)
#%% Data receive functions, receive datas from folders
def getData(pathd,shape):
#file i/o çalışılmalı
os.chdir(pathd)
Alldatas=[]
img_data=[]
img_data=os.listdir(".")
for image in img_data:
_,extension = os.path.splitext(image)
if(extension==".jpg" or extension==".jpeg" or extension==".png"):
img=load_img(image)
img=img.resize((shape[0],shape[1]))
x=img_to_array(img)
# x=x.reshape((1,) + x.shape)
Alldatas.append(x)
return Alldatas
scale=(540,258)
all_img=getData(curr_path+"/train",scale)
all_img_y=getData(curr_path+"/train_cleaned",scale)
#%%
all_img=tf.image.rgb_to_grayscale(all_img)
all_img_y=tf.image.rgb_to_grayscale(all_img_y)
#%%
def prepare(arr,flatten=True):
arr=np.asarray(arr,dtype="float32")
arr2=arr/255-0.5
if(flatten):
Count=arr2.shape[0]
arr2=arr2.flatten()
shap=int(arr2.shape[0]/Count)
arr2=arr2.reshape(Count,shap)
return arr2
# EDIT DATASET AND RESHAPE
train = prepare(all_img,flatten=False)
train_y = prepare(all_img_y,flatten=False)
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(train,train_y,test_size=0.1,random_state=42)
#%%
#CREATE AUTOENCODER
os.chdir(curr_path)
from keras.layers import Activation
from keras import optimizers
def cust(x):
return tf.keras.backend.sigmoid(x)-0.5
opt = optimizers.adamax(learning_rate=0.001)
model = Sequential()
model.add(Conv2D(128, (3, 3), padding='same',input_shape=(258,540,1),data_format="channels_last"))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(UpSampling2D((2, 2))) #SIGMOID TO EASILY GENERATE IMAGES IN WIDE RANGE
model.add(Conv2D(1, (3, 3), activation=cust, padding='same'))
model.compile(loss="mean_squared_error",optimizer=opt)
print(model.summary())
model.fit(x_train,
y_train,
epochs = 300,
batch_size = 7,
validation_data = (x_test,y_test),
verbose=1)
#%% Check difference between test images
for i in range(0,10):
check=x_test[i]
matrix=model.predict(check.reshape((1,)+check.shape)).reshape(258,540,1)
#Show real image and generated image from autoencoder
plt.figure(figsize=(50,50))
plt.subplot(10,10,1)
plt.imshow(array_to_img(check+0.5),cmap="gray")
plt.subplot(10,10,2)
plt.imshow(array_to_img(matrix+0.5),cmap="gray")
#%%
#Plot Loss
plt.figure(figsize=(10,10))
plt.plot(model.history.history['loss'])
plt.plot(model.history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
#%% Vısualizing Model
from keras.utils.vis_utils import plot_model
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)