-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
123 lines (123 loc) · 4.3 KB
/
test.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
import numpy as np
import cv2
import torch
import glob as glob
import pandas as pd
import os
import albumentations as A
import time
from albumentations.pytorch import ToTensorV2
from torch.nn import functional as F
from torch import topk
from model import build_model
device = 'cuda'
# Class names.
sign_names_df = pd.read_csv('../input/signnames.csv')
class_names = sign_names_df.SignName.tolist()
# DataFrame for ground truth.
gt_df = pd.read_csv(
'../input/GTSRB_Final_Test_GT/GT-final_test.csv',
delimiter=';'
)
gt_df = gt_df.set_index('Filename', drop=True)
# Initialize model, switch to eval model, load trained weights.
model = build_model(
pretrained=False,
fine_tune=False,
num_classes=43
).to(device)
model = model.eval()
model.load_state_dict(
torch.load(
'../output/model.pth', map_location=device
)['model_state_dict']
)
def returnCAM(feature_conv, weight_softmax, class_idx):
'''Tạo class activation maps'''
size_upsample = (256, 256)
bz, nc, h, w = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = weight_softmax[idx].dot(feature_conv.reshape((nc, h*w)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
def apply_color_map(CAMs, width, height, orig_image):
for i, cam in enumerate(CAMs):
heatmap = cv2.applyColorMap(cv2.resize(cam,(width, height)), cv2.COLORMAP_JET)
result = heatmap * 0.5 + orig_image * 0.5
result = cv2.resize(result, (224, 224))
return result
def visualize_and_save_map(
result, orig_image, gt_idx=None, class_idx=None, save_name=None
):
'''Đưa nhãn vào ảnh và lưu lại'''
if class_idx is not None:
cv2.putText(
result,
f"Pred: {str(class_names[int(class_idx)])}", (5, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2,
cv2.LINE_AA
)
if gt_idx is not None:
cv2.putText(
result,
f"GT: {str(class_names[int(gt_idx)])}", (5, 40),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2,
cv2.LINE_AA
)
orig_image = cv2.resize(orig_image, (224, 224))
img_concat = cv2.hconcat([
np.array(result, dtype=np.uint8),
np.array(orig_image, dtype=np.uint8)
])
if save_name is not None:
cv2.imwrite(f"../output/test_results/CAM_{save_name}.jpg", img_concat)
features_blobs = []
def hook_feature(module, input, output):
features_blobs.append(output.data.cpu().numpy())
model._modules.get('features').register_forward_hook(hook_feature)
params = list(model.parameters())
weight_softmax = np.squeeze(params[-4].data.cpu().numpy())
transform = A.Compose([
A.Resize(224, 224),
A.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
),
ToTensorV2(),
])
counter = 0
all_images = glob.glob('../input/GTSRB_Final_Test_Images/GTSRB/Final_Test/Images/*.ppm')
correct_count = 0
frame_count = 0 # To count total frames.
total_fps = 0 # To get the final frames per second.
# Kiem tra toan bo tap test
for i, image_path in enumerate(all_images):
image = cv2.imread(image_path)
orig_image = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width, _ = orig_image.shape
image_tensor = transform(image=image)['image']
image_tensor = image_tensor.unsqueeze(0)
start_time = time.time()
outputs = model(image_tensor.to(device))
end_time = time.time()
probs = F.softmax(outputs).data.squeeze()
class_idx = topk(probs, 1)[1].int()
image_name = image_path.split(os.path.sep)[-1]
gt_idx = gt_df.loc[image_name].ClassId
#Kiểm tra xem dự đoán có chính xác hay không
if gt_idx == class_idx:
correct_count += 1
CAMs = returnCAM(features_blobs[0], weight_softmax, class_idx)
save_name = f"{image_path.split('/')[-1].split('.')[0]}"
#Lưu lại kết quả
result = apply_color_map(CAMs, width, height, orig_image)
visualize_and_save_map(result, orig_image, gt_idx, class_idx, save_name)
print(f"Total number of test images: {len(all_images)}")
print(f"Total correct predictions: {correct_count}")
print(f"Accuracy: {correct_count/len(all_images)*100:.3f}")