Skip to content

Commit 92b5003

Browse files
authored
Merge branch 'main' into update-existing-mmap-path-handling
2 parents 3f86abb + da57470 commit 92b5003

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

src/lightly_train/_task_models/dinov2_eomt_semantic_segmentation/task_model.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,8 @@ def predict(self, image: PathLike | PILImage | Tensor) -> Tensor:
230230
The predicted mask as a tensor of shape (H, W). The values represent the
231231
class IDs as defined in the `classes` argument of your dataset. These
232232
classes are also stored in the `classes` attribute of the model.
233-
If your dataset contains ignored classes defined by the `ignore_classes`
234-
argument, the model will assign a special value for any pixels that it
235-
cannot assign to any of the known classes. This value is stored as
236-
`class_ignore_index` attribute of the model and is by default -100.
237-
If the dataset doesn't contain any ignored classes, the model will always
238-
assign a known class to each pixel.
233+
The model will always predict the pixels as one of the known classes even when
234+
your dataset contains ignored classes defined by the `ignore_classes` argument.
239235
"""
240236
if self.training:
241237
self.eval()
@@ -257,9 +253,8 @@ class IDs as defined in the `classes` argument of your dataset. These
257253
x = x.unsqueeze(0) # (1, C, H', W')
258254

259255
logits = self._forward_logits(x) # (1, K+1, H', W'), K = len(self.classes)
260-
if self.class_ignore_index is None:
261-
# Restrict logits to known classes only.
262-
logits = logits[:, :-1] # (1, K, H', W')
256+
# Restrict logits to known classes only.
257+
logits = logits[:, :-1] # (1, K, H', W')
263258
logits = F.interpolate(
264259
logits, size=(image_h, image_w), mode="bilinear"
265260
) # (1, K|K+1, H, W)
@@ -271,10 +266,9 @@ class IDs as defined in the `classes` argument of your dataset. These
271266

272267
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
273268
# Function used for ONNX export
274-
logits = self._forward_logits(x) # (B, C, H, W)
275-
if self.class_ignore_index is None:
276-
# Restrict logits to known classes only.
277-
logits = logits[:, :-1]
269+
logits = self._forward_logits(x) # (B, K+1, H, W), K = len(self.classes)
270+
# Restrict logits to known classes only.
271+
logits = logits[:, :-1] # (1, K, H, W)
278272
masks = logits.argmax(dim=1) # (B, H, W)
279273
# Map internal class IDs to class IDs.
280274
masks = self.internal_class_to_class[masks]

src/lightly_train/_task_models/dinov2_linear_semantic_segmentation/task_model.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,8 @@ def predict(self, image: PathLike | PILImage | Tensor) -> Tensor:
189189
The predicted mask as a tensor of shape (H, W). The values represent the
190190
class IDs as defined in the `classes` argument of your dataset. These
191191
classes are also stored in the `classes` attribute of the model.
192-
If your dataset contains ignored classes defined by the `ignore_classes`
193-
argument, the model will assign a special value for any pixels that it
194-
cannot assign to any of the known classes. This value is stored as
195-
`class_ignore_index` attribute of the model and is by default -100.
196-
If the dataset doesn't contain any ignored classes, the model will always
197-
assign a known class to each pixel.
192+
The model will always predict the pixels as one of the known classes even when
193+
your dataset contains ignored classes defined by the `ignore_classes` argument.
198194
"""
199195
if self.training:
200196
self.eval()
@@ -216,7 +212,9 @@ class IDs as defined in the `classes` argument of your dataset. These
216212
x = x.unsqueeze(0) # (1, C, H', W')
217213

218214
logits = self._forward_logits(x) # (1, K|K+1, H', W'), K=num_classes
219-
# (1, K|K+1, H, W)
215+
if self.class_ignore_index is not None:
216+
# Restrict logits to known classes only.
217+
logits = logits[:, :-1] # (1, K, H', W')
220218
logits = F.interpolate(logits, size=(image_h, image_w), mode="bilinear")
221219

222220
masks = logits.argmax(dim=1) # (1, H, W)
@@ -225,7 +223,10 @@ class IDs as defined in the `classes` argument of your dataset. These
225223

226224
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
227225
# Function used for ONNX export
228-
logits = self._forward_logits(x) # (B, K, H, W)
226+
logits = self._forward_logits(x) # (B, K|K+1, H, W), K=num_classes
227+
if self.class_ignore_index is not None:
228+
# Restrict logits to known classes only.
229+
logits = logits[:, :-1] # (1, K, H, W)
229230
masks = logits.argmax(dim=1) # (B, H, W)
230231
masks = self.internal_class_to_class[masks]
231232
return masks, logits

src/lightly_train/_task_models/dinov3_eomt_semantic_segmentation/task_model.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,8 @@ def predict(self, image: PathLike | PILImage | Tensor) -> Tensor:
240240
The predicted mask as a tensor of shape (H, W). The values represent the
241241
class IDs as defined in the `classes` argument of your dataset. These
242242
classes are also stored in the `classes` attribute of the model.
243-
If your dataset contains ignored classes defined by the `ignore_classes`
244-
argument, the model will assign a special value for any pixels that it
245-
cannot assign to any of the known classes. This value is stored as
246-
`class_ignore_index` attribute of the model and is by default -100.
247-
If the dataset doesn't contain any ignored classes, the model will always
248-
assign a known class to each pixel.
243+
The model will always predict the pixels as one of the known classes even when
244+
your dataset contains ignored classes defined by the `ignore_classes` argument.
249245
"""
250246
if self.training:
251247
self.eval()
@@ -267,9 +263,8 @@ class IDs as defined in the `classes` argument of your dataset. These
267263
x = x.unsqueeze(0) # (1, C, H', W')
268264

269265
logits = self._forward_logits(x) # (1, K+1, H', W'), K = len(self.classes)
270-
if self.class_ignore_index is None:
271-
# Restrict logits to known classes only.
272-
logits = logits[:, :-1] # (1, K, H', W')
266+
# Restrict logits to known classes only.
267+
logits = logits[:, :-1] # (1, K, H', W')
273268
logits = F.interpolate(
274269
logits, size=(image_h, image_w), mode="bilinear"
275270
) # (1, K|K+1, H, W)
@@ -281,10 +276,9 @@ class IDs as defined in the `classes` argument of your dataset. These
281276

282277
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
283278
# Function used for ONNX export
284-
logits = self._forward_logits(x) # (B, C, H, W)
285-
if self.class_ignore_index is None:
286-
# Restrict logits to known classes only.
287-
logits = logits[:, :-1]
279+
logits = self._forward_logits(x) # (B, K+1, H, W), K = len(self.classes)
280+
# Restrict logits to known classes only.
281+
logits = logits[:, :-1] # (1, K, H, W)
288282
masks = logits.argmax(dim=1) # (B, H, W)
289283
# Map internal class IDs to class IDs.
290284
masks = self.internal_class_to_class[masks]

0 commit comments

Comments
 (0)