【问题标题】:How can I vitalize single image with only One CNN and one Pooling layers?如何让只有一个 CNN 和一个 Pooling 层的单张图片焕发活力?
【发布时间】:2022-11-18 08:26:10
【问题描述】:

我编写此示例代码以在将其传递给我的模型后仅显示单个图像 该模型应该只有一个 CNN + 一个池层。或者以其他方式,我如何通过将其分配给具有一个 cnn 和一个池层的样本神经网络来 vitulize 单个图像。

`import torch
import torch.nn as nn #creating neural network
from PIL import Image
from numpy import asarray

#Set up GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
Her I am loading my image 
# load the image
image = Image.open('./img.png')
# convert image to numpy array
data = asarray(image)
print(type(data))
print(data.shape)`

现在正在建造拱门。

```
class ConvNet(nn.Module):
    def __init__(self):
        super().__init__()
        #convolutional layer
        self.layer = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=3, kernel_size=2, stride=1, padding=0),
            nn.MaxPool2d(kernel_size=2, stride=2))
        
    def forward(self, x):
        out = self.layer(x)
        return out


convnet = ConvNet().to(device) #set up for GPU if available
convnet`
```
pass image to my model 
`outputs = convnet(data)
imshow(outputs)`
```

got the error below 

``
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3184/1768392595.py in <module>
----> 1 outputs = convnet(data)
      2 imshow(outputs)


TypeError: conv2d() received an invalid combination of arguments - got (numpy.ndarray, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (numpy.ndarray, Parameter, Parameter, tuple, tuple, tuple, int)
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (numpy.ndarray, Parameter, Parameter, tuple, tuple, tuple, int)
``


I expect to show image after passed during this sample network 

【问题讨论】:

    标签: python deep-learning pytorch conv-neural-network


    【解决方案1】:

    CNN 的输入需要是 torch.Tensor 类型。您可以通过直接在 PIL 图像上应用变换来执行此操作,如下所示:

    data = torchvision.transforms.functional.to_tensor(image)
    

    或者

    transform = torchvision.transforms.ToTensor() # can be composed with other transforms if necessary
    data = transform(image)
    

    【讨论】:

      【解决方案2】:

      正如 GoodDeeds 所提到的,CNN 希望数据的类型为 Tensor 您已经使用 PIL 读取了图像,然后将其转换为 NumPy 数组,您需要使用 torch.from_numpy(data) 将 NumPy 数组转换为 Tensor

      下面的代码将解决这个问题

      import torch
      import torch.nn as nn #creating neural network
      from PIL import Image
      from numpy import asarray
      
      #Set up GPU
      device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
      Her I am loading my image 
      # load the image
      image = Image.open('./img.png')
      # convert image to numpy array
      data = asarray(image)
      data=torch.from_numpy(data)
      print(type(data))
      print(data.shape)`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-06
        • 1970-01-01
        • 2014-03-03
        • 1970-01-01
        • 2022-10-06
        • 1970-01-01
        相关资源
        最近更新 更多