-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.py
176 lines (145 loc) · 5.45 KB
/
main.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
DISCLAIMER:
This code has been written in 2017 in the optic
of my 'quick-n-dirty' Deep Learning series
on Medium (@juliendespois) to show the
concept. Please do not judge me by the
quality of the code.
¯\_(ツ)_/¯
"""
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras.callbacks import TensorBoard
from keras.models import model_from_json
import cv2
import sys
import os
from random import shuffle, randint, choice
# Generates random image with squares and circles
def get_random_image():
img_size = 100
size = 25
nb_shapes = 5
xy = lambda: randint(0,100)
# Create a white image
img = np.zeros((img_size,img_size,3), np.uint8)
cv2.rectangle(img,(0,0),(img_size,img_size),(122,122,122) ,-1)
grey_img = np.copy(img)
# Adds some shapes
for i in range(nb_shapes):
x0, y0 = xy(), xy()
is_rect = choice((True,False))
if is_rect:
cv2.rectangle(img,(x0,y0),(x0+size,y0+size),(255,0,0) ,-1)
cv2.rectangle(grey_img,(x0,y0),(x0+size,y0+size),(255,255,255) ,-1)
else:
cv2.circle(img,(x0,y0), size/2, (0,0,255), -1)
cv2.circle(grey_img,(x0,y0), size/2, (255,255,255), -1)
return cv2.resize(img,(48,48)), cv2.resize(grey_img,(48,48))
# Creates the dataset
def get_dataset(display=False):
# Show what the dataset looks like
if display:
colorImg, grey_img = get_random_image()
img = np.hstack((colorImg, grey_img))
cv2.imshow("Dataset",cv2.resize(img,(200,100)))
cv2.waitKey(0)
cv2.destroyAllWindows()
#for i in range
x_train, x_test, y_train, y_test = [], [], [], []
# Add training examples
for i in range(10000):
colorImg, grey_img = get_random_image()
grey_img = cv2.cvtColor(grey_img, cv2.COLOR_RGB2GRAY)
x_train.append(grey_img.astype('float32')/255.)
y_train.append(colorImg.astype('float32')/255.)
# Add test examples
for i in range(1000):
colorImg, grey_img = get_random_image()
grey_img = cv2.cvtColor(grey_img, cv2.COLOR_RGB2GRAY)
x_test.append(grey_img.astype('float32')/255.)
y_test.append(colorImg.astype('float32')/255.)
# Reshape
x_train = np.array(x_train).reshape((-1,48,48,1))
x_test = np.array(x_test).reshape((-1,48,48,1))
y_train = np.array(y_train).reshape((-1,48,48,3))
y_test = np.array(y_test).reshape((-1,48,48,3))
return x_train, y_train, x_test, y_test
# Creates the Convolutional Auto Encoder
def get_model():
input_img = Input(shape=(48, 48, 1))
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(input_img)
x = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(input_img)
x = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
encoded = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
#6x6x32 -- bottleneck
x = UpSampling2D((2, 2), dim_ordering='tf')(encoded)
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
x = UpSampling2D((2, 2), dim_ordering='tf')(x)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
decoded = Convolution2D(3, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
#Create model
autoencoder = Model(input_img, decoded)
return autoencoder
# Trains the model for 10 epochs
def train_model():
# Load dataset
print("Loading dataset...")
x_train_gray, x_train, x_test_gray, x_test = get_dataset()
# Create model description
print("Creating model...")
model = get_model()
model.compile(optimizer='rmsprop', loss='binary_crossentropy',metrics=['accuracy'])
# Train model
print("Training model...")
model.fit(x_train_gray, x_train, nb_epoch=10, batch_size=148, shuffle=True, validation_data=(x_test_gray, x_test), callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])
# Evaluate loaded model on test data
print("Evaluating model...")
score = model.evaluate(x_train_gray, x_train, verbose=0)
print "%s: %.2f%%" % (model.metrics_names[1], score[1]*100)
# Serialize model to JSON
print("Saving model...")
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# Serialize weights to HDF5
print("Saving weights...")
model.save_weights("model.h5")
# Tests the model and shows results
def test_model():
# Load JSON model description
with open('model.json', 'r') as json_file:
modelJSON = json_file.read()
# Build model from JSON description
print("Loading model...")
model = model_from_json(modelJSON)
# Load weights
print("Loading weights...")
model.load_weights("model.h5")
_, _, x_test_gray, x_test = get_dataset()
x_test_gray = x_test_gray[:10]
x_test = x_test[:10]
print("Making predictions...")
predictions = model.predict(x_test_gray)
x_test_gray = [cv2.cvtColor(img,cv2.COLOR_GRAY2RGB) for img in x_test_gray]
img = np.vstack((np.hstack(x_test_gray), np.hstack(predictions), np.hstack(x_test)))
cv2.imshow("Input - Reconstructed - Ground truth",cv2.resize(img, (img.shape[1], img.shape[0])))
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
arg = sys.argv[1] if len(sys.argv) == 2 else None
if arg is None:
print "Need argument"
elif arg == "train":
train_model()
elif arg == "test":
test_model()
elif arg == "dataset":
get_dataset(True)
else:
print "Wrong argument"