-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
43 lines (35 loc) · 1.35 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import argparse
import torch
from PIL import Image
from preprocess_data import preprocess_data
from model import ViT
def evaluate(model, image_path):
"""
Evaluates the given model on the given image.
Args:
model (nn.Module): The model to evaluate.
image_path (str): The path to the image file to evaluate.
Returns:
The model's prediction for the given image.
"""
image = Image.open(image_path)
if image.mode != 'RGB':
image = image.convert('RGB')
image = preprocess_data(image)
image = image.unsqueeze(0) # Add batch dimension
model.eval()
with torch.no_grad():
output = model(image)
_, predicted = torch.max(output, 1)
prediction = "boat" if predicted.item() == 1 else "not boat"
print(f"Prediction for image {image_path}: {prediction}")
return prediction
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Evaluate a ship image classification model.")
parser.add_argument("image_path", type=str, help="Path to image to evaluate.")
parser.add_argument("--model_path", type=str, default="model_checkpoints/model.pth", help="Path to saved model file.")
args = parser.parse_args()
model = ViT()
checkpoint = torch.load(args.model_path)
model.load_state_dict(checkpoint)
evaluate(model, args.image_path)