99from dataclasses import dataclass , field
1010import torch
1111
12+
1213def laplacian_pyramid_loss (
13- cortical_sheet : TensorType ["height" , "width" , "e" ], factor_w : float , factor_h : float , interpolation : str = "bilinear"
14+ cortical_sheet : TensorType ["height" , "width" , "e" ],
15+ factor_w : float ,
16+ factor_h : float ,
17+ interpolation : str = "bilinear" ,
1418):
1519 grid = cortical_sheet
1620 assert grid .ndim == 3 , "Expected grid to be a 3d tensor of shape (h, w, e)"
@@ -27,7 +31,9 @@ def laplacian_pyramid_loss(
2731 grid , scale_factor = (1 / factor_h , 1 / factor_w ), mode = interpolation
2832 )
2933 # Upscale the downscaled grid tensor
30- upscaled_grid = F .interpolate (downscaled_grid , size = grid .shape [2 :], mode = interpolation )
34+ upscaled_grid = F .interpolate (
35+ downscaled_grid , size = grid .shape [2 :], mode = interpolation
36+ )
3137
3238 # Calculate the MSE loss between the original grid and upscaled grid
3339 # loss = F.mse_loss(upscaled_grid, grid)
@@ -54,19 +60,30 @@ class LaplacianPyramid:
5460 scale : Optional [Union [None , float ]] = field (default = 1.0 )
5561
5662 @classmethod
57- def from_layer (cls , model , layer , factor_h , factor_w , scale = 1.0 , interpolation : str = "bilinear" ):
63+ def from_layer (
64+ cls ,
65+ model ,
66+ layer ,
67+ factor_h ,
68+ factor_w ,
69+ scale = 1.0 ,
70+ interpolation : str = "bilinear" ,
71+ ):
5872 layer_name = get_name_by_layer (model = model , layer = layer )
5973 return cls (
6074 layer_name = layer_name ,
6175 scale = scale ,
6276 factor_h = factor_h ,
6377 factor_w = factor_w ,
64- interpolation = interpolation
78+ interpolation = interpolation ,
6579 )
6680
6781
6882def laplacian_pyramid_loss_on_bias (
69- cortical_sheet : TensorType ["h" , "w" ], factor_w : float , factor_h : float , interpolation : str = "bilinear"
83+ cortical_sheet : TensorType ["h" , "w" ],
84+ factor_w : float ,
85+ factor_h : float ,
86+ interpolation : str = "bilinear" ,
7087):
7188
7289 grid = cortical_sheet
@@ -86,7 +103,9 @@ def laplacian_pyramid_loss_on_bias(
86103 grid , scale_factor = (1 / factor_h , 1 / factor_w ), mode = interpolation
87104 )
88105 # Upscale the downscaled grid tensor
89- upscaled_grid = F .interpolate (downscaled_grid , size = grid .shape [2 :], mode = interpolation )
106+ upscaled_grid = F .interpolate (
107+ downscaled_grid , size = grid .shape [2 :], mode = interpolation
108+ )
90109
91110 grid = rearrange (grid .squeeze (0 ).squeeze (0 ), "h w -> (h w)" ).unsqueeze (0 )
92111 upscaled_grid = rearrange (
@@ -113,7 +132,15 @@ class LaplacianPyramidOnBias:
113132 scale : Optional [Union [None , float ]] = field (default = 1.0 )
114133
115134 @classmethod
116- def from_layer (cls , model , layer , factor_h , factor_w , scale = 1.0 , interpolation : str = "bilinear" ):
135+ def from_layer (
136+ cls ,
137+ model ,
138+ layer ,
139+ factor_h ,
140+ factor_w ,
141+ scale = 1.0 ,
142+ interpolation : str = "bilinear" ,
143+ ):
117144 assert (
118145 layer .bias is not None
119146 ), "Expected layer to have a bias, but got None. *sad sad sad*"
@@ -123,9 +150,10 @@ def from_layer(cls, model, layer, factor_h, factor_w, scale=1.0, interpolation:
123150 scale = scale ,
124151 factor_h = factor_h ,
125152 factor_w = factor_w ,
126- interpolation = interpolation
153+ interpolation = interpolation ,
127154 )
128155
156+
129157@dataclass
130158class LaplacianPyramidOnInput :
131159 """
@@ -144,12 +172,20 @@ class LaplacianPyramidOnInput:
144172 scale : Optional [Union [None , float ]] = field (default = 1.0 )
145173
146174 @classmethod
147- def from_layer (cls , model , layer , factor_h , factor_w , scale = 1.0 , interpolation : str = "bilinear" ):
175+ def from_layer (
176+ cls ,
177+ model ,
178+ layer ,
179+ factor_h ,
180+ factor_w ,
181+ scale = 1.0 ,
182+ interpolation : str = "bilinear" ,
183+ ):
148184 layer_name = get_name_by_layer (model = model , layer = layer )
149185 return cls (
150186 layer_name = layer_name ,
151187 scale = scale ,
152188 factor_h = factor_h ,
153189 factor_w = factor_w ,
154- interpolation = interpolation
155- )
190+ interpolation = interpolation ,
191+ )
0 commit comments