KristofGaming39 commited on
Commit
4a91601
1 Parent(s): f3a6572

Create codeimadeforthepredicting.py

Browse files

This is the code i made for the predicting the test/new image.

Files changed (1) hide show
  1. codeimadeforthepredicting.py +29 -0
codeimadeforthepredicting.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # THIS IS THE CODE I MADE FOR THE PREDICTING
2
+
3
+ import torch
4
+ import torchvision
5
+ from torchvision.transforms import transforms
6
+ from PIL import Image
7
+
8
+ model = torchvision.models.resnet50(pretrained=False)
9
+ model.fc = torch.nn.Linear(in_features=2048, out_features=1)
10
+ model.load_state_dict(torch.load('/content/zero_shot_classification_model.pth'))
11
+ model.eval()
12
+
13
+ test_transform = transforms.Compose([
14
+ transforms.Resize((224, 224)),
15
+ transforms.ToTensor(),
16
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
17
+ ])
18
+
19
+ test_image_path = '/content/test_image.png'
20
+ test_image = Image.open(test_image_path).convert('RGB')
21
+ test_image = test_transform(test_image)
22
+ test_image = test_image.unsqueeze(0)
23
+
24
+ with torch.no_grad():
25
+ prediction = model(test_image)
26
+
27
+ probability = torch.sigmoid(prediction).item()
28
+
29
+ print(f"The probability of the image being a Roblox character is: {probability}")