| 37 | self.ref_database[dataset] = self.ref_database[dataset][:m] |
| 38 | |
| 39 | def constructExampleSet(self, args): |
| 40 | # breakpoint() |
| 41 | print("Constructing Example Set") |
| 42 | self.ref_names.append(self.cur_dataset) |
| 43 | new_dataset = torch.tensor(self.getNewDataset(args)) |
| 44 | image_feature = [] |
| 45 | num = new_dataset.shape[0] |
| 46 | |
| 47 | print("[Constructing] Calculating Distance") |
| 48 | for ndx in tqdm(np.arange(num)): |
| 49 | img = torch.unsqueeze(new_dataset[ndx], dim=0) |
| 50 | img = img.cuda() |
| 51 | img_feature = self.ref_model(img, None) |
| 52 | image_feature.append(img_feature.cpu().detach().tolist()) |
| 53 | image_feature = torch.tensor(image_feature) |
| 54 | image_feature = torch.squeeze(image_feature, dim=1) |
| 55 | image_feature = image_feature / image_feature.norm(dim=-1, keepdim=True) |
| 56 | image_feature = np.array(image_feature.cpu().detach()) |
| 57 | image_feature_average = image_feature.mean(axis=0) |
| 58 | |
| 59 | K, t = self.memory_size, len(self.ref_names) |
| 60 | m = K - K // t |
| 61 | update_dataset = [] |
| 62 | cur_embedding_sum = None |
| 63 | print("[Constructing] Collecting Examples") |
| 64 | for k in tqdm(np.arange(min(m, len(image_feature)))): |
| 65 | if not k: |
| 66 | index = np.argmin( |
| 67 | np.sum((image_feature_average - image_feature)**2, axis=1) |
| 68 | ) |
| 69 | cur_embedding_sum = image_feature[index] |
| 70 | update_dataset.append((new_dataset.cpu())[index].tolist()) |
| 71 | image_feature = np.delete(image_feature, index, axis=0) |
| 72 | else: |
| 73 | index = np.argmin( |
| 74 | np.sum(( |
| 75 | image_feature_average - (1/(k+1))*(image_feature + cur_embedding_sum) |
| 76 | )**2, axis=1) |
| 77 | ) |
| 78 | cur_embedding_sum += image_feature[index] |
| 79 | update_dataset.append((new_dataset.cpu())[index].tolist()) |
| 80 | image_feature = np.delete(image_feature, index, axis=0) |
| 81 | |
| 82 | self.ref_database[self.cur_dataset] = update_dataset |
| 83 | |
| 84 | def getNewDataset(self, args): |
| 85 | dataset_class = getattr(datasets, self.cur_dataset) |