質問編集履歴
3
誤字の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -43,7 +43,6 @@
|
|
43
43
|
z_mean, z_log_var = args
|
44
44
|
batch = K.shape(z_mean)[0]
|
45
45
|
dim = K.int_shape(z_mean)[1]
|
46
|
-
# by default, random_normal has mean=0 and std=1.0
|
47
46
|
epsilon = K.random_normal(shape=(batch, dim))
|
48
47
|
return z_mean + K.exp(0.5 * z_log_var) * epsilon
|
49
48
|
|
2
誤字の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -55,7 +55,6 @@
|
|
55
55
|
|
56
56
|
|
57
57
|
encoder, decoder = models
|
58
|
-
#x_test, y_test = data
|
59
58
|
os.makedirs(model_name, exist_ok=True)
|
60
59
|
|
61
60
|
filename = os.path.join(model_name, "vae_mean.png")
|
1
エラー内容の変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Python3
|
1
|
+
Python3 VInvalidArgumentError (see above for traceback): Incompatible shapesのエラー解消方法について
|
body
CHANGED
@@ -1,16 +1,248 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
KerasのVAEを自作データセットで実装中、以下のようなエラーが出ました。該当の部分を見ても、どう修正したら良いか分からず、
|
3
|
+
KerasのVAEを自作データセットで実装中、以下のようなエラーが出ました。該当の部分を見ても、どう修正したら良いか分からず、質問させて頂きました。
|
4
|
-
https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py
|
5
4
|
|
5
|
+
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
7
|
|
8
|
-
|
8
|
+
InvalidArgumentError Traceback (most recent call last)
|
9
9
|
|
10
|
+
InvalidArgumentError: Incompatible shapes: [12800] vs. [450]
|
11
|
+
|
12
|
+
|
10
13
|
### 該当のソースコード
|
14
|
+
from __future__ import absolute_import
|
15
|
+
from __future__ import division
|
16
|
+
from __future__ import print_function
|
17
|
+
|
18
|
+
from keras.layers import Dense, Input
|
19
|
+
from keras.layers import Conv2D, Flatten, Lambda
|
20
|
+
from keras.layers import Reshape, Conv2DTranspose
|
21
|
+
from keras.models import Model
|
22
|
+
from keras.losses import mse, binary_crossentropy
|
23
|
+
from keras.utils import plot_model
|
24
|
+
from keras import backend as K
|
25
|
+
from keras import optimizers
|
26
|
+
|
27
|
+
|
28
|
+
import numpy as np
|
29
|
+
import matplotlib.pyplot as plt
|
30
|
+
import argparse
|
31
|
+
import tensorflow as tf
|
32
|
+
import random as rn
|
33
|
+
import os
|
34
|
+
import easydict
|
35
|
+
|
36
|
+
import warnings
|
37
|
+
warnings.filterwarnings('ignore')
|
38
|
+
|
39
|
+
%matplotlib inline
|
40
|
+
|
41
|
+
def sampling(args):
|
42
|
+
|
43
|
+
z_mean, z_log_var = args
|
44
|
+
batch = K.shape(z_mean)[0]
|
45
|
+
dim = K.int_shape(z_mean)[1]
|
46
|
+
# by default, random_normal has mean=0 and std=1.0
|
47
|
+
epsilon = K.random_normal(shape=(batch, dim))
|
48
|
+
return z_mean + K.exp(0.5 * z_log_var) * epsilon
|
49
|
+
|
50
|
+
|
51
|
+
def plot_results(models,
|
52
|
+
data,
|
53
|
+
batch_size=50,
|
54
|
+
model_name="vae_mnist"):
|
55
|
+
|
56
|
+
|
11
|
-
|
57
|
+
encoder, decoder = models
|
58
|
+
#x_test, y_test = data
|
59
|
+
os.makedirs(model_name, exist_ok=True)
|
60
|
+
|
61
|
+
filename = os.path.join(model_name, "vae_mean.png")
|
62
|
+
|
63
|
+
z_mean, _, _ = encoder.predict(x_test,
|
64
|
+
batch_size=batch_size)
|
65
|
+
plt.figure(figsize=(12, 10))
|
66
|
+
plt.scatter(z_mean[:, 0], z_mean[:, 1])
|
67
|
+
plt.colorbar()
|
68
|
+
plt.xlabel("z[0]")
|
69
|
+
plt.ylabel("z[1]")
|
70
|
+
plt.savefig(filename)
|
71
|
+
plt.show()
|
72
|
+
|
73
|
+
filename = os.path.join(model_name, "digits_over_latent.png")
|
74
|
+
|
75
|
+
n = 30
|
76
|
+
digit_size = 224
|
77
|
+
figure = np.zeros((digit_size * n, digit_size * n))
|
78
|
+
|
79
|
+
grid_x = np.linspace(-4, 4, n)
|
80
|
+
grid_y = np.linspace(-4, 4, n)[::-1]
|
81
|
+
|
82
|
+
for i, yi in enumerate(grid_y):
|
83
|
+
for j, xi in enumerate(grid_x):
|
84
|
+
z_sample = np.array([[xi, yi]])
|
85
|
+
x_decoded = decoder.predict(z_sample)
|
86
|
+
digit = x_decoded[0].reshape(digit_size, digit_size)
|
87
|
+
figure[i * digit_size: (i + 1) * digit_size,
|
88
|
+
j * digit_size: (j + 1) * digit_size] = digit
|
89
|
+
|
90
|
+
plt.figure(figsize=(10, 10))
|
91
|
+
start_range = digit_size // 2
|
92
|
+
end_range = n * digit_size + start_range + 1
|
93
|
+
pixel_range = np.arange(start_range, end_range, digit_size)
|
94
|
+
sample_range_x = np.round(grid_x, 1)
|
95
|
+
sample_range_y = np.round(grid_y, 1)
|
96
|
+
plt.xticks(pixel_range, sample_range_x)
|
97
|
+
plt.yticks(pixel_range, sample_range_y)
|
98
|
+
plt.xlabel("z[0]")
|
99
|
+
plt.ylabel("z[1]")
|
100
|
+
plt.imshow(figure, cmap='Greys_r')
|
101
|
+
plt.savefig(filename)
|
102
|
+
plt.show()
|
103
|
+
|
104
|
+
|
105
|
+
x_train_tom = np.load('./folder_a.npy')
|
106
|
+
x_test_tom = np.load('./folder_b.npy')
|
107
|
+
|
108
|
+
image_size = x_train_tom.shape[1]
|
109
|
+
x_train = np.reshape(x_train_tom, [-1, image_size, image_size, 1])
|
110
|
+
x_test = np.reshape(x_test_tom, [-1, image_size, image_size, 1])
|
111
|
+
x_train = x_train.astype('float32') / 255
|
112
|
+
x_test = x_test.astype('float32') / 255
|
113
|
+
print(x_train.shape,x_test.shape)
|
114
|
+
|
115
|
+
input_shape = (image_size,image_size, 1)
|
116
|
+
batch_size = 50
|
117
|
+
kernel_size = 3
|
118
|
+
filters = 16
|
119
|
+
latent_dim = 2
|
120
|
+
epochs = 50
|
121
|
+
|
12
122
|
inputs = Input(shape=input_shape, name='encoder_input')
|
13
123
|
x = inputs
|
14
124
|
for i in range(4):
|
15
125
|
filters *= 2
|
126
|
+
x = Conv2D(filters=filters,
|
127
|
+
kernel_size=kernel_size,
|
128
|
+
activation='relu',
|
129
|
+
strides=2,
|
130
|
+
padding='same')(x)
|
131
|
+
|
132
|
+
shape = K.int_shape(x)
|
133
|
+
|
134
|
+
x = Flatten()(x)
|
135
|
+
x = Dense(64, activation='relu')(x)
|
136
|
+
z_mean = Dense(latent_dim, name='z_mean')(x)
|
137
|
+
z_log_var = Dense(latent_dim, name='z_log_var')(x)
|
138
|
+
|
139
|
+
z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])
|
140
|
+
|
141
|
+
encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
|
142
|
+
encoder.summary()
|
143
|
+
plot_model(encoder, to_file='vae_cnn_encoder.png', show_shapes=True)
|
144
|
+
|
145
|
+
latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
|
16
|
-
|
146
|
+
x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs)
|
147
|
+
x = Reshape((shape[1], shape[2], shape[3]))(x)
|
148
|
+
|
149
|
+
for i in range(4):
|
150
|
+
x = Conv2DTranspose(filters=filters,
|
151
|
+
kernel_size=kernel_size,
|
152
|
+
activation='relu',
|
153
|
+
strides=2,
|
154
|
+
padding='same')(x)
|
155
|
+
filters //= 2
|
156
|
+
|
157
|
+
outputs = Conv2DTranspose(filters=1,
|
158
|
+
kernel_size=kernel_size,
|
159
|
+
activation='sigmoid',
|
160
|
+
padding='same',
|
161
|
+
name='decoder_output')(x)
|
162
|
+
|
163
|
+
decoder = Model(latent_inputs, outputs, name='decoder')
|
164
|
+
decoder.summary()
|
165
|
+
plot_model(decoder, to_file='vae_cnn_decoder.png', show_shapes=True)
|
166
|
+
|
167
|
+
outputs = decoder(encoder(inputs)[2])
|
168
|
+
vae = Model(inputs, outputs, name='vae')
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
def plot_history(history):
|
173
|
+
|
174
|
+
plt.plot(history.history['loss'])
|
175
|
+
plt.plot(history.history['val_loss'])
|
176
|
+
plt.title('model accuracy')
|
177
|
+
plt.xlabel('epoch')
|
178
|
+
plt.ylabel('accuracy')
|
179
|
+
plt.legend(['acc', 'val_acc'], loc='lower right')
|
180
|
+
plt.show()
|
181
|
+
|
182
|
+
plt.plot(history.history['loss'])
|
183
|
+
plt.plot(history.history['val_loss'])
|
184
|
+
plt.title('model loss')
|
185
|
+
plt.xlabel('epoch')
|
186
|
+
plt.ylabel('loss')
|
187
|
+
plt.legend(['loss', 'val_loss'], loc='lower right')
|
188
|
+
plt.savefig('loss.png') # -----(2)
|
189
|
+
plt.show()
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
if __name__ == '__main__':
|
194
|
+
args = easydict.EasyDict({
|
195
|
+
"batchsize": 50,
|
196
|
+
"epoch": 50,
|
197
|
+
"gpu": 0,
|
198
|
+
"out": "result",
|
199
|
+
"resume": False,
|
200
|
+
"unit": 1000
|
201
|
+
})
|
202
|
+
models = (encoder, decoder)
|
203
|
+
|
204
|
+
|
205
|
+
os.environ['PYTHONHASHSEED'] = '0'
|
206
|
+
np.random.seed(5)
|
207
|
+
rn.seed(5)
|
208
|
+
|
209
|
+
config = tf.ConfigProto(
|
210
|
+
gpu_options=tf.GPUOptions(
|
211
|
+
visible_device_list="0,1",
|
212
|
+
allow_growth=True
|
213
|
+
)
|
214
|
+
)
|
215
|
+
|
216
|
+
tf.set_random_seed(5)
|
217
|
+
sess = tf.Session(graph=tf.get_default_graph(), config=config)
|
218
|
+
K.set_session(sess)
|
219
|
+
|
220
|
+
|
221
|
+
reconstruction_loss = binary_crossentropy(K.flatten(inputs),
|
222
|
+
K.flatten(outputs))
|
223
|
+
|
224
|
+
reconstruction_loss *= image_size * image_size
|
225
|
+
kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
|
226
|
+
kl_loss = K.sum(kl_loss, axis=-1)
|
227
|
+
kl_loss *= -0.5
|
228
|
+
vae_loss = K.mean(reconstruction_loss + kl_loss)
|
229
|
+
vae.add_loss(vae_loss)
|
230
|
+
Adam = optimizers.Adam(lr=0.0005)
|
231
|
+
vae.compile(optimizer=Adam)
|
232
|
+
vae.summary()
|
233
|
+
plot_model(vae, to_file='vae_cnn.png', show_shapes=True)
|
234
|
+
|
235
|
+
history = vae.fit(x_train,
|
236
|
+
epochs=epochs,
|
237
|
+
batch_size=batch_size,
|
238
|
+
validation_data=(x_test, None))
|
239
|
+
|
240
|
+
open('vae_cnn.json', "w").write(vae.to_json())
|
241
|
+
vae.save('vae_cnn.h5')
|
242
|
+
|
243
|
+
plot_results(models, data, batch_size=batch_size, model_name="vae_cnn")
|
244
|
+
plot_history(history)
|
245
|
+
|
246
|
+
#補足
|
247
|
+
kerasやtensorflowのバージョンが問題になっている場合もあるようですが、上手くいった人のバージョンに変更しても変化がありませんでした。
|
248
|
+
コードが長くてお手数をおかけしますが、アドバイス頂けると幸いです。どうぞよろしくお願い致します。
|