質問編集履歴

4

画像の前処理コードの反映

2021/09/22 06:54

投稿

ykykykk69
ykykykk69

スコア2

test CHANGED
File without changes
test CHANGED
@@ -92,276 +92,458 @@
92
92
 
93
93
  ```
94
94
 
95
+
96
+
95
- 指摘いだいた部分を修正
97
+ 下記に記載しのがエラー内容
98
+
99
+
96
100
 
97
101
  ```python
98
102
 
99
- imsize = (4, 4)
103
+ WARNING:tensorflow:Model was constructed with shape (None, 4, 4, 512) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4, 4, 512), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 150, 150, 3).
104
+
105
+ ---------------------------------------------------------------------------
106
+
107
+ ValueError Traceback (most recent call last)
108
+
109
+ <ipython-input-12-baa28c867e4e> in <module>
110
+
111
+ 35 model = load_model(keras_param)
112
+
113
+ 36 img = load_image(testpic)
114
+
115
+ ---> 37 prd = model.predict(np.array([img]))
116
+
117
+ 38 # print(prd) # 精度の表示
118
+
119
+ 39 prelabel = np.argmax(prd, axis=1)
100
120
 
101
121
  ```
102
122
 
103
-
104
-
105
- 下記に記載したがエラ内容
123
+ 学習済みファイル作成コ
106
-
107
-
108
124
 
109
125
  ```python
110
126
 
111
- WARNING:tensorflow:Model was constructed with shape (None, 4, 4, 512) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4, 4, 512), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 150, 150, 3).
112
-
113
- ---------------------------------------------------------------------------
114
-
115
- ValueError Traceback (most recent call last)
116
-
117
- <ipython-input-12-baa28c867e4e> in <module>
118
-
119
- 35 model = load_model(keras_param)
120
-
121
- 36 img = load_image(testpic)
122
-
123
- ---> 37 prd = model.predict(np.array([img]))
124
-
125
- 38 # print(prd) # 精度の表示
126
-
127
- 39 prelabel = np.argmax(prd, axis=1)
127
+ from keras.applications.vgg16 import VGG16
128
+
129
+ from keras.preprocessing.image import ImageDataGenerator
130
+
131
+ from keras.layers import Input
132
+
133
+ import numpy as np
134
+
135
+ import os
136
+
137
+
138
+
139
+ num_train =2000
140
+
141
+ num_validation=800
142
+
143
+ img_h,img_w=150,150
144
+
145
+ channels=3
146
+
147
+ batch_size=32
148
+
149
+ train_data_dir='data/train'
150
+
151
+ validation_data_dir='data/validation'
152
+
153
+ result_dir='results'
154
+
155
+
156
+
157
+ if not os.path.exists(result_dir):
158
+
159
+ os.mkdir(result_dir)
160
+
161
+
162
+
163
+ def save_VGG16_outputs():
164
+
165
+ model=VGG16(
166
+
167
+ include_top=False,
168
+
169
+ weights='imagenet',
170
+
171
+ input_shape=(img_h,img_w,channels))
172
+
173
+
174
+
175
+ model.summary()
176
+
177
+
178
+
179
+ datagen=ImageDataGenerator(rescale=1.0/255)
180
+
181
+
182
+
183
+ train_generator=datagen.flow_from_directory(
184
+
185
+ train_data_dir,
186
+
187
+ target_size=(img_w,img_h),
188
+
189
+ batch_size=batch_size,
190
+
191
+ class_mode=None,
192
+
193
+ shuffle=False)
194
+
195
+
196
+
197
+ print('train-label:',train_generator.class_indices)
198
+
199
+
200
+
201
+ vgg16_train=model.predict_generator(
202
+
203
+ train_generator,
204
+
205
+ verbose=1)
206
+
207
+
208
+
209
+ np.save(os.path.join(result_dir,'vgg16_train.npy'),vgg16_train)
210
+
211
+
212
+
213
+ validation_generator=datagen.flow_from_directory(
214
+
215
+ validation_data_dir,
216
+
217
+ target_size=(img_w,img_h),
218
+
219
+ batch_size=batch_size,
220
+
221
+ class_mode=None,
222
+
223
+ shuffle=False)
224
+
225
+
226
+
227
+ print('test-label:',validation_generator.class_indices)
228
+
229
+
230
+
231
+ vgg16_test=model.predict(
232
+
233
+ validation_generator,
234
+
235
+ verbose=1)
236
+
237
+
238
+
239
+ np.save(os.path.join(result_dir,'vgg16_test.npy'),vgg16_test)
240
+
241
+
242
+
243
+ save_VGG16_outputs()
244
+
245
+
246
+
247
+ import os
248
+
249
+ import numpy as np
250
+
251
+ from keras.models import Sequential
252
+
253
+ from keras import optimizers
254
+
255
+ from keras.layers import Activation,Dropout,Flatten,Dense
256
+
257
+
258
+
259
+ def train_FClayer():
260
+
261
+
262
+
263
+ train_data=np.load(
264
+
265
+ os.path.join(result_dir,'vgg16_train.npy'))
266
+
267
+
268
+
269
+ print(train_data.shape)
270
+
271
+
272
+
273
+ train_labels=np.array(
274
+
275
+ [0]*int(num_train/2)+[1]*int(num_train/2))
276
+
277
+
278
+
279
+ validation_data=np.load(
280
+
281
+ os.path.join(result_dir,'vgg16_test.npy'))
282
+
283
+
284
+
285
+ print(validation_data.shape)
286
+
287
+
288
+
289
+ validation_labels=np.array(
290
+
291
+ [0]*int(num_validation/2)+[1]*int(num_validation/2))
292
+
293
+
294
+
295
+ model=Sequential()
296
+
297
+
298
+
299
+ model.add(Flatten(input_shape=train_data.shape[1:]))
300
+
301
+
302
+
303
+ model.add(Dense(256,activation='relu'))
304
+
305
+
306
+
307
+ model.add(Dense(1,activation='sigmoid'))
308
+
309
+
310
+
311
+ model.compile(
312
+
313
+ loss='binary_crossentropy',
314
+
315
+ metrics=['accuracy'],
316
+
317
+ optimizer=optimizers.SGD(lr=1e-4,momentum=0.9),)
318
+
319
+
320
+
321
+ epoch=60
322
+
323
+ batch_size=32
324
+
325
+ history=model.fit(train_data,
326
+
327
+ train_labels,
328
+
329
+ epochs=epoch,
330
+
331
+ batch_size=batch_size,
332
+
333
+ verbose=1,
334
+
335
+ validation_data=(validation_data,validation_labels))
336
+
337
+ with open('model.json','w') as json_file:
338
+
339
+ json_file.write(model.to_json())
340
+
341
+ model.save('model_and_weight.h5')
342
+
343
+
344
+
345
+ return history
346
+
347
+
348
+
349
+ history=train_FClayer()
128
350
 
129
351
  ```
130
352
 
353
+
354
+
131
- 修正後エラーコード
355
+ 画像処理コード
132
356
 
133
357
  ```python
134
358
 
135
- WARNING:tensorflow:Model was constructed with shape (None, 4, 4, 512) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4, 4, 512), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 4, 4, 3).
359
+ from keras.applications.vgg16 import VGG16
360
+
361
+ from keras.preprocessing.image import ImageDataGenerator
362
+
363
+ from keras.layers import Input
364
+
365
+ import numpy as np
366
+
367
+ import os
368
+
369
+
370
+
371
+ num_eva =1
372
+
373
+ img_h,img_w=150,150
374
+
375
+ channels=3
376
+
377
+ batch_size=32
378
+
379
+ eva_data_dir='data/evaluation'
380
+
381
+ result_dir='results'
382
+
383
+
384
+
385
+ if not os.path.exists(result_dir):
386
+
387
+ os.mkdir(result_dir)
388
+
389
+
390
+
391
+ def save_VGG16_outputs():
392
+
393
+ model=VGG16(
394
+
395
+ include_top=False,
396
+
397
+ weights='imagenet',
398
+
399
+ input_shape=(img_h,img_w,channels))
400
+
401
+
402
+
403
+ model.summary()
404
+
405
+
406
+
407
+ datagen=ImageDataGenerator(rescale=1.0/255)
408
+
409
+
410
+
411
+ eva_generator=datagen.flow_from_directory(
412
+
413
+ eva_data_dir,
414
+
415
+ target_size=(img_w,img_h),
416
+
417
+ batch_size=batch_size,
418
+
419
+ class_mode=None,
420
+
421
+ shuffle=False)
422
+
423
+
424
+
425
+ print('eva-label:',eva_generator.class_indices)
426
+
427
+
428
+
429
+ vgg16_eva=model.predict_generator(
430
+
431
+ eva_generator,
432
+
433
+ verbose=1)
434
+
435
+
436
+
437
+ np.save(os.path.join(result_dir,'vgg16_eva.npy'),vgg16_eva)
438
+
439
+
440
+
441
+ print(vgg16_eva.shape)
442
+
443
+
444
+
445
+ save_VGG16_outputs()
446
+
447
+
448
+
449
+ Model: "vgg16"
450
+
451
+ _________________________________________________________________
452
+
453
+ Layer (type) Output Shape Param #
454
+
455
+ =================================================================
456
+
457
+ input_2 (InputLayer) [(None, 150, 150, 3)] 0
458
+
459
+ _________________________________________________________________
460
+
461
+ block1_conv1 (Conv2D) (None, 150, 150, 64) 1792
462
+
463
+ _________________________________________________________________
464
+
465
+ block1_conv2 (Conv2D) (None, 150, 150, 64) 36928
466
+
467
+ _________________________________________________________________
468
+
469
+ block1_pool (MaxPooling2D) (None, 75, 75, 64) 0
470
+
471
+ _________________________________________________________________
472
+
473
+ block2_conv1 (Conv2D) (None, 75, 75, 128) 73856
474
+
475
+ _________________________________________________________________
476
+
477
+ block2_conv2 (Conv2D) (None, 75, 75, 128) 147584
478
+
479
+ _________________________________________________________________
480
+
481
+ block2_pool (MaxPooling2D) (None, 37, 37, 128) 0
482
+
483
+ _________________________________________________________________
484
+
485
+ block3_conv1 (Conv2D) (None, 37, 37, 256) 295168
486
+
487
+ _________________________________________________________________
488
+
489
+ block3_conv2 (Conv2D) (None, 37, 37, 256) 590080
490
+
491
+ _________________________________________________________________
492
+
493
+ block3_conv3 (Conv2D) (None, 37, 37, 256) 590080
494
+
495
+ _________________________________________________________________
496
+
497
+ block3_pool (MaxPooling2D) (None, 18, 18, 256) 0
498
+
499
+ _________________________________________________________________
500
+
501
+ block4_conv1 (Conv2D) (None, 18, 18, 512) 1180160
502
+
503
+ _________________________________________________________________
504
+
505
+ block4_conv2 (Conv2D) (None, 18, 18, 512) 2359808
506
+
507
+ _________________________________________________________________
508
+
509
+ block4_conv3 (Conv2D) (None, 18, 18, 512) 2359808
510
+
511
+ _________________________________________________________________
512
+
513
+ block4_pool (MaxPooling2D) (None, 9, 9, 512) 0
514
+
515
+ _________________________________________________________________
516
+
517
+ block5_conv1 (Conv2D) (None, 9, 9, 512) 2359808
518
+
519
+ _________________________________________________________________
520
+
521
+ block5_conv2 (Conv2D) (None, 9, 9, 512) 2359808
522
+
523
+ _________________________________________________________________
524
+
525
+ block5_conv3 (Conv2D) (None, 9, 9, 512) 2359808
526
+
527
+ _________________________________________________________________
528
+
529
+ block5_pool (MaxPooling2D) (None, 4, 4, 512) 0
530
+
531
+ =================================================================
532
+
533
+ Total params: 14,714,688
534
+
535
+ Trainable params: 14,714,688
536
+
537
+ Non-trainable params: 0
538
+
539
+ _________________________________________________________________
540
+
541
+ Found 1 images belonging to 1 classes.
542
+
543
+ eva-label: {'1': 0}
544
+
545
+ 1/1 [==============================] - 2s 2s/step
546
+
547
+ (1, 4, 4, 512)
136
548
 
137
549
  ```
138
-
139
- 学習済みファイルの作成コード
140
-
141
- ```python
142
-
143
- from keras.applications.vgg16 import VGG16
144
-
145
- from keras.preprocessing.image import ImageDataGenerator
146
-
147
- from keras.layers import Input
148
-
149
- import numpy as np
150
-
151
- import os
152
-
153
-
154
-
155
- num_train =2000
156
-
157
- num_validation=800
158
-
159
- img_h,img_w=150,150
160
-
161
- channels=3
162
-
163
- batch_size=32
164
-
165
- train_data_dir='data/train'
166
-
167
- validation_data_dir='data/validation'
168
-
169
- result_dir='results'
170
-
171
-
172
-
173
- if not os.path.exists(result_dir):
174
-
175
- os.mkdir(result_dir)
176
-
177
-
178
-
179
- def save_VGG16_outputs():
180
-
181
- model=VGG16(
182
-
183
- include_top=False,
184
-
185
- weights='imagenet',
186
-
187
- input_shape=(img_h,img_w,channels))
188
-
189
-
190
-
191
- model.summary()
192
-
193
-
194
-
195
- datagen=ImageDataGenerator(rescale=1.0/255)
196
-
197
-
198
-
199
- train_generator=datagen.flow_from_directory(
200
-
201
- train_data_dir,
202
-
203
- target_size=(img_w,img_h),
204
-
205
- batch_size=batch_size,
206
-
207
- class_mode=None,
208
-
209
- shuffle=False)
210
-
211
-
212
-
213
- print('train-label:',train_generator.class_indices)
214
-
215
-
216
-
217
- vgg16_train=model.predict_generator(
218
-
219
- train_generator,
220
-
221
- verbose=1)
222
-
223
-
224
-
225
- np.save(os.path.join(result_dir,'vgg16_train.npy'),vgg16_train)
226
-
227
-
228
-
229
- validation_generator=datagen.flow_from_directory(
230
-
231
- validation_data_dir,
232
-
233
- target_size=(img_w,img_h),
234
-
235
- batch_size=batch_size,
236
-
237
- class_mode=None,
238
-
239
- shuffle=False)
240
-
241
-
242
-
243
- print('test-label:',validation_generator.class_indices)
244
-
245
-
246
-
247
- vgg16_test=model.predict(
248
-
249
- validation_generator,
250
-
251
- verbose=1)
252
-
253
-
254
-
255
- np.save(os.path.join(result_dir,'vgg16_test.npy'),vgg16_test)
256
-
257
-
258
-
259
- save_VGG16_outputs()
260
-
261
-
262
-
263
- import os
264
-
265
- import numpy as np
266
-
267
- from keras.models import Sequential
268
-
269
- from keras import optimizers
270
-
271
- from keras.layers import Activation,Dropout,Flatten,Dense
272
-
273
-
274
-
275
- def train_FClayer():
276
-
277
-
278
-
279
- train_data=np.load(
280
-
281
- os.path.join(result_dir,'vgg16_train.npy'))
282
-
283
-
284
-
285
- print(train_data.shape)
286
-
287
-
288
-
289
- train_labels=np.array(
290
-
291
- [0]*int(num_train/2)+[1]*int(num_train/2))
292
-
293
-
294
-
295
- validation_data=np.load(
296
-
297
- os.path.join(result_dir,'vgg16_test.npy'))
298
-
299
-
300
-
301
- print(validation_data.shape)
302
-
303
-
304
-
305
- validation_labels=np.array(
306
-
307
- [0]*int(num_validation/2)+[1]*int(num_validation/2))
308
-
309
-
310
-
311
- model=Sequential()
312
-
313
-
314
-
315
- model.add(Flatten(input_shape=train_data.shape[1:]))
316
-
317
-
318
-
319
- model.add(Dense(256,activation='relu'))
320
-
321
-
322
-
323
- model.add(Dense(1,activation='sigmoid'))
324
-
325
-
326
-
327
- model.compile(
328
-
329
- loss='binary_crossentropy',
330
-
331
- metrics=['accuracy'],
332
-
333
- optimizer=optimizers.SGD(lr=1e-4,momentum=0.9),)
334
-
335
-
336
-
337
- epoch=60
338
-
339
- batch_size=32
340
-
341
- history=model.fit(train_data,
342
-
343
- train_labels,
344
-
345
- epochs=epoch,
346
-
347
- batch_size=batch_size,
348
-
349
- verbose=1,
350
-
351
- validation_data=(validation_data,validation_labels))
352
-
353
- with open('model.json','w') as json_file:
354
-
355
- json_file.write(model.to_json())
356
-
357
- model.save('model_and_weight.h5')
358
-
359
-
360
-
361
- return history
362
-
363
-
364
-
365
- history=train_FClayer()
366
-
367
- ```

3

指摘いただいた部分の修正箇所を反映

2021/09/22 06:54

投稿

ykykykk69
ykykykk69

スコア2

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,16 @@
92
92
 
93
93
  ```
94
94
 
95
+ 指摘いただいた部分を修正
96
+
97
+ ```python
98
+
99
+ imsize = (4, 4)
100
+
101
+ ```
102
+
103
+
104
+
95
105
  下記に記載したのがエラー内容
96
106
 
97
107
 
@@ -118,7 +128,13 @@
118
128
 
119
129
  ```
120
130
 
121
-
131
+ 修正後のエラーコード
132
+
133
+ ```python
134
+
135
+ WARNING:tensorflow:Model was constructed with shape (None, 4, 4, 512) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4, 4, 512), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 4, 4, 3).
136
+
137
+ ```
122
138
 
123
139
  学習済みファイルの作成コード
124
140
 

2

学習済みファイルの作成コードの掲載

2021/09/22 06:22

投稿

ykykykk69
ykykykk69

スコア2

test CHANGED
File without changes
test CHANGED
@@ -117,3 +117,235 @@
117
117
  39 prelabel = np.argmax(prd, axis=1)
118
118
 
119
119
  ```
120
+
121
+
122
+
123
+ 学習済みファイルの作成コード
124
+
125
+ ```python
126
+
127
+ from keras.applications.vgg16 import VGG16
128
+
129
+ from keras.preprocessing.image import ImageDataGenerator
130
+
131
+ from keras.layers import Input
132
+
133
+ import numpy as np
134
+
135
+ import os
136
+
137
+
138
+
139
+ num_train =2000
140
+
141
+ num_validation=800
142
+
143
+ img_h,img_w=150,150
144
+
145
+ channels=3
146
+
147
+ batch_size=32
148
+
149
+ train_data_dir='data/train'
150
+
151
+ validation_data_dir='data/validation'
152
+
153
+ result_dir='results'
154
+
155
+
156
+
157
+ if not os.path.exists(result_dir):
158
+
159
+ os.mkdir(result_dir)
160
+
161
+
162
+
163
+ def save_VGG16_outputs():
164
+
165
+ model=VGG16(
166
+
167
+ include_top=False,
168
+
169
+ weights='imagenet',
170
+
171
+ input_shape=(img_h,img_w,channels))
172
+
173
+
174
+
175
+ model.summary()
176
+
177
+
178
+
179
+ datagen=ImageDataGenerator(rescale=1.0/255)
180
+
181
+
182
+
183
+ train_generator=datagen.flow_from_directory(
184
+
185
+ train_data_dir,
186
+
187
+ target_size=(img_w,img_h),
188
+
189
+ batch_size=batch_size,
190
+
191
+ class_mode=None,
192
+
193
+ shuffle=False)
194
+
195
+
196
+
197
+ print('train-label:',train_generator.class_indices)
198
+
199
+
200
+
201
+ vgg16_train=model.predict_generator(
202
+
203
+ train_generator,
204
+
205
+ verbose=1)
206
+
207
+
208
+
209
+ np.save(os.path.join(result_dir,'vgg16_train.npy'),vgg16_train)
210
+
211
+
212
+
213
+ validation_generator=datagen.flow_from_directory(
214
+
215
+ validation_data_dir,
216
+
217
+ target_size=(img_w,img_h),
218
+
219
+ batch_size=batch_size,
220
+
221
+ class_mode=None,
222
+
223
+ shuffle=False)
224
+
225
+
226
+
227
+ print('test-label:',validation_generator.class_indices)
228
+
229
+
230
+
231
+ vgg16_test=model.predict(
232
+
233
+ validation_generator,
234
+
235
+ verbose=1)
236
+
237
+
238
+
239
+ np.save(os.path.join(result_dir,'vgg16_test.npy'),vgg16_test)
240
+
241
+
242
+
243
+ save_VGG16_outputs()
244
+
245
+
246
+
247
+ import os
248
+
249
+ import numpy as np
250
+
251
+ from keras.models import Sequential
252
+
253
+ from keras import optimizers
254
+
255
+ from keras.layers import Activation,Dropout,Flatten,Dense
256
+
257
+
258
+
259
+ def train_FClayer():
260
+
261
+
262
+
263
+ train_data=np.load(
264
+
265
+ os.path.join(result_dir,'vgg16_train.npy'))
266
+
267
+
268
+
269
+ print(train_data.shape)
270
+
271
+
272
+
273
+ train_labels=np.array(
274
+
275
+ [0]*int(num_train/2)+[1]*int(num_train/2))
276
+
277
+
278
+
279
+ validation_data=np.load(
280
+
281
+ os.path.join(result_dir,'vgg16_test.npy'))
282
+
283
+
284
+
285
+ print(validation_data.shape)
286
+
287
+
288
+
289
+ validation_labels=np.array(
290
+
291
+ [0]*int(num_validation/2)+[1]*int(num_validation/2))
292
+
293
+
294
+
295
+ model=Sequential()
296
+
297
+
298
+
299
+ model.add(Flatten(input_shape=train_data.shape[1:]))
300
+
301
+
302
+
303
+ model.add(Dense(256,activation='relu'))
304
+
305
+
306
+
307
+ model.add(Dense(1,activation='sigmoid'))
308
+
309
+
310
+
311
+ model.compile(
312
+
313
+ loss='binary_crossentropy',
314
+
315
+ metrics=['accuracy'],
316
+
317
+ optimizer=optimizers.SGD(lr=1e-4,momentum=0.9),)
318
+
319
+
320
+
321
+ epoch=60
322
+
323
+ batch_size=32
324
+
325
+ history=model.fit(train_data,
326
+
327
+ train_labels,
328
+
329
+ epochs=epoch,
330
+
331
+ batch_size=batch_size,
332
+
333
+ verbose=1,
334
+
335
+ validation_data=(validation_data,validation_labels))
336
+
337
+ with open('model.json','w') as json_file:
338
+
339
+ json_file.write(model.to_json())
340
+
341
+ model.save('model_and_weight.h5')
342
+
343
+
344
+
345
+ return history
346
+
347
+
348
+
349
+ history=train_FClayer()
350
+
351
+ ```

1

説明文にコードのことを追加

2021/09/22 02:24

投稿

ykykykk69
ykykykk69

スコア2

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,8 @@
1
1
  下記のようなコードで画像分類器を作成したいと考えています。
2
2
 
3
3
  しかし、エラーが発生してしまい実行できません。
4
+
5
+ コードは同じ本から採用しています。
4
6
 
5
7
  解決方法がありましたら、教えていただけますでしょうか。
6
8