-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg.py
55 lines (49 loc) · 2.03 KB
/
vgg.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
import torchvision.models as models
from torchvision import datasets, transforms
import torch
from torch.autograd import Variable
import cv2
from PIL import Image
from torchvision import transforms
filepath = 'D:\Torrent\ILSVRC2012_img_val'
vgg = models.vgg16(pretrained=True)
vgg.eval()
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
#transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])
#dataset = datasets.ImageNet("D:\Torrent", split="val", transform=transform)
target = []
with open(filepath+'\ILSVRC2012_validation_ground_truth.txt', 'r') as f:
for line in f:
num = int(line)
target.append(num)
correct=0
num_test=1000
#test_loader = torch.utils.data.DataLoader("D:\Torrent\ILSVRC2012_img_val", batch_size=1, shuffle=False, num_workers=8, pin_memory=True)
for i in range(num_test):
filename = filepath+"\ILSVRC2012_val_"+ '{0:08d}.JPEG'.format(i+1)
#print("Reading "+filename)
input_image = Image.open(filename)
if(input_image.mode=='L'): #skip black and white
continue
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
# move the input and model to GPU for speed if available
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
vgg.to('cuda')
with torch.no_grad():
output = vgg(input_batch)
# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
#print(output[0])
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
#print(torch.nn.functional.softmax(output[0], dim=0))
prediction = torch.argmax(output).item()
#print(prediction,target[i])
correct += (prediction == target[i])
print("Number of correct classifications: ", correct)
print("Accuracy:", correct/num_test)