This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification.py
66 lines (47 loc) · 1.94 KB
/
classification.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import codecs
import json
import os
from typing import Tuple
import cv2
import torch
import torchvision.transforms as transforms
from torchvision.models import AlexNet
from annotation_parser import parse_xml
def classify(image_path: str, rect: Tuple[int, int, int, int]) -> Tuple[int, float]:
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
])
model = AlexNet(num_classes=94)
model.load_state_dict(torch.load('./weights/AlexNet.pth'))
model.eval()
model = model.to(device)
image = cv2.imread(image_path)
image_rect = image[rect[1]:rect[3], rect[0]:rect[2]]
image_rect = transform(image_rect).to(device)
image_rect = image_rect.unsqueeze(0)
# classify the image
with torch.no_grad():
output = model(image_rect)
prediction = torch.argmax(output[0]).item()
probability = torch.softmax(output[0], dim=0)[prediction].item()
return prediction, probability
if __name__ == "__main__":
for files in os.listdir('./datasets/JPEGImages'):
image_path = os.path.join('./datasets/JPEGImages', files)
anno_path = os.path.join('./datasets/Annotations', files.replace('.jpg', '.xml'))
with codecs.open('categories.json', 'r', 'utf-8') as f:
dx_to_class = json.load(f)
annotation = parse_xml(anno_path)
for obj in annotation['objects']:
box = obj['bndbox']
rect = (box['xmin'], box['ymin'], box['xmax'], box['ymax'])
prediction, probability = classify(image_path, rect)
prediction = dx_to_class[str(prediction)]
print(f'Prediction: {prediction}, '
f'Probability: {probability}, '
f'Label: {obj["name"]}')