Skip to content

Commit 38de2d2

Browse files
authored
Add files via upload
1 parent 19cd4ec commit 38de2d2

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

085-auto_encode_single_image_V3.0.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
__author__ = "Sreenivas Bhattiprolu"
3+
__license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists"
4+
5+
# https://www.youtube.com/watch?v=D9HjlqIrB-c
6+
7+
8+
9+
10+
"""
11+
@author: Sreenivas Bhattiprolu
12+
Working great.
13+
Good example to demo image reconstruction using autoencoders
14+
15+
To launch tensorboard type this in the console: !tensorboard --logdir=logs/ --host localhost --port 8088
16+
then go to: http://localhost:8088/
17+
18+
The ! is because we are executing shell commands from Python console.Try different optimizers and loss
19+
20+
Try:
21+
Only 5 epochs, 50 epochs, 500 and 5000
22+
"""
23+
24+
from matplotlib.pyplot import imshow
25+
import numpy as np
26+
import cv2
27+
from keras.preprocessing.image import img_to_array
28+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D
29+
from tensorflow.keras.models import Sequential
30+
31+
32+
np.random.seed(42)
33+
34+
SIZE=256
35+
img_data=[]
36+
37+
img=cv2.imread('images/monalisa.jpg', 1) #Change 1 to 0 for grey images
38+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #Changing BGR to RGB to show images in true colors
39+
img=cv2.resize(img,(SIZE, SIZE))
40+
img_data.append(img_to_array(img))
41+
42+
img_array = np.reshape(img_data, (len(img_data), SIZE, SIZE, 3))
43+
img_array = img_array.astype('float32') / 255.
44+
45+
46+
model = Sequential()
47+
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(SIZE, SIZE, 3)))
48+
model.add(MaxPooling2D((2, 2), padding='same'))
49+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
50+
model.add(MaxPooling2D((2, 2), padding='same'))
51+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
52+
53+
54+
model.add(MaxPooling2D((2, 2), padding='same'))
55+
56+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
57+
model.add(UpSampling2D((2, 2)))
58+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
59+
model.add(UpSampling2D((2, 2)))
60+
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
61+
model.add(UpSampling2D((2, 2)))
62+
model.add(Conv2D(3, (3, 3), activation='relu', padding='same'))
63+
64+
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
65+
model.summary()
66+
67+
68+
69+
model.fit(img_array, img_array,
70+
epochs=5000,
71+
shuffle=True)
72+
73+
74+
print("Neural network output")
75+
pred = model.predict(img_array)
76+
77+
78+
79+
imshow(pred[0].reshape(SIZE,SIZE,3), cmap="gray")
80+

086--auto_denoise_mnist.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
__author__ = "Sreenivas Bhattiprolu"
3+
__license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists"
4+
5+
# https://www.youtube.com/watch?v=Sm54KXD-L1k
6+
7+
8+
9+
from tensorflow.keras.datasets import mnist
10+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D
11+
from tensorflow.keras.models import Sequential
12+
13+
import numpy as np
14+
import matplotlib.pyplot as plt
15+
16+
(x_train, _), (x_test, _) = mnist.load_data()
17+
18+
x_train = x_train.astype('float32') / 255.
19+
x_test = x_test.astype('float32') / 255.
20+
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
21+
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
22+
23+
#adding some noise
24+
noise_factor = 0.5
25+
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
26+
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
27+
28+
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
29+
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
30+
31+
#Displaying images with noise
32+
plt.figure(figsize=(20, 2))
33+
for i in range(1,10):
34+
ax = plt.subplot(1, 10, i)
35+
plt.imshow(x_test_noisy[i].reshape(28, 28), cmap="binary")
36+
plt.show()
37+
38+
39+
model = Sequential()
40+
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)))
41+
model.add(MaxPooling2D((2, 2), padding='same'))
42+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
43+
model.add(MaxPooling2D((2, 2), padding='same'))
44+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
45+
46+
47+
model.add(MaxPooling2D((2, 2), padding='same'))
48+
49+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
50+
model.add(UpSampling2D((2, 2)))
51+
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
52+
model.add(UpSampling2D((2, 2)))
53+
model.add(Conv2D(32, (3, 3), activation='relu'))
54+
model.add(UpSampling2D((2, 2)))
55+
model.add(Conv2D(1, (3, 3), activation='relu', padding='same'))
56+
57+
model.compile(optimizer='adam', loss='mean_squared_error')
58+
59+
model.summary()
60+
61+
model.fit(x_train_noisy, x_train, epochs=10, batch_size=256, shuffle=True,
62+
validation_data=(x_test_noisy, x_test))
63+
64+
65+
66+
model.evaluate(x_test_noisy, x_test)
67+
68+
model.save('denoising_autoencoder.model')
69+
70+
no_noise_img = model.predict(x_test_noisy)
71+
72+
plt.figure(figsize=(40, 4))
73+
for i in range(10):
74+
# display original
75+
ax = plt.subplot(3, 20, i + 1)
76+
plt.imshow(x_test_noisy[i].reshape(28, 28), cmap="binary")
77+
78+
# display reconstructed (after noise removed) image
79+
ax = plt.subplot(3, 20, 40 +i+ 1)
80+
plt.imshow(no_noise_img[i].reshape(28, 28), cmap="binary")
81+
82+
plt.show()
83+
84+
85+
86+
87+

0 commit comments

Comments
 (0)