質問編集履歴

4

コードの統一

2020/02/24 11:57

投稿

pontyo
pontyo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -28,8 +28,6 @@
28
28
 
29
29
  img = None
30
30
 
31
- label = []
32
-
33
31
  for i in range(len(old_train)):
34
32
 
35
33
  if np.isin(old_train[i][1], mask_list):
@@ -60,8 +58,6 @@
60
58
 
61
59
  img = None
62
60
 
63
- label = []
64
-
65
61
  for i in range(len(old_test)):
66
62
 
67
63
  if np.isin(old_test[i][1], mask_list):
@@ -70,266 +66,258 @@
70
66
 
71
67
  img = old_train[i][0]
72
68
 
73
- #label = old_train[i][1]
69
+ label = old_train[i][1]
74
70
 
75
71
  count += 1
76
72
 
77
73
  else:
78
74
 
75
+ img = np.vstack((img, old_test[i][0]))
76
+
77
+ label = np.hstack((label, old_test[i][1]))
78
+
79
+
80
+
81
+ test = tuple_dataset.TupleDataset(img, label)
82
+
83
+ ```
84
+
85
+ 学習部分も含めたコードは以下になります.
86
+
87
+ ```python
88
+
89
+ def main():
90
+
91
+ gane = [64, 64, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512]
92
+
93
+
94
+
95
+ # 抽出するデータラベル
96
+
97
+ mask_list = [4, 30, 55, 72, 95, 1, 32, 67, 73, 91]
98
+
99
+
100
+
101
+ batchsize = 32
102
+
103
+ epoch = 300
104
+
105
+ learnrate = 0.05
106
+
107
+ gpu = 0
108
+
109
+ out = "result"
110
+
111
+
112
+
113
+ print('Using CIFAR100 dataset.')
114
+
115
+ # クラス数を設定
116
+
117
+ class_labels = len(mask_list)
118
+
119
+ # cifar100を全データ格納
120
+
121
+ old_train, old_test = get_cifar100()
122
+
123
+
124
+
125
+ # 学習用データを抽出
126
+
127
+ count = 0
128
+
129
+ img = None
130
+
131
+ for i in range(len(old_train)):
132
+
133
+ if np.isin(old_train[i][1], mask_list):
134
+
135
+ if count == 0:
136
+
137
+ img = old_train[i][0]
138
+
139
+ label = old_train[i][1]
140
+
141
+ count += 1
142
+
143
+ else:
144
+
79
145
  img = np.vstack((img, old_train[i][0]))
80
146
 
81
- #label = np.hstack((label, old_train[i][1]))
147
+ label = np.hstack((label, old_train[i][1]))
148
+
149
+
150
+
82
-
151
+ train = tuple_dataset.TupleDataset(img, label)
152
+
153
+
154
+
155
+ # 評価用データを抽出
156
+
157
+ count = 0
158
+
159
+ img = None
160
+
161
+ for i in range(len(old_test)):
162
+
163
+ if np.isin(old_test[i][1], mask_list):
164
+
165
+ if count == 0:
166
+
167
+ img = old_train[i][0]
168
+
83
- label += old_train[i][1]
169
+ label = old_train[i][1]
170
+
171
+ count += 1
172
+
173
+ else:
174
+
175
+ img = np.vstack((img, old_test[i][0]))
176
+
177
+ label = np.hstack((label, old_test[i][1]))
84
178
 
85
179
 
86
180
 
87
181
  test = tuple_dataset.TupleDataset(img, label)
88
182
 
183
+
184
+
185
+ model = L.Classifier(VGG(gane, class_labels))
186
+
187
+
188
+
189
+ if gpu >= 0:
190
+
191
+ # Make a specified GPU current
192
+
193
+ chainer.cuda.get_device_from_id(gpu).use()
194
+
195
+ model.to_gpu() # Copy the model to the GPU
196
+
197
+
198
+
199
+ optimizer = chainer.optimizers.MomentumSGD(learnrate)
200
+
201
+ optimizer.setup(model)
202
+
203
+ optimizer.add_hook(chainer.optimizer.WeightDecay(5e-4))
204
+
205
+
206
+
207
+ train_iter = chainer.iterators.SerialIterator(train, batchsize)
208
+
209
+ test_iter = chainer.iterators.SerialIterator(test, batchsize,
210
+
211
+ repeat=False, shuffle=False)
212
+
213
+ # Set up a trainer
214
+
215
+ updater = training.StandardUpdater(train_iter, optimizer, device=gpu)
216
+
217
+ trainer = training.Trainer(updater, (epoch, 'epoch'), out=out)
218
+
219
+
220
+
221
+ # Evaluate the model with the test dataset for each epoch
222
+
223
+ trainer.extend(extensions.Evaluator(test_iter, model, device=gpu))
224
+
225
+
226
+
227
+ # Reduce the learning rate by half every 25 epochs.
228
+
229
+ trainer.extend(extensions.ExponentialShift('lr', 0.5),
230
+
231
+ trigger=(25, 'epoch'))
232
+
233
+
234
+
235
+ # Dump a computational graph from 'loss' variable at the first iteration
236
+
237
+ # The "main" refers to the target link of the "main" optimizer.
238
+
239
+ trainer.extend(extensions.dump_graph('main/loss'))
240
+
241
+
242
+
243
+ # Take a snapshot at each epoch
244
+
245
+ trainer.extend(extensions.snapshot(), trigger=(epoch, 'epoch'))
246
+
247
+
248
+
249
+ # Write a log of evaluation statistics for each epoch
250
+
251
+ trainer.extend(extensions.LogReport())
252
+
253
+
254
+
255
+ trainer.extend(extensions.PrintReport(
256
+
257
+ ['epoch', 'main/loss', 'validation/main/loss',
258
+
259
+ 'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
260
+
261
+
262
+
263
+ trainer.run()
264
+
265
+
266
+
267
+ if __name__ == '__main__':
268
+
269
+ main()
270
+
89
271
  ```
90
272
 
273
+
274
+
91
- 学習部分も含めたコは以下になります.
275
+ エラは以下になります.
92
-
93
- ```python
94
-
95
- def main():
96
-
97
- gane = [64, 64, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512]
98
-
99
-
100
-
101
- # 抽出するデータラベル
102
-
103
- mask_list = [4, 30, 55, 72, 95, 1, 32, 67, 73, 91]
104
-
105
-
106
-
107
- batchsize = 32
108
-
109
- epoch = 300
110
-
111
- learnrate = 0.05
112
-
113
- gpu = 0
114
-
115
- out = "result"
116
-
117
-
118
-
119
- print('Using CIFAR100 dataset.')
120
-
121
- # クラス数を設定
122
-
123
- class_labels = len(mask_list)
124
-
125
- # cifar100を全データ格納
126
-
127
- old_train, old_test = get_cifar100()
128
-
129
-
130
-
131
- # 学習用データを抽出
132
-
133
- count = 0
134
-
135
- img = None
136
-
137
- label = []
138
-
139
- for i in range(len(old_train)):
140
-
141
- if np.isin(old_train[i][1], mask_list):
142
-
143
- if count == 0:
144
-
145
- img = old_train[i][0]
146
-
147
- label = old_train[i][1]
148
-
149
- count += 1
150
-
151
- else:
152
-
153
- img = np.vstack((img, old_train[i][0]))
154
-
155
- label = np.hstack((label, old_train[i][1]))
156
-
157
-
158
-
159
- train = tuple_dataset.TupleDataset(img, label)
160
-
161
-
162
-
163
- # 評価用データを抽出
164
-
165
- count = 0
166
-
167
- img = None
168
-
169
- label = []
170
-
171
- for i in range(len(old_test)):
172
-
173
- if np.isin(old_test[i][1], mask_list):
174
-
175
- if count == 0:
176
-
177
- img = old_train[i][0]
178
-
179
- #label = old_train[i][1]
180
-
181
- count += 1
182
-
183
- else:
184
-
185
- img = np.vstack((img, old_train[i][0]))
186
-
187
- #label = np.hstack((label, old_train[i][1]))
188
-
189
- label += old_train[i][1]
190
-
191
-
192
-
193
- test = tuple_dataset.TupleDataset(img, label)
194
-
195
-
196
-
197
- model = L.Classifier(VGG(gane, class_labels))
198
-
199
-
200
-
201
- if gpu >= 0:
202
-
203
- # Make a specified GPU current
204
-
205
- chainer.cuda.get_device_from_id(gpu).use()
206
-
207
- model.to_gpu() # Copy the model to the GPU
208
-
209
-
210
-
211
- optimizer = chainer.optimizers.MomentumSGD(learnrate)
212
-
213
- optimizer.setup(model)
214
-
215
- optimizer.add_hook(chainer.optimizer.WeightDecay(5e-4))
216
-
217
-
218
-
219
- train_iter = chainer.iterators.SerialIterator(train, batchsize)
220
-
221
- test_iter = chainer.iterators.SerialIterator(test, batchsize,
222
-
223
- repeat=False, shuffle=False)
224
-
225
- # Set up a trainer
226
-
227
- updater = training.StandardUpdater(train_iter, optimizer, device=gpu)
228
-
229
- trainer = training.Trainer(updater, (epoch, 'epoch'), out=out)
230
-
231
-
232
-
233
- # Evaluate the model with the test dataset for each epoch
234
-
235
- trainer.extend(extensions.Evaluator(test_iter, model, device=gpu))
236
-
237
-
238
-
239
- # Reduce the learning rate by half every 25 epochs.
240
-
241
- trainer.extend(extensions.ExponentialShift('lr', 0.5),
242
-
243
- trigger=(25, 'epoch'))
244
-
245
-
246
-
247
- # Dump a computational graph from 'loss' variable at the first iteration
248
-
249
- # The "main" refers to the target link of the "main" optimizer.
250
-
251
- trainer.extend(extensions.dump_graph('main/loss'))
252
-
253
-
254
-
255
- # Take a snapshot at each epoch
256
-
257
- trainer.extend(extensions.snapshot(), trigger=(epoch, 'epoch'))
258
-
259
-
260
-
261
- # Write a log of evaluation statistics for each epoch
262
-
263
- trainer.extend(extensions.LogReport())
264
-
265
-
266
-
267
- trainer.extend(extensions.PrintReport(
268
-
269
- ['epoch', 'main/loss', 'validation/main/loss',
270
-
271
- 'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
272
-
273
-
274
-
275
- trainer.run()
276
-
277
-
278
-
279
- if __name__ == '__main__':
280
-
281
- main()
282
276
 
283
277
  ```
284
278
 
285
-
279
+ ---------------------------------------------------------------------------
280
+
286
-
281
+ ValueError Traceback (most recent call last)
282
+
283
+ <ipython-input-4-eb3fae69fa84> in <module>()
284
+
285
+ 224
286
+
287
+ 225 if __name__ == '__main__':
288
+
289
+ --> 226 main()
290
+
291
+
292
+
293
+ <ipython-input-4-eb3fae69fa84> in main()
294
+
295
+ 145 print(len(label))
296
+
297
+ 146 print(label.ndim)
298
+
299
+ --> 147 train = tuple_dataset.TupleDataset(img, label)
300
+
301
+ 148
302
+
287
- エラー文は以下になります.
303
+ 149 count = 0
304
+
305
+
306
+
307
+ /usr/local/lib/python3.6/dist-packages/chainer/datasets/tuple_dataset.py in __init__(self, *datasets)
308
+
309
+ 35 if len(dataset) != length:
310
+
311
+ 36 raise ValueError(
312
+
313
+ ---> 37 'dataset of the index {} has a wrong length'.format(i))
314
+
315
+ 38 self._datasets = datasets
316
+
317
+ 39 self._length = length
318
+
319
+
320
+
321
+ ValueError: dataset of the index 1 has a wrong length
288
322
 
289
323
  ```
290
-
291
- ---------------------------------------------------------------------------
292
-
293
- ValueError Traceback (most recent call last)
294
-
295
- <ipython-input-4-eb3fae69fa84> in <module>()
296
-
297
- 224
298
-
299
- 225 if __name__ == '__main__':
300
-
301
- --> 226 main()
302
-
303
-
304
-
305
- <ipython-input-4-eb3fae69fa84> in main()
306
-
307
- 145 print(len(label))
308
-
309
- 146 print(label.ndim)
310
-
311
- --> 147 train = tuple_dataset.TupleDataset(img, label)
312
-
313
- 148
314
-
315
- 149 count = 0
316
-
317
-
318
-
319
- /usr/local/lib/python3.6/dist-packages/chainer/datasets/tuple_dataset.py in __init__(self, *datasets)
320
-
321
- 35 if len(dataset) != length:
322
-
323
- 36 raise ValueError(
324
-
325
- ---> 37 'dataset of the index {} has a wrong length'.format(i))
326
-
327
- 38 self._datasets = datasets
328
-
329
- 39 self._length = length
330
-
331
-
332
-
333
- ValueError: dataset of the index 1 has a wrong length
334
-
335
- ```

3

エラー文の追加

2020/02/24 11:57

投稿

pontyo
pontyo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -292,17 +292,29 @@
292
292
 
293
293
  ValueError Traceback (most recent call last)
294
294
 
295
- <ipython-input-1-b519d4e5f5fa> in <module>()
295
+ <ipython-input-4-eb3fae69fa84> in <module>()
296
-
296
+
297
- 223
297
+ 224
298
-
298
+
299
- 224 if __name__ == '__main__':
299
+ 225 if __name__ == '__main__':
300
-
300
+
301
- --> 225 main()
301
+ --> 226 main()
302
+
303
+
304
+
302
-
305
+ <ipython-input-4-eb3fae69fa84> in main()
303
-
304
-
306
+
305
- 1 frames
307
+ 145 print(len(label))
308
+
309
+ 146 print(label.ndim)
310
+
311
+ --> 147 train = tuple_dataset.TupleDataset(img, label)
312
+
313
+ 148
314
+
315
+ 149 count = 0
316
+
317
+
306
318
 
307
319
  /usr/local/lib/python3.6/dist-packages/chainer/datasets/tuple_dataset.py in __init__(self, *datasets)
308
320
 

2

エラー文全体を表示

2020/02/24 11:51

投稿

pontyo
pontyo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -288,6 +288,22 @@
288
288
 
289
289
  ```
290
290
 
291
+ ---------------------------------------------------------------------------
292
+
293
+ ValueError Traceback (most recent call last)
294
+
295
+ <ipython-input-1-b519d4e5f5fa> in <module>()
296
+
297
+ 223
298
+
299
+ 224 if __name__ == '__main__':
300
+
301
+ --> 225 main()
302
+
303
+
304
+
305
+ 1 frames
306
+
291
307
  /usr/local/lib/python3.6/dist-packages/chainer/datasets/tuple_dataset.py in __init__(self, *datasets)
292
308
 
293
309
  35 if len(dataset) != length:

1

情報追加

2020/02/23 12:11

投稿

pontyo
pontyo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -6,8 +6,92 @@
6
6
 
7
7
 
8
8
 
9
+ データ抽出部分のみのコードは以下になります.
10
+
9
11
  ```python
10
12
 
13
+ # 抽出するデータラベル
14
+
15
+ mask_list = [4, 30, 55, 72, 95, 1, 32, 67, 73, 91]
16
+
17
+
18
+
19
+ # cifar100を全データ格納
20
+
21
+ old_train, old_test = get_cifar100()
22
+
23
+
24
+
25
+ # 学習用データを抽出
26
+
27
+ count = 0
28
+
29
+ img = None
30
+
31
+ label = []
32
+
33
+ for i in range(len(old_train)):
34
+
35
+ if np.isin(old_train[i][1], mask_list):
36
+
37
+ if count == 0:
38
+
39
+ img = old_train[i][0]
40
+
41
+ label = old_train[i][1]
42
+
43
+ count += 1
44
+
45
+ else:
46
+
47
+ img = np.vstack((img, old_train[i][0]))
48
+
49
+ label = np.hstack((label, old_train[i][1]))
50
+
51
+
52
+
53
+ train = tuple_dataset.TupleDataset(img, label)
54
+
55
+
56
+
57
+ # 評価用データを抽出
58
+
59
+ count = 0
60
+
61
+ img = None
62
+
63
+ label = []
64
+
65
+ for i in range(len(old_test)):
66
+
67
+ if np.isin(old_test[i][1], mask_list):
68
+
69
+ if count == 0:
70
+
71
+ img = old_train[i][0]
72
+
73
+ #label = old_train[i][1]
74
+
75
+ count += 1
76
+
77
+ else:
78
+
79
+ img = np.vstack((img, old_train[i][0]))
80
+
81
+ #label = np.hstack((label, old_train[i][1]))
82
+
83
+ label += old_train[i][1]
84
+
85
+
86
+
87
+ test = tuple_dataset.TupleDataset(img, label)
88
+
89
+ ```
90
+
91
+ 学習部分も含めたコードは以下になります.
92
+
93
+ ```python
94
+
11
95
  def main():
12
96
 
13
97
  gane = [64, 64, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512]