Morelli001 commited on
Commit
badc3e1
·
verified ·
1 Parent(s): 59ce18d

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -83,9 +83,9 @@ result = segmenter("ultrasound.png", threshold=0.7)
83
 
84
  ## Preprocessing
85
 
86
- Images are converted to RGB, resized directly to 512 x 512, and normalized in
87
- the 0-255 value range with mean `[123.675, 116.28, 103.53]` and standard
88
- deviation `[58.395, 57.12, 57.375]`.
89
 
90
  ## Limitations
91
 
 
83
 
84
  ## Preprocessing
85
 
86
+ Images are converted to RGB, resized with preserved aspect ratio, and padded to
87
+ 512 x 512 with black pixels. Each image is then z-score normalized using the
88
+ mean and standard deviation of its non-black pixels, matching the training run.
89
 
90
  ## Limitations
91
 
config.json CHANGED
@@ -31,7 +31,7 @@
31
  },
32
  "image_size": 512,
33
  "in_channels": 3,
34
- "keep_aspect_ratio": false,
35
  "label2id": {
36
  "foreground": 0
37
  },
@@ -39,6 +39,7 @@
39
  "n_heads": 8,
40
  "n_organs": 10,
41
  "patch_size": 8,
 
42
  "shape_res": 32,
43
  "size": 16,
44
  "transformers_version": "5.16.1",
 
31
  },
32
  "image_size": 512,
33
  "in_channels": 3,
34
+ "keep_aspect_ratio": true,
35
  "label2id": {
36
  "foreground": 0
37
  },
 
39
  "n_heads": 8,
40
  "n_organs": 10,
41
  "patch_size": 8,
42
+ "self_normalize": true,
43
  "shape_res": 32,
44
  "size": 16,
45
  "transformers_version": "5.16.1",
configuration_cond_unet.py CHANGED
@@ -23,7 +23,8 @@ class CondUNetConfig(PretrainedConfig):
23
  use_shape=False,
24
  shape_res=32,
25
  unknown_organ_id=-1,
26
- keep_aspect_ratio=False,
 
27
  **kwargs,
28
  ):
29
  super().__init__(**kwargs)
@@ -45,5 +46,6 @@ class CondUNetConfig(PretrainedConfig):
45
  self.shape_res = shape_res
46
  self.unknown_organ_id = unknown_organ_id
47
  self.keep_aspect_ratio = keep_aspect_ratio
 
48
  self.id2label = {0: "foreground"}
49
  self.label2id = {"foreground": 0}
 
23
  use_shape=False,
24
  shape_res=32,
25
  unknown_organ_id=-1,
26
+ keep_aspect_ratio=True,
27
+ self_normalize=True,
28
  **kwargs,
29
  ):
30
  super().__init__(**kwargs)
 
46
  self.shape_res = shape_res
47
  self.unknown_organ_id = unknown_organ_id
48
  self.keep_aspect_ratio = keep_aspect_ratio
49
+ self.self_normalize = self_normalize
50
  self.id2label = {0: "foreground"}
51
  self.label2id = {"foreground": 0}
image_processing_cond_unet.py CHANGED
@@ -13,7 +13,8 @@ class CondUNetImageProcessor(BaseImageProcessor):
13
  def __init__(
14
  self,
15
  image_size=512,
16
- keep_aspect_ratio=False,
 
17
  mean=None,
18
  std=None,
19
  **kwargs,
@@ -21,6 +22,7 @@ class CondUNetImageProcessor(BaseImageProcessor):
21
  super().__init__(**kwargs)
22
  self.image_size = image_size
23
  self.keep_aspect_ratio = keep_aspect_ratio
 
24
  self.mean = mean or [123.675, 116.28, 103.53]
25
  self.std = std or [58.395, 57.12, 57.375]
26
 
@@ -67,6 +69,17 @@ class CondUNetImageProcessor(BaseImageProcessor):
67
  image = image.to(dtype=torch.float32)
68
  if image.max() <= 1:
69
  image = image * 255.0
 
 
 
 
 
 
 
 
 
 
 
70
  mean = torch.tensor(self.mean, dtype=image.dtype).view(-1, 1, 1)
71
  std = torch.tensor(self.std, dtype=image.dtype).view(-1, 1, 1)
72
  return (image - mean) / std
 
13
  def __init__(
14
  self,
15
  image_size=512,
16
+ keep_aspect_ratio=True,
17
+ self_normalize=True,
18
  mean=None,
19
  std=None,
20
  **kwargs,
 
22
  super().__init__(**kwargs)
23
  self.image_size = image_size
24
  self.keep_aspect_ratio = keep_aspect_ratio
25
+ self.self_normalize = self_normalize
26
  self.mean = mean or [123.675, 116.28, 103.53]
27
  self.std = std or [58.395, 57.12, 57.375]
28
 
 
69
  image = image.to(dtype=torch.float32)
70
  if image.max() <= 1:
71
  image = image * 255.0
72
+ if self.self_normalize:
73
+ mask = (image > 0).any(dim=0)
74
+ if mask.any():
75
+ valid_pixels = image[:, mask]
76
+ mean = valid_pixels.mean()
77
+ std = valid_pixels.std()
78
+ if std > 1e-8:
79
+ return (image - mean) / std
80
+ return image - mean
81
+ return image.clone()
82
+
83
  mean = torch.tensor(self.mean, dtype=image.dtype).view(-1, 1, 1)
84
  std = torch.tensor(self.std, dtype=image.dtype).view(-1, 1, 1)
85
  return (image - mean) / std
pipeline.py CHANGED
@@ -14,6 +14,7 @@ class CondUNetImageSegmentationPipeline(Pipeline):
14
  self.image_processor = CondUNetImageProcessor(
15
  image_size=self.model.config.image_size,
16
  keep_aspect_ratio=self.model.config.keep_aspect_ratio,
 
17
  )
18
 
19
  def _sanitize_parameters(self, organ_id=None, threshold=None, **kwargs):
 
14
  self.image_processor = CondUNetImageProcessor(
15
  image_size=self.model.config.image_size,
16
  keep_aspect_ratio=self.model.config.keep_aspect_ratio,
17
+ self_normalize=self.model.config.self_normalize,
18
  )
19
 
20
  def _sanitize_parameters(self, organ_id=None, threshold=None, **kwargs):
preprocessor_config.json CHANGED
@@ -1,12 +1,13 @@
1
  {
2
  "image_processor_type": "CondUNetImageProcessor",
3
  "image_size": 512,
4
- "keep_aspect_ratio": false,
5
  "mean": [
6
  123.675,
7
  116.28,
8
  103.53
9
  ],
 
10
  "std": [
11
  58.395,
12
  57.12,
 
1
  {
2
  "image_processor_type": "CondUNetImageProcessor",
3
  "image_size": 512,
4
+ "keep_aspect_ratio": true,
5
  "mean": [
6
  123.675,
7
  116.28,
8
  103.53
9
  ],
10
+ "self_normalize": true,
11
  "std": [
12
  58.395,
13
  57.12,