Documentation
Search…
wandb.data_types.ImageMask
Format image masks or overlays for logging to W&B.
1
ImageMask(
2
val: dict,
3
key: str
4
) -> None
Copied!
Arguments
Text
val
(dictionary) One of these two keys to represent the image: mask_data : (2D numpy array) The mask containing an integer class label for each pixel in the image path : (string) The path to a saved image file of the mask class_labels : (dictionary of integers to strings, optional) A mapping of the integer class labels in the mask to readable class names. These will default to class_0, class_1, class_2, etc.
key
(string) The readable name or id for this mask type (e.g. predictions, ground_truth)

Examples:

Logging a single masked image

1
import numpy as np
2
import wandb
3
4
wandb.init()
5
image = np.random.randint(low=0, high=256, size=(100, 100, 3), dtype=np.uint8)
6
predicted_mask = np.empty((100, 100), dtype=np.uint8)
7
ground_truth_mask = np.empty((100, 100), dtype=np.uint8)
8
9
predicted_mask[:50, :50] = 0
10
predicted_mask[50:, :50] = 1
11
predicted_mask[:50, 50:] = 2
12
predicted_mask[50:, 50:] = 3
13
14
ground_truth_mask[:25, :25] = 0
15
ground_truth_mask[25:, :25] = 1
16
ground_truth_mask[:25, 25:] = 2
17
ground_truth_mask[25:, 25:] = 3
18
19
class_labels = {
20
0: "person",
21
1: "tree",
22
2: "car",
23
3: "road"
24
}
25
26
masked_image = wandb.Image(image, masks={
27
"predictions": {
28
"mask_data": predicted_mask,
29
"class_labels": class_labels
30
},
31
"ground_truth": {
32
"mask_data": ground_truth_mask,
33
"class_labels": class_labels
34
}
35
})
36
wandb.log({"img_with_masks" : masked_image})
Copied!

Log a masked image inside a Table

1
import numpy as np
2
import wandb
3
4
wandb.init()
5
image = np.random.randint(low=0, high=256, size=(100, 100, 3), dtype=np.uint8)
6
predicted_mask = np.empty((100, 100), dtype=np.uint8)
7
ground_truth_mask = np.empty((100, 100), dtype=np.uint8)
8
9
predicted_mask[:50, :50] = 0
10
predicted_mask[50:, :50] = 1
11
predicted_mask[:50, 50:] = 2
12
predicted_mask[50:, 50:] = 3
13
14
ground_truth_mask[:25, :25] = 0
15
ground_truth_mask[25:, :25] = 1
16
ground_truth_mask[:25, 25:] = 2
17
ground_truth_mask[25:, 25:] = 3
18
19
class_labels = {
20
0: "person",
21
1: "tree",
22
2: "car",
23
3: "road"
24
}
25
26
class_set = wandb.Classes([
27
{"name" : "person", "id" : 0},
28
{"name" : "tree", "id" : 1},
29
{"name" : "car", "id" : 2},
30
{"name" : "road", "id" : 3}
31
])
32
33
masked_image = wandb.Image(image, masks={
34
"predictions": {
35
"mask_data": predicted_mask,
36
"class_labels": class_labels
37
},
38
"ground_truth": {
39
"mask_data": ground_truth_mask,
40
"class_labels": class_labels
41
}
42
}, classes=class_set)
43
44
table = wandb.Table(columns=["image"])
45
table.add_data(masked_image)
46
wandb.log({"random_field": table})
Copied!

Methods

type_name

1
@classmethod
2
type_name() -> str
Copied!

validate

1
validate(
2
val: dict
3
) -> bool
Copied!