-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassification.py
105 lines (94 loc) · 4.21 KB
/
classification.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
import os
import torch
from torch import Tensor
from torch.nn.parameter import Parameter
from torch.utils.data import Dataset
from typing import Iterator, List
from base_experiment import BaseExperiment
from dataset import get_example_shape
from models import create_model
from plot import get_plot_fn, get_random_example_with_label
class ClassificationExperiment(BaseExperiment):
def __init__(self, config: dict, enable_tune: bool = False, **kwargs):
super().__init__(config=config, enable_tune=enable_tune, **kwargs)
self.classifier = create_model(**config['model_params'],
input_shape=get_example_shape(
config['exp_params']['data']))
def sample_images(self, plot: dict, batches: List[Tensor]):
test_input = []
predictions = []
targets = []
for class_batch in batches:
class_input = []
class_predictions = []
class_targets = []
for x, y in class_batch:
x = x.unsqueeze(0)
class_input.append(x)
x = self.classifier(x.to(self.curr_device)).detach().cpu()
class_predictions.append(x)
class_targets.append(y.unsqueeze(0))
class_input = torch.cat(class_input, dim=0)
test_input.append(class_input.unsqueeze(0))
predictions.append(
torch.cat(class_predictions, dim=0).unsqueeze(0))
targets.append(torch.cat(class_targets, dim=0).unsqueeze(0))
test_input = torch.cat(test_input, dim=0)
targets = torch.cat(targets, dim=0)
predictions = torch.cat(predictions, dim=0)
# Extensionless output path (let plotting function choose extension)
out_path = os.path.join(
self.logger.save_dir, self.logger.name,
f"version_{self.logger.version}",
f"{self.logger.name}_{plot['fn']}_{self.global_step}")
fn = get_plot_fn(plot['fn'])
image = fn(test_input=test_input,
targets=targets,
predictions=predictions,
classes=plot['classes'],
out_path=out_path,
**plot['params'])
self.logger.experiment.add_image(plot['fn'], image, self.global_step)
vis = self.visdom()
if vis is not None:
vis.image(image, win=plot['fn'])
def training_step(self, batch, batch_idx):
real_img, labels = batch
self.curr_device = self.device
real_img = real_img.to(self.curr_device)
y = self.classifier(real_img)
train_loss = self.classifier.loss_function(
y.cpu(), labels.cpu(), **self.params.get('loss_params', {}))
self.log_train_step(train_loss)
return train_loss
def validation_step(self, batch, batch_idx):
real_img, labels = batch
self.curr_device = self.device
real_img = real_img.to(self.curr_device)
y = self.classifier(real_img)
val_loss = self.classifier.loss_function(
y.cpu(), labels.cpu(), **self.params.get('loss_params', {}))
self.log_val_step(val_loss)
return val_loss
def trainable_parameters(self) -> Iterator[Parameter]:
return self.classifier.parameters()
def get_val_batches(self, dataset: Dataset) -> list:
val_batches = []
for plot in self.plots:
classes = plot['classes']
examples_per_class = plot['examples_per_class']
class_batches = []
for obj in classes:
batch = []
class_indices = []
for _ in range(examples_per_class):
idx = get_random_example_with_label(dataset,
torch.Tensor(
obj['labels']),
all_=obj['all'],
exclude=class_indices)
batch.append(dataset[idx])
class_indices.append(idx)
class_batches.append(batch)
val_batches.append(class_batches)
return val_batches