|
a |
|
b/Classifier/Classes/visualizer.py |
|
|
1 |
|
|
|
2 |
import numpy as np |
|
|
3 |
import cv2 |
|
|
4 |
import matplotlib.pyplot as plt |
|
|
5 |
import torch |
|
|
6 |
import torchvision |
|
|
7 |
|
|
|
8 |
mean_nums = [0.485, 0.456, 0.406] |
|
|
9 |
std_nums = [0.229, 0.224, 0.225] |
|
|
10 |
|
|
|
11 |
|
|
|
12 |
def load_image(img_path, resize=True): |
|
|
13 |
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB) |
|
|
14 |
|
|
|
15 |
if resize: |
|
|
16 |
img = cv2.resize(img, (64, 64), interpolation = cv2.INTER_AREA) |
|
|
17 |
|
|
|
18 |
return img |
|
|
19 |
|
|
|
20 |
def show_image(img_path): |
|
|
21 |
img = load_image(img_path) |
|
|
22 |
plt.imshow(img) |
|
|
23 |
plt.axis('off') |
|
|
24 |
|
|
|
25 |
def show_grid_image(image_paths): |
|
|
26 |
images = [load_image(img) for img in image_paths] |
|
|
27 |
images = torch.as_tensor(images) |
|
|
28 |
images = images.permute(0, 3, 1, 2) |
|
|
29 |
grid_img = torchvision.utils.make_grid(images, nrow=11) |
|
|
30 |
plt.figure(figsize=(24, 12)) |
|
|
31 |
plt.imshow(grid_img.permute(1, 2, 0)) |
|
|
32 |
plt.axis('off'); |
|
|
33 |
|
|
|
34 |
def image_show(inp, title=None): |
|
|
35 |
inp = inp.numpy().transpose((1, 2, 0)) |
|
|
36 |
mean = np.array([mean_nums]) |
|
|
37 |
std = np.array([std_nums]) |
|
|
38 |
inp = std * inp + mean |
|
|
39 |
inp = np.clip(inp, 0, 1) |
|
|
40 |
plt.imshow(inp) |
|
|
41 |
if title is not None: |
|
|
42 |
plt.title(title) |
|
|
43 |
plt.axis('off') |