我想在链接器中训练Convolution3d模型。在我的训练队伍中,我遇到此错误。
TypeError: numpy.ndarray or cuda.ndarray are expected.
我认为错误的原因是输入是一个列表。
因此,我将输入数组更改为numpy数组。但是我有同样的错误。
这是火车代码。
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
这是装载机功能
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])
这是模特
class conv_3d(Chain):
def __init__(self):
super(conv_3d, self).__init__()
with self.init_scope():
self.conv1 = L.Convolution3D(None,out_channels=3, ksize=3, stride=1, pad=1)
def __call__(self,x):
return F.relu(self.conv1)
我希望火车能上班,但上面有错误。
最佳答案
F.relu(self.conv1)
应该固定为F.relu(self.conv1(x))
。您可能还需要将输入发送到GPU。