pythonで関数を扱っています。
引数が一つしかいらないはずの関数に対して、以下のように関数に値を代入するとエラーがでてしまします。原因を教えてもらえればと思います
infer_netのcallはselfとxしか引数として取らないはずなのに何故、引数が一つだけではエラーがでるんでしょうか…
環境は
python2.7.12
chainer 4.1.0
CuPy 4.1.0
python
1#coding:UTF-8 2import os 3import numpy as np 4from PIL import Image 5import matplotlib 6matplotlib.use('Agg') 7import matplotlib.pyplot as plt 8import chainer 9from chainer import cuda,Function,report,training,utils,Variable 10from chainer import datasets,iterators,optimizers,serializers,cuda 11from chainer import Link,Chain,ChainList 12import chainer.functions as F 13import chainer.links as L 14from chainer.training import extensions 15import cupy 16 17 18class MyChain(Chain): 19 def __init__(self): 20 super(MyChain,self).__init__( 21 cn1 = L.Convolution2D(3,32,5,stride=1,pad=0), 22 cn2 = L.Convolution2D(32,48,5,stride=1,pad=0), 23 cn3 = L.Convolution2D(48,64,5,stride=1,pad=0), 24 cn4 = L.Convolution2D(64,80,4,stride=1,pad=0), 25 l1 = L.Linear(720,500), 26 l2 = L.Linear(500,2), 27 bn4 = L.BatchNormalization(500) 28 ) 29 30 def __call__(self,x): 31 h1 = F.max_pooling_2d(F.relu(self.cn1(x)),ksize=2) 32 h2 = F.max_pooling_2d(F.relu(self.cn2(h1)),ksize=2) 33 h3 = F.max_pooling_2d(F.relu(self.cn3(h2)),ksize=2) 34 h4 = F.max_pooling_2d(F.relu(self.cn4(h3)),ksize=2) 35 h5 = F.relu(self.bn4(self.l1(h4))) 36 37 return self.l2(h5) 38 39 40test_files = 'tes_5904e.txt' 41test_dataset = chainer.datasets.LabeledImageDataset(test_files) 42 43batchsize = 123 44test_iter = chainer.iterators.SerialIterator(test_dataset,batchsize,repeat=False,shuffle=False) 45 46infer_net = L.Classifier(MyChain()) 47serializers.load_npz('result_validation/snapshot_epoch-100',infer_net,path='updataer/model:main/predictor/',strict=False) 48 49x,t = test_dataset[0] 50print("xshape:"+str(x.shape)) 51plt.imshow(x.reshape(100,100,3),cmap = 'gray',vmin = 0,vmax = 255,interpolation = 'none') 52plt.show() 53plt.savefig('validation.png') 54print("xshape:"+str(x.shape)) 55d = infer_net.xp.asarray(x[None, ...]) 56with chainer.using_config('train',False), chainer.using_config('enable_backdrop',False): 57 y = infer_net(x) #ここでエラーが出る 58y = to_cpu(y.array) 59 60print("予測ラベル:",y.argmax(axis=1)[0]) 61
実行結果は以下の通り
xshape:(3, 100, 100)
xshape:(3, 100, 100)
Traceback (most recent call last):
File "chain_tes.py", line 71, in <module>
y = infer_net(x)
File "/usr/local/lib/python2.7/dist-packages/chainer/links/model/classifier.py", line 114, in call
self.y = self.predictor(*args, **kwargs)
TypeError: call() takes exactly 2 arguments (1 given)
あなたの回答
tips
プレビュー