-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (108 loc) · 4.37 KB
/
main.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
import glob
import random
import os
from sklearn.metrics import precision_recall_curve
from PIL import Image
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
import matplotlib.pyplot as plt
from tqdm import tqdm
# gpu setting
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('device', device)
def seed_torch(seed=4):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed) # 为了禁止hash随机化,使得实验可复现
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
class CS4243_dataset(Dataset):
def __init__(self, root_path, mode='train', transform=None, L = None):
self.transform = transform
self.root_path = root_path
self.mode = mode
self.L = L
self.img_path_list = sorted(glob.glob(f"{self.root_path}/*.jpg"))
self.labels = 'bag_data/empt_arr.txt'
self.labels = np.loadtxt(self.labels).astype(np.int)
def __getitem__(self, index):
index += self.L
# index = 102
N_img_name = self.img_path_list[index]
N_image = Image.open(N_img_name)
first2N_L_img_list = []
compare_index_list = []
for i in range(index - self.L + 1):
first2N_L_img = self.img_path_list[i]
first2N_L_img = Image.open(first2N_L_img)
first2N_L_img = self.transform(first2N_L_img)
first2N_L_img_list.append(first2N_L_img)
compare_index_list.append(i)
compare_index_list = torch.tensor(np.array(compare_index_list, dtype=np.int))
first2N_L_img_tensor = torch.stack(first2N_L_img_list, dim=0)
target = torch.tensor(self.labels[index,:])
target = target[compare_index_list]
N_image = self.transform(N_image)
return [N_image,first2N_L_img_tensor, target]
def __len__(self):
return len(self.img_path_list)
# val/test a Epoch
def infer(model, test_loader):
model.eval() # close BN and dropout layer
print('===== Validation =====')
with torch.no_grad():
label_list = []
cos_sim_list = []
for i, data in tqdm(enumerate(test_loader), total=len(test_loader)):
if i + L >= len(test_loader) - 1 :
break
X_train, sequence ,label = data
X_train = X_train.to(device)
sequence = sequence.to(device)
label = label.to(device).long()
X_embed = net(X_train)
seq_embed = net(sequence.squeeze(0))
cos_sim = torch.cosine_similarity(X_embed, seq_embed, dim=1)
del X_embed,seq_embed
label_list.extend(label.cpu().numpy().flatten().astype(np.float32))
cos_sim_list.extend(cos_sim.cpu().numpy().flatten())
precision, recall, thresholds = precision_recall_curve(label_list, cos_sim_list)
plt.figure(1)
plt.plot(recall, precision)
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.savefig('p-r.png')
plt.show()
# precision , recall ,thresholds = precision_recall_curve(label.cpu().numpy().flatten().astype(np.float32), cos_sim.cpu().numpy().flatten())
if __name__ == '__main__':
seed_torch()
img_names = [] # ID
targets = [] # labels
root_path = 'bag_data/cam1' # locally
"""#### Image preporcess: transform"""
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize = transforms.Normalize(mean, std)
resize_H, resized_W = 224, 224 #
resize = transforms.Resize([resize_H, resized_W])
transformations = transforms.Compose([
resize,
transforms.ToTensor(), # Tturn gray level from 0-255 into 0-1
normalize
]) # change 0-1 into (-1, 1)
L = 1000
batch_size = 1
train_dataset = CS4243_dataset(root_path, mode='train', transform=transformations,L=L)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, num_workers=0, shuffle=False, pin_memory=True)
"""### initalize Model & Hyper params"""
from torchvision import models
net = models.vgg16(pretrained=True).cuda()
net.eval()
# inference
infer(net, train_loader)