I wrote this sample code to show only a single image after passing it to my model. The model should have only one convolutional layer and one pooling layer. Or in another way, how can I visualize a single image by passing it to a simple neural network that has one convolutional and one pooling layer?
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")
# load the image
image = Image.open('./img.png')
# convert image to numpy array
data = asarray(image)
print(type(data))
print(data.shape)
now building the arch.
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
as GoodDeeds mentioned, CNN expects the data to be of type Tensor
you have read the image using PIL and then converted it to NumPy array, you will need to convert the NumPy array to Tensor using torch.from_numpy(data)
Below code will solve the issue
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)`