googlecolab上でChainerを用いて224枚のシカの画像を学習させ、その学習データを生成するプログラムを作成しているのですが、おそらく、L.Convolution2DとL.linearの使い方がいまいちわからず、不適切な値を入れているせいで以下のエラーが出てしまいます。どうしたらよろしいでしょうか。
エラー文
Exception in main training loop: index 218 is out of bounds for axis 0 with size 3
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/chainer/training/trainer.py", line 343, in run
update()
File "/usr/local/lib/python3.6/dist-packages/chainer/training/updaters/standard_updater.py", line 240, in update
self.update_core()
File "/usr/local/lib/python3.6/dist-packages/chainer/training/updaters/standard_updater.py", line 253, in update_core
optimizer.update(loss_func, *in_arrays)
File "/usr/local/lib/python3.6/dist-packages/chainer/optimizer.py", line 874, in update
loss = lossfun(*args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/chainer/link.py", line 287, in call
out = forward(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/chainer/links/model/classifier.py", line 144, in forward
self.loss = self.lossfun(self.y, t)
File "/usr/local/lib/python3.6/dist-packages/chainer/functions/loss/softmax_cross_entropy.py", line 556, in softmax_cross_entropy
loss, = func.apply((x, t))
File "/usr/local/lib/python3.6/dist-packages/chainer/function_node.py", line 334, in apply
outputs = self.forward(in_data)
File "/usr/local/lib/python3.6/dist-packages/chainer/function_node.py", line 592, in forward
return self.forward_cpu(inputs)
File "/usr/local/lib/python3.6/dist-packages/chainer/functions/loss/softmax_cross_entropy.py", line 162, in forward_cpu
log_p = log_yd[t.ravel(), numpy.arange(t.size)]
Will finalize trainer extensions and updater before reraising the exception.
IndexError Traceback (most recent call last)
<ipython-input-1-3bf3422f7400> in <module>()
81 trainer.extend(extensions.PlotReport(['main/loss'],'epoch',file_name='loss.png'))
82 trainer.extend(extensions.PlotReport(['main/accuracy'],'epoch',file_name='accuracy.png'))
---> 83 trainer.run()
84
85 serializer.save_npz("mymodel.npz",model)
/usr/local/lib/python3.6/dist-packages/chainer/functions/loss/softmax_cross_entropy.py in forward_cpu(self, inputs)
160 t_valid = t != self.ignore_label
161 t = t * t_valid
--> 162 log_p = log_yd[t.ravel(), numpy.arange(t.size)]
163
164 log_p *= t_valid.ravel()
IndexError: index 218 is out of bounds for axis 0 with size 3
コード
python
1import chainer 2import glob 3from itertools import chain 4from chainer.datasets import LabeledImageDataset 5from chainer import iterators,training,optimizers,datasets,serializers 6from chainer.training import extensions,triggers 7from chainer.dataset import concat_examples 8import chainer.functions as F 9import chainer.links as L 10 11#-----MyChain----- 12 13class MyChain(chainer.Chain): 14 15 def __init__(self): 16 super(MyChain,self).__init__() 17 with self.init_scope(): 18 self.conv1 = L.Convolution2D(None,16,3,pad=2) 19 self.conv2 = L.Convolution2D(None,32,3,pad=2) 20 self.l3 = L.Linear(None,256) 21 self.l4 = L.Linear(None,3) 22 23 def __call__(self,x): 24 h = F.max_pooling_2d(F.relu(self.conv1(x)),ksize=3,stride=2,pad=2) 25 h = F.max_pooling_2d(F.relu(self.conv2(x)),ksize=3,stride=2,pad=2) 26 h = F.dropout(F.relu(self.l3(h))) 27 y = self.l4(h) 28 return y 29 30 31#-----img----- 32 33train_path = glob.glob('img/Deers/Deers_train/*') 34 35tr = 0 36label = [] 37for index,item in enumerate(train_path): 38 label.append(tr) 39 tr = tr+1 40 41pa_tuple = tuple(train_path) 42la_tuple = tuple(label) 43 44train_data = [(0,0)] * len(label) 45 46tr = 0 47for index,item in enumerate(train_path): 48 train_data[tr] = [train_path[tr],label[tr]] 49 tr = tr + 1 50 51dataset_train = chainer.datasets.LabeledImageDataset(train_data) 52 53from chainercv.transforms import resize 54from chainer.datasets import TransformDataset 55 56def transform(data): 57 img,label = data 58 img = resize(img,(500,500)) 59 return img,label 60 61deerset_train = chainer.datasets.TransformDataset(dataset_train,transform) 62 63#-----train----- 64 65epoch = 10 66batch = 5 67 68model = L.Classifier(MyChain()) 69optimizer = optimizers.Adam() 70optimizer.setup(model) 71 72train_iter = iterators.SerialIterator(deerset_train,batch) 73updater = training.StandardUpdater(train_iter,optimizer) 74trainer = training.Trainer(updater,(epoch,'epoch'),out='result') 75 76trainer.extend(extensions.dump_graph('main/loss')) 77trainer.extend(extensions.snapshot(),trigger=(epoch,'epoch')) 78trainer.extend(extensions.LogReport()) 79trainer.extend(extensions.PrintReport(['epoch','main/loss','main/accuracy'])) 80trainer.extend(extensions.ProgressBar()) 81trainer.extend(extensions.PlotReport(['main/loss'],'epoch',file_name='loss.png')) 82trainer.extend(extensions.PlotReport(['main/accuracy'],'epoch',file_name='accuracy.png')) 83trainer.run() 84 85serializer.save_npz("mymodel.npz",model) 86
回答1件
あなたの回答
tips
プレビュー