1- from __future__ import annotations
21import os
32import torch .nn as nn
43from einops import rearrange
1211 LaplacianPyramid ,
1312 laplacian_pyramid_loss_on_bias ,
1413 LaplacianPyramidOnBias ,
15- LaplacianPyramidOnInput
14+ LaplacianPyramidOnInput ,
1615)
1716from .cortical_sheet .output import find_cortical_sheet_size
1817
18+
1919class 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