-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecheck_odds.py
156 lines (120 loc) · 4.17 KB
/
recheck_odds.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import torch.nn.functional as F
from PIL import Image
import glob
print("Loading Images")
#bald_males = ["5imgs of bald males"]
#bald_females = ["5imgs of bald females"]
labels = torch.tensor([0, 0, 0, 0, 0])
bald_males = glob.glob('./Bald_Males/*.jpg')
bald_females = glob.glob('./Bald_Females/*.jpg')
#print(len(bald_males))
#print(len(bald_females))
transform = transforms.Compose(
[transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
"""
image_set_bald_males = []
for i in range(len(bald_males)):
image_set_bald_males.append(Image.open(bald_males[i]))
image_tensors = []
for i in range(len(image_set_bald_males)):
image_tensors.append(transform(image_set_bald_males[i]))
for i in range(len(image_tensors)):
image_tensors[i] = torch.unsqueeze(image_tensors[i], 0)
#print(len(image_set_bald_males))
#print(image_set_bald_males)
#print(len(image_tensors))
print("Images Loaded Sucessfully")
# Load Model
print("Loading Model Architecture")
model = torchvision.models.resnet18(weights='IMAGENET1K_V1')
in_features = model.fc.in_features
out_features = 1
class Classifier(nn.Module):
def __init__(self, in_features, out_features):
super(Classifier, self).__init__()
self.fc = nn.Linear(in_features, out_features)
def forward(self, x):
x = self.fc(x)
x = torch.sigmoid(x)
return x
model.fc = Classifier(in_features=in_features, out_features=out_features)
#print(model)
print("Model Architecture Loaded")
# Load model checkpoints
print("Loading Model Checkpoints")
checkpoint_path = 'adversarial_classifier_checkpoint.pth'
checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
# Load model state dict
model.load_state_dict(checkpoint['model_state_dict'])
print("Model Checkpoints Loaded Successfully")
model.eval()
correct = 0
for i in range(len(image_tensors)):
output = model(image_tensors[i])
_, predicted = torch.max(output, 1)
#break
correct += (predicted == labels[i]).sum().item()
#print(predicted == labels[i])
#print(correct)
#break
acc = correct/(len(image_tensors))
print("Accuracy:", acc*100)
#print(output)
#print(predicted)
"""
image_set_bald_females = []
for i in range(len(bald_females)):
image_set_bald_females.append(Image.open(bald_females[i]))
image_tensors = []
for i in range(len(image_set_bald_females)):
image_tensors.append(transform(image_set_bald_females[i]))
for i in range(len(image_tensors)):
image_tensors[i] = torch.unsqueeze(image_tensors[i], 0)
#print(len(image_set_bald_males))
#print(image_set_bald_males)
#print(len(image_tensors))
print("Images Loaded Sucessfully")
# Load Model
print("Loading Model Architecture")
model = torchvision.models.resnet18(weights='IMAGENET1K_V1')
in_features = model.fc.in_features
out_features = 1
class Classifier(nn.Module):
def __init__(self, in_features, out_features):
super(Classifier, self).__init__()
self.fc = nn.Linear(in_features, out_features)
def forward(self, x):
x = self.fc(x)
x = torch.sigmoid(x)
return x
model.fc = Classifier(in_features=in_features, out_features=out_features)
#print(model)
print("Model Architecture Loaded")
# Load model checkpoints
print("Loading Model Checkpoints")
checkpoint_path = 'adversarial_classifier_checkpoint.pth'
checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
# Load model state dict
model.load_state_dict(checkpoint['model_state_dict'])
print("Model Checkpoints Loaded Successfully")
model.eval()
correct = 0
for i in range(len(image_tensors)):
output = model(image_tensors[i])
_, predicted = torch.max(output, 1)
#break
correct += (predicted == labels[i]).sum().item()
#print(predicted == labels[i])
#print(correct)
#break
acc = correct/(len(image_tensors))
print("Accuracy:", acc*100)
#print(output)
#print(predicted)