我写了一个非常简单的玩具,chainer模型:
class Upscale(chainer.Chain):
def __init__(self):
super(Upscale, self).__init__(
d1=L.Deconvolution2D(3, 40, 4, stride=2),
c1=L.Convolution2D(40, 3, 4)
)
def __call__(self, x, test=False):
h = self.c1(self.d1(x))
return h
我可以打电话给它,它似乎有效。但是,我必须将其称为:
model = Upscale()
...
xp = cuda.cupy
...
image = xp.zeros((1, 3, 768, 1024), dtype=xp.float32)
image[0] = load_image("foo.jpg", xp)
...
y = model(image[0:1])
作为参考,load_image是:
def load_image(path, xp):
image = Image.open(path).convert('RGB')
return xp.asarray(image, dtype=xp.float32).transpose(2, 0, 1)
我的模型将接受一个形状为(1,3,768,1024)但不是阵列形状(3,768,1024)的阵列。我没有看到为什么。或者,如何编写接受单个图像的chainer模型会有所帮助。我得到的错误是:
Traceback (most recent call last):
File "upscale.py", line 92, in <module>
main()
File "upscale.py", line 68, in main
y0 = model(image, test=True)
File "upscale.py", line 21, in __call__
h = self.c1(self.d1(x))
File "/usr/local/lib/python2.7/dist-packages/chainer/links/connection/deconvolution_2d.py", line 116, in __call__
deterministic=self.deterministic)
File "/usr/local/lib/python2.7/dist-packages/chainer/functions/connection/deconvolution_2d.py", line 332, in deconvolution_2d
return func(x, W, b)
File "/usr/local/lib/python2.7/dist-packages/chainer/function.py", line 189, in __call__
self._check_data_type_forward(in_data)
File "/usr/local/lib/python2.7/dist-packages/chainer/function.py", line 273, in _check_data_type_forward
self.check_type_forward(in_type)
File "/usr/local/lib/python2.7/dist-packages/chainer/functions/connection/deconvolution_2d.py", line 50, in check_type_forward
x_type.shape[1] == w_type.shape[0]
File "/usr/local/lib/python2.7/dist-packages/chainer/utils/type_check.py", line 487, in expect
expr.expect()
File "/usr/local/lib/python2.7/dist-packages/chainer/utils/type_check.py", line 449, in expect
'{0} {1} {2}'.format(left, self.inv, right))
chainer.utils.type_check.InvalidType:
Invalid operation is performed in: Deconvolution2DFunction (Forward)
Expect: in_types[0].ndim == 4
Actual: 3 != 4
答案 0 :(得分:0)
在我的环境中,我确认以下代码正常运行。 请告诉我打印结果(image.shape),打印(图像[0] .shape)和打印(图像[0:]。形状)。 我也想知道你的chainer版本。
import cupy
from PIL import Image
import chainer.links as L
import chainer
def load_image(path):
image = Image.open(path).convert('RGB')
return cupy.asarray(image, dtype=cupy.float32).transpose(2, 0, 1)
image = cupy.zeros((1, 3, 768, 1024), dtype=cupy.float32)
image[0] = load_image("foo.png")
print(image[0].shape) # (3, 768, 1024)
print(image.shape) # (1, 3, 768, 1024)
print(image[0:1].shape) # (1, 3, 768, 1024)
class Upscale(chainer.Chain):
def __init__(self):
super(Upscale, self).__init__(
d1=L.Deconvolution2D(3, 40, 4, stride=2),
c1=L.Convolution2D(40, 3, 4)
)
def __call__(self, x, test=False):
h = self.c1(self.d1(x))
return h
model = Upscale()
model.to_gpu()
y = model(image[0:1]) # work