File size: 7,755 Bytes
b84549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from .datasets.ab_dataset import ABDataset
import matplotlib.pyplot as plt
from torchvision.utils import make_grid
import math
import torch


def visualize_classes_image_classification(dataset: ABDataset, class_to_idx_map, rename_map, 
                      fig_save_path: str, num_imgs_per_class=2, max_num_classes=20, 
                      unknown_class_idx=None):
    
    idx_to_images = {}
    idx_to_class = {}
    idx_to_original_idx = {}
    
    reach_max_num_class_limit = False
    for i, (c, idx) in enumerate(class_to_idx_map.items()):
        if unknown_class_idx is not None and idx == unknown_class_idx:
            continue
        
        idx_to_images[idx] = []
        idx_to_class[idx] = c
        idx_to_original_idx[idx] = dataset.raw_classes.index(c)
        
        if unknown_class_idx is not None and len(idx_to_images.keys()) == max_num_classes - 1:
            reach_max_num_class_limit = True
            break
        if unknown_class_idx is None and len(idx_to_images.keys()) == max_num_classes:
            reach_max_num_class_limit = True
            break
        
    if unknown_class_idx is not None:
        idx_to_images[unknown_class_idx] = []
        idx_to_class[unknown_class_idx] = ['(unknown classes)']
    
    full_flags = {k: False for k in idx_to_images.keys()}
    
    i = 0
    while True:
        x, y = dataset[i]
        i += 1
        y = int(y)
        
        if full_flags[y]:
            continue
        
        idx_to_images[y] += [x]
        if len(idx_to_images[y]) == num_imgs_per_class:
            full_flags[y] = True
            
        if all(full_flags.values()):
            break
        
    shown_num_classes = len(idx_to_images.keys())
    if reach_max_num_class_limit:
        shown_num_classes += 1
    num_cols = 3
    num_rows = math.ceil(shown_num_classes / num_cols)
    
    plt.figure(figsize=(6.4, 4.8 * num_rows // 2))

    draw_i = 1
    for class_idx, imgs in idx_to_images.items():
        class_name = idx_to_class[class_idx]
            
        grid = make_grid(imgs, normalize=True)
        plt.subplot(num_rows, num_cols, draw_i)
        draw_i += 1
        
        plt.axis('off')
        img = grid.permute(1, 2, 0).numpy()
        plt.imshow(img)
        
        if unknown_class_idx is not None and class_idx == unknown_class_idx:
            plt.title(f'(unknown classes)\n'
                      f'current index: {class_idx}')
        else:
            class_i = idx_to_original_idx[class_idx]
            if class_name in rename_map.keys():
                renamed_class = rename_map[class_name]
                plt.title(f'{class_i}-th original class\n'
                        f'"{class_name}" (→ "{renamed_class}")\n'
                        f'current index: {class_idx}')
            else:
                plt.title(f'{class_i}-th original class\n'
                        f'"{class_name}"\n'
                        f'current index: {class_idx}')
        
    if reach_max_num_class_limit:
        plt.subplot(num_rows, num_cols, draw_i)
        plt.axis('off')
        plt.imshow(torch.ones_like(grid).permute(1, 2, 0).numpy())
        plt.title(f'(Show up to {max_num_classes} classes...)')
    
    plt.tight_layout()
    plt.savefig(fig_save_path, dpi=300)
    plt.clf()


def visualize_classes_in_object_detection(dataset: ABDataset, class_to_idx_map, rename_map, 
                      fig_save_path: str, num_imgs_per_class=2, max_num_classes=20, 
                      unknown_class_idx=None):
    
    idx_to_images = {}
    idx_to_class = {}
    idx_to_original_idx = {}
    
    reach_max_num_class_limit = False
    for i, (c, idx) in enumerate(class_to_idx_map.items()):
        if unknown_class_idx is not None and idx == unknown_class_idx:
            continue
        
        idx_to_images[idx] = []
        idx_to_class[idx] = c
        idx_to_original_idx[idx] = dataset.raw_classes.index(c)
        
        if unknown_class_idx is not None and len(idx_to_images.keys()) == max_num_classes - 1:
            reach_max_num_class_limit = True
            break
        if unknown_class_idx is None and len(idx_to_images.keys()) == max_num_classes:
            reach_max_num_class_limit = True
            break
        
    if unknown_class_idx is not None:
        idx_to_images[unknown_class_idx] = []
        idx_to_class[unknown_class_idx] = ['(unknown classes)']
    
    full_flags = {k: False for k in idx_to_images.keys()}
    
    # print(idx_to_images.keys())
    
    ii = 0
    
    import time
    start_time = time.time()

    while True:
        # print(dataset[i])
        x, y = dataset[ii][:2]
        ii += 1
        
        cur_map = {}
        
        for label_info in y:
            if sum(label_info[1:]) == 0: # pad label
                break
            
            ci = label_info[0]
            print(f'cur ci: {ci}')
            # print(ci, label_info)
            
            if ci in cur_map.keys():
                continue # do not visualize multiple objects in an image
            
            if len(idx_to_images[ci]) == num_imgs_per_class:
                full_flags[ci] = True
                break
            
            idx_to_images[ci] += [(x, label_info[1:])]
            print(f'add image, ci: {ci}')
            cur_map[ci] = 1
        
        if time.time() - start_time > 40:
            break
        
        if sum(list(full_flags.values())) > len(full_flags.values()) * 0.7:
            break
        
    shown_num_classes = len(idx_to_images.keys())
    if reach_max_num_class_limit:
        shown_num_classes += 1
    num_cols = 3
    num_rows = math.ceil(shown_num_classes / num_cols)
    
    plt.figure(figsize=(6.4, 4.8 * num_rows // 2))
    
    from torchvision.transforms import ToTensor
    from PIL import Image, ImageDraw
    import numpy as np
    
    def draw_bbox(img, bbox):
        img = Image.fromarray(np.uint8(img.transpose(1, 2, 0)))
        draw = ImageDraw.Draw(img)
        draw.rectangle(bbox, outline=(255, 0, 0), width=6)
        return np.array(img)

    draw_i = 1
    for class_idx, imgs in idx_to_images.items():
        if len(imgs) == 0:
            draw_i += 1
            continue
        
        imgs, bboxes = [img[0] for img in imgs], [img[1] for img in imgs]
        class_name = idx_to_class[class_idx]
        
        # draw bbox
        imgs = [draw_bbox(img, bbox) for img, bbox in zip(imgs, bboxes)]
        imgs = [ToTensor()(img) for img in imgs]
        
        grid = make_grid(imgs, normalize=True)
        plt.subplot(num_rows, num_cols, draw_i)
        draw_i += 1
        
        plt.axis('off')
        img = grid.permute(1, 2, 0).numpy()
        plt.imshow(img)
        
        if unknown_class_idx is not None and class_idx == unknown_class_idx:
            plt.title(f'(unknown classes)\n'
                      f'current index: {class_idx}')
        else:
            class_i = idx_to_original_idx[class_idx]
            if class_name in rename_map.keys():
                renamed_class = rename_map[class_name]
                plt.title(f'{class_i}-th original class\n'
                        f'"{class_name}" (→ "{renamed_class}")\n'
                        f'current index: {class_idx}')
            else:
                plt.title(f'{class_i}-th original class\n'
                        f'"{class_name}"\n'
                        f'current index: {class_idx}')
        
    if reach_max_num_class_limit:
        plt.subplot(num_rows, num_cols, draw_i)
        plt.axis('off')
        plt.imshow(torch.ones_like(grid).permute(1, 2, 0).numpy())
        plt.title(f'(Show up to {max_num_classes} classes...)')
    
    plt.tight_layout()
    plt.savefig(fig_save_path, dpi=300)
    plt.clf()