openmlcore 1.1.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {openmlcore-1.1.0 → openmlcore-1.2.0}/PKG-INFO +1 -1
  2. {openmlcore-1.1.0 → openmlcore-1.2.0}/pyproject.toml +1 -1
  3. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/loss/loss.py +53 -71
  4. openmlcore-1.2.0/src/openmlcore/models/MobileUnetR.py +329 -0
  5. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/base_model.py +54 -5
  6. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/unetpp.py +1 -1
  7. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore.egg-info/PKG-INFO +1 -1
  8. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore.egg-info/SOURCES.txt +1 -0
  9. {openmlcore-1.1.0 → openmlcore-1.2.0}/tests/test_loss.py +6 -12
  10. {openmlcore-1.1.0 → openmlcore-1.2.0}/README.md +0 -0
  11. {openmlcore-1.1.0 → openmlcore-1.2.0}/setup.cfg +0 -0
  12. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/__init__.py +0 -0
  13. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/base/data.py +0 -0
  14. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/metrics/metrics.py +0 -0
  15. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/UNets.py +0 -0
  16. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/attUnets.py +0 -0
  17. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/lib/attention.py +0 -0
  18. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/lib/convolutions.py +0 -0
  19. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore/models/segformer.py +0 -0
  20. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore.egg-info/dependency_links.txt +0 -0
  21. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore.egg-info/requires.txt +0 -0
  22. {openmlcore-1.1.0 → openmlcore-1.2.0}/src/openmlcore.egg-info/top_level.txt +0 -0
  23. {openmlcore-1.1.0 → openmlcore-1.2.0}/tests/test_base_model.py +0 -0
  24. {openmlcore-1.1.0 → openmlcore-1.2.0}/tests/test_main.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: openmlcore
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: OpenMLCore is an open source framework (wrapper which simplify the use of PyTorch)
5
5
  Author-email: Doyez Sébastien <doyez.sebastien34090@gmail.com>
6
6
  Project-URL: Homepage, https://sebastien-doyez2812.github.io/openMLCore.github.io/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "openmlcore"
7
- version = "1.1.0"
7
+ version = "1.2.0"
8
8
  authors = [
9
9
  { name="Doyez Sébastien", email="doyez.sebastien34090@gmail.com" },
10
10
  ]
@@ -29,6 +29,9 @@ class BaseLoss(nn.Module):
29
29
  class BCEWithLogitsLoss(BaseLoss):
30
30
  def __init__(self, weights=None):
31
31
  super().__init__(name="BCEWithLogitsLoss", doc_url="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html")
32
+ if weights is None:
33
+ raise ValueError("weights need to be set!")
34
+
32
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
33
36
 
34
37
  pos_weights = None
@@ -48,6 +51,9 @@ class BCEWithLogitsLoss(BaseLoss):
48
51
  class DiceLoss(BaseLoss):
49
52
  def __init__(self, weights = None):
50
53
  super().__init__(name="DiceLoss", doc_url="https://arxiv.org/abs/1606.04797")
54
+ if weights is None:
55
+ raise ValueError("weights need to be set!")
56
+
51
57
  self.weights = weights
52
58
 
53
59
  def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
@@ -71,76 +77,67 @@ class DiceLoss(BaseLoss):
71
77
 
72
78
  return dice_loss.sum(dim=1).mean()
73
79
  return dice_loss.mean()
74
-
75
- # TODO:
76
- # 1 Hot encoding loss
77
- #
78
-
79
80
 
80
81
  class MeanSquaredErrorLoss(BaseLoss):
81
- def __init__(self, index_ignore=255):
82
+ def __init__(self, weights = None):
82
83
  super().__init__(name="MeanSquaredErrorLoss", doc_url="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html")
83
- self.index_ignore = index_ignore
84
+ if weights is None:
85
+ raise ValueError("weights need to be set!")
86
+
87
+ self.weights = weights
84
88
 
85
89
  def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
86
- num_classes = outputs.shape[1]
87
- probs = F.softmax(outputs, dim=1)
88
- targets_one_hot, valid_mask = self._to_one_hot_clean(targets, num_classes, self.index_ignore)
89
-
90
- mse = (probs - targets_one_hot) ** 2
91
- mse = mse * valid_mask
92
-
93
- num_valids = valid_mask.sum() * num_classes
94
- return mse.sum() / torch.clamp(num_valids, min=1.0)
90
+ probs = torch.sigmoid(outputs)
91
+ probs = probs.view (probs.size(0) , probs.size(1), -1)
92
+ targets = targets.view(probs.size(0), probs.size(1), -1)
95
93
 
94
+ mse = (probs - targets) ** 2
95
+ weighted_mse = 0
96
+ for idx, weight in enumerate(self.weights):
97
+ weighted_mse+= weight * mse[:, idx, :]
96
98
 
99
+ return weighted_mse.mean()
97
100
 
98
101
  class FocalLoss(BaseLoss):
99
- def __init__(self, alpha=0.25, gamma=2.0, index_ignore=255):
102
+ def __init__(self, alpha=0.25, gamma=2.0, weights = None):
100
103
  super().__init__(name="FocalLoss", doc_url="https://arxiv.org/abs/1708.02002")
104
+ if weights is None:
105
+ raise ValueError("weights need to be set!")
106
+
101
107
  self.alpha = alpha
102
108
  self.gamma = gamma
103
- self.index_ignore = index_ignore
109
+ self.weights = weights
104
110
 
105
111
  def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
106
- num_classes = outputs.shape[1]
107
- targets_one_hot, valid_mask = self._to_one_hot_clean(targets, num_classes, self.index_ignore)
108
-
109
- bce_loss = F.binary_cross_entropy_with_logits(outputs, targets_one_hot, reduction='none')
112
+ probs = outputs.view(outputs.size(0) , outputs.size(1), -1)
113
+ targets = targets.view(outputs.size(0), outputs.size(1), -1)
114
+
115
+ bce_loss = F.binary_cross_entropy_with_logits(probs, targets, reduction='none') # Apply a sigmoid inside...
110
116
  pt = torch.exp(-bce_loss)
111
117
  focal_loss = self.alpha * ((1 - pt) ** self.gamma) * bce_loss
112
- focal_loss = focal_loss * valid_mask
113
- num_valids = valid_mask.sum() * num_classes
114
- return focal_loss.sum() / torch.clamp(num_valids, min=1.0)
115
-
116
- class MeanSquaredErrorLoss(BaseLoss):
117
- def __init__(self, index_ignore=255):
118
- super().__init__(name="MeanSquaredErrorLoss", doc_url="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html")
119
- self.index_ignore = index_ignore
120
-
121
- def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
122
- num_classes = outputs.shape[1]
123
- probs = F.softmax(outputs, dim=1)
124
- targets_one_hot, valid_mask = self._to_one_hot_clean(targets, num_classes, self.index_ignore)
125
-
126
- mse = (probs - targets_one_hot) ** 2
127
- mse = mse * valid_mask
128
-
129
- num_valids = valid_mask.sum() * num_classes
130
- return mse.sum() / torch.clamp(num_valids, min=1.0)
131
118
 
119
+ final_loss = 0
120
+ for idx, weight in enumerate(self.weights):
121
+ final_loss += weight * focal_loss[:, idx, :]
122
+ return final_loss.mean()
123
+
132
124
  class HuberLoss(BaseLoss):
133
- def __init__(self, delta=1.0, index_ignore=255):
125
+ def __init__(self, delta=1.0, weights = None):
134
126
  super().__init__(name="HuberLoss", doc_url="https://pytorch.org/docs/stable/generated/torch.nn.HuberLoss.html")
127
+ if weights is None:
128
+ raise ValueError("weights need to be set!")
129
+ self.weights = weights
135
130
  self.delta = delta
136
- self.index_ignore = index_ignore
137
131
 
138
132
  def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
139
- num_classes = outputs.shape[1]
140
- probs = F.softmax(outputs, dim=1)
141
- targets_one_hot, valid_mask = self._to_one_hot_clean(targets, num_classes, self.index_ignore)
142
-
143
- error = probs - targets_one_hot
133
+ # num_classes = outputs.shape[1]
134
+ # probs = F.softmax(outputs, dim=1)
135
+
136
+ probs = torch.sigmoid(outputs)
137
+ probs = probs.view (probs.size(0) , probs.size(1), -1)
138
+ targets = targets.view(probs.size(0), probs.size(1), -1)
139
+
140
+ error = probs - targets
144
141
  abs_error = torch.abs(error)
145
142
 
146
143
  huber = torch.where(
@@ -148,30 +145,15 @@ class HuberLoss(BaseLoss):
148
145
  0.5 * (error ** 2),
149
146
  self.delta * (abs_error - 0.5 * self.delta)
150
147
  )
151
- huber = huber * valid_mask
152
-
153
- num_valids = valid_mask.sum() * num_classes
154
- return huber.sum() / torch.clamp(num_valids, min=1.0)
155
-
156
- class KLDivergenceLoss(BaseLoss):
157
- def __init__(self, index_ignore=255):
158
- super().__init__(name="KLDivergenceLoss", doc_url="https://pytorch.org/docs/stable/generated/torch.nn.KLDivLoss.html")
159
- self.eps = 1e-8
160
- self.index_ignore = index_ignore
161
-
162
- def forward(self, outputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
163
- num_classes = outputs.shape[1]
164
- log_probs = F.log_softmax(outputs, dim=1)
165
- targets_one_hot, valid_mask = self._to_one_hot_clean(targets, num_classes, self.index_ignore)
166
-
167
- # KL-Div = p(x) * (log(p(x)) - log(q(x)))
168
- # Pour une distribution One-Hot, cela se réduit à : - log(q(x)) sur la classe vraie
169
- kl = F.kl_div(log_probs, targets_one_hot, reduction='none')
170
- kl = kl * valid_mask
171
-
172
- num_valids = valid_mask.sum() * num_classes
173
- return kl.sum() / torch.clamp(num_valids, min=1.0)
174
148
 
149
+ if not isinstance(self.weights, torch.Tensor):
150
+ weights_tensor = torch.tensor(self.weights, device=outputs.device, dtype=outputs.dtype)
151
+ else:
152
+ weights_tensor = self.weights.to(outputs.device)
153
+ huber = huber * weights_tensor.view(1, -1, 1)
154
+
155
+ return huber.mean()
156
+
175
157
  ####################################
176
158
  # Custom Loss implementation #
177
159
  ####################################
@@ -0,0 +1,329 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ import timm
5
+ from .base_model import BaseModel
6
+
7
+ class MobileNetBlockv2(nn.Module):
8
+ def __init__(self, in_channels, out_channels, stride=1, expand_ratio=4, **kwargs):
9
+ super().__init__(**kwargs)
10
+ self.stride = stride
11
+ self.use_residual = (self.stride == 1 and in_channels == out_channels)
12
+
13
+ hidden_dim = in_channels * expand_ratio
14
+ self.layers = nn.ModuleList()
15
+
16
+ if expand_ratio != 1:
17
+ self.layers.append(nn.Conv2d(in_channels, hidden_dim, kernel_size=1, bias=False))
18
+ self.layers.append(nn.BatchNorm2d(hidden_dim))
19
+ self.layers.append(nn.ReLU6(inplace=True)) # ReLU6 est souvent préféré pour MobileNet
20
+ else:
21
+ hidden_dim = in_channels
22
+
23
+ self.layers.append(nn.Conv2d(hidden_dim, hidden_dim, kernel_size=3, stride=self.stride, padding=1, groups=hidden_dim, bias=False))
24
+ self.layers.append(nn.BatchNorm2d(hidden_dim))
25
+ self.layers.append(nn.ReLU6(inplace=True))
26
+
27
+ self.layers.append(nn.Conv2d(hidden_dim, out_channels, kernel_size=1, bias=False))
28
+ self.layers.append(nn.BatchNorm2d(out_channels))
29
+
30
+ def forward(self, X):
31
+ inputX = X if self.use_residual else None
32
+
33
+ for layer in self.layers:
34
+ X = layer(X)
35
+
36
+ if inputX is not None:
37
+ return inputX + X
38
+ return X
39
+
40
+ class STEM(nn.Module):
41
+ def __init__(self, in_channels=3, **kwargs):
42
+ super().__init__(**kwargs)
43
+
44
+ self.conv2D = nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=3, stride=2, padding=1, bias=False)
45
+ self.batchNorm = nn.BatchNorm2d(32)
46
+ self.relu = nn.ReLU6(inplace=True)
47
+
48
+ self.mobile_block1 = MobileNetBlockv2(in_channels=32, out_channels=16, stride=1, expand_ratio=1)
49
+ self.mobile_block2a = MobileNetBlockv2(in_channels=16, out_channels=24, stride=2, expand_ratio=6)
50
+ self.mobile_block2b = MobileNetBlockv2(in_channels=24, out_channels=24, stride=1, expand_ratio=6)
51
+ self.mobile_block3 = MobileNetBlockv2(in_channels=24, out_channels=24, stride=1, expand_ratio=6)
52
+
53
+ def forward(self, X):
54
+ X = self.relu(self.batchNorm(self.conv2D(X)))
55
+ X = self.mobile_block1(X)
56
+ X = self.mobile_block2a(X)
57
+ X = self.mobile_block2b(X)
58
+ X = self.mobile_block3(X)
59
+ return X
60
+
61
+ class MobileViTBlock(nn.Module):
62
+ def __init__(self, in_channels, out_channels, patch_size=2, l=2, dim=64, **kwargs):
63
+ super().__init__(**kwargs)
64
+ self.patchsize = patch_size
65
+ self.dim = dim
66
+ self.out_channels = out_channels
67
+ self.L = l
68
+
69
+ self.conv3x3_1 = nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=3, padding=1, bias=False)
70
+ self.conv1x1_1 = nn.Conv2d(in_channels=in_channels, out_channels=dim, kernel_size=1, padding=0, bias=False)
71
+
72
+ self.conv3x3_2 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1, bias=False)
73
+ self.conv1x1_2 = nn.Conv2d(in_channels=dim, out_channels=in_channels, kernel_size=1, padding=0, bias=False)
74
+
75
+ self.transformers = nn.ModuleList([ViTransformer(dim=self.patchsize * self.patchsize * dim) for _ in range(self.L)])
76
+
77
+ self.unfold = nn.Unfold(
78
+ kernel_size=patch_size,
79
+ stride=patch_size,
80
+ padding=0
81
+ )
82
+
83
+ def forward(self, X):
84
+ skip_connection = X
85
+
86
+ X = self.conv3x3_1(X)
87
+ X = self.conv1x1_1(X)
88
+
89
+ B, C, H, W = X.shape
90
+ pH, pW = H // self.patchsize, W // self.patchsize
91
+
92
+ X = self.unfold(X)
93
+ X = X.reshape(B, C * self.patchsize * self.patchsize, pH * pW)
94
+ X = X.permute(0, 2, 1)
95
+
96
+ for transformer in self.transformers:
97
+ X = transformer(X)
98
+
99
+ X = X.permute(0, 2, 1)
100
+ X = X.reshape(B, C, self.patchsize, self.patchsize, pH, pW)
101
+ X = X.permute(0, 1, 4, 2, 5, 3)
102
+ X = X.reshape(B, C, pH * self.patchsize, pW * self.patchsize)
103
+
104
+ X = self.conv1x1_2(X)
105
+ X = X + skip_connection
106
+ X = self.conv3x3_2(X)
107
+
108
+ return X
109
+
110
+ class ViTransformer(nn.Module):
111
+ def __init__(self, dim, num_heads=8, ffn_ratio=4, dropout=0.0, **kwargs):
112
+ super().__init__(**kwargs)
113
+ self.dim = dim
114
+ self.num_heads = num_heads
115
+ self.ffn_ratio = ffn_ratio
116
+ self.dropout_rate = dropout
117
+
118
+ self.norm1 = nn.LayerNorm(dim, eps=1e-6)
119
+ self.attn = nn.MultiheadAttention(embed_dim=dim, num_heads=num_heads, batch_first=True, dropout=dropout)
120
+ self.drop1 = nn.Dropout(dropout)
121
+
122
+ self.norm2 = nn.LayerNorm(dim, eps=1e-6)
123
+ self.ffn_1 = nn.Linear(in_features=dim, out_features=dim * ffn_ratio)
124
+ self.gelu = nn.GELU()
125
+ self.ffn_2 = nn.Linear(in_features=dim * ffn_ratio, out_features=dim)
126
+ self.drop2 = nn.Dropout(dropout)
127
+
128
+ def forward(self, X):
129
+ input_tensor = X
130
+ X = self.norm1(X)
131
+ X, _ = self.attn(X, X, X)
132
+ X = self.drop1(X)
133
+ X = X + input_tensor
134
+
135
+ res = X
136
+ X = self.norm2(X)
137
+ X = self.ffn_1(X)
138
+ X = self.gelu(X)
139
+ X = self.drop2(X)
140
+ X = self.ffn_2(X)
141
+ X = self.drop2(X)
142
+ return X + res
143
+
144
+ class HybridEncoderBlock(nn.Module):
145
+ def __init__(self, input_channels, out_channels, stride=2, **kwargs):
146
+ super().__init__(**kwargs)
147
+ self.Conv1x1 = nn.Conv2d(in_channels=input_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, bias=False)
148
+ self.Conv3x3 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
149
+ self.mobileViT_block = MobileViTBlock(in_channels=out_channels, out_channels=out_channels)
150
+
151
+ def forward(self, X):
152
+ X = self.Conv1x1(X)
153
+ X = self.Conv3x3(X)
154
+ X = self.mobileViT_block(X)
155
+ return X
156
+
157
+ class HybridDecoderBlock(nn.Module):
158
+ def __init__(self, in_channels, skip_channels, out_channels, **kwargs):
159
+ super().__init__(**kwargs)
160
+ self.transpose_conv = nn.ConvTranspose2d(
161
+ in_channels=in_channels,
162
+ out_channels=out_channels,
163
+ kernel_size=2,
164
+ stride=2
165
+ )
166
+
167
+ concat_channels = out_channels + skip_channels
168
+
169
+ self.conv1x1 = nn.Conv2d(
170
+ in_channels=concat_channels,
171
+ out_channels=out_channels,
172
+ kernel_size=1,
173
+ stride=1,
174
+ bias=False
175
+ )
176
+ self.silu1 = nn.SiLU()
177
+
178
+ self.conv_depth_wise = nn.Conv2d(
179
+ in_channels=out_channels,
180
+ out_channels=out_channels,
181
+ kernel_size=3,
182
+ stride=1,
183
+ padding=1,
184
+ groups=out_channels,
185
+ bias=False
186
+ )
187
+ self.silu2 = nn.SiLU()
188
+
189
+ self.conv1x1_2 = nn.Conv2d(
190
+ in_channels=out_channels,
191
+ out_channels=out_channels,
192
+ kernel_size=1,
193
+ stride=1,
194
+ bias=False
195
+ )
196
+ self.silu3 = nn.SiLU()
197
+
198
+ self.mobileViTBlock = MobileViTBlock(
199
+ in_channels=out_channels,
200
+ out_channels=out_channels
201
+ )
202
+
203
+ def forward(self, decoder_in, skip_in):
204
+ x = self.transpose_conv(decoder_in)
205
+
206
+ if x.shape[2:] != skip_in.shape[2:]:
207
+ x = F.interpolate(x, size=skip_in.shape[2:], mode='bilinear', align_corners=False)
208
+
209
+ x = torch.cat([x, skip_in], dim=1)
210
+ x = self.silu1(self.conv1x1(x))
211
+ x = self.silu2(self.conv_depth_wise(x))
212
+ x = self.silu3(self.conv1x1_2(x))
213
+ return self.mobileViTBlock(x)
214
+
215
+ class MobileUnetRTorch(nn.Module):
216
+ def __init__(self, input_channels, output_channels):
217
+ super().__init__()
218
+ self.STEM = STEM(in_channels=input_channels) #should be 3, because RGB = 3 channels
219
+ # HEB = Hybrid Encoder Block
220
+ self.HEB_1 = HybridEncoderBlock(input_channels=24, out_channels=64, stride=2) # H/8
221
+ self.HEB_2 = HybridEncoderBlock(input_channels=64, out_channels=128, stride=2) # H/16
222
+ self.HEB_3 = HybridEncoderBlock(input_channels=128, out_channels=256, stride=2) # H/32
223
+ self.HEB_4 = HybridEncoderBlock(input_channels=256, out_channels=512, stride=2) # H/64
224
+
225
+ # HDB = Hybrid Decoder Block
226
+ self.HDB_1 = HybridDecoderBlock(in_channels= 512, skip_channels=256, out_channels=256) #H/32
227
+ self.HDB_2 = HybridDecoderBlock(in_channels= 256, skip_channels=128, out_channels=128) #H/16
228
+ self.HDB_3 = HybridDecoderBlock(in_channels= 128, skip_channels=64, out_channels=64) #H/8
229
+
230
+ self.final_conv = nn.ConvTranspose2d(in_channels=64, out_channels=output_channels, kernel_size=4, stride=4, padding=0)
231
+ self.sigmoid = nn.Sigmoid()
232
+
233
+
234
+ def forward(self, X):
235
+ input_size = X.shape[2:]
236
+ skip_connection = []
237
+
238
+ X = self.STEM(X) # Embedding
239
+ skip_connection.append(X)
240
+ X = self.HEB_1(X)
241
+ skip_connection.append(X)
242
+
243
+ X = self.HEB_2(X)
244
+ skip_connection.append(X)
245
+
246
+ X = self.HEB_3(X)
247
+ skip_connection.append(X)
248
+
249
+ X = self.HEB_4(X)
250
+
251
+ X = self.HDB_1(X, skip_connection.pop())
252
+ X = self.HDB_2(X, skip_connection.pop())
253
+ X = self.HDB_3(X, skip_connection.pop())
254
+
255
+ X = self.final_conv(X)
256
+
257
+ if X.shape[2:] != input_size:
258
+ X = F.interpolate(X, size=input_size, mode='bilinear', align_corners=False)
259
+ return X
260
+ # return self.sigmoid(X)
261
+
262
+ class MobileUnetRTorch_v2(nn.Module):
263
+ def __init__(self, input_channels, output_channels):
264
+ super().__init__()
265
+
266
+ self.encoder = timm.create_model(
267
+ "mobilevit_s.cvnets_in1k",
268
+ pretrained=True,
269
+ features_only=True,
270
+ out_indices=(1, 2, 3, 4)
271
+ )
272
+ for param in self.encoder.parameters():
273
+ param.requires_grad = True
274
+
275
+ # features[3] (bottleneck) = 640 channels
276
+ # features[2] (skip3) = 128 channels
277
+ # features[1] (skip2) = 96 channels
278
+ # features[0] (skip1) = 64 channels
279
+
280
+ self.HDB_1 = HybridDecoderBlock(in_channels=640, skip_channels=128, out_channels=256)
281
+ self.HDB_2 = HybridDecoderBlock(in_channels=256, skip_channels=96, out_channels=128)
282
+ self.HDB_3 = HybridDecoderBlock(in_channels=128, skip_channels=64, out_channels=64)
283
+
284
+ self.final_conv = nn.ConvTranspose2d(in_channels=64, out_channels=output_channels, kernel_size=4, stride=4, padding=0)
285
+ self.sigmoid = nn.Sigmoid()
286
+
287
+ def forward(self, X):
288
+ input_size = X.shape[2:]
289
+
290
+ features = self.encoder(X)
291
+ skip1, skip2, skip3, bottleneck = features[0], features[1], features[2], features[3]
292
+
293
+ X = self.HDB_1(bottleneck, skip3)
294
+ X = self.HDB_2(X, skip2)
295
+ X = self.HDB_3(X, skip1)
296
+
297
+ X = self.final_conv(X)
298
+
299
+ if X.shape[2:] != input_size:
300
+ X = F.interpolate(X, size=input_size, mode='bilinear', align_corners=False)
301
+
302
+ return X
303
+ # return self.sigmoid(X)
304
+
305
+ class MobileUnetR(BaseModel):
306
+ def __init__(self, input_channels=1, output_channels=1, **kwargs):
307
+ super().__init__(name="MobileUnetR", **kwargs)
308
+ self.input_channels = input_channels
309
+ self.output_channels = output_channels
310
+
311
+ def create_model(self):
312
+ self.model = MobileUnetRTorch(
313
+ input_channels=self.input_channels,
314
+ output_channels=self.output_channels
315
+ )
316
+ return self.model
317
+
318
+ class MobileUnetR_v2(BaseModel):
319
+ def __init__(self, input_channels=1, output_channels=1, **kwargs):
320
+ super().__init__(name="MobileUnetR_apple_version", **kwargs)
321
+ self.input_channels = input_channels
322
+ self.output_channels = output_channels
323
+
324
+ def create_model(self):
325
+ self.model = MobileUnetRTorch_v2(
326
+ input_channels=self.input_channels,
327
+ output_channels=self.output_channels
328
+ )
329
+ return self.model
@@ -1,4 +1,6 @@
1
1
  import torch
2
+ import csv
3
+ import mlflow
2
4
  import torch.nn as nn
3
5
  from torchinfo import summary
4
6
  from torch.utils.data import DataLoader
@@ -6,7 +8,7 @@ from tqdm.auto import tqdm
6
8
  import numpy as np
7
9
 
8
10
  class BaseModel(nn.Module):
9
- def __init__(self, name: str = "BaseModel", model = None, metrics = None, loss_fn = None, optimizer = None, device = None):
11
+ def __init__(self, name: str = "BaseModel", model = None, metrics = None, loss_fn = None, optimizer = None, device = None, write_result_in_CSV = True, csv_path= None, use_mlflow = True):
10
12
  super().__init__()
11
13
  self.name = name
12
14
  self.model = model
@@ -14,6 +16,13 @@ class BaseModel(nn.Module):
14
16
  self.loss_fn = loss_fn
15
17
  self.optimizer = optimizer
16
18
  self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ self.use_mlflow = use_mlflow
21
+ self.write_result_in_CSV = write_result_in_CSV
22
+ if csv_path is None:
23
+ self.csv_path = f"{self.name}.csv"
24
+ else:
25
+ self.csv_path = csv_path
17
26
 
18
27
  def help(self):
19
28
  raise NotImplementedError("Subclasses should implement this method.")
@@ -32,6 +41,27 @@ class BaseModel(nn.Module):
32
41
  raise ValueError("Optimizer has not been set. Please set an optimizer before training.")
33
42
 
34
43
  self.model.to(self.device)
44
+
45
+ # MLFlow:
46
+ if self.use_mlflow:
47
+ lr = self.optimizer.param_groups[0]['lr'] if self.optimizer and len(self.optimizer.param_groups) > 0 else None
48
+ mlflow.log_params({
49
+ "model_name": self.name,
50
+ "epochs": epochs,
51
+ "learning_rate": lr,
52
+ "device": str(self.device)
53
+ })
54
+
55
+ # CSV Logger:
56
+ if self.write_result_in_CSV:
57
+ csv_fields = ["epoch", "train_loss"]
58
+ for name_metric in self.metrics.keys():
59
+ csv_fields.append(f"val_{name_metric}")
60
+
61
+ with open(self.csv_path, 'w', newline='', encoding='utf-8') as file:
62
+ writer = csv.DictWriter(file, fieldnames=csv_fields, extrasaction='ignore')
63
+ writer.writeheader()
64
+
35
65
  for current_epoch in range(epochs):
36
66
  # For each loop: a first phase of training and then validation.
37
67
  # Training:
@@ -63,6 +93,11 @@ class BaseModel(nn.Module):
63
93
  avg_loss = total_loss / len(train_loader)
64
94
  tqdm.write(f"Epoch {current_epoch + 1}/{epochs} — average loss: {avg_loss:.4f}")
65
95
 
96
+
97
+ # Create a dictionnary for the csv:
98
+ epoch_metrics = {"epoch": current_epoch,
99
+ "train_loss" : avg_loss}
100
+
66
101
  # Validation:
67
102
  if val_loader:
68
103
  self.model.eval()
@@ -130,13 +165,27 @@ class BaseModel(nn.Module):
130
165
 
131
166
  class_details = ", ".join([f"C{i}:{v:.3f}" for i, v in enumerate(avg_list)])
132
167
  metrics_summary_str.append(f"{name_metric}_Mean: {mean_all_classes:.4f} ({class_details})")
168
+ epoch_metrics[f"val_{name_metric}"] = mean_all_classes
169
+ for idx, value in enumerate(avg_list):
170
+ epoch_metrics[f"val_{name_metric}"] = value
171
+
133
172
  else:
134
173
  avg_val = val_sum / num_batchs
135
174
  metrics_summary_str.append(f"{name_metric}: {avg_val:.4f}")
175
+ epoch_metrics[f"val_{name_metric}"] = avg_val
176
+
136
177
 
137
178
  formatted_metrics = " | ".join(metrics_summary_str)
138
179
  tqdm.write(f"Epoch {current_epoch + 1}/{epochs} — Validation: {formatted_metrics}")
139
-
180
+
181
+ if self.use_mlflow:
182
+ mlflow.log_metrics(epoch_metrics, step=current_epoch + 1)
183
+
184
+ if self.write_result_in_CSV:
185
+ with open(self.csv_path, 'a', newline='', encoding='utf-8') as file:
186
+ writer = csv.DictWriter(file, fieldnames=csv_fields, extrasaction='ignore')
187
+ writer.writerow(epoch_metrics)
188
+
140
189
  if save_checkpoint:
141
190
  self.save_model(f"checkpoint_{self.name}.pt")
142
191
 
@@ -190,11 +239,11 @@ class BaseModel(nn.Module):
190
239
  num_batches = len(eval_loader)
191
240
  final_metrics = {}
192
241
 
193
- for name_metrics, val_sum in dict_metrics_sum.items():
242
+ for current_metrics, val_sum in dict_metrics_sum.items():
194
243
  if isinstance(val_sum, list):
195
- final_metrics[name_metric] = [v / num_batches for v in val_sum]
244
+ final_metrics[current_metrics] = [v / num_batches for v in val_sum]
196
245
  else:
197
- final_metrics[name_metric] = val_sum / num_batches
246
+ final_metrics[current_metrics] = val_sum / num_batches
198
247
 
199
248
  return final_metrics
200
249
 
@@ -7,7 +7,7 @@ from .lib.convolutions import DoubleConv
7
7
  class UnetPPTorch(nn.Module):
8
8
  def __init__(self, input_channels=1, output_channels=1, depth=4, initial_filters=64,
9
9
  kernel_size=3, padding=1):
10
- super(UnetPPTorch, self).__init__()
10
+ super(UnetPPTorch, self).__init__(name= "Unet++")
11
11
  self.depth = depth
12
12
  self.downs = nn.ModuleList()
13
13
  self.inter_down = nn.ModuleList()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: openmlcore
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: OpenMLCore is an open source framework (wrapper which simplify the use of PyTorch)
5
5
  Author-email: Doyez Sébastien <doyez.sebastien34090@gmail.com>
6
6
  Project-URL: Homepage, https://sebastien-doyez2812.github.io/openMLCore.github.io/
@@ -9,6 +9,7 @@ src/openmlcore.egg-info/top_level.txt
9
9
  src/openmlcore/base/data.py
10
10
  src/openmlcore/loss/loss.py
11
11
  src/openmlcore/metrics/metrics.py
12
+ src/openmlcore/models/MobileUnetR.py
12
13
  src/openmlcore/models/UNets.py
13
14
  src/openmlcore/models/attUnets.py
14
15
  src/openmlcore/models/base_model.py
@@ -6,27 +6,21 @@ sys.path.append(str(root_dir))
6
6
  from src.openmlcore.loss.loss import *
7
7
 
8
8
  def test_BCEWithLogits_loss():
9
- loss = BCEWithLogitsLoss()
9
+ loss = BCEWithLogitsLoss(weights = [1.0])
10
10
  assert loss is not None
11
11
 
12
12
  def test_mean_squared_error_loss():
13
- loss = MeanSquaredErrorLoss()
13
+ loss = MeanSquaredErrorLoss(weights = [1.0])
14
14
  assert loss is not None
15
15
 
16
16
  def test_dice_loss():
17
- loss = DiceLoss()
17
+ loss = DiceLoss(weights = [1.0])
18
18
  assert loss is not None
19
19
 
20
20
  def test_focal_loss():
21
- loss = FocalLoss()
21
+ loss = FocalLoss(weights = [1.0])
22
22
  assert loss is not None
23
23
 
24
24
  def test_huber_loss():
25
- loss = HuberLoss()
26
- assert loss is not None
27
-
28
- def test_kl_divergence_loss():
29
- loss = KLDivergenceLoss()
30
- assert loss is not None
31
-
32
-
25
+ loss = HuberLoss(weights = [1.0])
26
+ assert loss is not None
File without changes
File without changes