-
Notifications
You must be signed in to change notification settings - Fork 20
/
fine2coarse.py
180 lines (174 loc) · 4.83 KB
/
fine2coarse.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
This file generates the pytorch fine-label --> coarse-label mapping, in CIFAR-100N.
Part of the code is copied from https://gist.github.com/adam-dziedzic/4322df7fc26a1e75bee3b355b10e30bc
"""
import torch
noise_file = torch.load('./data/CIFAR-100_human.pt')
clean_label = noise_file['clean_label']
noisy_label = noise_file['noisy_label']
fine_labels = [
'apple', # id 0
'aquarium_fish',
'baby',
'bear',
'beaver',
'bed',
'bee',
'beetle',
'bicycle',
'bottle',
'bowl',
'boy',
'bridge',
'bus',
'butterfly',
'camel',
'can',
'castle',
'caterpillar',
'cattle',
'chair',
'chimpanzee',
'clock',
'cloud',
'cockroach',
'couch',
'crab',
'crocodile',
'cup',
'dinosaur',
'dolphin',
'elephant',
'flatfish',
'forest',
'fox',
'girl',
'hamster',
'house',
'kangaroo',
'computer_keyboard',
'lamp',
'lawn_mower',
'leopard',
'lion',
'lizard',
'lobster',
'man',
'maple_tree',
'motorcycle',
'mountain',
'mouse',
'mushroom',
'oak_tree',
'orange',
'orchid',
'otter',
'palm_tree',
'pear',
'pickup_truck',
'pine_tree',
'plain',
'plate',
'poppy',
'porcupine',
'possum',
'rabbit',
'raccoon',
'ray',
'road',
'rocket',
'rose',
'sea',
'seal',
'shark',
'shrew',
'skunk',
'skyscraper',
'snail',
'snake',
'spider',
'squirrel',
'streetcar',
'sunflower',
'sweet_pepper',
'table',
'tank',
'telephone',
'television',
'tiger',
'tractor',
'train',
'trout',
'tulip',
'turtle',
'wardrobe',
'whale',
'willow_tree',
'wolf',
'woman',
'worm',
]
mapping_coarse_fine = {
'aquatic mammals': ['beaver', 'dolphin', 'otter', 'seal', 'whale'],
'fish': ['aquarium_fish', 'flatfish', 'ray', 'shark', 'trout'],
'flowers': ['orchid', 'poppy', 'rose', 'sunflower', 'tulip'],
'food containers': ['bottle', 'bowl', 'can', 'cup', 'plate'],
'fruit and vegetables': ['apple', 'mushroom', 'orange', 'pear',
'sweet_pepper'],
'household electrical device': ['clock', 'computer_keyboard', 'lamp',
'telephone', 'television'],
'household furniture': ['bed', 'chair', 'couch', 'table', 'wardrobe'],
'insects': ['bee', 'beetle', 'butterfly', 'caterpillar', 'cockroach'],
'large carnivores': ['bear', 'leopard', 'lion', 'tiger', 'wolf'],
'large man-made outdoor things': ['bridge', 'castle', 'house', 'road',
'skyscraper'],
'large natural outdoor scenes': ['cloud', 'forest', 'mountain', 'plain',
'sea'],
'large omnivores and herbivores': ['camel', 'cattle', 'chimpanzee',
'elephant', 'kangaroo'],
'medium-sized mammals': ['fox', 'porcupine', 'possum', 'raccoon', 'skunk'],
'non-insect invertebrates': ['crab', 'lobster', 'snail', 'spider', 'worm'],
'people': ['baby', 'boy', 'girl', 'man', 'woman'],
'reptiles': ['crocodile', 'dinosaur', 'lizard', 'snake', 'turtle'],
'small mammals': ['hamster', 'mouse', 'rabbit', 'shrew', 'squirrel'],
'trees': ['maple_tree', 'oak_tree', 'palm_tree', 'pine_tree',
'willow_tree'],
'vehicles 1': ['bicycle', 'bus', 'motorcycle', 'pickup_truck', 'train'],
'vehicles 2': ['lawn_mower', 'rocket', 'streetcar', 'tank', 'tractor'],
}
# fine label name -> id of fine label
fine_id = dict()
# id of fine label -> fine label name
id_fine = dict()
for id, label in enumerate(fine_labels):
fine_id[label] = id
id_fine[id] = label
# coarse label name -> id of coarse label
coarse_id = dict()
# id of coarse label -> name of the coarse label
id_coarse = dict()
# name of fine label -> name of coarse label
fine_coarse = dict()
# id of fine label -> id of coarse label
fine_id_coarse_id = dict()
# id of coarse label -> id of fine label
coarse_id_fine_id = dict()
for id, (coarse, fines) in enumerate(mapping_coarse_fine.items()):
coarse_id[coarse] = id
id_coarse[id] = coarse
fine_labels_ids = []
for fine in fines:
fine_coarse[fine] = coarse
fine_label_id = fine_id[fine]
fine_id_coarse_id[fine_label_id] = id
fine_labels_ids.append(fine_label_id)
coarse_id_fine_id[id] = fine_labels_ids
coarse_label_noisy = []
coarse_label_clean = []
for i in range(len(noisy_label)):
tmp_noisy = fine_id_coarse_id[noisy_label[i]]
coarse_label_noisy.append(tmp_noisy)
tmp_clean = fine_id_coarse_id[clean_label[i]]
coarse_label_clean.append(tmp_clean)
new_dict = {'clean_label': clean_label, 'noisy_label': noisy_label, 'clean_coarse_label': coarse_label_clean, 'noisy_coarse_label': coarse_label_noisy}
torch.save(new_dict, './data/CIFAR-100_human.pt')