(path, model, batch_size, dims, device, num_workers)
| 137 | return self.model(x) |
| 138 | |
| 139 | def get_activations(path, model, batch_size, dims, device, num_workers): |
| 140 | model.eval() |
| 141 | activations = [] |
| 142 | dataset = ImageFolder(path, transform=transforms.Compose([ |
| 143 | transforms.Resize((299, 299)), |
| 144 | transforms.ToTensor(), |
| 145 | ])) |
| 146 | dataloader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers) |
| 147 | |
| 148 | with torch.no_grad(): |
| 149 | for batch, _ in tqdm(dataloader, desc="Computing activations"): |
| 150 | pred = model(batch.to(device)) |
| 151 | if pred.size(1) != dims: |
| 152 | pred = adaptive_avg_pool2d(pred, 1).squeeze() |
| 153 | activations.append(pred.cpu().numpy()) |
| 154 | return np.concatenate(activations, axis=0) |
| 155 | |
| 156 | model = InceptionV3().to(device).eval() |
| 157 | is_main = not dist.is_initialized() or dist.get_rank() == 0 |
no outgoing calls
no test coverage detected