Hannes Kuchelmeister
commited on
Commit
•
dce8df2
1
Parent(s):
72d30c1
Add simple fully convolutional network
Browse files
configs/experiment/focusConvMSE_150.yaml
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# @package _global_
|
2 |
+
|
3 |
+
# to execute this experiment run:
|
4 |
+
# python train.py experiment=example
|
5 |
+
|
6 |
+
defaults:
|
7 |
+
- override /datamodule: focus150.yaml
|
8 |
+
- override /model: focusConv_150.yaml
|
9 |
+
- override /callbacks: default.yaml
|
10 |
+
- override /logger: many_loggers
|
11 |
+
- override /trainer: default.yaml
|
12 |
+
|
13 |
+
# all parameters below will be merged with parameters from default configurations set above
|
14 |
+
# this allows you to overwrite only specified parameters
|
15 |
+
|
16 |
+
# name of the run determines folder name in logs
|
17 |
+
name: "focusConvMSE_150"
|
18 |
+
seed: 12345
|
19 |
+
|
20 |
+
trainer:
|
21 |
+
min_epochs: 1
|
22 |
+
max_epochs: 100
|
23 |
+
|
24 |
+
model:
|
25 |
+
image_size: 150
|
26 |
+
pool_size: 2
|
27 |
+
conv1_size: 5
|
28 |
+
conv1_channels: 6
|
29 |
+
conv2_size: 5
|
30 |
+
conv2_channels: 16
|
31 |
+
lin1_size: 100
|
32 |
+
lin2_size: 80
|
33 |
+
output_size: 1
|
34 |
+
lr: 0.001
|
35 |
+
weight_decay: 0.0005
|
36 |
+
|
37 |
+
datamodule:
|
38 |
+
batch_size: 128
|
39 |
+
|
configs/model/focusConv_150.yaml
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
_target_: src.models.focus_conv_module.FocusConvLitModule
|
2 |
+
|
3 |
+
image_size: 150
|
4 |
+
pool_size: 2
|
5 |
+
conv1_size: 5
|
6 |
+
conv1_channels: 6
|
7 |
+
conv2_size: 5
|
8 |
+
conv2_channels: 16
|
9 |
+
lin1_size: 100
|
10 |
+
lin2_size: 80
|
11 |
+
output_size: 1
|
12 |
+
lr: 0.001
|
13 |
+
weight_decay: 0.0005
|
src/models/focus_conv_module.py
CHANGED
@@ -9,54 +9,42 @@ from torchmetrics.classification.accuracy import Accuracy
|
|
9 |
|
10 |
|
11 |
class SimpleConvNet(nn.Module):
|
12 |
-
def __init__(self):
|
13 |
super().__init__()
|
14 |
-
self.conv1 = nn.Conv2d(3, 6, 5)
|
15 |
-
self.pool = nn.MaxPool2d(2, 2)
|
16 |
-
self.conv2 = nn.Conv2d(6, 16, 5)
|
17 |
-
self.pool = nn.MaxPool2d(2, 2)
|
18 |
-
self.conv3 = nn.Conv2d(6, 16, 5)
|
19 |
-
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
20 |
-
self.fc2 = nn.Linear(120, 84)
|
21 |
-
self.fc3 = nn.Linear(84, 10)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
x = self.fc3(x)
|
30 |
-
return x
|
31 |
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
self.model = nn.Sequential(
|
38 |
-
nn.
|
39 |
-
nn.
|
40 |
-
nn.
|
41 |
-
nn.
|
42 |
-
nn.
|
43 |
-
nn.
|
44 |
-
nn.Linear(
|
45 |
-
nn.
|
46 |
-
nn.ReLU(),
|
47 |
-
nn.Linear(hparams["lin3_size"], hparams["output_size"]),
|
48 |
)
|
49 |
|
50 |
def forward(self, x):
|
51 |
-
|
52 |
-
|
53 |
-
# (batch, 1, width, height) -> (batch, 1*width*height)
|
54 |
-
x = x.view(batch_size, -1)
|
55 |
-
|
56 |
-
return self.model(x)
|
57 |
|
58 |
|
59 |
-
class
|
60 |
"""
|
61 |
Example of LightningModule for MNIST classification.
|
62 |
|
@@ -73,10 +61,14 @@ class FocusLitModule(LightningModule):
|
|
73 |
|
74 |
def __init__(
|
75 |
self,
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
80 |
output_size: int = 1,
|
81 |
lr: float = 0.001,
|
82 |
weight_decay: float = 0.0005,
|
@@ -87,10 +79,10 @@ class FocusLitModule(LightningModule):
|
|
87 |
# it also ensures init params will be stored in ckpt
|
88 |
self.save_hyperparameters(logger=False)
|
89 |
|
90 |
-
self.model =
|
91 |
|
92 |
# loss function
|
93 |
-
self.criterion = torch.nn.
|
94 |
|
95 |
# use separate metric instance for train, val and test step
|
96 |
# to ensure a proper reduction over the epoch
|
@@ -108,7 +100,7 @@ class FocusLitModule(LightningModule):
|
|
108 |
x = batch["image"]
|
109 |
y = batch["focus_value"]
|
110 |
logits = self.forward(x)
|
111 |
-
loss = self.criterion(logits, y)
|
112 |
preds = torch.squeeze(logits)
|
113 |
return loss, preds, y
|
114 |
|
@@ -122,7 +114,8 @@ class FocusLitModule(LightningModule):
|
|
122 |
|
123 |
# we can return here dict with any tensors
|
124 |
# and then read it in some callback or in `training_epoch_end()`` below
|
125 |
-
# remember to always return loss from `training_step()` or else
|
|
|
126 |
return {"loss": loss, "preds": preds, "targets": targets}
|
127 |
|
128 |
def training_epoch_end(self, outputs: List[Any]):
|
@@ -169,7 +162,8 @@ class FocusLitModule(LightningModule):
|
|
169 |
def configure_optimizers(self):
|
170 |
"""Choose what optimizers and learning-rate schedulers.
|
171 |
|
172 |
-
Normally you'd need one. But in the case of GANs or similar you might
|
|
|
173 |
|
174 |
See examples here:
|
175 |
https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers
|
|
|
9 |
|
10 |
|
11 |
class SimpleConvNet(nn.Module):
|
12 |
+
def __init__(self, hparams):
|
13 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
pool_size = hparams["pool_size"] # 2
|
16 |
+
conv1_size = hparams["conv1_size"] # 5
|
17 |
+
conv1_out = hparams["conv1_channels"] # 6
|
18 |
+
conv2_size = hparams["conv1_channels"] # 5
|
19 |
+
conv2_out = hparams["conv2_channels"] # 16
|
20 |
+
size_img = hparams["image_size"] # 150
|
|
|
|
|
21 |
|
22 |
+
lin1_size = hparams["lin1_size"] # 100
|
23 |
+
lin2_size = hparams["lin2_size"] # 80
|
24 |
+
output_size = hparams["output_size"] # 1
|
25 |
|
26 |
+
size_img -= conv1_size - 1
|
27 |
+
size_img = int((size_img) / pool_size)
|
28 |
+
size_img -= conv2_size - 1
|
29 |
+
size_img = int(size_img / pool_size)
|
30 |
|
31 |
self.model = nn.Sequential(
|
32 |
+
nn.Conv2d(3, conv1_out, conv1_size),
|
33 |
+
nn.MaxPool2d(pool_size, pool_size),
|
34 |
+
nn.Conv2d(conv1_out, conv2_out, conv2_size),
|
35 |
+
nn.MaxPool2d(pool_size, pool_size),
|
36 |
+
nn.Flatten(),
|
37 |
+
nn.Linear(conv2_out * size_img * size_img, lin1_size),
|
38 |
+
nn.Linear(lin1_size, lin2_size),
|
39 |
+
nn.Linear(lin2_size, output_size),
|
|
|
|
|
40 |
)
|
41 |
|
42 |
def forward(self, x):
|
43 |
+
x = self.model(x)
|
44 |
+
return x
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
+
class FocusConvLitModule(LightningModule):
|
48 |
"""
|
49 |
Example of LightningModule for MNIST classification.
|
50 |
|
|
|
61 |
|
62 |
def __init__(
|
63 |
self,
|
64 |
+
image_size: int = 150,
|
65 |
+
pool_size: int = 2,
|
66 |
+
conv1_size: int = 5,
|
67 |
+
conv1_channels: int = 6,
|
68 |
+
conv2_size: int = 5,
|
69 |
+
conv2_channels: int = 16,
|
70 |
+
lin1_size: int = 100,
|
71 |
+
lin2_size: int = 80,
|
72 |
output_size: int = 1,
|
73 |
lr: float = 0.001,
|
74 |
weight_decay: float = 0.0005,
|
|
|
79 |
# it also ensures init params will be stored in ckpt
|
80 |
self.save_hyperparameters(logger=False)
|
81 |
|
82 |
+
self.model = SimpleConvNet(hparams=self.hparams)
|
83 |
|
84 |
# loss function
|
85 |
+
self.criterion = torch.nn.MSELoss()
|
86 |
|
87 |
# use separate metric instance for train, val and test step
|
88 |
# to ensure a proper reduction over the epoch
|
|
|
100 |
x = batch["image"]
|
101 |
y = batch["focus_value"]
|
102 |
logits = self.forward(x)
|
103 |
+
loss = self.criterion(logits, y.unsqueeze(1))
|
104 |
preds = torch.squeeze(logits)
|
105 |
return loss, preds, y
|
106 |
|
|
|
114 |
|
115 |
# we can return here dict with any tensors
|
116 |
# and then read it in some callback or in `training_epoch_end()`` below
|
117 |
+
# remember to always return loss from `training_step()` or else
|
118 |
+
# backpropagation will fail!
|
119 |
return {"loss": loss, "preds": preds, "targets": targets}
|
120 |
|
121 |
def training_epoch_end(self, outputs: List[Any]):
|
|
|
162 |
def configure_optimizers(self):
|
163 |
"""Choose what optimizers and learning-rate schedulers.
|
164 |
|
165 |
+
Normally you'd need one. But in the case of GANs or similar you might
|
166 |
+
have multiple.
|
167 |
|
168 |
See examples here:
|
169 |
https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers
|