Pytorch production

help me I’m having an error:
TypeError: forward() missing 1 required positional argument: ‘X’
this is my code
"import torch
import torchvision
import torch.nn as nn
import torchvision.transforms as transforms
from torch.autograd import Variable
from PIL import Image, ImageOps, ImageEnhance

class ConvolutionalNetwork(nn.Module):
def init(self):
super().init()
self.conv2 = nn.Conv2d(6, 16, kernel_size=3, stride=1,padding=1)
self.fc1 = nn.Linear(565616, 120)
#hidden layer de 120 y 84
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 18)#este 18 son las 18 clases

def forward(self, X):
    X = F.relu(self.conv1(X))
    X = F.max_pool2d(X, 2, 2)#max pooling de 2x2
    X = F.relu(self.conv2(X))
    X = F.max_pool2d(X, 2, 2)
    X = X.view(-1, 56*56*16)
    X = F.relu(self.fc1(X))
    X = F.relu(self.fc2(X))
    X = self.fc3(X)
    return F.log_softmax(X, dim=1)

torch.manual_seed(101)
CNNmodel = ConvolutionalNetwork()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(CNNmodel.parameters(), lr=0.001)

torch.manual_seed(0)
model=torch.load(‘quinua_pytorch.pt’)

model = CNNmodel()
model.load_state_dict(torch.load(‘quinua_pytorch.pt’)) #line to change
model.eval()

transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) # Same as for your validation data, e.g. Resize, ToTensor, Normalize, …

img = Image.open(’…/ejecutar/1.jpg’) # Load image as PIL.Image
x = transform(img) # Preprocess image
x = x.unsqueeze(0) # Add batch dimension

output = model(x) # Forward pass
pred = torch.argmax(output, 1) # Get predicted class if multi-class classification
print('Image predicted as ', pred)"

The posted code is not executable, as e.g. self.conv1 is missing.
Once this is fixed, one would need to remove other undefined classes until your code runs fine:

class ConvolutionalNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 3, 1, 1)
        self.conv2 = nn.Conv2d(6, 16, kernel_size=3, stride=1,padding=1)
        self.fc1 = nn.Linear(56*56*16, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 18)
    
    def forward(self, X):
        X = F.relu(self.conv1(X))
        X = F.max_pool2d(X, 2, 2)
        X = F.relu(self.conv2(X))
        X = F.max_pool2d(X, 2, 2)
        X = X.view(x.size(0), -1)
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = self.fc3(X)
        return F.log_softmax(X, dim=1)

torch.manual_seed(101)
model = ConvolutionalNetwork()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(CNNmodel.parameters(), lr=0.001)

x = torch.randn(1, 3, 224, 224)
output = model(x)
pred = torch.argmax(output, 1) 

I’ve also changed the view operation, as it was failing since the input shape was undefined.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.
Also, once posted, copy/paste the code into a new script file, make sure it’s executable, and reproduces the error.

how would I adapt to predict an image I want to predict with the model quinua_pytorch.pt

I don’t know which model quinua_pytorch is, but to get a prediction for an input image you should reuse your previous image loading and processing code.
I.e. load the image, transform (and normalize) it to a tensor and get the prediction as seen in my code.

quinua_pytorch.pt is the training file after training the image dataset.
in your code you do not specify where I should load the file where it is already trained.

please support me I want to predict which class has a new image to the trained model, in your code it does not include the pytorch model load how it would be please