max_pool2d() 得到了意外的关键字参数“ctx”。我能做什么?

问题描述 投票:0回答:1

我的代码:

def max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), padding=(0, 0), dilation=(1, 1)): batch_size, channels, height, width = x.shape
    
    Apply padding to the input tensor x_padded = np.pad(x, ((0, 0), (0, 0), (padding[0], padding[0]), (padding[1], padding[1])), mode='constant')
    
    Compute output dimensions after pooling out_height = (height + 2 * padding[0] - dilation[0] * (kernel_size[0] - 1) - 1) // stride[0] + 1 out_width = (width + 2 * padding[1] - dilation[1] * (kernel_size[1] - 1) - 1) // stride[1] + 1
    
    Initialize the output tensor y = np.zeros((batch_size, channels, out_height, out_width))
    
    Perform max pooling for i in range(out_height): for j in range(out_width): Extract the region of interest roi = x_padded[:, :, i * stride[0]:i * stride[0] + kernel_size[0], j * stride[1]:j * stride[1] + kernel_size[1]]
    
    Apply max pooling along height and width dimensions y[:, :, i, j] = np.max(roi, axis=(2, 3))
    
    return y
    
def max_pool2d_backward(y_grad, x, index, kernel_size, padding, stride, dilation=1): Calculate unfolded input shape unfolded_shape = (
         (x.shape[2] + 2 * padding[1] - dilation * (kernel_size[1] - 1) - 1) // stride[1] + 1,
         (x.shape[3] + 2 * padding[0] - dilation * (kernel_size[0] - 1) - 1) // stride[0] + 1)
    
    #Unfold the input using F.fold 
    unfolded_x = torch.nn.functional.unfold(x, kernel_size, dilation=dilation, padding=padding, stride=stride)
        
    #Reshape the unfolded input to get the windows 
    unfolded_x = unfolded_x.view(x.shape[0], x.shape[1], kernel_size[0] * kernel_size[1], unfolded_shape[0], unfolded_shape[1])
        
    #Create a tensor with zeros of the same shape as 
    unfolded_x grad_input = torch.zeros_like(unfolded_x)
        
    #Scatter the gradients to the correct locations using the index grad_input.scatter_add_(2, index.unsqueeze(2), y_grad.unsqueeze(2))
        
    #Fold the gradients back to the original 
    shape grad_input = torch.nn.functional.fold(grad_input.view(x.shape[0], x.shape[1], -1, unfolded_shape[0],unfolded_shape[1]),
                                                (x.shape[2], x.shape[3]),
                                                kernel_size, dilation=dilation,
                                                padding=padding, stride=stride)
    
    return grad_inputt

AND A LOT OF ERRORS : 

    ERROR: testBackwardKernel1NoBatch (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 551, in testBackwardKernel1NoBatch
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel2Stride2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 581, in testBackwardKernel2Stride2
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel3 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 571, in testBackwardKernel3
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel3NoBatch (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 561, in testBackwardKernel3NoBatch
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel3Padding1 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 591, in testBackwardKernel3Padding1
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel4Dilation2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 601, in testBackwardKernel4Dilation2
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testBackwardKernel4Padding1Stride2Dilation2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 611, in testBackwardKernel4Padding1Stride2Dilation2
        self._test_backward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 455, in _test_backward
        y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testContext (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 434, in testContext
        y = max_pool2d(x, kernel_size=kernel_size, ctx=ctx)
    TypeError: max_pool2d() got an unexpected keyword argument 'ctx'
    
    ======================================================================
    ERROR: testKernel1NoBatch (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 481, in testKernel1NoBatch
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel2Stride2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 511, in testKernel2Stride2
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel3 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 501, in testKernel3
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel3NoBatch (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 491, in testKernel3NoBatch
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel3Padding1 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 521, in testKernel3Padding1
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel4Dilation2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 531, in testKernel4Dilation2
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ======================================================================
    ERROR: testKernel4Padding1Stride2Dilation2 (test_functional.TestMaxPool2d)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 541, in testKernel4Padding1Stride2Dilation2
        self._test_forward(batch_size, cin, input_size, kernel_size, padding, stride, dilation)
      File "/content/gdrive/MyDrive/DL4CV/hw3/test_functional.py", line 449, in _test_forward
        torch.testing.assert_close(y, y_, rtol=self.rtol, atol=self.atol, msg=dbg)
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1498, in assert_close
        error_metas = not_close_error_metas(
      File "/usr/local/lib/python3.10/dist-packages/torch/testing/_comparison.py", line 1217, in not_close_error_metas
        raise error_meta.to_error() from None
    TypeError: No comparison pair was able to handle inputs of type <class 'numpy.ndarray'> and <class 'torch.Tensor'>.
    
    ----------------------------------------------------------------------
    Ran 15 tests in 0.115s
    
    FAILED (errors=15)


I tried to solve a problem, but I was not succeed. What can I do with these errors? 
python opencv pytorch computer-vision
1个回答
0
投票

您的函数输入定义为

def max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), padding=(0, 0), dilation=(1, 1)):

你称之为

y = max_pool2d(x, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, ctx=ctx)

max_pool2d
定义没有kwarg
ctx
,所以当你通过
ctx=ctx
时你会得到错误。

© www.soinside.com 2019 - 2024. All rights reserved.