-
Notifications
You must be signed in to change notification settings - Fork 0
/
psol_collect_samples_imagenet.py
161 lines (130 loc) · 6.25 KB
/
psol_collect_samples_imagenet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 17:54:36 2022
@author: tibrayev
"""
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data_utils
from torchvision import datasets, transforms
from torchviz import make_dot
import matplotlib.pyplot as plt
import copy
import argparse
torch.set_printoptions(linewidth = 160)
np.set_printoptions(linewidth = 160)
np.set_printoptions(precision=4)
np.set_printoptions(suppress='True')
from psol_config_imagenet import psol_config
from psol_models import choose_locmodel
from cls_models import choose_clsmodel
from utils.utils_dataloaders import get_dataloaders
from utils.utils_custom_tvision_functions import plot_curve, imshow, plotregions, plotspots, plotspots_at_regioncenters, region_iou, region_area
parser = argparse.ArgumentParser(description='Collect predicted and groundtruth samples', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--start', default=1, type=int, help='The index of the first sample to collect')
parser.add_argument('--end', default=-1, type=int, help='The index of the last sample to collect')
global args
args = parser.parse_args()
#%% Instantiate parameters, dataloaders, and model
# Parameters
config_3 = psol_config
config_3_copy = {k: v for k, v in config_3.__dict__.items() if '__' not in k}
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# SEED instantiation
SEED = config_3.seed
torch.manual_seed(SEED)
np.random.seed(SEED)
#random.seed(SEED)
torch.cuda.manual_seed_all(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Dataloaders
valid_loader = get_dataloaders(config_3, loader_type=config_3.loader_type)
# For testing, we allow the datasets (CUB and ImageNet) to fetch more than one bounding box
# However, then it requires to work on valid_loader.dataset, rather than valid_loader itself,
# since we did not customized default collate_fn in the loaders!
valid_loader.dataset.fetch_one_bbox = False
# Loss(es)
bce_loss = nn.BCEWithLogitsLoss()
ce_loss = nn.CrossEntropyLoss()
# cls model
cls_model = choose_clsmodel(config_3.cls_model_name, config_3.cls_pretrained, config_3.cls_ckpt_dir, config_3.num_classes).to(device)
for p in cls_model.parameters():
p.requires_grad_(False)
cls_model.eval()
print("Classification model:\n")
print(cls_model)
# localization model
loc_model = choose_locmodel('densenet161', pretrained=True).to(device)
for p in loc_model.parameters():
loc_model.requires_grad_(False)
loc_model.eval()
print("Localization model:\n")
print(loc_model)
#%% Evaluate the model performance.
args.end = len(valid_loader.dataset) if args.end == -1 else args.end
collected_samples = {}
if config_3.dataset == 'cub':
ten_crop = None
elif config_3.dataset == 'imagenet':
# ten_crop = transforms.Compose([transforms.TenCrop(size=(224, 224))])
ten_crop = None
with torch.no_grad():
for i in range(args.start-1, args.end, 1):
if config_3.dataset == 'cub':
image, (target_class, target_bbox) = valid_loader.dataset[i]
target_class = target_class.unsqueeze(0)
elif config_3.dataset == 'imagenet':
image, target_class, target_bbox = valid_loader.dataset[i]
target_class = torch.tensor(target_class)
image, target_class, target_bbox = image.unsqueeze(0).to(device), target_class.to(device), target_bbox.to(device)
psol_results = {}
(h, w) = config_3.full_res_img_size
predicted_bbox = loc_model(image) # PSOL localizer, predictions are in (w,y,w,h) format, in range(0,1)
predicted_bbox[:, 0] *= w
predicted_bbox[:, 1] *= h
predicted_bbox[:, 2] *= w
predicted_bbox[:, 3] *= h
psol_results["xywh_box"] = copy.deepcopy(predicted_bbox[0])
predicted_bbox[:, 2] += predicted_bbox[:, 0]
predicted_bbox[:, 3] += predicted_bbox[:, 1]
psol_results["xyxy_box"] = copy.deepcopy(predicted_bbox[0])
if ten_crop:
ten_cropped_image = torch.cat(ten_crop(image))
outputs = cls_model(ten_cropped_image)
outputs_probabilities = F.softmax(outputs, dim=-1)
output_probabilities = torch.mean(outputs_probabilities, dim=0, keepdim=True)
else:
output = cls_model(image)
output_probabilities = F.softmax(output, dim=-1)
prediction_confidence = torch.max(output_probabilities, dim=-1)[0].item()
prediction_label = torch.max(output_probabilities, dim=-1)[1].item()
psol_results["prediction_label"] = prediction_label
psol_results["prediction_confidence"] = prediction_confidence
sample_stats = {}
sample_stats["gt_labels"] = copy.deepcopy(target_class)
sample_stats["gt_bboxes"] = copy.deepcopy(target_bbox)
sample_stats["gt_resized_wh"] = (config_3.full_res_img_size[1], config_3.full_res_img_size[0])
sample_stats["predictions"] = copy.deepcopy([psol_results])
collected_samples[i] = copy.deepcopy(sample_stats)
if config_3.dataset == 'cub' or config_3.dataset == 'imagenet':
torch.save(collected_samples, './psol/' + "{}_collected_sample_from{}to{}.pth".format(config_3.dataset, args.start, args.end))
if (i+1) %100 == 0 or i == len(valid_loader.dataset)-1:
print("{}/{} requested samples processed!\n".format(
(i+1), (args.end - args.start + 1)))
#%% Analyze ImageNet on test set annotations as WSOD
# if config_3.dataset == 'imagenet':
# from voclike_imagenet_evaluator import do_voc_evaluation
# collected_samples = {}
# path_to_samples = './psol/'
# partial_sample_collections = list(filter((lambda x: ('imagenet_collected_sample_from' in x)), os.listdir(path_to_samples)))
# for partial in partial_sample_collections:
# ckpt = torch.load(os.path.join(path_to_samples, partial))
# collected_samples.update(ckpt)
# ## For WSOD results:
# results_ap = do_voc_evaluation(collected_samples)
# print("TEST (WSOD) STATS: mAP: {}".format(results_ap["map"]))