前提・実現したいこと
ここに質問の内容を詳しく書いてください。
(例)PHP(C
発生している問題・エラーメッセージ
PythonでChainerを用いて画像の判別をするプログラムを作っているのですが、プログラムを実行すると ValueError : too many value to unpack (expected 2) とエラーが出てしまいます。
おそらくnet.pyで行っているデータセットの作り方に問題があるのかと思いますが、対処法がわかりません。
情報が不足しているというご指摘がありましたので、下にエラーメッセージの全文を追加しました。
Exception in main training loop: too many values to unpack (expected 2)
Traceback (most recent call last):
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/trainer.py", line 343, in run
update()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/updaters/standard_updater.py", line 240, in update
self.update_core()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/updaters/standard_updater.py", line 245, in update_core
batch = iterator.next()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/iterators/serial_iterator.py", line 77, in next
batch = [self.dataset[index] for index in indices]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/iterators/serial_iterator.py", line 77, in <listcomp>
batch = [self.dataset[index] for index in indices]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/dataset/dataset_mixin.py", line 67, in getitem
return self.get_example(index)
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/datasets/transform_dataset.py", line 51, in get_example
in_data = self._dataset[i]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/dataset/dataset_mixin.py", line 67, in getitem
return self.get_example(index)
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/datasets/image_dataset.py", line 158, in get_example
path, int_label = self._pairs[i]
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "net.py", line 56, in <module>
trainer.run()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/trainer.py", line 376, in run
six.reraise(*exc_info)
File "/usr/lib/python3/dist-packages/six.py", line 703, in reraise
raise value
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/trainer.py", line 343, in run
update()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/updaters/standard_updater.py", line 240, in update
self.update_core()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/training/updaters/standard_updater.py", line 245, in update_core
batch = iterator.next()
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/iterators/serial_iterator.py", line 77, in next
batch = [self.dataset[index] for index in indices]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/iterators/serial_iterator.py", line 77, in <listcomp>
batch = [self.dataset[index] for index in indices]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/dataset/dataset_mixin.py", line 67, in getitem
return self.get_example(index)
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/datasets/transform_dataset.py", line 51, in get_example
in_data = self._dataset[i]
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/dataset/dataset_mixin.py", line 67, in getitem
return self.get_example(index)
File "/home/bigfoot/.local/lib/python3.8/site-packages/chainer/datasets/image_dataset.py", line 158, in get_example
path, int_label = self._pairs[i]
ValueError: too many values to unpack (expected 2)
該当のソースコード
python
1---my_chain_model.py--- 2import chainer 3import chainer.functions as F 4import chainer.links as L 5 6class MyChain(chainer.Chain): 7 8 def __init__(self): 9 super(MyChain,self).__init__() 10 with self.init_scope(): 11 self.conv1 = L.Convolution2D(None,16,5,pad=2) 12 self.conv2 = L.Convolution2D(None,32,5,pad=2) 13 self.l3 = L.Linear(None,256) 14 self.l4 = L.Linear(None,5) 15 16 def __call__(self,x): 17 h = F.max_pooling_2d(F.relu(self.conv1(x)),ksize=5,stride=2,pad=2) 18 h = F.max_pooling_2d(F.relu(self.conv2(x)),ksize=5,stride=2,pad=2) 19 h = F.dropout(F.relu(self.l3(h))) 20 y = sef.l4(h) 21 return y 22 23---net.py--- 24import chainer 25import glob 26from itertools import chain 27from chainer.datasets import LabeledImageDataset 28from chainer import iterators,training,optimizers,datasets,serializers 29from chainer.training import extensions,triggers 30from chainer.dataset import concat_examples 31import chainer.function as F 32import chainer.links as L 33from my_chain_model import MyChain 34 35#img----------- 36 37train_path = glob.glob('img/Deers/Deers_train/*') 38 39tr = 0 40labels = list() 41for index,item in enumerate(train_path): 42 labels.append(tr) 43 tr = tr+1 44 45train_path_l = list(train_path) 46 47dataset_train = chainer.datasets.LabeledImageDataset(train_path_l,labels) 48 49from chainercv.transforms import resize 50from chainer.datasets import TransformDataset 51 52def transform(data): 53 img,label = data 54 img = resize(img,(500,500)) 55 return img,label 56 57deerset_train = chainer.datasets.TransformDataset(dataset_train,transform) 58 59#train--------- 60 61epoch = 10 62batch = 5 63 64model = L.Classifier(MyChain()) 65optimizer = optimizers.Adam() 66optimizer.setup(model) 67 68train_iter = iterators.SerialIterator(deerset_train,batch) 69updater = training.StandardUpdater(train_iter,optimizer) 70trainer = training.Trainer(updater,(epoch,'epoch'),out='result') 71 72trainer.extend(extensions.dump_graph('main/loss')) 73trainer.extend(extensions.snapshot(),trigger=(epoch,'epoch')) 74trainer.extend(extensions.LogReport()) 75trainer.extend(extensions.PrintReport(['epoch','main/loss','main/accuracy'])) 76trainer.extend(extensions.ProgressBar()) 77trainer.extend(extensions.PlotReport(['main/loss'],'epoch',file_name='loss.png')) 78trainer.extend(extensions.PlotReport(['main/accuracy'],'epoch',file_name='accuracy.png')) 79trainer.run() 80 81serializer.save_npz("mymodel.npz",model) 82 83
試したこと
ネットで対処法を調べたり、別の書き方を試した。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー