-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpredict.py
162 lines (127 loc) · 5.84 KB
/
predict.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
157
158
159
160
161
162
## predict.py
# Run the chosen model on every image in the desired dataset
# and save the encodings to the file: "encodings.csv"
import argparse
import datetime
import os
import random
import numpy as np
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
from lib.models.deep_gestalt import DeepGestalt
from lib.models.face_recog_net import FaceRecogNet
saved_model_dir = "saved_models"
def normalize(img, type='float'):
normalized = (img - img.min()) / (img.max() - img.min())
if type == 'int':
return (normalized * 255).int()
# Else: float
return normalized
# Simple preprocessing used for the input images
def preprocess(img):
resize = transforms.Resize((100, 100)) # Default size is (100,100)
img = resize(img)
# desired number of channels is 1, so we convert to gray
img = transforms.Grayscale(1)(img)
#img = transforms.RandomHorizontalFlip(p=1.0)(img)
return transforms.ToTensor()(img)
def parse_args():
parser = argparse.ArgumentParser(description='Predict DeepGestalt')
parser.add_argument('--batch-size', type=int, default=1, metavar='N',
help='input batch size for training (default: 1)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--model-type', default='DeepGestalt', dest='model_type',
help='Model type to use. Default: DeepGestalt. (Options: \'FaceRecogNet\', \'DeepGestalt\')')
parser.add_argument('--seed', type=int, default=11, metavar='S',
help='random seed (default: 11)')
parser.add_argument('--act_type', default='ReLU', dest='act_type',
help='activation function to use in model. (Options: ReLU, PReLU, Swish)')
parser.add_argument('--in_channels', default=1, dest='in_channels')
parser.add_argument('--num_classes', default=139, dest='num_classes', type=int)
parser.add_argument('--data_dir', default='../data/GestaltMatcherDB/images_cropped', dest='data_dir',
help='Path to the data directory containing the images to run the model on.')
return parser.parse_args()
def predict(model, device, data, args):
model.eval()
f = None
if args.model_type == "FaceRecogNet":
f = open("healthy_encodings.csv", "w+")
f.write(f"img_name;arg_max;representations\n")
elif args.model_type == "DeepGestalt":
f = open("encodings.csv", "w+")
f.write(f"img_name;class_conf;representations\n")
else:
raise NotImplementedError
tick = datetime.datetime.now()
with torch.no_grad():
for idx, img_path in enumerate(data):
print(f"{img_path=}")
img = Image.open(f"{args.data_dir}/{img_path}")
img = preprocess(img).to(device, dtype=torch.float32)
pred, pred_rep = model(img.unsqueeze(0))
if args.model_type == "FaceRecogNet":
f.write(f"{img_path};{torch.argmax(pred)};{pred_rep.squeeze().tolist()}\n")
else:
f.write(f"{img_path};{pred.squeeze().tolist()};{pred_rep.squeeze().tolist()}\n")
f.flush()
f.close()
print(f"Predictions took {datetime.datetime.now() - tick}s")
model.train()
return
def main():
# Training settings
args = parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
np.random.seed(args.seed)
torch.manual_seed(args.seed)
random.seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
if use_cuda:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed_all(args.seed)
torch.cuda.manual_seed(args.seed)
kwargs = {}
if use_cuda:
kwargs.update({'num_workers': 0, 'pin_memory': True})
data = os.listdir(args.data_dir)
# data = [f"{root.split('/')[-1]}/{img_name}" for dir in data for root,_,img_names in os.walk(f"{args.data_dir}/{dir}") for img_name in img_names]
if args.act_type == "ReLU":
act_type = nn.ReLU
elif args.act_type == "PReLU":
act_type = nn.PReLU
elif args.act_type == "LeakyReLU":
act_type = nn.LeakyReLU
else:
raise NotImplementedError
if args.model_type == 'FaceRecogNet':
model = FaceRecogNet(in_channels=args.in_channels,
num_classes=10575,
# num_classes=args.num_classes,
act_type=act_type).to(device)
# load model:
model.load_state_dict(
torch.load(f"saved_models/s1_casia_adam_FaceRecogNet_e50_ReLU_BN_bs100.pt",
map_location=device))
elif args.model_type == 'DeepGestalt':
model = DeepGestalt(in_channels=args.in_channels,
num_classes=args.num_classes,
device=device,
pretrained=False, # No need to load them as we're loading full weights after..
act_type=act_type).to(device)
# load model:
model.load_state_dict(
#torch.load(f"saved_models/s1_casia_adam_FaceRecogNet_e50_ReLU_BN_bs100.pt",
torch.load(f"saved_models/s2_gmdb_aug_adam_DeepGestalt_e310_ReLU_BN_bs280.pt",
#torch.load(f"saved_models/encoderdecoder_test.pt",
# torch.load(f"saved_models/s3_gmdb_aug_adam_DeepGestalt_e150_ReLU_bs280.pt",
map_location=device))
else:
print(f"No valid model type given! (got model_type: {args.model_type})")
raise NotImplementedError
predict(model, device, data, args)
if __name__ == '__main__':
main()