2017-03-22 3 views
0

:Chainer, 내 모델에서 이미지의 ** 배열 **을 기대하는 이유는 무엇입니까? 내가 아주 간단한 장난감, 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). 나는 을 볼 수 없다. 이유는이다. 또는 단일 이미지를 허용하는 체인저 모델을 작성하는 방법이 도움이됩니다. 내가 얻는 오류는 다음과 같습니다.

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), 인쇄 (image [0] .shape) 및 인쇄 (image [0 :]. 모양) 결과를 알려주십시오. 또한 체인어 버전을 알고 싶습니다.

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 
+0

에 여분의 차원을 추가 할 수 있습니다. 하지만 내 질문은 : 당신의 샘플과 함께, 왜 y = 모델 (이미지 [0]) 작동하지? Chainer는 이미지가 필요한 배열을 필요로하는 것 같습니다. – Jeffrey

+0

체인 기능 및 레이어의 대부분은 입력 배열의 첫 번째 차원이 미니 배치의 차원이어야합니다. 디컨 볼 루션 층의 입력 배열의 형태는 (N, c, h, w)이어야한다. N = 미니 배치 크기, c = 채널 크기, h = 이미지 높이, w = 이미지 폭. 1 개의 이미지를 입력하고 싶다면 1 개의 이미지를 포함한 미니 배치를 구성해야합니다. – fukatani

+0

좋습니다. 대회는 미니 배치를 받고, 추가 치수는 미니 배치입니다. 공정하다. – Jeffrey