File size: 11,897 Bytes
8966f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d82623
 
8966f37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from copy import deepcopy
from abc import ABC, abstractmethod
import torch
import torch.nn.functional as F
from safetensors.torch import load_file
from torch import nn
from torchvision.transforms import v2
import wandb


class BaseUnet(nn.Module, ABC):
    def __init__(
        self,
        in_channels: int,
        num_classes: int,
        n_organs: int,
        size: int = 32,
        depth: int = 3,
        **kwargs
    ):
        super().__init__()
        self.in_channels = in_channels
        self.out_channels = num_classes
        self.n_organs = n_organs
        self.size = size
        self.depth = depth
        self.distill = False
        self.distill_unet = False
        self.distill_model = None
        self.distill_adapter = None
        self.distill_loss = None
        self._build_model(**kwargs)

    @staticmethod
    def _pad_to_2d(x: torch.Tensor, stride: int):
        h, w = x.shape[-2:]

        new_h = h if h % stride == 0 else h + stride - (h % stride)
        new_w = w if w % stride == 0 else w + stride - (w % stride)

        top = (new_h - h) // 2
        bottom = (new_h - h) - top
        left = (new_w - w) // 2
        right = (new_w - w) - left

        pads = (left, right, top, bottom)
        x_pad = F.pad(x, pads, mode="constant", value=0)
        return x_pad, pads

    @staticmethod
    def _unpad_2d(x: torch.Tensor, pads):
        left, right, top, bottom = pads

        if top or bottom:
            end_h = -bottom if bottom > 0 else None
            x = x[:, :, top:end_h, :]

        if left or right:
            end_w = -right if right > 0 else None
            x = x[:, :, :, left:end_w]

        return x

    @abstractmethod
    def _build_model(self, **kwargs):
        pass

    def _prepare_forward(
        self,
        *,
        pixel_values: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        **kwargs,
    ) -> dict:
        return {}

    @abstractmethod
    def _encode(
        self,
        layer: nn.Module,
        x: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
    ):
        pass

    @abstractmethod
    def _bottleneck(
        self,
        x: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
    ):
        pass

    @abstractmethod
    def _decode(
        self,
        layer: nn.Module,
        x: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
    ):
        pass

    def encode(
        self,
        x: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
    ):
        feat_list = []
        pads = None

        pre_padding = (
            (x.size(-1) % 2**self.depth != 0)
            or (x.size(-2) % 2**self.depth != 0)
            or (x.size(-3) % 2**self.depth != 0)
        )
        if pre_padding:
            x, pads = self._pad_to_2d(x, 2**self.depth)

        out, feat = self._encode(
            self.encoder["0"], x, organ_id=organ_id, forward_ctx=forward_ctx
        )
        feat_list.append(feat)

        for key in list(self.encoder.keys())[1:]:
            out, feat = self._encode(
                self.encoder[key], out, organ_id=organ_id, forward_ctx=forward_ctx
            )
            feat_list.append(feat)

        out = self._bottleneck(out, organ_id=organ_id, forward_ctx=forward_ctx)
        return out, feat_list, pads

    def decode(
        self,
        out: torch.Tensor,
        feat_list: list[torch.Tensor],
        pads,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
    ):
        for key in self.decoder:
            out = self._decode(
                self.decoder[key],
                torch.cat((out, feat_list[int(key)]), dim=1),
                organ_id=organ_id,
                forward_ctx=forward_ctx,
            )
            del feat_list[int(key)]

        out = self.out_layer(out)
        if pads is not None:
            out = self._unpad_2d(out, pads).squeeze(1)
        return out

    def _apply_auxiliary_losses(
        self,
        *,
        loss: torch.Tensor | float,
        logits: torch.Tensor,
        masks: torch.Tensor | None,
        organ_id: torch.Tensor | None = None,
        forward_ctx: dict | None = None,
        **kwargs,
    ):
        return loss

    def _init_distillation(
        self,
        *,
        distill: bool = False,
        distill_unet: bool = False,
        medsam_teacher_ckpt: str = "/work/phd_ultrasounds/UUSIC_new/checkpoints/medsam_unfreezed/model.safetensors",
        unet_teacher_ckpt: str = "/work/phd_ultrasounds/UUSIC_new/checkpoints/unet5_attn_distilled/model.safetensors",
        unet_teacher_kwargs: dict | None = None,
    ):
        if distill and distill_unet:
            raise ValueError("distill and distill_unet cannot both be enabled.")

        self.distill = bool(distill)
        self.distill_unet = bool(distill_unet)
        self.distill_model = None
        self.distill_adapter = None
        self.distill_loss = None

        if not self.distill and not self.distill_unet:
            return

        from .segm_net import DistillationLoss, MedSAM

        student_channels = (2048 // (32 // self.size)) // (2 ** (5 - self.depth))

        if self.distill:
            self.distill_adapter = nn.Conv2d(student_channels, 256, kernel_size=1)
        else:
            self.distill_adapter = nn.Conv2d(student_channels, 2048, kernel_size=1)

        if self.distill:
            import importlib
            sam_model_registry = importlib.import_module("segment_anything").sam_model_registry
            from utils.paths import MEDSAM_BASE_WEIGHTS

            sam_model = sam_model_registry["vit_b"](checkpoint=MEDSAM_BASE_WEIGHTS)
            self.distill_model = MedSAM(
                image_encoder=deepcopy(sam_model.image_encoder),
                mask_decoder=deepcopy(sam_model.mask_decoder),
                prompt_encoder=deepcopy(sam_model.prompt_encoder),
                predict_bboxes=True,
                freeze_image_encoder=0,
            )
            state_dict = load_file(medsam_teacher_ckpt)
            load_result = self.distill_model.load_state_dict(state_dict)
            print(f"Loaded MedSam teacher model and loaded weights:\n{load_result}")
        else:
            from .unet_attn import UNet2DAttn

            teacher_kwargs = {
                "in_channels": 3,
                "num_classes": 1,
                "n_organs": 10,
                "size": 32,
                "depth": 5,
                "attn_start": 0,
                "use_attn": True,
                "img_size": 512,
                "patch_size": 8,
                "emb_dim": 768,
                "n_heads": 8,
                "distill": False,
                "distill_unet": False,
                "use_dwt": False,
                "wavelet": "haar",
                "use_shape": False,
                "shape_res": 64,
            }
            if unet_teacher_kwargs is not None:
                teacher_kwargs.update(unet_teacher_kwargs)

            self.distill_model = UNet2DAttn(**teacher_kwargs)
            state_dict = load_file(unet_teacher_ckpt)
            state_dict = {k: v for k, v in state_dict.items() if "distill" not in k}
            load_result = self.distill_model.load_state_dict(state_dict)
            print(f"Loaded UNet teacher model and loaded weights:\n{load_result}")

        for p in self.distill_model.parameters():
            p.requires_grad = False
        self.distill_model.eval()
        self.distill_loss = DistillationLoss()

    def _forward_distillation(
        self,
        *,
        student_logits: torch.Tensor,
        student_bottleneck: torch.Tensor,
        pixel_values: torch.Tensor,
        organ_id: torch.Tensor | None = None,
        pixel_values_medsam: torch.Tensor | None = None,
    ) -> torch.Tensor | None:
        if not self.distill and not self.distill_unet:
            return None

        if self.distill:
            if pixel_values_medsam is None:
                raise ValueError("pixel_values_medsam is required when distill=True.")

            with torch.no_grad():
                up_pixel_values = v2.functional.resize(
                    pixel_values_medsam, 1024, v2.InterpolationMode.BICUBIC
                )
                image_embedding = self.distill_model.image_encoder(up_pixel_values)

            student_resized = F.interpolate(
                student_bottleneck,
                size=(image_embedding.shape[-1], image_embedding.shape[-1]),
                mode="bilinear",
                align_corners=False,
            )
            up_feat = self.distill_adapter(student_resized)
            distill_loss_emb = self.distill_loss(
                student_logits=up_feat,
                teacher_logits=image_embedding.detach(),
            )

            if wandb.run is not None:
                wandb.log(
                    {
                        "distill_loss_emb": distill_loss_emb["loss"].item(),
                    },
                    commit=False,
                )

            return distill_loss_emb["loss"]

        with torch.no_grad():
            forward_ctx = self._prepare_forward(
                pixel_values=pixel_values,
                organ_id=organ_id,
            )

            teacher_embedding, _, _ = self.encode(
                pixel_values, organ_id=organ_id, forward_ctx=forward_ctx
            )

        student_resized = F.interpolate(
                student_bottleneck,
                size=(teacher_embedding.shape[-1], teacher_embedding.shape[-1]),
                mode="bilinear",
                align_corners=False,
            )
        up_feat = self.distill_adapter(student_resized)
        distill_loss_emb = self.distill_loss(
            student_logits=student_bottleneck,
            teacher_logits=teacher_embedding.detach(),
        )
        if wandb.run is not None:
            wandb.log(
                {"distill_loss_logits": distill_loss_emb["loss"].item()},
                commit=False,
            )
        return distill_loss_emb["loss"]

    def forward(
        self,
        pixel_values,
        organ_id=None,
        labels=None,
        masks=None,
        bbox_coords=None,
        organ_id_metric=None,
        teacher_embedding=None,
        teacher_mask=None,
        pixel_values_medsam=None,
        **kwargs,
    ):
        forward_ctx = self._prepare_forward(
            pixel_values=pixel_values,
            organ_id=organ_id,
            masks=masks,
            **kwargs,
        )

        out_bottleneck, feat_list, pads = self.encode(
            pixel_values, organ_id=organ_id, forward_ctx=forward_ctx
        )
        out = self.decode(
            out_bottleneck,
            feat_list,
            pads,
            organ_id=organ_id,
            forward_ctx=forward_ctx,
        )

        if masks is not None:
            loss = self.criterion(out, masks)
        else:
            loss = 0.0

        distill_loss = self._forward_distillation(
            student_logits=out,
            student_bottleneck=out_bottleneck,
            pixel_values=pixel_values,
            organ_id=organ_id,
            pixel_values_medsam=pixel_values_medsam,
        )
        if distill_loss is not None:
            loss = loss + distill_loss

        loss = self._apply_auxiliary_losses(
            loss=loss,
            logits=out,
            masks=masks,
            organ_id=organ_id,
            forward_ctx=forward_ctx,
            pixel_values=pixel_values,
            **kwargs,
        )

        return {
            "loss": loss,
            "logits": out,
            "labels": masks,
            "organ_id": organ_id,
            "organ_id_metric": organ_id_metric,
        }