1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ from tensorflow .contrib .keras .python .keras .callbacks import TensorBoard
4
+ from tensorflow .contrib .keras .python .keras .datasets import mnist
5
+ from tensorflow .contrib .keras .python .keras .engine import Input , Model
6
+ from tensorflow .contrib .keras .python .keras .layers import Dense , Conv2D , MaxPooling2D , UpSampling2D
7
+ from tensorflow .contrib .keras .python .keras import backend as K
8
+
9
+
10
+ (x_train , _ ), (x_test , _ ) = mnist .load_data ()
11
+
12
+ if K .image_data_format () == 'channels_last' :
13
+ shape_ord = (28 , 28 , 1 )
14
+ else :
15
+ shape_ord = (1 , 28 , 28 )
16
+
17
+ (x_train , _ ), (x_test , _ ) = mnist .load_data ()
18
+
19
+ x_train = x_train .astype ('float32' ) / 255.
20
+ x_test = x_test .astype ('float32' ) / 255.
21
+
22
+ x_train = np .reshape (x_train , ((x_train .shape [0 ],) + shape_ord ))
23
+ x_test = np .reshape (x_test , ((x_test .shape [0 ],) + shape_ord ))
24
+
25
+
26
+
27
+
28
+ input_img = Input (shape = (28 , 28 , 1 ))
29
+
30
+ x = Conv2D (16 , (3 , 3 ), activation = 'relu' , padding = 'same' )(input_img )
31
+ x = MaxPooling2D ((2 , 2 ), padding = 'same' )(x )
32
+ x = Conv2D (8 , (3 , 3 ), activation = 'relu' , padding = 'same' )(x )
33
+ x = MaxPooling2D ((2 , 2 ), padding = 'same' )(x )
34
+ x = Conv2D (8 , (3 , 3 ), activation = 'relu' , padding = 'same' )(x )
35
+ encoded = MaxPooling2D ((2 , 2 ), padding = 'same' )(x )
36
+
37
+ # at this point the representation is (4, 4, 8) i.e. 128-dimensional
38
+
39
+ x = Conv2D (8 , (3 , 3 ), activation = 'relu' , padding = 'same' )(encoded )
40
+ x = UpSampling2D ((2 , 2 ))(x )
41
+ x = Conv2D (8 , (3 , 3 ), activation = 'relu' , padding = 'same' )(x )
42
+ x = UpSampling2D ((2 , 2 ))(x )
43
+ x = Conv2D (16 , (3 , 3 ), activation = 'relu' )(x )
44
+ x = UpSampling2D ((2 , 2 ))(x )
45
+ decoded = Conv2D (1 , (3 , 3 ), activation = 'sigmoid' , padding = 'same' )(x )
46
+
47
+ conv_autoencoder = Model (input_img , decoded )
48
+ conv_autoencoder .compile (optimizer = 'adadelta' , loss = 'binary_crossentropy' )
49
+
50
+
51
+ batch_size = 128
52
+ steps_per_epoch = np .int (np .floor (x_train .shape [0 ] / batch_size ))
53
+ conv_autoencoder .fit (x_train , x_train , epochs = 50 , batch_size = 128 ,
54
+ shuffle = True , validation_data = (x_test , x_test ),
55
+ callbacks = [TensorBoard (log_dir = './tf_autoencoder_logs' )])
56
+
57
+
58
+ decoded_imgs = conv_autoencoder .predict (x_test )
59
+
60
+ n = 10
61
+ plt .figure (figsize = (20 , 4 ))
62
+ for i in range (n ):
63
+ # display original
64
+ ax = plt .subplot (2 , n , i + 1 )
65
+ plt .imshow (x_test [i ].reshape (28 , 28 ))
66
+ plt .gray ()
67
+ ax .get_xaxis ().set_visible (False )
68
+ ax .get_yaxis ().set_visible (False )
69
+
70
+ # display reconstruction
71
+ ax = plt .subplot (2 , n , i + n + 1 )
72
+ plt .imshow (decoded_imgs [i ].reshape (28 , 28 ))
73
+ plt .gray ()
74
+ ax .get_xaxis ().set_visible (False )
75
+ ax .get_yaxis ().set_visible (False )
76
+ plt .show ()
0 commit comments