質問編集履歴

3

コードの追加

2022/10/10 16:01

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -239,5 +239,60 @@
239
239
  res_q.put((float(model.loss.data), float(model.accuracy.data)))
240
240
  del x, t
241
241
 
242
+ data_qにデータを入れる
243
+
244
+  def feed_data():
245
+ # Data feeder
246
+ i = 0
247
+ count = 0
248
+
249
+ x_batch = np.ndarray(
250
+ (args.batchsize, 3, model.insize, model.insize), dtype=np.float32)
251
+ y_batch = np.ndarray((args.batchsize,), dtype=np.int32)
252
+ val_x_batch = np.ndarray(
253
+ (args.val_batchsize, 3, model.insize, model.insize), dtype=np.float32)
254
+ val_y_batch = np.ndarray((args.val_batchsize,), dtype=np.int32)
255
+
256
+ batch_pool = [None] * args.batchsize
257
+ val_batch_pool = [None] * args.val_batchsize
258
+ pool = multiprocessing.Pool(args.loaderjob)
259
+ data_q.put('train')
260
+ for epoch in six.moves.range(1, 1 + args.epoch):
261
+ print('epoch', epoch, file=sys.stderr)
262
+ print('learning rate', optimizer.lr, file=sys.stderr)
263
+ perm = np.random.permutation(len(train_list))
264
+ for idx in perm:
265
+ path, label = train_list[idx]
266
+ batch_pool[i] = pool.apply_async(read_image, (path, False, True))
267
+ y_batch[i] = label
268
+ i += 1
269
+
270
+ if i == args.batchsize:
271
+ for j, x in enumerate(batch_pool):
272
+ x_batch[j] = x.get()
273
+ data_q.put((x_batch.copy(), y_batch.copy()))
274
+ i = 0
275
+
276
+ count += 1
277
+ if count % denominator == 0:
278
+ data_q.put('val')
279
+ j = 0
280
+ for path, label in val_list:
281
+ val_batch_pool[j] = pool.apply_async(
282
+ read_image, (path, True, False))
283
+ val_y_batch[j] = label
284
+ j += 1
285
+
286
+ if j == args.val_batchsize:
287
+ for k, x in enumerate(val_batch_pool):
288
+ val_x_batch[k] = x.get()
289
+ data_q.put((val_x_batch.copy(), val_y_batch.copy()))
290
+ j = 0
291
+ data_q.put('train')
292
+
293
+ optimizer.lr *= 0.97
294
+ pool.close()
295
+ pool.join()
296
+ data_q.put('end')
242
297
  ### 試したこと
243
298
  学習させる画像の枚数や学習回数を変更した。

2

コードの追加

2022/10/10 13:59

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -193,6 +193,51 @@
193
193
  h = F.relu(self.conv4b(h))
194
194
  h = F.reshape(F.average_pooling_2d(h, 6), (x_data.shape[0], 1000))
195
195
  return F.softmax(h)
196
+
197
+ 学習用のコード
198
+ def train_loop():
199
+ # Trainer
200
+ graph_generated = False
201
+ while True:
202
+ while data_q.empty():
203
+ time.sleep(0.1)
204
+ inp = data_q.get()
205
+ if inp == 'end': # quit
206
+ res_q.put('end')
207
+ break
208
+ elif inp == 'train': # restart training
209
+ res_q.put('train')
210
+ model.train = True
211
+ continue
212
+ elif inp == 'val': # start validation
213
+ res_q.put('val')
214
+ #serializers.save_npz(args.out, model)
215
+ #model.to_cpu()
216
+ pickle.dump(model, open(args.out, 'wb'), -1)
217
+ serializers.save_npz(args.outstate, optimizer)
218
+ model.train = False
219
+ continue
220
+
221
+ volatile = 'off'if model.train else 'on'
222
+ #x = chainer.Variable(xp.asarray(inp[0]), volatile=volatile)
223
+ x = chainer.Variable(xp.asarray(inp[0]))
224
+ with chainer.no_backprop_mode():
225
+ #t = chainer.Variable(xp.asarray(inp[1]),volatile=volatile)
226
+ t = chainer.Variable(xp.asarray(inp[1]))
227
+ with chainer.no_backprop_mode():
228
+     #if model train:
229
+ if model:
230
+ optimizer.update(model, x, t)
231
+ if not graph_generated:
232
+ with open('graph.dot', 'w') as o:
233
+ o.write(computational_graph.build_computational_graph(
234
+ (model.loss,)).dump())
235
+ print('generated graph', file=sys.stderr)
236
+ graph_generated = True
237
+ else:
238
+ model(x, t)
239
+ res_q.put((float(model.loss.data), float(model.accuracy.data)))
240
+ del x, t
241
+
196
242
  ### 試したこと
197
243
  学習させる画像の枚数や学習回数を変更した。
198
-

1

学習で用いたコードの追加

2022/10/10 13:14

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -129,6 +129,70 @@
129
129
  print('#%d | %s | %4.1f%%' % (rank, name, score * 100))
130
130
 
131
131
 
132
+ 今回使用した nin.py
133
+ import math
134
+ import chainer
135
+ import chainer.functions as F
136
+ import chainer.links as L
137
+
138
+
139
+ class NIN(chainer.Chain):
140
+
141
+ """Network-in-Network example model."""
142
+
143
+ insize = 227
144
+
145
+ def __init__(self):
146
+ w = math.sqrt(2) # MSRA scaling
147
+ super(NIN, self).__init__(
148
+ mlpconv1=L.MLPConvolution2D(
149
+ 3, (96, 96, 96), 11, stride=4),
150
+ mlpconv2=L.MLPConvolution2D(
151
+ 96, (256, 256, 256), 5, pad=2), #wscale=w削除
152
+ mlpconv3=L.MLPConvolution2D(
153
+ 256, (384, 384, 384), 3, pad=1),
154
+ mlpconv4=L.MLPConvolution2D(
155
+ 384, (1024, 1024, 1000), 3, pad=2),
156
+ )
157
+ self.train = True
158
+
159
+ def clear(self):
160
+ self.loss = None
161
+ self.accuracy = None
162
+
163
+ def __call__(self, x, t):
164
+ self.clear()
165
+ h = F.max_pooling_2d(F.relu(self.mlpconv1(x)), 3, stride=2)
166
+ h = F.max_pooling_2d(F.relu(self.mlpconv2(h)), 3, stride=2)
167
+ h = F.max_pooling_2d(F.relu(self.mlpconv3(h)), 3, stride=2)
168
+ h = self.mlpconv4(F.dropout(h)) #変更
169
+ h = F.reshape(F.average_pooling_2d(h, 6), (x.data.shape[0], 1000))
170
+
171
+ self.loss = F.softmax_cross_entropy(h, t)
172
+ self.accuracy = F.accuracy(h, t)
173
+ return self.loss
174
+
175
+ def predict(self, x_data, train=False):
176
+ x = chainer.Variable(x_data, volatile=True)
177
+
178
+ h = F.relu(self.conv1(x))
179
+ h = F.relu(self.conv1a(h))
180
+ h = F.relu(self.conv1b(h))
181
+ h = F.max_pooling_2d(h, 3, stride=2)
182
+ h = F.relu(self.conv2(h))
183
+ h = F.relu(self.conv2a(h))
184
+ h = F.relu(self.conv2b(h))
185
+ h = F.max_pooling_2d(h, 3, stride=2)
186
+ h = F.relu(self.conv3(h))
187
+ h = F.relu(self.conv3a(h))
188
+ h = F.relu(self.conv3b(h))
189
+ h = F.max_pooling_2d(h, 3, stride=2)
190
+ h = F.dropout(h, train=train)
191
+ h = F.relu(self.conv4(h))
192
+ h = F.relu(self.conv4a(h))
193
+ h = F.relu(self.conv4b(h))
194
+ h = F.reshape(F.average_pooling_2d(h, 6), (x_data.shape[0], 1000))
195
+ return F.softmax(h)
132
196
  ### 試したこと
133
197
  学習させる画像の枚数や学習回数を変更した。
134
198