質問編集履歴
4
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -23,36 +23,6 @@
|
|
23
23
|
|
24
24
|
ソースコード
|
25
25
|
train_imagenet.py
|
26
|
-
|
27
|
-
from __future__ import print_function
|
28
|
-
import argparse
|
29
|
-
import datetime
|
30
|
-
import json
|
31
|
-
import multiprocessing
|
32
|
-
import os
|
33
|
-
import random
|
34
|
-
import sys
|
35
|
-
import threading
|
36
|
-
import time
|
37
|
-
|
38
|
-
import numpy as np
|
39
|
-
from PIL import Image
|
40
|
-
import six
|
41
|
-
import six.moves.cPickle as pickle
|
42
|
-
from six.moves import queue
|
43
|
-
|
44
|
-
import chainer
|
45
|
-
from chainer import serializers
|
46
|
-
from chainer import computational_graph
|
47
|
-
from chainer import cuda
|
48
|
-
from chainer import optimizers
|
49
|
-
from chainer import serializers
|
50
|
-
|
51
|
-
#追加
|
52
|
-
import alex
|
53
|
-
import googlenet
|
54
|
-
import googlenetbn
|
55
|
-
import nin
|
56
26
|
|
57
27
|
|
58
28
|
parser = argparse.ArgumentParser(description='Learning convnet from ILSVRC2012 dataset')
|
@@ -100,142 +70,9 @@
|
|
100
70
|
else:
|
101
71
|
denominator = 100000
|
102
72
|
|
103
|
-
|
104
|
-
def load_image_list(path, root): **←**
|
105
|
-
tuples = []
|
106
|
-
for line in open(path):
|
107
|
-
pair = line.strip().split()
|
108
|
-
tuples.append((os.path.join(root, pair[0]), np.int32(pair[1])))
|
109
|
-
return tuples
|
110
|
-
|
111
|
-
|
112
73
|
# Prepare dataset
|
113
74
|
train_list = load_image_list(args.train, args.root) **←**
|
114
75
|
|
115
76
|
val_list = load_image_list(args.val, args.root)
|
116
77
|
|
117
78
|
mean_image = np.load(args.mean)
|
118
|
-
|
119
|
-
|
120
|
-
# Prepare model
|
121
|
-
if args.arch == 'nin':
|
122
|
-
import nin
|
123
|
-
model = nin.NIN()
|
124
|
-
elif args.arch == 'i2vvgg':
|
125
|
-
import i2vvgg
|
126
|
-
model = i2vvgg.i2vVGG()
|
127
|
-
elif args.arch == 'alex':
|
128
|
-
import alex
|
129
|
-
model = alex.Alex()
|
130
|
-
elif args.arch == 'alexbn':
|
131
|
-
import alexbn
|
132
|
-
model = alexbn.AlexBN()
|
133
|
-
elif args.arch == 'googlenet':
|
134
|
-
import googlenet
|
135
|
-
model = googlenet.GoogLeNet()
|
136
|
-
elif args.arch == 'googlenetbn':
|
137
|
-
import googlenetbn
|
138
|
-
model = googlenetbn.GoogLeNetBN()
|
139
|
-
else:
|
140
|
-
raise ValueError('Invalid architecture name')
|
141
|
-
|
142
|
-
if args.gpu >= 0:
|
143
|
-
cuda.get_device(args.gpu).use()
|
144
|
-
model.to_gpu()
|
145
|
-
|
146
|
-
# Setup optimizer
|
147
|
-
optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
|
148
|
-
optimizer.setup(model)
|
149
|
-
|
150
|
-
# Init/Resume
|
151
|
-
if args.initmodel:
|
152
|
-
print('Load model from', args.initmodel)
|
153
|
-
serializers.load_npz(args.initmodel, model)
|
154
|
-
if args.resume:
|
155
|
-
print('Load optimizer state from', args.resume)
|
156
|
-
serializers.load_npz(args.resume, optimizer)
|
157
|
-
|
158
|
-
|
159
|
-
# ------------------------------------------------------------------------------
|
160
|
-
# This example consists of three threads: data feeder, logger and trainer.
|
161
|
-
# These communicate with each other via Queue.
|
162
|
-
data_q = queue.Queue(maxsize=1)
|
163
|
-
res_q = queue.Queue()
|
164
|
-
|
165
|
-
cropwidth = 256 - model.insize
|
166
|
-
|
167
|
-
|
168
|
-
def read_image(path, center=False, flip=False):
|
169
|
-
# Data loading routine
|
170
|
-
image = np.asarray(Image.open(path)).transpose(2, 0, 1)
|
171
|
-
if center:
|
172
|
-
top = left = cropwidth // 2
|
173
|
-
else:
|
174
|
-
top = random.randint(0, cropwidth - 1)
|
175
|
-
left = random.randint(0, cropwidth - 1)
|
176
|
-
bottom = model.insize + top
|
177
|
-
right = model.insize + left
|
178
|
-
|
179
|
-
image = image[:, top:bottom, left:right].astype(np.float32)
|
180
|
-
image -= mean_image[:, top:bottom, left:right]
|
181
|
-
image /= 255
|
182
|
-
if flip and random.randint(0, 1) == 0:
|
183
|
-
return image[:, :, ::-1]
|
184
|
-
else:
|
185
|
-
return image
|
186
|
-
|
187
|
-
|
188
|
-
def feed_data():
|
189
|
-
# Data feeder
|
190
|
-
i = 0
|
191
|
-
count = 0
|
192
|
-
|
193
|
-
x_batch = np.ndarray(
|
194
|
-
(args.batchsize, 3, model.insize, model.insize), dtype=np.float32)
|
195
|
-
y_batch = np.ndarray((args.batchsize,), dtype=np.float32)
|
196
|
-
val_x_batch = np.ndarray(
|
197
|
-
(args.val_batchsize, 3, model.insize, model.insize), dtype=np.float32)
|
198
|
-
val_y_batch = np.ndarray((args.val_batchsize,), dtype=np.float32)
|
199
|
-
|
200
|
-
batch_pool = [None] * args.batchsize
|
201
|
-
val_batch_pool = [None] * args.val_batchsize
|
202
|
-
pool = multiprocessing.Pool(args.loaderjob)
|
203
|
-
data_q.put('train')
|
204
|
-
for epoch in six.moves.range(1, 1 + args.epoch):
|
205
|
-
print('epoch', epoch, file=sys.stderr)
|
206
|
-
print('learning rate', optimizer.lr, file=sys.stderr)
|
207
|
-
perm = np.random.permutation(len(train_list))
|
208
|
-
for idx in perm:
|
209
|
-
path, label = train_list[idx]
|
210
|
-
batch_pool[i] = pool.apply_async(read_image, (path, False, True))
|
211
|
-
y_batch[i] = label
|
212
|
-
i += 1
|
213
|
-
|
214
|
-
if i == args.batchsize:
|
215
|
-
for j, x in enumerate(batch_pool):
|
216
|
-
x_batch[j] = x.get()
|
217
|
-
data_q.put((x_batch.copy(), y_batch.copy()))
|
218
|
-
i = 0
|
219
|
-
|
220
|
-
count += 1
|
221
|
-
if count % denominator == 0:
|
222
|
-
data_q.put('val')
|
223
|
-
j = 0
|
224
|
-
for path, label in val_list:
|
225
|
-
val_batch_pool[j] = pool.apply_async(
|
226
|
-
read_image, (path, True, False))
|
227
|
-
val_y_batch[j] = label
|
228
|
-
j += 1
|
229
|
-
|
230
|
-
if j == args.val_batchsize:
|
231
|
-
for k, x in enumerate(val_batch_pool):
|
232
|
-
val_x_batch[k] = x.get()
|
233
|
-
data_q.put((val_x_batch.copy(), val_y_batch.copy()))
|
234
|
-
j = 0
|
235
|
-
data_q.put('train')
|
236
|
-
|
237
|
-
optimizer.lr *= 0.97
|
238
|
-
pool.close()
|
239
|
-
pool.join()
|
240
|
-
data_q.put('end')
|
241
|
-
|
3
エラー箇所を分かりやすくした
test
CHANGED
File without changes
|
test
CHANGED
@@ -101,7 +101,7 @@
|
|
101
101
|
denominator = 100000
|
102
102
|
|
103
103
|
|
104
|
-
def load_image_list(path, root):
|
104
|
+
def load_image_list(path, root): **←**
|
105
105
|
tuples = []
|
106
106
|
for line in open(path):
|
107
107
|
pair = line.strip().split()
|
@@ -110,7 +110,7 @@
|
|
110
110
|
|
111
111
|
|
112
112
|
# Prepare dataset
|
113
|
-
train_list = load_image_list(args.train, args.root)
|
113
|
+
train_list = load_image_list(args.train, args.root) **←**
|
114
114
|
|
115
115
|
val_list = load_image_list(args.val, args.root)
|
116
116
|
|
2
題名の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
###
|
1
|
+
### Chainerでの機械学習
|
2
2
|
Chainerでの機械学習の実装中でしたが、コマンドプロンプトでpython train_imagenet.py -g 0 train.txt test.txt (2>&1 | tee log)と打ち込んだ際にエラーが出てしまいました。インターネットなどで調べましたがどうすればいいか分かりませんでした。
|
3
3
|
初歩的な問題だと思いますがどうかよろしくお願いします。
|
4
4
|
|
1
見にくい文章であったので見やすくなるように改良を加えた。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,36 +1,29 @@
|
|
1
1
|
### 前提
|
2
|
-
|
3
|
-
|
2
|
+
Chainerでの機械学習の実装中でしたが、コマンドプロンプトでpython train_imagenet.py -g 0 train.txt test.txt (2>&1 | tee log)と打ち込んだ際にエラーが出てしまいました。インターネットなどで調べましたがどうすればいいか分かりませんでした。
|
4
|
-
(例)
|
5
|
-
|
3
|
+
初歩的な問題だと思いますがどうかよろしくお願いします。
|
4
|
+
|
6
|
-
|
5
|
+
※文字数の都合上後半のコードを省いてあります。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
7
|
-
|
12
|
+
```python
|
8
|
-
|
9
13
|
エラーメッセージ
|
10
|
-
```
|
11
|
-
|
14
|
+
File "train_imagenet.py", line 96, in <module>
|
12
15
|
train_list = load_image_list(args.train, args.root)
|
13
16
|
File "train_imagenet.py", line 89, in load_image_list
|
14
17
|
for line in open(path):
|
15
18
|
TypeError: invalid file: None
|
16
19
|
|
17
|
-
|
20
|
+
|
18
|
-
|
19
|
-
|
21
|
+
|
22
|
+
|
23
|
+
|
20
24
|
ソースコード
|
21
|
-
```
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#!/usr/bin/env python
|
26
|
-
"""Example code of learning a large scale convnet from ILSVRC2012 dataset.
|
27
|
-
|
28
|
-
Prerequisite: To run this example, crop the center of ILSVRC2012 training and
|
29
|
-
validation images and scale them to 256x256, and make two lists of space-
|
30
|
-
separated CSV whose first column is full path to image and second column is
|
31
|
-
zero-origin label (this format is same as that used by Caffe's ImageDataLayer).
|
32
|
-
|
33
|
-
|
25
|
+
train_imagenet.py
|
26
|
+
|
34
27
|
from __future__ import print_function
|
35
28
|
import argparse
|
36
29
|
import datetime
|
@@ -246,5 +239,3 @@
|
|
246
239
|
pool.join()
|
247
240
|
data_q.put('end')
|
248
241
|
|
249
|
-
|
250
|
-
|