fyrizi commited on
Commit
bc84937
·
verified ·
1 Parent(s): 8ebad6e

Delete main/src/model_unet_multiclass_deepsupervision_configured_v1.py with huggingface_hub

Browse files
main/src/model_unet_multiclass_deepsupervision_configured_v1.py DELETED
@@ -1,560 +0,0 @@
1
- from functools import partial
2
- from timeit import repeat
3
- from torch import nn
4
- from basic_typing import Batch
5
- from layers.unet_base import LatentConv, UNetBase
6
- from layers.blocks import BlockConvNormActivation, BlockUpsampleNnConvNormActivation
7
- from typing import Dict, Optional
8
- import torch
9
-
10
- import torch
11
- import torch.nn as nn
12
- import torch.nn.functional as F
13
-
14
- from model_unet_multiclass import ModelUNetMulticlass, LossDiceCrossEntropyFocal
15
- from model_unet_multiclass_deepsupervision import ModelUNetMulticlassDeepSupervision, LossDiceCrossEntropyFocal2
16
-
17
- from layers.blocks import BlockConvNormActivation, BlockUpDeconvSkipConv
18
- from layers.layer_config import LayerConfig, default_layer_config, NormType
19
- from layers.unet_base import BlockConvType, BlockTypeConvSkip, Up
20
- from layers.unet_attention import MergeBlockAttention_Gating_Input
21
-
22
-
23
- class Down(nn.Module):
24
- def __init__(
25
- self,
26
- layer_config: LayerConfig,
27
- bloc_level: int,
28
- input_channels: int,
29
- output_channels: int,
30
- block: BlockConvType = BlockConvNormActivation,
31
- **block_kwargs):
32
- super().__init__()
33
-
34
- ops = [
35
- block(
36
- config=layer_config,
37
- input_channels=input_channels,
38
- output_channels=output_channels,
39
- **block_kwargs
40
- ),
41
- block(
42
- config=layer_config,
43
- input_channels=output_channels,
44
- output_channels=output_channels,
45
- )
46
- ]
47
-
48
- self.ops = nn.Sequential(*ops)
49
-
50
- def forward(self, x: torch.Tensor) -> torch.Tensor:
51
- return self.ops(x)
52
-
53
-
54
- SimpleMulticlassUNet_dice_ce_fov_v5 = partial(
55
- ModelUNetMulticlass,
56
- model=UNetBase(
57
- dim=3,
58
- input_channels=2,
59
- channels=[32, 64, 128, 256, 320],
60
- output_channels=2,
61
- config=default_layer_config(
62
- norm_type=NormType.InstanceNorm,
63
- conv_kwargs={'padding': 'same', 'bias': False},
64
- deconv_kwargs={'padding': 'same'},
65
- norm_kwargs={'affine': True},
66
- activation=partial(nn.LeakyReLU, negative_slope=0.01)
67
- ),
68
- activation=None,
69
- down_block_fn=Down,
70
- # , deconv_kernel_size=2
71
- up_block_fn=partial(Up, block=partial(BlockUpDeconvSkipConv, nb_repeats=2)),
72
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=3))
73
- ),
74
- loss_fn=partial(LossDiceCrossEntropyFocal, ce_factor=1.0)
75
- )
76
-
77
-
78
- SimpleMulticlassUNet_dice_ce_fov_v6 = partial(
79
- ModelUNetMulticlassDeepSupervision,
80
- model=UNetBase(
81
- dim=3,
82
- input_channels=2,
83
- channels=[48, 64, 128, 256, 320],
84
- output_channels=2,
85
- config=default_layer_config(
86
- norm_type=NormType.InstanceNorm,
87
- #conv_kwargs={'padding': 'same', 'bias': False},
88
- conv_kwargs={'padding': 'same'},
89
- deconv_kwargs={'padding': 'same'},
90
- #norm_kwargs={'affine': True},
91
- activation=partial(nn.LeakyReLU, negative_slope=0.01)
92
- ),
93
- activation=None,
94
- down_block_fn=Down,
95
- up_block_fn=partial(Up, block=partial(BlockUpDeconvSkipConv, nb_repeats=2)),
96
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=3))
97
- ),
98
- with_deep_supervision=False,
99
- #loss_fn=LossDiceCrossEntropyFocal2_nnUnet
100
- loss_fn=LossDiceCrossEntropyFocal2
101
- )
102
-
103
-
104
- SimpleMulticlassUNet_dice_ce_fov_v6_ds = partial(
105
- ModelUNetMulticlassDeepSupervision,
106
- model=UNetBase(
107
- dim=3,
108
- input_channels=2,
109
- channels=[48, 64, 128, 256, 320],
110
- output_channels=2,
111
- config=default_layer_config(
112
- norm_type=NormType.InstanceNorm,
113
- #conv_kwargs={'padding': 'same', 'bias': False},
114
- conv_kwargs={'padding': 'same'},
115
- deconv_kwargs={'padding': 'same'},
116
- #norm_kwargs={'affine': True},
117
- activation=partial(nn.LeakyReLU, negative_slope=0.01)
118
- ),
119
- activation=None,
120
- down_block_fn=Down,
121
- up_block_fn=partial(Up, block=partial(BlockUpDeconvSkipConv, nb_repeats=2)),
122
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=3))
123
- ),
124
- with_deep_supervision=True,
125
- #loss_fn=LossDiceCrossEntropyFocal2_nnUnet
126
- loss_fn=LossDiceCrossEntropyFocal2
127
- )
128
-
129
-
130
- SimpleMulticlassUNet_dice_ce_fov_v7_ds_lung = partial(
131
- ModelUNetMulticlassDeepSupervision,
132
- model=UNetBase(
133
- dim=3,
134
- input_channels=3,
135
- channels=[64, 96, 128, 156],
136
- output_channels=2,
137
- config=default_layer_config(
138
- norm_type=NormType.InstanceNorm,
139
- activation=nn.LeakyReLU,
140
- norm_kwargs={'affine': True},
141
- ),
142
- activation=None,
143
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
144
- ),
145
- with_deep_supervision=True,
146
- loss_fn=LossDiceCrossEntropyFocal2,
147
- with_ct_lung=True,
148
- input_target_shape=(3, 96, 96, 96)
149
- )
150
-
151
- SimpleMulticlassUNet_dice_ce_fov_v8_ds_lung = partial(
152
- ModelUNetMulticlassDeepSupervision,
153
- model=UNetBase(
154
- dim=3,
155
- input_channels=3,
156
- channels=[64, 96, 128, 156],
157
- output_channels=2,
158
- config=default_layer_config(
159
- norm_type=NormType.InstanceNorm,
160
- activation=nn.LeakyReLU,
161
- norm_kwargs={'affine': True},
162
- ),
163
- activation=None,
164
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
165
- ),
166
- with_deep_supervision=True,
167
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=1.0, gamma=1.0),
168
- with_ct_lung=True,
169
- input_target_shape=(3, 96, 96, 96)
170
- )
171
-
172
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot = partial(
173
- ModelUNetMulticlassDeepSupervision,
174
- model=UNetBase(
175
- dim=3,
176
- input_channels=5,
177
- channels=[64, 96, 128, 156],
178
- output_channels=2,
179
- config=default_layer_config(
180
- norm_type=NormType.InstanceNorm,
181
- activation=nn.LeakyReLU,
182
- norm_kwargs={'affine': True},
183
- ),
184
- activation=None,
185
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
186
- ),
187
- with_deep_supervision=True,
188
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=1.0, gamma=1.0),
189
- with_ct_lung=True,
190
- with_ct_soft=True,
191
- with_pet_hot=True,
192
- input_target_shape=(5, 96, 96, 96)
193
- )
194
-
195
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary = partial(
196
- ModelUNetMulticlassDeepSupervision,
197
- model=UNetBase(
198
- dim=3,
199
- input_channels=5,
200
- channels=[64, 96, 128, 156],
201
- output_channels=2,
202
- config=default_layer_config(
203
- norm_type=NormType.InstanceNorm,
204
- activation=nn.LeakyReLU,
205
- norm_kwargs={'affine': True},
206
- ),
207
- activation=None,
208
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
209
- ),
210
- with_deep_supervision=True,
211
- boundary_loss_factor=1.0,
212
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0),
213
- with_ct_lung=True,
214
- with_ct_soft=True,
215
- with_pet_hot=True,
216
- input_target_shape=(5, 96, 96, 96)
217
- )
218
-
219
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary_sensitivity = partial(
220
- ModelUNetMulticlassDeepSupervision,
221
- model=UNetBase(
222
- dim=3,
223
- input_channels=5,
224
- channels=[64, 96, 128, 156],
225
- output_channels=2,
226
- config=default_layer_config(
227
- norm_type=NormType.InstanceNorm,
228
- activation=nn.LeakyReLU,
229
- norm_kwargs={'affine': True},
230
- ),
231
- activation=None,
232
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
233
- ),
234
- with_deep_supervision=True,
235
- boundary_loss_factor=1.0,
236
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0, sensitivity=2.0),
237
- with_ct_lung=True,
238
- with_ct_soft=True,
239
- with_pet_hot=True,
240
- input_target_shape=(5, 96, 96, 96)
241
- )
242
-
243
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary_coord = partial(
244
- ModelUNetMulticlassDeepSupervision,
245
- model=UNetBase(
246
- dim=3,
247
- input_channels=8,
248
- channels=[64, 96, 128, 156],
249
- output_channels=2,
250
- config=default_layer_config(
251
- norm_type=NormType.InstanceNorm,
252
- activation=nn.LeakyReLU,
253
- norm_kwargs={'affine': True},
254
- ),
255
- activation=None,
256
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
257
- ),
258
- with_deep_supervision=True,
259
- boundary_loss_factor=1.0,
260
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0),
261
- with_ct_lung=True,
262
- with_ct_soft=True,
263
- with_pet_hot=True,
264
- with_additional_features=('z_coords', 'y_coords', 'x_coords'),
265
- input_target_shape=(8, 96, 96, 96)
266
- )
267
-
268
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary_composite = partial(
269
- ModelUNetMulticlassDeepSupervision,
270
- model=UNetBase(
271
- dim=3,
272
- input_channels=5,
273
- channels=[64, 96, 128, 156],
274
- output_channels=2,
275
- config=default_layer_config(
276
- norm_type=NormType.InstanceNorm,
277
- activation=nn.LeakyReLU,
278
- norm_kwargs={'affine': True},
279
- ),
280
- activation=None,
281
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
282
- ),
283
- with_deep_supervision=False,
284
- with_deep_supervision_composite=True,
285
- boundary_loss_factor=1.0,
286
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0),
287
- with_ct_lung=True,
288
- with_ct_soft=True,
289
- with_pet_hot=True,
290
- input_target_shape=(5, 96, 96, 96)
291
- )
292
-
293
-
294
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_larger = partial(
295
- ModelUNetMulticlassDeepSupervision,
296
- model=UNetBase(
297
- dim=3,
298
- input_channels=5,
299
- channels=[96, 128, 156, 192],
300
- output_channels=2,
301
- config=default_layer_config(
302
- norm_type=NormType.InstanceNorm,
303
- activation=nn.LeakyReLU,
304
- norm_kwargs={'affine': True},
305
- ),
306
- activation=None,
307
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
308
- ),
309
- with_deep_supervision=True,
310
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=1.0, gamma=1.0),
311
- with_ct_lung=True,
312
- with_ct_soft=True,
313
- with_pet_hot=True,
314
- input_target_shape=(5, 96, 96, 96)
315
- )
316
-
317
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_larger2 = partial(
318
- ModelUNetMulticlassDeepSupervision,
319
- model=UNetBase(
320
- dim=3,
321
- input_channels=5,
322
- channels=[128, 156, 192, 224],
323
- output_channels=2,
324
- config=default_layer_config(
325
- norm_type=NormType.InstanceNorm,
326
- activation=nn.LeakyReLU,
327
- norm_kwargs={'affine': True},
328
- ),
329
- activation=None,
330
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=7))
331
- ),
332
- with_deep_supervision=True,
333
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=1.0, gamma=1.0),
334
- with_ct_lung=True,
335
- with_ct_soft=True,
336
- with_pet_hot=True,
337
- input_target_shape=(5, 96, 96, 96)
338
- )
339
-
340
-
341
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_larger2_sensitivity = partial(
342
- ModelUNetMulticlassDeepSupervision,
343
- model=UNetBase(
344
- dim=3,
345
- input_channels=5,
346
- channels=[128, 156, 192, 224],
347
- output_channels=2,
348
- config=default_layer_config(
349
- norm_type=NormType.InstanceNorm,
350
- activation=nn.LeakyReLU,
351
- norm_kwargs={'affine': True},
352
- ),
353
- activation=None,
354
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=7))
355
- ),
356
- with_deep_supervision=True,
357
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=1.0, gamma=1.0, sensitivity=1.0),
358
- with_ct_lung=True,
359
- with_ct_soft=True,
360
- with_pet_hot=True,
361
- input_target_shape=(5, 96, 96, 96)
362
- )
363
-
364
-
365
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_focal_composite = partial(
366
- ModelUNetMulticlassDeepSupervision,
367
- model=UNetBase(
368
- dim=3,
369
- input_channels=5,
370
- channels=[64, 96, 128, 156],
371
- output_channels=2,
372
- config=default_layer_config(
373
- norm_type=NormType.InstanceNorm,
374
- activation=nn.LeakyReLU,
375
- norm_kwargs={'affine': True},
376
- ),
377
- activation=None,
378
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
379
- ),
380
- with_deep_supervision=False,
381
- with_deep_supervision_composite=True,
382
- boundary_loss_factor=None,
383
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=2.0),
384
- with_ct_lung=True,
385
- with_ct_soft=True,
386
- with_pet_hot=True,
387
- input_target_shape=(5, 96, 96, 96)
388
- )
389
-
390
-
391
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_focal = partial(
392
- ModelUNetMulticlassDeepSupervision,
393
- model=UNetBase(
394
- dim=3,
395
- input_channels=5,
396
- channels=[64, 96, 128, 156],
397
- output_channels=2,
398
- config=default_layer_config(
399
- norm_type=NormType.InstanceNorm,
400
- activation=nn.LeakyReLU,
401
- norm_kwargs={'affine': True},
402
- ),
403
- activation=None,
404
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9))
405
- ),
406
- with_deep_supervision=True,
407
- with_deep_supervision_composite=False,
408
- boundary_loss_factor=None,
409
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=2.0),
410
- with_ct_lung=True,
411
- with_ct_soft=True,
412
- with_pet_hot=True,
413
- input_target_shape=(5, 96, 96, 96)
414
- )
415
-
416
-
417
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary_attention = partial(
418
- ModelUNetMulticlassDeepSupervision,
419
- model=UNetBase(
420
- dim=3,
421
- input_channels=5,
422
- channels=[64, 96, 128, 156],
423
- output_channels=2,
424
- config=default_layer_config(
425
- norm_type=NormType.InstanceNorm,
426
- activation=nn.LeakyReLU,
427
- norm_kwargs={'affine': True},
428
- ),
429
- activation=None,
430
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9)),
431
- up_block_fn=partial(Up, block=partial(
432
- BlockUpDeconvSkipConv,
433
- merge_layer_fn=MergeBlockAttention_Gating_Input,
434
- deconv_block=BlockUpsampleNnConvNormActivation)
435
- )
436
- ),
437
- with_deep_supervision=True,
438
- boundary_loss_factor=1.0,
439
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0),
440
- with_ct_lung=True,
441
- with_ct_soft=True,
442
- with_pet_hot=True,
443
- input_target_shape=(5, 96, 96, 96)
444
- )
445
-
446
-
447
- from basic_typing import KernelSize, Padding, Stride
448
- from typing import Union, Sequence
449
- from layers.blocks import BlockMerge, BlockDeconvNormActivation
450
- from layers.non_local import BlockNonLocal, linear_embedding
451
-
452
- class BlockUpDeconvSkipConvNonLocal(nn.Module):
453
- def __init__(
454
- self,
455
- config: LayerConfig,
456
- skip_channels: int,
457
- input_channels: int,
458
- output_channels: int,
459
- *,
460
- nb_repeats: int = 1,
461
- kernel_size: Optional[KernelSize] = None,
462
- deconv_kernel_size: Optional[KernelSize] = None,
463
- padding: Optional[Padding] = None,
464
- output_padding: Optional[Union[int, Sequence[int]]] = None,
465
- deconv_block=BlockDeconvNormActivation,
466
- stride: Optional[Stride] = None,
467
- merge_layer_fn=BlockMerge):
468
- super().__init__()
469
-
470
- self.merge_layer = merge_layer_fn(config=config, layer_channels=[skip_channels, output_channels])
471
-
472
- if deconv_kernel_size is None:
473
- deconv_kernel_size = kernel_size
474
-
475
- self.ops_deconv = deconv_block(
476
- config,
477
- input_channels=input_channels,
478
- output_channels=output_channels,
479
- kernel_size=deconv_kernel_size,
480
- padding=padding,
481
- output_padding=output_padding,
482
- stride=stride
483
- )
484
-
485
- convs = [
486
- BlockConvNormActivation(
487
- config,
488
- input_channels=self.merge_layer.get_output_channels(),
489
- output_channels=output_channels,
490
- kernel_size=kernel_size,
491
- stride=1
492
- )
493
- ]
494
-
495
- for _ in range(1, nb_repeats):
496
- conv = BlockConvNormActivation(
497
- config,
498
- input_channels=output_channels,
499
- output_channels=output_channels,
500
- kernel_size=kernel_size,
501
- stride=1
502
- )
503
- convs.append(conv)
504
-
505
- self.ops_conv: Union[nn.Sequential, BlockConvNormActivation]
506
- if len(convs) == 1:
507
- self.ops_conv = convs[0]
508
- else:
509
- self.ops_conv = nn.Sequential(*convs)
510
-
511
- self.non_local = BlockNonLocal(
512
- config=config,
513
- input_channels=output_channels,
514
- f_mapping_fn=linear_embedding,
515
- g_mapping_fn=linear_embedding,
516
- intermediate_channels=max(16, output_channels // 2)
517
- )
518
-
519
- self.skip_channels = skip_channels
520
- self.input_channels = input_channels
521
- self.output_channels = output_channels
522
-
523
- def forward(self, skip: torch.Tensor, previous: torch.Tensor) -> torch.Tensor:
524
- assert skip.shape[1] == self.skip_channels
525
- assert previous.shape[1] == self.input_channels
526
- x = self.ops_deconv(previous)
527
- assert x.shape[2:] == skip.shape[2:], f'got shape={x.shape[2:]}, expected={skip.shape[2:]}'
528
-
529
- x = self.merge_layer([skip, x])
530
- x = self.ops_conv(x)
531
- if x.shape[2] <= 32:
532
- # else it is just too slow...
533
- x = self.non_local(x)
534
- return x
535
-
536
-
537
- SimpleMulticlassUNet_dice_ce_fov_v9_ds_lung_soft_hot_boundary_non_local = partial(
538
- ModelUNetMulticlassDeepSupervision,
539
- model=UNetBase(
540
- dim=3,
541
- input_channels=5,
542
- channels=[64, 96, 128, 156],
543
- output_channels=2,
544
- config=default_layer_config(
545
- norm_type=NormType.InstanceNorm,
546
- activation=nn.LeakyReLU,
547
- norm_kwargs={'affine': True},
548
- ),
549
- activation=None,
550
- middle_block_fn=partial(LatentConv, block=partial(BlockConvNormActivation, kernel_size=9)),
551
- up_block_fn=partial(Up, block=BlockUpDeconvSkipConvNonLocal)
552
- ),
553
- with_deep_supervision=True,
554
- boundary_loss_factor=1.0,
555
- loss_fn=partial(LossDiceCrossEntropyFocal2, ce_factor=0.5, gamma=1.0),
556
- with_ct_lung=True,
557
- with_ct_soft=True,
558
- with_pet_hot=True,
559
- input_target_shape=(5, 96, 96, 96)
560
- )