0

我写了一个非常简单的玩具链接器模型:

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)。我不明白这是为什么。或者,如何编写一个接受单个图像的链接器模型会有所帮助。我得到的错误是:

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
4

1 回答 1

0

在我的环境中,我确认以下代码可以正常工作。请告诉我 print(image.shape)、print(image[0].shape) 和 print(image[0:].shape) 的结果。我也想知道你的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
于 2017-03-25T01:15:34.987 回答