Image Segmentation
Transformers
Safetensors
cond_unet
ultrasound
medical-image-segmentation
attention-unet
custom-pipeline
custom_code
Instructions to use AImageLab-Zip/US_Cond-UNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AImageLab-Zip/US_Cond-UNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="AImageLab-Zip/US_Cond-UNet", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForImageSegmentation model = AutoModelForImageSegmentation.from_pretrained("AImageLab-Zip/US_Cond-UNet", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- README.md +3 -3
- config.json +2 -1
- configuration_cond_unet.py +3 -1
- image_processing_cond_unet.py +14 -1
- pipeline.py +1 -0
- preprocessor_config.json +2 -1
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
|
| 87 |
-
|
| 88 |
-
deviation
|
| 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":
|
| 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=
|
|
|
|
| 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=
|
|
|
|
| 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":
|
| 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,
|