質問編集履歴

2

一部修正

2020/10/05 00:58

投稿

tanukichi4010
tanukichi4010

スコア1

test CHANGED
File without changes
test CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
  IMAGE_SIZE = 224
64
64
 
65
- N_CATEGORIES = 22
65
+ N_CATEGORIES = 20
66
66
 
67
67
  BATCH_SIZE = 64
68
68
 

1

ソースコードの追加

2020/10/05 00:58

投稿

tanukichi4010
tanukichi4010

スコア1

test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,221 @@
23
23
 
24
24
 
25
25
  初歩的なことで申し訳ありませんがよろしくお願いいたします。
26
+
27
+
28
+
29
+ ##追記
30
+
31
+ ソースコードは以下のWEBサイトを参考にしました。
32
+
33
+ ・https://qiita.com/tomo_20180402/items/e8c55bdca648f4877188
34
+
35
+ ・https://spjai.com/keras-fine-tuning/
36
+
37
+
38
+
39
+ ```python
40
+
41
+ import os
42
+
43
+ import numpy as np
44
+
45
+ from keras.preprocessing.image import ImageDataGenerator
46
+
47
+ from keras import models
48
+
49
+ from keras import layers
50
+
51
+ from keras import optimizers
52
+
53
+ from keras.layers import Conv2D, MaxPooling2D,Input
54
+
55
+ from keras.layers import Dense, Dropout, Flatten
56
+
57
+ import matplotlib.pyplot as plt
58
+
59
+ import keras
60
+
61
+
62
+
63
+ IMAGE_SIZE = 224
64
+
65
+ N_CATEGORIES = 22
66
+
67
+ BATCH_SIZE = 64
68
+
69
+ NUM_EPOCHS = 20
70
+
71
+
72
+
73
+ train_data_dir = ''
74
+
75
+ validation_data_dir = ''
76
+
77
+
78
+
79
+ NUM_TRAINING = 80000
80
+
81
+ NUM_VALIDATION = 12000
82
+
83
+
84
+
85
+
86
+
87
+ model = models.Sequential()
88
+
89
+ model.add(layers.Conv2D(32,(3,3),activation="relu",input_shape=(224,224,3)))
90
+
91
+ model.add(layers.MaxPooling2D((2,2)))
92
+
93
+ model.add(layers.Conv2D(64,(3,3),activation="relu"))
94
+
95
+ model.add(layers.MaxPooling2D((2,2)))
96
+
97
+ model.add(layers.Conv2D(128,(3,3),activation="relu"))
98
+
99
+ model.add(layers.MaxPooling2D((2,2)))
100
+
101
+ model.add(layers.Conv2D(256,(3,3),activation="relu"))
102
+
103
+ model.add(layers.MaxPooling2D((2,2)))
104
+
105
+
106
+
107
+
108
+
109
+ model.add(layers.Flatten())
110
+
111
+ model.add(layers.Dense(128,activation="relu"))
112
+
113
+ model.add(layers.Dropout(0.5))
114
+
115
+ model.add(layers.Dense(N_CATEGORIES,activation="softmax"))
116
+
117
+
118
+
119
+
120
+
121
+ model.compile(loss='categorical_crossentropy',
122
+
123
+ optimizer=optimizers.SGD(lr=1e-4,momentum=0.9),
124
+
125
+ metrics=['acc'])
126
+
127
+ model.summary()
128
+
129
+
130
+
131
+
132
+
133
+ train_datagen = ImageDataGenerator(
134
+
135
+ rescale=1.0 / 255,
136
+
137
+ shear_range=0.2,
138
+
139
+ zoom_range=0.2,
140
+
141
+ horizontal_flip=True,
142
+
143
+ rotation_range=10)
144
+
145
+ test_datagen = ImageDataGenerator(
146
+
147
+ rescale=1.0 / 255,
148
+
149
+ )
150
+
151
+
152
+
153
+ train_generator = train_datagen.flow_from_directory(
154
+
155
+ train_data_dir,
156
+
157
+ target_size=(IMAGE_SIZE, IMAGE_SIZE),
158
+
159
+ batch_size=BATCH_SIZE,
160
+
161
+ class_mode='categorical',
162
+
163
+ shuffle=True
164
+
165
+ )
166
+
167
+ validation_generator = test_datagen.flow_from_directory(
168
+
169
+ validation_data_dir,
170
+
171
+ target_size=(IMAGE_SIZE, IMAGE_SIZE),
172
+
173
+ batch_size=BATCH_SIZE,
174
+
175
+ class_mode='categorical',
176
+
177
+ shuffle=True
178
+
179
+ )
180
+
181
+
182
+
183
+ history = model.fit_generator(train_generator,
184
+
185
+ steps_per_epoch=NUM_TRAINING//BATCH_SIZE,
186
+
187
+ epochs=NUM_EPOCHS,
188
+
189
+ verbose=1,
190
+
191
+ validation_data=validation_generator,
192
+
193
+ validation_steps=NUM_VALIDATION//BATCH_SIZE,
194
+
195
+ )
196
+
197
+
198
+
199
+
200
+
201
+ model.save('model.h5')
202
+
203
+
204
+
205
+
206
+
207
+ # グラフ描画
208
+
209
+ # Accuracy
210
+
211
+ plt.plot(range(1, NUM_EPOCHS+1), history.history['acc'], "o-")
212
+
213
+ plt.plot(range(1, NUM_EPOCHS+1), history.history['val_acc'], "o-")
214
+
215
+ plt.title('model accuracy')
216
+
217
+ plt.ylabel('accuracy') # Y軸ラベル
218
+
219
+ plt.xlabel('epoch') # X軸ラベル
220
+
221
+ plt.legend(['train', 'test'], loc='upper left')
222
+
223
+ plt.show()
224
+
225
+ # loss
226
+
227
+ plt.plot(range(1, NUM_EPOCHS+1), history.history['loss'], "o-")
228
+
229
+ plt.plot(range(1, NUM_EPOCHS+1), history.history['val_loss'], "o-")
230
+
231
+ plt.title('model loss')
232
+
233
+ plt.ylabel('loss') # Y軸ラベル
234
+
235
+ plt.xlabel('epoch') # X軸ラベル
236
+
237
+ plt.legend(['train', 'test'], loc='upper right')
238
+
239
+ plt.show()
240
+
241
+
242
+
243
+ ```