teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コードの修正

2019/05/12 13:13

投稿

SuzuAya
SuzuAya

スコア71

title CHANGED
File without changes
body CHANGED
@@ -128,12 +128,12 @@
128
128
  #original dataset
129
129
  #train
130
130
  filenames = glob.glob("./train/NORMAL_train_dataset/*.jpeg")
131
- x_train = []
131
+ X = []
132
132
 
133
133
  for filename in filenames:
134
134
  img = img_to_array(load_img(
135
135
  filename, color_mode = "grayscale", target_size=(512,496))
136
- x_train.append(img)
136
+ X.append(img)
137
137
 
138
138
  x_train = np.asarray(X)
139
139
 

2

全コード掲載

2019/05/12 13:13

投稿

SuzuAya
SuzuAya

スコア71

title CHANGED
File without changes
body CHANGED
@@ -48,21 +48,239 @@
48
48
  import cv2
49
49
  import easydict
50
50
  from PIL import Image
51
+ #from google.colab.patches import cv2_imshow
51
52
 
53
+ import warnings
54
+ warnings.filterwarnings('ignore')
55
+
56
+ %matplotlib inline
57
+
58
+ def sampling(args):
59
+ z_mean, z_log_var = args
60
+ batch = K.shape(z_mean)[0]
61
+ dim = K.int_shape(z_mean)[1]
62
+ # by default, random_normal has mean=0 and std=1.0
63
+ epsilon = K.random_normal(shape=(batch, dim))
64
+ return z_mean + K.exp(0.5 * z_log_var) * epsilon
65
+
66
+ #def plot_results(models,
67
+ #data,
68
+ #batch_size=500,#128,
69
+ #model_name="vae_OCT"):
70
+ """Plots labels and MNIST digits as function of 2-dim latent vector
71
+
72
+ # Arguments
73
+ models (tuple): encoder and decoder models
74
+ data (tuple): test data and label
75
+ batch_size (int): prediction batch size
76
+ encoder, decoder = models
77
+ x_test = data #, y_test削除
78
+ os.makedirs(model_name, exist_ok=True)
79
+ model_name (string): which model is using this function
80
+ """
81
+
82
+ filename = os.path.join('.', "vae_mean.png")
83
+ # display a 2D plot of the digit classes in the latent space
84
+ z_mean, _, _ = encoder.predict(x_test,
85
+ batch_size=batch_size)
86
+ #plt.figure(figsize=(12, 10))
87
+ #plt.scatter(z_mean[:, 0], z_mean[:, 1])#c=y_test削除
88
+ #plt.colorbar()
89
+ #plt.xlabel("z[0]")
90
+ #plt.ylabel("z[1]")
91
+ #plt.savefig(filename)
92
+ #plt.show()
93
+
94
+ #filename = os.path.join(model_name, "digits_over_latent.png")
95
+ # display a 30x30 2D manifold of digits
96
+ #n = 30
97
+ #digit_size = 496 うまく動かないので一旦プロット部分はすべてコメントアウト
98
+ #digit_size_width = 512 #width追加
99
+ #digit_size_height = 496 #height追加
100
+ #figure = np.zeros((digit_size_width * n, digit_size_height * n))#width, height追加
101
+ # linearly spaced coordinates corresponding to the 2D plot
102
+ # of digit classes in the latent space
103
+ #grid_x = np.linspace(-4, 4, n)
104
+ #grid_y = np.linspace(-4, 4, n)[::-1]
105
+
106
+ #for i, yi in enumerate(grid_y):
107
+ #for j, xi in enumerate(grid_x):
108
+ #z_sample = np.array([[xi, yi]])
109
+ #x_decoded = decoder.predict(z_sample)
110
+ #digit = x_decoded[0].reshape(digit_size_width, digit_size_height)#width, height追加
111
+ #figure[i * digit_size_width: (i + 1) * digit_size_height,#width, height追加
112
+ #j * digit_size_width: (j + 1) * digit_size_height] = digit#width, height追加
113
+
114
+ #plt.figure(figsize=(10, 10))
115
+ #start_range = digit_size // 2
116
+ #end_range = n * digit_size + start_range + 1
117
+ #pixel_range = np.arange(start_range, end_range, digit_size)
118
+ #sample_range_x = np.round(grid_x, 1)
119
+ #sample_range_y = np.round(grid_y, 1)
120
+ #plt.xticks(pixel_range, sample_range_x)
121
+ #plt.yticks(pixel_range, sample_range_y)
122
+ #plt.xlabel("z[0]")
123
+ #plt.ylabel("z[1]")
124
+ #plt.imshow(figure, cmap='Greys_r')
125
+ #plt.savefig(filename)
126
+ #plt.show()
127
+
52
128
  #original dataset
53
129
  #train
54
130
  filenames = glob.glob("./train/NORMAL_train_dataset/*.jpeg")
131
+ x_train = []
55
132
 
133
+ for filename in filenames:
56
- X = []
134
+ img = img_to_array(load_img(
135
+ filename, color_mode = "grayscale", target_size=(512,496))
136
+ x_train.append(img)
57
137
 
138
+ x_train = np.asarray(X)
139
+
140
+ #test
141
+ filenames = glob.glob("./validation/*.jpeg")
142
+ x_test = []
143
+
58
144
  for filename in filenames:
59
145
  img = img_to_array(load_img(
60
- filename, color_mode = "grayscale"
146
+ filename, color_mode = "grayscale", target_size=(512,496))
61
- , target_size=(512,496)))
62
- X.append(img)
147
+ x_test.append(img)
148
+
149
+ x_test = np.asarray(x_test)
150
+
151
+ image_size = x_train.shape[1]
152
+ original_dim = 512 * 496 *1 #3削除
153
+ x_train = np.reshape(x_train, [-1, original_dim,1])# x_train = np.reshape(x_train, [-1, original_dim])
154
+ x_test = np.reshape(x_test, [-1, original_dim,1])# x_test = np.reshape(x_test, [-1, original_dim])
155
+ x_train = x_train.astype('float32') / 255
156
+ x_test = x_test.astype('float32') / 255
157
+
158
+ #train_generator = train_datagen.flow(x_train)#追記。generator作成
159
+ #test_generator = test_datagen.flow(x_test)#追記。generator作成
160
+
161
+ print(x_train.shape)
162
+ print(x_test.shape)
163
+
164
+
165
+ # network parameters
166
+ input_shape = (512, 496, 1)# (original_dim,)
167
+ kernel_size = 3
168
+ filters = 16
169
+ #intermediate_dim = 512
170
+ batch_size = 500#128
171
+ latent_dim = 2# Dimensionality of the latent space: a plane 潜在空間の次元数:平面 https://fisproject.jp/2018/09/vae-with-python-keras/#vae-with-keras
172
+ epochs = 5#1#50
173
+
174
+
175
+ # build encoder model
176
+ inputs = Input(shape=input_shape, name='encoder_input')
177
+ x = inputs
178
+ for i in range(4):
179
+ filters *= 2
180
+ x = Conv2D(filters=filters,kernel_size=kernel_size,activation='relu',strides=2,padding='same')(x)
181
+
182
+ # shape info needed to build decoder model これは画像のshapeとって割る時とかに結構使う. ちなみにtensorflowでのみ動作します. https://www.mathgram.xyz/entry/keras/backend
183
+ shape = K.int_shape(x)
184
+
185
+ # use reparameterization trick to push the sampling out as input
186
+ # note that "output_shape" isn't necessary with the TensorFlow backend
187
+ z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])
188
+
189
+ # instantiate encoder model
190
+ encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
191
+ encoder.summary()
192
+ plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True)
193
+
194
+ # build decoder model
195
+ latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
196
+ x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs)
197
+ x = Reshape((shape[1], shape[2], shape[3]))(x)
198
+
199
+ for i in range(4):
200
+ x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, activation='relu', strides=2, padding='same')(x)
201
+ filters //= 2
202
+
203
+ outputs = Conv2DTranspose(filters=1, kernel_size=kernel_size, activation='sigmoid', padding='same', name='decoder_output')(x)
204
+
205
+ # instantiate decoder model
206
+ decoder = Model(latent_inputs, outputs, name='decoder')
207
+ decoder.summary()
208
+ plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True)
209
+
210
+ # instantiate VAE model
211
+ outputs = decoder(encoder(inputs)[2])
212
+ vae = Model(inputs, outputs, name='vae_mlp')
213
+
214
+
215
+
216
+ if __name__ == '__main__':
217
+ args = easydict.EasyDict({
218
+ "batchsize": 500,#40,
219
+ "epoch": 1,#50,
220
+ #"gpu": 0,
221
+ "out": "result",
222
+ "resume": False,
223
+ #"unit": 1000
224
+ })
225
+ #parser = argparse.ArgumentParser() parserがうまくうごかないため削除
226
+ #help_ = "Load h5 model trained weights"
227
+ #parser.add_argument("-w", "--weights", help=help_)
228
+ #help_ = "Use mse loss instead of binary cross entropy (default)"
229
+ #parser.add_argument("-m",
230
+ #"--mse",
231
+ #help=help_, action='store_true')
232
+ #args = parser.parse_args()
233
+ models = (encoder, decoder)
234
+ data = (x_test)#, y_test削除
235
+
236
+ # VAE loss = mse_loss or xent_loss + kl_loss
237
+ #if args.mse:
238
+ #reconstruction_loss = mse(inputs, outputs)
239
+ #else:
240
+ reconstruction_loss = binary_crossentropy(inputs,
241
+ outputs)
242
+
243
+ reconstruction_loss *= original_dim
244
+ kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
245
+ kl_loss = K.sum(kl_loss, axis=-1)
246
+ kl_loss *= -0.5
247
+ vae_loss = K.mean(reconstruction_loss + kl_loss)
248
+ vae.add_loss(vae_loss)
249
+ vae.compile(optimizer='adam')
250
+ vae.summary()
251
+ plot_model(vae,
252
+ to_file='vae_mlp.png',
253
+ show_shapes=True)
254
+
255
+
256
+
257
+ callbacks = []
258
+ callbacks.append(ModelCheckpoint(filepath="model.ep{epoch:02d}.h5"))# 各epochでのモデルの保存
259
+ callbacks.append(EarlyStopping(monitor='val_loss', patience=0, verbose=1))
260
+ callbacks.append(LearningRateScheduler(lambda ep: float(1e-3 / 3 ** (ep * 4 // MAX_EPOCH))))
261
+ callbacks.append(CSVLogger("history.csv"))
262
+
63
263
 
264
+ #if args.weights:
265
+ #vae.load_weights(args.weights)
266
+ #else:
267
+ # train the autoencoder
268
+ history = vae.fit(x_train,
269
+ epochs=epochs,
270
+ batch_size=batch_size,
271
+ validation_data=(x_test, None),
272
+ callbacks=callbacks)
273
+
274
+ score = model.evaluate(x_test, verbose=0)#y_test削除
275
+ print('Test loss:', score[0])
276
+ print('Test accuracy:', score[1])
64
277
 
278
+ plt.plot(history.history["acc"], label="acc", ls="-", marker="o")
279
+ plt.plot(history.history["val_acc"], label="val_acc", ls="-", marker="x")
65
- x_train = np.asarray(X)
280
+ plt.ylabel("accuracy")
281
+ plt.xlabel("epoch")
282
+ plt.legend(loc="best")
283
+ plt.show()
66
284
  ```
67
285
 
68
286
 

1

コードの修正

2019/05/12 13:11

投稿

SuzuAya
SuzuAya

スコア71

title CHANGED
File without changes
body CHANGED
@@ -53,16 +53,16 @@
53
53
  #train
54
54
  filenames = glob.glob("./train/NORMAL_train_dataset/*.jpeg")
55
55
 
56
- x_train = []
56
+ X = []
57
57
 
58
58
  for filename in filenames:
59
59
  img = img_to_array(load_img(
60
60
  filename, color_mode = "grayscale"
61
61
  , target_size=(512,496)))
62
- x_train.append(img)
62
+ X.append(img)
63
63
 
64
64
 
65
- x_train = np.asarray(x_train)
65
+ x_train = np.asarray(X)
66
66
  ```
67
67
 
68
68