(model, num_images=6)
| 225 | # |
| 226 | |
| 227 | def visualize_model(model, num_images=6): |
| 228 | was_training = model.training |
| 229 | model.eval() |
| 230 | images_so_far = 0 |
| 231 | fig = plt.figure() |
| 232 | |
| 233 | with torch.no_grad(): |
| 234 | for i, (inputs, labels) in enumerate(dataloaders['val']): |
| 235 | inputs = inputs.to(device) |
| 236 | labels = labels.to(device) |
| 237 | |
| 238 | outputs = model(inputs) |
| 239 | _, preds = torch.max(outputs, 1) |
| 240 | |
| 241 | for j in range(inputs.size()[0]): |
| 242 | images_so_far += 1 |
| 243 | ax = plt.subplot(num_images//2, 2, images_so_far) |
| 244 | ax.axis('off') |
| 245 | ax.set_title(f'predicted: {class_names[preds[j]]}') |
| 246 | imshow(inputs.cpu().data[j]) |
| 247 | |
| 248 | if images_so_far == num_images: |
| 249 | model.train(mode=was_training) |
| 250 | return |
| 251 | model.train(mode=was_training) |
| 252 | |
| 253 | ###################################################################### |
| 254 | # Finetuning the ConvNet |
no test coverage detected