Pytorch production

username: jasg
cooked: <p>help me I’m having an error:<br>
TypeError: forward() missing 1 required positional argument: ‘X’<br>
this is my code<br>
"import torch<br>
import torchvision<br>
import torch.nn as nn<br>
import torchvision.transforms as transforms<br>
from torch.autograd import Variable<br>
from PIL import Image, ImageOps, ImageEnhance</p>
<p>class ConvolutionalNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv2 = nn.Conv2d(6, 16, kernel_size=3, stride=1,padding=1)<br>
self.fc1 = nn.Linear(56<em>56</em>16, 120)<br>
<span class="hashtag">#hidden</span> layer de 120 y 84<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 18)<span class="hashtag">#este</span> 18 son las 18 clases</p>
<pre><code>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)
</code></pre>
<p>torch.manual_seed(101)<br>
CNNmodel = ConvolutionalNetwork()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(CNNmodel.parameters(), lr=0.001)</p>
<p>torch.manual_seed(0)<br>
model=torch.load(‘quinua_pytorch.pt’)</p>
<p>model = CNNmodel()<br>
model.load_state_dict(torch.load(‘quinua_pytorch.pt’)) <span class="hashtag">#line</span> to change<br>
model.eval()</p>
<p>transform = transforms.Compose(<br>
[transforms.ToTensor(),<br>
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, …</p>
<p>img = Image.open(’…/ejecutar/1.jpg’)  # Load image as PIL.Image<br>
x = transform(img)  # Preprocess image<br>
x = x.unsqueeze(0)  # Add batch dimension</p>
<p>output = model(x)  # Forward pass<br>
pred = torch.argmax(output, 1)  # Get predicted class if multi-class classification<br>
print('Image predicted as ', pred)"</p>

username: ptrblck
cooked: <p>The posted code is not executable, as e.g. <code>self.conv1</code> is missing.<br>
Once this is fixed, one would need to remove other undefined classes until your code runs fine:</p>
<pre><code class="lang-python">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) 
</code></pre>
<p>I’ve also changed the <code>view</code> operation, as it was failing since the input shape was undefined.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.<br>
Also, once posted, copy/paste the code into a new script file, make sure it’s executable, and reproduces the error.</p>

username: jasg
cooked: <p>how would I adapt to predict an image I want to predict with the model quinua_pytorch.pt</p>

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

username: jasg
cooked: <p>quinua_pytorch.pt is the training file after training the image dataset.<br>
in your code you do not specify where I should load the file where it is already trained.</p>

username: jasg
cooked: <p>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</p>