-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_N_Ways_K_Shots_cub200.py
192 lines (150 loc) · 6.33 KB
/
random_N_Ways_K_Shots_cub200.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import argparse
import os
import sys
import numpy as np
import time
import torch
from PIL import Image
from torch.utils.data import Dataset
class CUB200(Dataset):
def __init__(self, root='./', train=True):
self.root = os.path.expanduser(root)
self.train = train # training set or test set
self._pre_operate(self.root)
def text_read(self, file):
with open(file, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
lines[i] = line.strip('\n')
return lines
def list2dict(self, list):
dict = {}
for l in list:
s = l.split(' ')
id = int(s[0])
cls = s[1]
if id not in dict.keys():
dict[id] = cls
else:
raise EOFError('The same ID can only appear once')
return dict
def _pre_operate(self, root):
image_file = os.path.join(root, 'CUB_200_2011/images.txt')
split_file = os.path.join(root, 'CUB_200_2011/train_test_split.txt')
class_file = os.path.join(root, 'CUB_200_2011/image_class_labels.txt')
id2image = self.list2dict(self.text_read(image_file))
id2train = self.list2dict(self.text_read(split_file)) # 1: train images; 0: test iamges
id2class = self.list2dict(self.text_read(class_file))
train_idx = []
test_idx = []
for k in sorted(id2train.keys()):
if id2train[k] == '1':
train_idx.append(k)
else:
test_idx.append(k)
self.data = []
self.targets = []
self.data2label = {}
if self.train:
for k in train_idx:
image_path = os.path.join(root, 'CUB_200_2011/images', id2image[k])
self.data.append(image_path)
self.targets.append(int(id2class[k]) - 1)
self.data2label[image_path] = (int(id2class[k]) - 1)
else:
for k in test_idx:
image_path = os.path.join(root, 'CUB_200_2011/images', id2image[k])
self.data.append(image_path)
self.targets.append(int(id2class[k]) - 1)
self.data2label[image_path] = (int(id2class[k]) - 1)
def getAllDataAndPaths(self):
return self.data, self.targets
def __len__(self):
return len(self.data)
def selectUpToKShotsPerIncrementalClass(data, targets, K_shots_split):
selected_data = []
selected_targets = []
for index in range(100):
indexOfIncrementalClass = index+100
temp_data = []
temp_targets = []
for j in range(len(data)):
if targets[j] == indexOfIncrementalClass:
temp_data.append(data[j])
temp_targets.append(targets[j])
chosen_indices = np.random.choice(range(len(temp_data)), K_shots_split[index], replace=False)
for k in chosen_indices:
selected_data.append(temp_data[k])
selected_targets.append(temp_targets[k])
return selected_data, selected_targets
def main(dataroot, N_ways, K_shots, Num_increments, Num_classes, exp_dir):
#dataroot = '/net/patriot/scratch/tahmad/FSCIL_datasets/CUB_200'
trainset = CUB200(root=dataroot, train=True)
print('Total number of examples in dataset')
print(trainset.__len__())
data, targets = trainset.getAllDataAndPaths()
#for k in range(trainset.__len__()):
# print(data[k], targets[k])
N_sum = 0
while (N_sum != 100):
N_ways_split = [np.random.randint(1,N_ways) for i in range(Num_increments)]
N_sum = np.sum(N_ways_split)
print('N_ways_split in incremental sessions')
print(N_ways_split)
print('Number of total incremental classes')
print(np.sum(N_ways_split))
classes_so_far = []
classes_so_far.append(100)
for j in range(len(N_ways_split)):
classes_so_far.append(100+np.sum(N_ways_split[:j+1]))
print('classes_so_far array')
print(classes_so_far)
fileName = exp_dir + 'class_dist_per_session.npy'
np.save(fileName,classes_so_far)
K_shots_split = [np.random.randint(1,K_shots) for i in range(Num_classes)]
print('K_shots_split')
print(K_shots_split)
print('Number of total incremental shots')
print(np.sum(K_shots_split))
selected_data, selected_targets = selectUpToKShotsPerIncrementalClass(data, targets, K_shots_split)
fileName = exp_dir + 'all_inc_sessions.txt'
file1 = open(fileName, 'w')
for k in range(len(selected_data)):
line = selected_data[k] + ',' + str(selected_targets[k])
file1.write(line)
file1.write('\n')
#print(selected_data[k], selected_targets[k])
file1.close()
for index in range(Num_increments):
num_class_so_excluding_this_session = np.sum(N_ways_split[:index])
num_class_so_including_this_session = np.sum(N_ways_split[:index+1])
#print('incremental session: ', index, 'classes:', int(num_class_so_excluding_this_session), num_class_so_including_this_session)
print('incremental session # ', index+1, ': classes up to this session:', num_class_so_including_this_session)
if num_class_so_excluding_this_session == 0:
start_line_from = 0
end_line_to = np.sum(K_shots_split[:num_class_so_including_this_session])
else:
start_line_from = np.sum(K_shots_split[:num_class_so_excluding_this_session])
end_line_to = np.sum(K_shots_split[:num_class_so_including_this_session])
#print(index, 'lines', start_line_from, end_line_to)
print('incremental session # ', index+1, ': shots for these ', N_ways_split[index] ,'classes: ', K_shots_split[int(num_class_so_excluding_this_session):int(num_class_so_including_this_session)])
if index+2 < 10:
fileName = exp_dir + 'session_0' + str(index+2) + '.txt'
else:
fileName = exp_dir + 'session_' + str(index+2) + '.txt'
file1 = open(fileName, 'w')
for k in range(start_line_from, end_line_to):
#line = selected_data[k] + ',' + str(selected_targets[k])
line = selected_data[k][39:]
file1.write(line)
file1.write('\n')
file1.close()
if __name__ == '__main__':
# arguments that need to be changed according to experimental setting
dataroot = '/scratch/tahmad/FSCIL_datasets/CUB_200'
N_ways = 6 #11
K_shots = 11 #11
Num_increments = 30 #15
Num_classes = 100
exp_dir = './experiments_cub200/experiment_21/'
main(dataroot, N_ways, K_shots, Num_increments, Num_classes, exp_dir)