-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfm_cls_config.py
165 lines (138 loc) · 4.71 KB
/
fm_cls_config.py
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
from typing import List, Callable
import torch
from torch import nn
import torchvision.transforms as tvtr
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from mirage_wrapper import miragecls_factory
from mutils.transforms import (
RandomIntensityChannel,
RandomAffineChannel,
Identity,
MinMaxNormChannel,
ToRGB,
NaiveNormChannel
)
from mutils.factory import get_factory_adder
import mutils.lr_utils as lru
from mutils.vit import vit_large_patch16, vit_base_patch16
add_config, fm_config_factory = get_factory_adder()
class FoundModel:
def __init__(self, args):
args.weight_decay = 1e-2
if args.fill is not None:
if args.fill < 0:
args.fill = None
if args.linear_probing:
print('> Linear probing')
args.lr = 1e-3
else:
print('> Full finetune')
args.lr = 1e-5
self.args = args
self.model: nn.Module
def get_optimizer(self, model):
return torch.optim.AdamW(
model.parameters(), lr=self.args.lr, weight_decay=self.args.weight_decay
)
def build_transform(self, subset, augment):
print(f'>>> Building transform "{subset}"')
intensity_msg = 'Random intensity shift'
intensity = RandomIntensityChannel()
if self.args.fill is None:
if 'kermany' in self.args.data_set.lower():
fill = 1
else:
fill = 0
else:
fill = self.args.fill
affine_msg = f'Random affine (fill={fill})'
affine = RandomAffineChannel(
degrees=10,
translate=(0.1, 0.1),
scale=(0.9, 1.1),
shear=5,
interpolation=tvtr.InterpolationMode.BILINEAR,
fill=fill,
)
if not self.args.affine:
affine_msg = 'No random affine'
affine = Identity()
grayscale = Identity()
grayscale = tvtr.Grayscale(num_output_channels=1)
norm_list = [ NaiveNormChannel() ]
min_max = self.get_min_max()
norm_list += self.get_model_norm()
transforms_list = [
tvtr.Resize(
size=(self.args.input_size, self.args.input_size),
interpolation=tvtr.InterpolationMode.BILINEAR,
),
grayscale,
tvtr.ToTensor(),
tvtr.ConvertImageDtype(torch.float32),
min_max,
]
if augment:
print('Random horizontal flip (0.5)')
print(intensity_msg)
print(affine_msg)
transforms_list += [
tvtr.RandomHorizontalFlip(p=0.5),
intensity,
affine,
]
print('Norm list:', norm_list)
transforms_list += norm_list
transforms = tvtr.Compose(transforms_list)
return transforms
def get_model_norm(self) -> List[Callable]:
# By default, convert to RGB and apply ImageNet normalization
return [
ToRGB(),
tvtr.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD)
]
def get_min_max(self) -> Callable:
return Identity()
def set_requires_grad(self):
if self.args.linear_probing:
# print(model.state_dict().keys())
print('Freezing encoder layers for linear probing')
# freeze encoder layers for linear probing
print('Tuned parameters:')
for name, param in self.model.named_parameters():
if 'head.' not in name: # and 'norm.' not in name:
param.requires_grad = False
else:
print('\t', name)
else:
for param in self.model.parameters():
param.requires_grad = True
class FoundSOTAModel(FoundModel):
def __init__(self, args):
super().__init__(args)
if self.args.input_size is None:
self.args.input_size = 224
class MIRAGEFM(FoundModel):
def __init__(self, args):
super().__init__(args)
if args.input_size is None:
args.input_size = 512
self.model = miragecls_factory[self.args.pool](
input_size=args.input_size,
patch_size=32,
num_classes=args.num_classes,
modalities='bscan',
# NOTE: weights are loaded in the model
weights=args.weights,
)
self.args = args
def get_model_norm(self) -> List[Callable]:
return [ MinMaxNormChannel() ]
def get_min_max(self) -> Callable:
return MinMaxNormChannel()
@add_config('mirage-large')
class MIRAGELargeFM(MIRAGEFM):
pass
@add_config('mirage-base')
class MIRAGEBaseFM(MIRAGEFM):
pass