質問編集履歴
1
エラー内容の更新
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Python UnboundLocalError: local variable 'batch_x' referenced before assignment
|
test
CHANGED
@@ -6,23 +6,77 @@
|
|
6
6
|
|
7
7
|
https://qiita.com/koshian2/items/909360f50e3dd5922f32
|
8
8
|
|
9
|
-
しかし、コードを実行してみると、
|
9
|
+
しかし、コードを実行してみると、以下のようなエラーが発生してしまいますが、どう修正したらいいかわからず困っております。
|
10
|
-
|
10
|
+
|
11
|
-
お手数をお掛けするのですが、
|
11
|
+
お手数をお掛けするのですが、修正方法についてお分かりの方がいらっしゃいましたら教えていただけないでしょうか。
|
12
12
|
|
13
13
|
どうぞよろしくお願いいたします。
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
###
|
18
|
+
|
19
|
+
while True:
|
20
|
+
|
21
|
+
40 batch_x_2, batch_y_2 = next(batches)
|
22
|
+
|
23
|
+
---> 41 m1, m2 = batch_x.shape[0], batch_x_2.shape[0]
|
24
|
+
|
25
|
+
42 if m1 < m2:
|
26
|
+
|
27
|
+
43 batch_x_2 = batch_x_2[:m1]
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
UnboundLocalError: local variable 'batch_x' referenced before assignment
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
17
37
|
### 該当のソースコード
|
18
38
|
|
19
39
|
|
20
40
|
|
21
41
|
```Python
|
22
42
|
|
43
|
+
# model構築の準備
|
44
|
+
|
45
|
+
import keras
|
46
|
+
|
47
|
+
from keras.models import Model
|
48
|
+
|
49
|
+
from keras.layers import Dense, GlobalAveragePooling2D,Input,Dropout,Activation
|
50
|
+
|
51
|
+
from keras.preprocessing.image import ImageDataGenerator
|
52
|
+
|
53
|
+
from keras import applications
|
54
|
+
|
55
|
+
from keras.preprocessing.image import ImageDataGenerator
|
56
|
+
|
57
|
+
from keras.optimizers import Adam
|
58
|
+
|
59
|
+
from keras.callbacks import CSVLogger,EarlyStopping
|
60
|
+
|
23
61
|
import numpy as np
|
24
62
|
|
63
|
+
from keras import backend as K
|
64
|
+
|
65
|
+
from keras.engine.topology import Layer
|
66
|
+
|
67
|
+
import numpy as np
|
68
|
+
|
69
|
+
import tensorflow as tf
|
70
|
+
|
25
|
-
from keras.preprocessing.image import
|
71
|
+
from keras.preprocessing.image import load_img, img_to_array, array_to_img
|
72
|
+
|
73
|
+
import os
|
74
|
+
|
75
|
+
import matplotlib.pyplot as plt
|
76
|
+
|
77
|
+
%matplotlib inline
|
78
|
+
|
79
|
+
|
26
80
|
|
27
81
|
|
28
82
|
|
@@ -140,8 +194,6 @@
|
|
140
194
|
|
141
195
|
|
142
196
|
|
143
|
-
#認識されない
|
144
|
-
|
145
197
|
train_generator=train_datagen.flow_from_directory(
|
146
198
|
|
147
199
|
train_dir,
|
@@ -160,9 +212,7 @@
|
|
160
212
|
|
161
213
|
validation_datagen=ImageDataGenerator(rescale=1.0/255.)
|
162
214
|
|
163
|
-
|
164
|
-
|
165
|
-
|
215
|
+
|
166
216
|
|
167
217
|
validation_generator=validation_datagen.flow_from_directory(
|
168
218
|
|
@@ -178,24 +228,76 @@
|
|
178
228
|
|
179
229
|
|
180
230
|
|
231
|
+
|
232
|
+
|
233
|
+
batch_size = 12
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
base_model=keras.applications.inception_resnet_v2.InceptionResNetV2(input_shape=(299,299,3),
|
238
|
+
|
239
|
+
weights='imagenet',
|
240
|
+
|
241
|
+
include_top=False)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
x = base_model.output
|
246
|
+
|
247
|
+
x = GlobalAveragePooling2D()(x)
|
248
|
+
|
249
|
+
x = Dense(1024, activation='relu')(x)
|
250
|
+
|
251
|
+
predictions = Dense(4, activation='softmax')(x)
|
252
|
+
|
253
|
+
model = Model(inputs=base_model.input,outputs=predictions)
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
model.summary()
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
model.compile(optimizer=Adam(lr=0.001),
|
264
|
+
|
265
|
+
loss='categorical_crossentropy',
|
266
|
+
|
267
|
+
metrics=['accuracy'])
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
callbacks_list = [
|
272
|
+
|
273
|
+
callbacks.ModelCheckpoint(
|
274
|
+
|
275
|
+
filepath="model.ep{epoch:02d}.h5",#delsavepath,
|
276
|
+
|
277
|
+
save_best_only=True),
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
#バリデーションlossが改善しなくなったら学習率を変更する
|
282
|
+
|
283
|
+
callbacks.ReduceLROnPlateau(
|
284
|
+
|
285
|
+
monitor="val_loss",
|
286
|
+
|
287
|
+
factor=0.8,
|
288
|
+
|
289
|
+
patience=5,
|
290
|
+
|
291
|
+
verbose=1)]#,
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
#callbacks.EarlyStopping(monitor='val_loss', patience=7, verbose=1)]
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
model.fit_generator(train_generator, steps_per_epoch=7208, epochs=10, validation_steps=1805, validation_data=validation_generator, callbacks=callbacks_list)
|
300
|
+
|
301
|
+
|
302
|
+
|
181
303
|
```
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
### 試したこと
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
train_generator=flow_from_directory(
|
190
|
-
|
191
|
-
train_dir,
|
192
|
-
|
193
|
-
target_size=(299,299),
|
194
|
-
|
195
|
-
batch_size=12,#25,
|
196
|
-
|
197
|
-
class_mode='categorical',
|
198
|
-
|
199
|
-
shuffle=True)
|
200
|
-
|
201
|
-
に修正してみたところ、「'flow_from_directory' is not defined」というエラーが発生しました。
|