chainerのモデルに入力する変数について
TypeError: numpy.ndarray or cuda.ndarray are expected.
chainerにおいて自分でトレーニングループを書いて実行したのですが、
modelに代入する段階でエラーがおきます。
以下にエラーを示します。
TypeError Traceback (most recent call last)
<ipython-input-131-22fce9e330d5> in <module>
32 target_movie = target_movie
33
---> 34 prediction_train = model(input_movie)
35 loss = F.mean_squared_error(prediction_train,target_movie)
36
<ipython-input-125-12b6f13f8eed> in __call__(self, x)
5 self.conv1 = L.Convolution3D(None,out_channels=3, ksize=3, stride=1, pad=1)
6 def __call__(self,x):
----> 7 return F.relu(self.conv1)
~/.local/lib/python3.6/site-packages/chainer/functions/activation/relu.py in relu(x)
174
175 """
--> 176 y, = ReLU().apply((x,))
177 return y
~/.local/lib/python3.6/site-packages/chainer/function_node.py in apply(self, inputs)
224
225 """
--> 226 input_vars = [chainer.as_variable(x) for x in inputs]
227 in_data = tuple([x.data for x in input_vars])
228 requires_grad = any([x.requires_grad for x in input_vars])
~/.local/lib/python3.6/site-packages/chainer/function_node.py in <listcomp>(.0)
224
225 """
--> 226 input_vars = [chainer.as_variable(x) for x in inputs]
227 in_data = tuple([x.data for x in input_vars])
228 requires_grad = any([x.requires_grad for x in input_vars])
~/.local/lib/python3.6/site-packages/chainer/variable.py in as_variable(obj)
1373 if isinstance(obj, Variable):
1374 return obj
-> 1375 return Variable(obj, requires_grad=False)
1376
1377
~/.local/lib/python3.6/site-packages/chainer/variable.py in __init__(self, data, **kwargs)
503 msg = '''numpy.ndarray or cuda.ndarray are expected.
504 Actual: {0}'''.format(type(data))
--> 505 raise TypeError(msg)
506
507 # Use a list as a data structure to hold the data array indirectly to
TypeError: numpy.ndarray or cuda.ndarray are expected.
Actual: <class 'chainer.links.connection.convolution_nd.Convolution3D'>
該当部分のコードを示します。
model = conv_3d()
model.to_gpu(0)
optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
optimizer.setup(model)
max_epoch = 100
batch_size = 50
epoch_idx = 0
while epoch_idx < max_epoch:
train_path = random.sample(train_path, len(train_path))
train_losses = []
for i in range(int(len(train_path) // batch_size)):
batch = train_path[i * batch_size: (i+1) * batch_size]
input_movie, target_movie = loader(batch)
prediction_train = model(input_movie)
loss = F.mean_squared_error(prediction_train,target_movie)
train_losses.append(to_cpu(loss.array))
model.cleargrads()
loss.backward()
optimizer.update()
print('epoch:{:03d} train_loss:{:.04f} '.format(epoch_idx + 1, np.mean(train_losses)), end='')
test_losses = []
for test_batch in range(len(validation)//batch_size):
batch = validation[test_batch * batch_size:(test_batch + 1) * 50]
validation_input_movie, validation_target_movie = loader(batch)
prediction_validation = model(validation_input_movie)
loss_validation = F.mean_squared_error(prediction_validation,validation_target_movie)
test_losses.append(to_cpu(loss_test.array))
print('val_loss:{:.04f}'.format(
np.mean(test_losses)))
epoch_idx += 1
また、loader関数は
def loader(path_list):
input_movie = [i[0] for i in path_list]
target_movie = [i[1] for i in path_list]
input_movie = np.asarray([[np.asarray(cv2.resize(cv2.imread("../image/" + img),(1024//10,780//10))) for img in img_path] for img_path in input_movie])
target_movie = np.asarray([[np.asarray(cv2.resize(cv2.imread("../image/" + img),(1024//10,780//10))) for img in img_path] for img_path in target_movie])
return tuple([input_movie,target_movie])
です。
エラーが出てから、numpyの配列にしてから入力しているのですが、うまく行きません。
ご教授のほどよろしくお願いいたします。