Skip to content

Commit 09d4b2d

Browse files
committed
core: add new arg strict_layer_type + formatting
1 parent db00642 commit 09d4b2d

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

topoloss/core.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import annotations
21
import os
32
import torch.nn as nn
43
from einops import rearrange
@@ -12,26 +11,31 @@
1211
LaplacianPyramid,
1312
laplacian_pyramid_loss_on_bias,
1413
LaplacianPyramidOnBias,
15-
LaplacianPyramidOnInput
14+
LaplacianPyramidOnInput,
1615
)
1716
from .cortical_sheet.output import find_cortical_sheet_size
1817

18+
1919
class TopoLoss:
2020
def __init__(
2121
self,
2222
losses: list[Union[LaplacianPyramid]],
23+
strict_layer_type: bool = True, ## this is a safety feature to make sure you're not using something other than a conv or linear layer
2324
):
2425
self.losses = losses
26+
self.strict_layer_type = strict_layer_type ## this is a safety feature to make sure you're not using something other than a conv or linear layer
2527

2628
def check_layer_type(self, layer):
27-
assert isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear), f"Expect layer to be either nn.Conv2d or nn.Linear, but got: {type(layer)}"
28-
29+
assert isinstance(layer, nn.Conv2d) or isinstance(
30+
layer, nn.Linear
31+
), f"Expect layer to be either nn.Conv2d or nn.Linear, but got: {type(layer)}"
2932

3033
def get_layerwise_topo_losses(self, model, do_scaling: bool = True) -> dict:
3134
layer_wise_losses = {}
3235
for loss_info in self.losses:
3336
layer = get_layer_by_name(model=model, layer_name=loss_info.layer_name)
34-
self.check_layer_type(layer=layer)
37+
if self.strict_layer_type is True:
38+
self.check_layer_type(layer=layer)
3539

3640
if isinstance(loss_info, LaplacianPyramid):
3741
if isinstance(layer, nn.Linear):
@@ -93,7 +97,9 @@ def get_layerwise_topo_losses(self, model, do_scaling: bool = True) -> dict:
9397
return layer_wise_losses
9498

9599
def get_wandb_logging_dict(self, model: nn.Module):
96-
layer_wise_losses = self.get_layerwise_topo_losses(do_scaling=False, model=model)
100+
layer_wise_losses = self.get_layerwise_topo_losses(
101+
do_scaling=False, model=model
102+
)
97103
for key in layer_wise_losses:
98104
layer_wise_losses[key] = layer_wise_losses[key].item()
99105

@@ -118,21 +124,22 @@ def save_json(self, filename: str):
118124
data = []
119125
for loss in self.losses:
120126
d = loss.__dict__
121-
d['name'] = loss.__class__.__name__
127+
d["name"] = loss.__class__.__name__
122128
data.append(d)
123129

124130
with open(filename, "w") as f:
125131
json.dump(data, f, indent=4)
126-
132+
127133
@classmethod
128134
def from_json(cls, filename: str):
129135
assert os.path.exists(filename), f"File not found: {filename}"
130136
with open(filename, "r") as f:
131137
data = json.load(f)
132-
assert isinstance(data, list), f"Expected data to be a list but got: {type(data)}"
138+
assert isinstance(
139+
data, list
140+
), f"Expected data to be a list but got: {type(data)}"
133141
losses = []
134142
for d in data:
135143
name = d.pop("name")
136144
losses.append(globals()[name](**d))
137145
return cls(losses=losses)
138-

0 commit comments

Comments
 (0)