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

質問編集履歴

2

タグの追加

2019/03/15 10:54

投稿

yusukee345
yusukee345

スコア31

title CHANGED
File without changes
body CHANGED
File without changes

1

ソースコードの簡略化、実行環境追記

2019/03/15 10:54

投稿

yusukee345
yusukee345

スコア31

title CHANGED
@@ -1,1 +1,1 @@
1
- CNN-Autoencoderでlossがnanばかり出て、Accuracyも出
1
+ CNN-Autoencoderでlossがnanばかり出て、Accuracyも出ずに困ってます。
body CHANGED
@@ -1,27 +1,35 @@
1
1
  失礼します。
2
- CNN-Autoencoderを用いた画像分類器を作ろうとしていて、folder1内の28*28画像(460枚)を読み込んでCNN-Autoencoderで学習させて、更にfolder1内の画像を用いてテストした後にテスト画像の復元(decoded)を出力しようとしています。以下の様にlossやAccuracyの値が出ません。更には復元画像もほとんど真っ黒か真っ白な画像しか出ず、明らかに学習できていません。
2
+ CNN-Autoencoderを用いた画像分類器を作ろうとしていて、folder1内の28*28画像(460枚)を読み込んでCNN-Autoencoderで学習させて、更にfolder1内の画像を用いてテストした後にテスト画像の復元(decoded)を出力しようとしています。学習を進めていくと以下の様にlossやAccuracyの値が出ません。更には復元画像もほとんど真っ白な画像しか出ず、明らかに学習できていません。
3
3
  ```
4
- step, loss, accuracy = 1: -204.758 0.000
5
- step, loss, accuracy = 2: -734.519 0.000
6
- step, loss, accuracy = 3: nan 0.957
4
+ step, loss, accuracy = 1: nan 0.935
5
+ step, loss, accuracy = 2: nan 0.935
6
+ step, loss, accuracy = 3: nan 0.891
7
- step, loss, accuracy = 4: nan 0.848
7
+ step, loss, accuracy = 4: nan 0.891
8
8
  step, loss, accuracy = 5: nan 0.913
9
+ step, loss, accuracy = 6: nan 0.978
9
- step, loss, accuracy = 6: nan 0.913
10
+ step, loss, accuracy = 7: nan 0.913
11
+ step, loss, accuracy = 8: nan 0.935
10
- step, loss, accuracy = 7: nan 0.848
12
+ step, loss, accuracy = 9: nan 0.848
11
- step, loss, accuracy = 8: nan 0.848
12
- step, loss, accuracy = 9: nan 0.891
13
+ step, loss, accuracy = 10: nan 0.913
14
+ step, loss, accuracy = 11: nan 0.935
13
- step, loss, accuracy = 10: nan 0.870
15
+ step, loss, accuracy = 12: nan 0.870
16
+ step, loss, accuracy = 13: nan 0.848
17
+ step, loss, accuracy = 14: nan 0.913
18
+ step, loss, accuracy = 15: nan 0.935
19
+ step, loss, accuracy = 16: nan 0.935
20
+ step, loss, accuracy = 17: nan 0.913
21
+ step, loss, accuracy = 18: nan 0.913
22
+ step, loss, accuracy = 19: nan 0.913
23
+ step, loss, accuracy = 20: nan 0.826
24
+ loss (test) = nan
25
+ accuracy(test) = 0.9347826
14
26
  ```
15
27
 
16
- 以下のコードを使用しています。my_nn_lib.pyでconvolution層等を定義したうえで、cnn_ae.pyで実行するという構造になっています。
28
+ 以下のコードを使用しています。
17
- なお、コードはこの方 https://qiita.com/TomokIshii/items/26b7414bdb3cd3052786 のコードを使わせて頂きました。my_nn_lib.pyは同一のコドを拝借しておりま
29
+ コードはこの方 https://qiita.com/TomokIshii/items/26b7414bdb3cd3052786 のコードを使わせて頂きました。ネットワクの定義はよ高レベルに書き換えした(tf.nn.conv2dでなくtf.layers.conv2dを使用)
18
30
 
19
31
  `cnn_ae.py`
20
32
  ```
21
- from __future__ import absolute_import
22
- from __future__ import division
23
- from __future__ import print_function
24
-
25
33
  import numpy as np
26
34
  import matplotlib as mpl
27
35
  import random
@@ -40,91 +48,32 @@
40
48
  two_layer = 7
41
49
 
42
50
 
43
-
44
- # Up-sampling 2-D Layer (deconvolutoinal Layer)
45
- class Conv2Dtranspose(object):
46
- '''
47
- constructor's args:
48
- input : input image (2D matrix)
49
- output_siz : output image size
50
- in_ch : number of incoming image channel
51
- out_ch : number of outgoing image channel
52
- patch_siz : filter(patch) size
53
- '''
54
-
55
- def __init__(self, input, output_siz, in_ch, out_ch, patch_siz, activation='relu'):
56
- self.input = input
57
- self.rows = output_siz[0]
58
- self.cols = output_siz[1]
59
- self.out_ch = out_ch
60
- self.activation = activation
61
-
62
- wshape = [patch_siz[0], patch_siz[1], out_ch, in_ch] # note the arguments order
63
-
64
- w_cvt = tf.Variable(tf.truncated_normal(wshape, stddev=0.1),
65
- trainable=True)
66
- b_cvt = tf.Variable(tf.constant(0.1, shape=[out_ch]),
67
- trainable=True)
68
- self.batsiz = tf.shape(input)[0]
69
- self.w = w_cvt
70
- self.b = b_cvt
71
- self.params = [self.w, self.b]
72
-
73
- def output(self):
74
- shape4D = [self.batsiz, self.rows, self.cols, self.out_ch]
75
- linout = tf.nn.conv2d_transpose(self.input, self.w, output_shape=shape4D,
76
- strides=[1, 2, 2, 1], padding='SAME') + self.b
77
- if self.activation == 'relu':
78
- self.output = tf.nn.relu(linout)
79
- elif self.activation == 'sigmoid':
80
- self.output = tf.sigmoid(linout)
81
- else:
82
- self.output = linout
83
-
84
- return self.output
85
-
86
-
87
51
  # Create the model
88
52
  def mk_nn_model(x):
89
53
  # Encoding phase
90
54
  x_image = tf.reshape(x, [-1, image_size, image_size, 1])
91
- conv1 = Convolution2D(x_image, (image_size, image_size), 1, 16,
92
- (3, 3), activation='relu')
93
- conv1_out = conv1.output()
94
55
 
56
+ conv1 = tf.layers.conv2d(inputs=x_image, filters=16, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
95
- pool1 = MaxPooling2D(conv1_out)
57
+ pool1 = tf.layers.max_pooling2d(conv1, pool_size=(2, 2), strides=(2, 2), padding='same')
96
- pool1_out = pool1.output()
97
58
 
59
+ conv2 = tf.layers.conv2d(inputs=pool1, filters=8, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
98
- conv2 = Convolution2D(pool1_out, (one_layer, one_layer), 16, 8,
60
+ pool2 = tf.layers.max_pooling2d(conv2, pool_size=(2, 2), strides=(2, 2), padding='same')
99
- (3, 3), activation='relu')
100
- conv2_out = conv2.output()
101
61
 
102
- pool2 = MaxPooling2D(conv2_out)
62
+ conv3 = tf.layers.conv2d(pool2, filters=8, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
103
- pool2_out = pool2.output()
104
-
105
- conv3 = Convolution2D(pool2_out, (two_layer, two_layer), 8, 8, (3, 3), activation='relu')
63
+ encoded = tf.layers.max_pooling2d(conv3, pool_size=(2, 2), strides=(2, 2), padding='same')
106
- conv3_out = conv3.output()
107
-
108
- pool3 = MaxPooling2D(conv3_out)
109
- pool3_out = pool3.output()
110
64
  # at this point the representation is (8, 4, 4) i.e. 128-dimensional
111
65
  # Decoding phase
112
- conv_t1 = Conv2Dtranspose(pool3_out, (two_layer, two_layer), 8, 8,
66
+ upsample1 = tf.image.resize_images(encoded, size=(two_layer, two_layer), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
113
- (3, 3), activation='relu')
67
+ conv4 = tf.layers.conv2d(inputs=upsample1, filters=8, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
114
- conv_t1_out = conv_t1.output()
115
68
 
116
- conv_t2 = Conv2Dtranspose(conv_t1_out, (one_layer, one_layer), 8, 8,
69
+ upsample2 = tf.image.resize_images(conv4, size=(one_layer, one_layer), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
117
- (3, 3), activation='relu')
70
+ conv5 = tf.layers.conv2d(inputs=upsample2, filters=8, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
118
- conv_t2_out = conv_t2.output()
119
71
 
120
- conv_t3 = Conv2Dtranspose(conv_t2_out, (image_size, image_size), 8, 16,
72
+ upsample3 = tf.image.resize_images(conv5, size=(image_size, image_size), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
121
- (3, 3), activation='relu')
73
+ conv6 = tf.layers.conv2d(inputs=upsample3, filters=16, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)
122
- conv_t3_out = conv_t3.output()
123
74
 
124
- conv_last = Convolution2D(conv_t3_out, (image_size, image_size), 16, 1, (3, 3),
75
+ logits = tf.layers.conv2d(inputs=conv6, filters=1, kernel_size=(3, 3), padding='same', activation=None)
125
- activation='sigmoid')
126
- decoded = conv_last.output()
76
+ decoded = tf.nn.sigmoid(logits)
127
-
128
77
  decoded = tf.reshape(decoded, [-1, image_size*image_size])
129
78
 
130
79
  cross_entropy = -1. * x * tf.log(decoded) - (1. - x) * tf.log(1. - decoded)
@@ -154,7 +103,7 @@
154
103
  loss, decoded, accuracy = mk_nn_model(x)
155
104
  train_step = tf.train.AdagradOptimizer(learning_rate).minimize(loss)
156
105
 
157
- epochs = 10
106
+ epochs = 20
158
107
  batch_size = data_number // 10
159
108
 
160
109
  init = tf.initialize_all_variables()
@@ -172,9 +121,9 @@
172
121
  # generate decoded image with test data
173
122
  X = np.random.permutation(fileset)
174
123
  test_image = X[0:batch_size]
175
- decoded_imgs = decoded.eval(test_image)
124
+ decoded_imgs, test_loss, test_accuracy = sess.run([decoded, loss, accuracy], feed_dict={x: test_image})
176
- print('loss (test) = ', loss.eval(test_image))
125
+ print('loss (test) = ', test_loss)
177
- print('accuracy(test) = ', accuracy.eval(test_image))
126
+ print('accuracy(test) = ', test_accuracy)
178
127
 
179
128
  x_test = test_image
180
129
  n = 10 # how many digits we will display
@@ -198,6 +147,11 @@
198
147
  plt.savefig('images.png')
199
148
 
200
149
  ```
150
+ ####実行環境
151
+ OS Windows10 64bit
152
+ プロセッサ Intel(R) Core(TM)i7-8700k CPU @ 3.70GHz
153
+ RAM 32.0 GB
154
+ Anaconda Prompt
201
155
 
202
156
  ####試したこと
203
157
  ・decoded直前でsigmoidをsoftmaxにした。その結果、nanばかり表示されてしまい、全く改善せず。