Views
No views yet
1class CNNV0(nn.Module):
2 def __init__(self, input_shape: int, hidden_units: int, output_shape: int):
3 super().__init__()
4 self.conv_block_1 = nn.Sequential(
5 nn.Conv2d(in_channels=input_shape, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
6 nn.ReLU(),
7 nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
8 nn.ReLU(),
9 nn.MaxPool2d(kernel_size=2)
10 )
11 self.conv_block_2 = nn.Sequential(
12 nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
13 nn.ReLU(),
14 nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
15 nn.ReLU(),
16 nn.MaxPool2d(kernel_size=2)
17 )
18 self.classifier = nn.Sequential(
19 nn.Flatten(),
20 nn.Linear(in_features=hidden_units*576, out_features=output_shape)
21 )
22
23 def forward(self, x):
24 x = self.conv_block_1(x)
25 x = self.conv_block_2(x)
26 x = self.classifier(x)
27 return x1git clone <repository-url>
2cd <repository-folder>
3pip install torch torchvision1import torch
2from torchvision import transforms
3from PIL import Image
4
5# Load the model
6model = torch.load('model_0.pth')
7model.eval() # Set to evaluation mode
8
9# Load and preprocess the image
10transform = transforms.Compose([
11 transforms.Resize((224, 224)),
12 transforms.ToTensor(),
13])
14img = Image.open('path_to_image.jpg')
15img = transform(img).view(1, 3, 224, 224) # Reshape to (1, 3, 224, 224) for batch processing
16
17# Predict
18with torch.no_grad():
19 output = model(img)
20 _, predicted = torch.max(output, 1)
21 print("Predicted Aircraft Type:", predicted.item())