keras-hub-nightly 0.16.1.dev202410020340__py3-none-any.whl → 0.16.1.dev202410030339__py3-none-any.whl

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 (35) hide show
  1. keras_hub/api/layers/__init__.py +0 -3
  2. keras_hub/api/models/__init__.py +1 -1
  3. keras_hub/src/layers/preprocessing/audio_converter.py +3 -7
  4. keras_hub/src/layers/preprocessing/image_converter.py +164 -34
  5. keras_hub/src/models/backbone.py +3 -9
  6. keras_hub/src/models/csp_darknet/csp_darknet_image_classifier.py +0 -109
  7. keras_hub/src/models/densenet/densenet_image_classifier.py +0 -128
  8. keras_hub/src/models/densenet/densenet_image_converter.py +2 -4
  9. keras_hub/src/models/feature_pyramid_backbone.py +1 -1
  10. keras_hub/src/models/image_classifier.py +147 -2
  11. keras_hub/src/models/image_classifier_preprocessor.py +3 -3
  12. keras_hub/src/models/image_segmenter.py +0 -5
  13. keras_hub/src/models/mix_transformer/mix_transformer_classifier.py +0 -109
  14. keras_hub/src/models/mobilenet/mobilenet_image_classifier.py +0 -92
  15. keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py +2 -4
  16. keras_hub/src/models/preprocessor.py +3 -5
  17. keras_hub/src/models/resnet/resnet_backbone.py +1 -11
  18. keras_hub/src/models/resnet/resnet_image_classifier.py +0 -137
  19. keras_hub/src/models/resnet/resnet_image_converter.py +2 -4
  20. keras_hub/src/models/sam/__init__.py +5 -0
  21. keras_hub/src/models/sam/sam_image_converter.py +2 -4
  22. keras_hub/src/models/sam/sam_image_segmenter_preprocessor.py +11 -1
  23. keras_hub/src/models/sam/sam_presets.py +3 -3
  24. keras_hub/src/models/task.py +23 -25
  25. keras_hub/src/models/vgg/vgg_backbone.py +1 -20
  26. keras_hub/src/models/vgg/vgg_image_classifier.py +108 -29
  27. keras_hub/src/tokenizers/tokenizer.py +3 -6
  28. keras_hub/src/utils/preset_utils.py +103 -61
  29. keras_hub/src/utils/timm/preset_loader.py +8 -9
  30. keras_hub/src/version_utils.py +1 -1
  31. {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.16.1.dev202410030339.dist-info}/METADATA +1 -1
  32. {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.16.1.dev202410030339.dist-info}/RECORD +34 -35
  33. keras_hub/src/layers/preprocessing/resizing_image_converter.py +0 -138
  34. {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.16.1.dev202410030339.dist-info}/WHEEL +0 -0
  35. {keras_hub_nightly-0.16.1.dev202410020340.dist-info → keras_hub_nightly-0.16.1.dev202410030339.dist-info}/top_level.txt +0 -0
@@ -15,11 +15,156 @@ class ImageClassifier(Task):
15
15
 
16
16
  To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)`
17
17
  labels where `x` is a string and `y` is a integer from `[0, num_classes)`.
18
+ All `ImageClassifier` tasks include a `from_preset()` constructor which can
19
+ be used to load a pre-trained config and weights.
18
20
 
19
- All `ImageClassifier` tasks include a `from_preset()` constructor which can be
20
- used to load a pre-trained config and weights.
21
+ Args:
22
+ backbone: A `keras_hub.models.Backbone` instance or a `keras.Model`.
23
+ num_classes: int. The number of classes to predict.
24
+ preprocessor: `None`, a `keras_hub.models.Preprocessor` instance,
25
+ a `keras.Layer` instance, or a callable. If `None` no preprocessing
26
+ will be applied to the inputs.
27
+ pooling: `"avg"` or `"max"`. The type of pooling to apply on backbone
28
+ output. Defaults to average pooling.
29
+ activation: `None`, str, or callable. The activation function to use on
30
+ the `Dense` layer. Set `activation=None` to return the output
31
+ logits. Defaults to `"softmax"`.
32
+ head_dtype: `None`, str, or `keras.mixed_precision.DTypePolicy`. The
33
+ dtype to use for the classification head's computations and weights.
34
+
35
+ Examples:
36
+
37
+ Call `predict()` to run inference.
38
+ ```python
39
+ # Load preset and train
40
+ images = np.random.randint(0, 256, size=(2, 224, 224, 3))
41
+ classifier = keras_hub.models.ImageClassifier.from_preset(
42
+ "resnet_50_imagenet"
43
+ )
44
+ classifier.predict(images)
45
+ ```
46
+
47
+ Call `fit()` on a single batch.
48
+ ```python
49
+ # Load preset and train
50
+ images = np.random.randint(0, 256, size=(2, 224, 224, 3))
51
+ labels = [0, 3]
52
+ classifier = keras_hub.models.ImageClassifier.from_preset(
53
+ "resnet_50_imagenet"
54
+ )
55
+ classifier.fit(x=images, y=labels, batch_size=2)
56
+ ```
57
+
58
+ Call `fit()` with custom loss, optimizer and backbone.
59
+ ```python
60
+ classifier = keras_hub.models.ImageClassifier.from_preset(
61
+ "resnet_50_imagenet"
62
+ )
63
+ classifier.compile(
64
+ loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
65
+ optimizer=keras.optimizers.Adam(5e-5),
66
+ )
67
+ classifier.backbone.trainable = False
68
+ classifier.fit(x=images, y=labels, batch_size=2)
69
+ ```
70
+
71
+ Custom backbone.
72
+ ```python
73
+ images = np.random.randint(0, 256, size=(2, 224, 224, 3))
74
+ labels = [0, 3]
75
+ backbone = keras_hub.models.ResNetBackbone(
76
+ stackwise_num_filters=[64, 64, 64],
77
+ stackwise_num_blocks=[2, 2, 2],
78
+ stackwise_num_strides=[1, 2, 2],
79
+ block_type="basic_block",
80
+ use_pre_activation=True,
81
+ pooling="avg",
82
+ )
83
+ classifier = keras_hub.models.ImageClassifier(
84
+ backbone=backbone,
85
+ num_classes=4,
86
+ )
87
+ classifier.fit(x=images, y=labels, batch_size=2)
88
+ ```
21
89
  """
22
90
 
91
+ def __init__(
92
+ self,
93
+ backbone,
94
+ num_classes,
95
+ preprocessor=None,
96
+ pooling="avg",
97
+ activation=None,
98
+ dropout=0.0,
99
+ head_dtype=None,
100
+ **kwargs,
101
+ ):
102
+ head_dtype = head_dtype or backbone.dtype_policy
103
+ data_format = getattr(backbone, "data_format", None)
104
+
105
+ # === Layers ===
106
+ self.backbone = backbone
107
+ self.preprocessor = preprocessor
108
+ if pooling == "avg":
109
+ self.pooler = keras.layers.GlobalAveragePooling2D(
110
+ data_format,
111
+ dtype=head_dtype,
112
+ name="pooler",
113
+ )
114
+ elif pooling == "max":
115
+ self.pooler = keras.layers.GlobalMaxPooling2D(
116
+ data_format,
117
+ dtype=head_dtype,
118
+ name="pooler",
119
+ )
120
+ else:
121
+ raise ValueError(
122
+ "Unknown `pooling` type. Polling should be either `'avg'` or "
123
+ f"`'max'`. Received: pooling={pooling}."
124
+ )
125
+ self.output_dropout = keras.layers.Dropout(
126
+ dropout,
127
+ dtype=head_dtype,
128
+ name="output_dropout",
129
+ )
130
+ self.output_dense = keras.layers.Dense(
131
+ num_classes,
132
+ activation=activation,
133
+ dtype=head_dtype,
134
+ name="predictions",
135
+ )
136
+
137
+ # === Functional Model ===
138
+ inputs = self.backbone.input
139
+ x = self.backbone(inputs)
140
+ x = self.pooler(x)
141
+ x = self.output_dropout(x)
142
+ outputs = self.output_dense(x)
143
+ super().__init__(
144
+ inputs=inputs,
145
+ outputs=outputs,
146
+ **kwargs,
147
+ )
148
+
149
+ # === Config ===
150
+ self.num_classes = num_classes
151
+ self.activation = activation
152
+ self.pooling = pooling
153
+ self.dropout = dropout
154
+
155
+ def get_config(self):
156
+ # Backbone serialized in `super`
157
+ config = super().get_config()
158
+ config.update(
159
+ {
160
+ "num_classes": self.num_classes,
161
+ "pooling": self.pooling,
162
+ "activation": self.activation,
163
+ "dropout": self.dropout,
164
+ }
165
+ )
166
+ return config
167
+
23
168
  def compile(
24
169
  self,
25
170
  optimizer="auto",
@@ -38,15 +38,15 @@ class ImageClassifierPreprocessor(Preprocessor):
38
38
  )
39
39
 
40
40
  # Resize a single image for resnet 50.
41
- x = np.ones((512, 512, 3))
41
+ x = np.random.randint(0, 256, (512, 512, 3))
42
42
  x = preprocessor(x)
43
43
 
44
44
  # Resize a labeled image.
45
- x, y = np.ones((512, 512, 3)), 1
45
+ x, y = np.random.randint(0, 256, (512, 512, 3)), 1
46
46
  x, y = preprocessor(x, y)
47
47
 
48
48
  # Resize a batch of labeled images.
49
- x, y = [np.ones((512, 512, 3)), np.zeros((512, 512, 3))], [1, 0]
49
+ x, y = [np.random.randint(0, 256, (512, 512, 3)), np.zeros((512, 512, 3))], [1, 0]
50
50
  x, y = preprocessor(x, y)
51
51
 
52
52
  # Use a `tf.data.Dataset`.
@@ -16,11 +16,6 @@ class ImageSegmenter(Task):
16
16
  be used to load a pre-trained config and weights.
17
17
  """
18
18
 
19
- def __init__(self, *args, **kwargs):
20
- super().__init__(*args, **kwargs)
21
- # Default compilation.
22
- self.compile()
23
-
24
19
  def compile(
25
20
  self,
26
21
  optimizer="auto",
@@ -1,5 +1,3 @@
1
- import keras
2
-
3
1
  from keras_hub.src.api_export import keras_hub_export
4
2
  from keras_hub.src.models.image_classifier import ImageClassifier
5
3
  from keras_hub.src.models.mix_transformer.mix_transformer_backbone import (
@@ -9,111 +7,4 @@ from keras_hub.src.models.mix_transformer.mix_transformer_backbone import (
9
7
 
10
8
  @keras_hub_export("keras_hub.models.MiTImageClassifier")
11
9
  class MiTImageClassifier(ImageClassifier):
12
- """MiTImageClassifier image classifier model.
13
-
14
- Args:
15
- backbone: A `keras_hub.models.MiTBackbone` instance.
16
- num_classes: int. The number of classes to predict.
17
- activation: `None`, str or callable. The activation function to use on
18
- the `Dense` layer. Set `activation=None` to return the output
19
- logits. Defaults to `"softmax"`.
20
-
21
- To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)`
22
- where `x` is a tensor and `y` is a integer from `[0, num_classes)`.
23
- All `ImageClassifier` tasks include a `from_preset()` constructor which can
24
- be used to load a pre-trained config and weights.
25
-
26
- Examples:
27
-
28
- Call `predict()` to run inference.
29
- ```python
30
- # Load preset and train
31
- images = np.ones((2, 224, 224, 3), dtype="float32")
32
- classifier = keras_hub.models.MiTImageClassifier.from_preset(
33
- "mit_b0_imagenet")
34
- classifier.predict(images)
35
- ```
36
-
37
- Call `fit()` on a single batch.
38
- ```python
39
- # Load preset and train
40
- images = np.ones((2, 224, 224, 3), dtype="float32")
41
- labels = [0, 3]
42
- classifier = keras_hub.models.MixTransformerImageClassifier.from_preset(
43
- "mit_b0_imagenet")
44
- classifier.fit(x=images, y=labels, batch_size=2)
45
- ```
46
-
47
- Call `fit()` with custom loss, optimizer and backbone.
48
- ```python
49
- classifier = keras_hub.models.MiTImageClassifier.from_preset(
50
- "mit_b0_imagenet")
51
- classifier.compile(
52
- loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
53
- optimizer=keras.optimizers.Adam(5e-5),
54
- )
55
- classifier.backbone.trainable = False
56
- classifier.fit(x=images, y=labels, batch_size=2)
57
- ```
58
-
59
- Custom backbone.
60
- ```python
61
- images = np.ones((2, 224, 224, 3), dtype="float32")
62
- labels = [0, 3]
63
- backbone = keras_hub.models.MiTBackbone(
64
- stackwise_num_filters=[128, 256, 512, 1024],
65
- stackwise_depth=[3, 9, 9, 3],
66
- block_type="basic_block",
67
- image_shape = (224, 224, 3),
68
- )
69
- classifier = keras_hub.models.MiTImageClassifier(
70
- backbone=backbone,
71
- num_classes=4,
72
- )
73
- classifier.fit(x=images, y=labels, batch_size=2)
74
- ```
75
- """
76
-
77
10
  backbone_cls = MiTBackbone
78
-
79
- def __init__(
80
- self,
81
- backbone,
82
- num_classes,
83
- activation="softmax",
84
- preprocessor=None, # adding this dummy arg for saved model test
85
- # TODO: once preprocessor flow is figured out, this needs to be updated
86
- **kwargs,
87
- ):
88
- # === Layers ===
89
- self.backbone = backbone
90
- self.output_dense = keras.layers.Dense(
91
- num_classes,
92
- activation=activation,
93
- name="predictions",
94
- )
95
-
96
- # === Functional Model ===
97
- inputs = self.backbone.input
98
- x = self.backbone(inputs)
99
- outputs = self.output_dense(x)
100
- super().__init__(
101
- inputs=inputs,
102
- outputs=outputs,
103
- **kwargs,
104
- )
105
-
106
- # === Config ===
107
- self.num_classes = num_classes
108
- self.activation = activation
109
-
110
- def get_config(self):
111
- # Backbone serialized in `super`
112
- config = super().get_config()
113
- config.update(
114
- {
115
- "num_classes": self.num_classes,
116
- "activation": self.activation,
117
- }
118
- )
119
- return config
@@ -1,5 +1,3 @@
1
- import keras
2
-
3
1
  from keras_hub.src.api_export import keras_hub_export
4
2
  from keras_hub.src.models.image_classifier import ImageClassifier
5
3
  from keras_hub.src.models.mobilenet.mobilenet_backbone import MobileNetBackbone
@@ -7,94 +5,4 @@ from keras_hub.src.models.mobilenet.mobilenet_backbone import MobileNetBackbone
7
5
 
8
6
  @keras_hub_export("keras_hub.models.MobileNetImageClassifier")
9
7
  class MobileNetImageClassifier(ImageClassifier):
10
- """MobileNetV3 image classifier task model.
11
-
12
- To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)`
13
- where `x` is a tensor and `y` is a integer from `[0, num_classes)`.
14
- All `ImageClassifier` tasks include a `from_preset()` constructor which can
15
- be used to load a pre-trained config and weights.
16
-
17
- Args:
18
- backbone: A `keras_hub.models.MobileNetBackbone` instance.
19
- num_classes: int. The number of classes to predict.
20
- activation: `None`, str or callable. The activation function to use on
21
- the `Dense` layer. Set `activation=None` to return the output
22
- logits. Defaults to `"softmax"`.
23
-
24
- Examples:
25
-
26
- Call `predict()` to run inference.
27
- ```python
28
- # Load preset and train
29
- images = np.ones((2, 224, 224, 3), dtype="float32")
30
- classifier = keras_hub.models.MobileNetImageClassifier.from_preset(
31
- "mobilenet_v3_small_imagenet")
32
- classifier.predict(images)
33
- ```
34
-
35
- Custom backbone.
36
- ```python
37
- images = np.ones((2, 224, 224, 3), dtype="float32")
38
- labels = [0, 3]
39
- model = MobileNetBackbone(
40
- stackwise_expansion = [1, 4, 6],
41
- stackwise_filters = [4, 8, 16],
42
- stackwise_kernel_size = [3, 3, 5],
43
- stackwise_stride = [2, 2, 1],
44
- stackwise_se_ratio = [ 0.25, None, 0.25],
45
- stackwise_activation = ["relu", "relu", "hard_swish"],
46
- output_filter=1280,
47
- activation="hard_swish",
48
- inverted_res_block=True,
49
- )
50
- classifier = keras_hub.models.MobileNetImageClassifier(
51
- backbone=backbone,
52
- num_classes=4,
53
- )
54
- classifier.fit(x=images, y=labels, batch_size=2)
55
- ```
56
- """
57
-
58
8
  backbone_cls = MobileNetBackbone
59
-
60
- def __init__(
61
- self,
62
- backbone,
63
- num_classes,
64
- activation="softmax",
65
- preprocessor=None, # adding this dummy arg for saved model test
66
- # TODO: once preprocessor flow is figured out, this needs to be updated
67
- **kwargs,
68
- ):
69
- # === Layers ===
70
- self.backbone = backbone
71
- self.output_dense = keras.layers.Dense(
72
- num_classes,
73
- activation=activation,
74
- name="predictions",
75
- )
76
-
77
- # === Functional Model ===
78
- inputs = self.backbone.input
79
- x = self.backbone(inputs)
80
- outputs = self.output_dense(x)
81
- super().__init__(
82
- inputs=inputs,
83
- outputs=outputs,
84
- **kwargs,
85
- )
86
-
87
- # === Config ===
88
- self.num_classes = num_classes
89
- self.activation = activation
90
-
91
- def get_config(self):
92
- # Backbone serialized in `super`
93
- config = super().get_config()
94
- config.update(
95
- {
96
- "num_classes": self.num_classes,
97
- "activation": self.activation,
98
- }
99
- )
100
- return config
@@ -1,12 +1,10 @@
1
1
  from keras_hub.src.api_export import keras_hub_export
2
- from keras_hub.src.layers.preprocessing.resizing_image_converter import (
3
- ResizingImageConverter,
4
- )
2
+ from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
5
3
  from keras_hub.src.models.pali_gemma.pali_gemma_backbone import (
6
4
  PaliGemmaBackbone,
7
5
  )
8
6
 
9
7
 
10
8
  @keras_hub_export("keras_hub.layers.PaliGemmaImageConverter")
11
- class PaliGemmaImageConverter(ResizingImageConverter):
9
+ class PaliGemmaImageConverter(ImageConverter):
12
10
  backbone_cls = PaliGemmaBackbone
@@ -8,7 +8,7 @@ from keras_hub.src.utils.preset_utils import PREPROCESSOR_CONFIG_FILE
8
8
  from keras_hub.src.utils.preset_utils import builtin_presets
9
9
  from keras_hub.src.utils.preset_utils import find_subclass
10
10
  from keras_hub.src.utils.preset_utils import get_preset_loader
11
- from keras_hub.src.utils.preset_utils import save_serialized_object
11
+ from keras_hub.src.utils.preset_utils import get_preset_saver
12
12
  from keras_hub.src.utils.python_utils import classproperty
13
13
 
14
14
 
@@ -209,7 +209,5 @@ class Preprocessor(PreprocessingLayer):
209
209
  Args:
210
210
  preset_dir: The path to the local model preset directory.
211
211
  """
212
- save_serialized_object(self, preset_dir, config_file=self.config_name)
213
- for layer in self._flatten_layers(include_self=False):
214
- if hasattr(layer, "save_to_preset"):
215
- layer.save_to_preset(preset_dir)
212
+ saver = get_preset_saver(preset_dir)
213
+ saver.save_preprocessor(self)
@@ -51,16 +51,6 @@ class ResNetBackbone(FeaturePyramidBackbone):
51
51
  `True` for ResNetV2, `False` for ResNet.
52
52
  image_shape: tuple. The input shape without the batch size.
53
53
  Defaults to `(None, None, 3)`.
54
- pooling: `None` or str. Pooling mode for feature extraction. Defaults
55
- to `"avg"`.
56
- - `None` means that the output of the model will be the 4D tensor
57
- from the last convolutional block.
58
- - `avg` means that global average pooling will be applied to the
59
- output of the last convolutional block, resulting in a 2D
60
- tensor.
61
- - `max` means that global max pooling will be applied to the
62
- output of the last convolutional block, resulting in a 2D
63
- tensor.
64
54
  data_format: `None` or str. If specified, either `"channels_last"` or
65
55
  `"channels_first"`. The ordering of the dimensions in the
66
56
  inputs. `"channels_last"` corresponds to inputs with shape
@@ -75,7 +65,7 @@ class ResNetBackbone(FeaturePyramidBackbone):
75
65
 
76
66
  Examples:
77
67
  ```python
78
- input_data = np.random.uniform(0, 255, size=(2, 224, 224, 3))
68
+ input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
79
69
 
80
70
  # Pretrained ResNet backbone.
81
71
  model = keras_hub.models.ResNetBackbone.from_preset("resnet50")
@@ -1,5 +1,3 @@
1
- import keras
2
-
3
1
  from keras_hub.src.api_export import keras_hub_export
4
2
  from keras_hub.src.models.image_classifier import ImageClassifier
5
3
  from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone
@@ -10,140 +8,5 @@ from keras_hub.src.models.resnet.resnet_image_classifier_preprocessor import (
10
8
 
11
9
  @keras_hub_export("keras_hub.models.ResNetImageClassifier")
12
10
  class ResNetImageClassifier(ImageClassifier):
13
- """ResNet image classifier task model.
14
-
15
- Args:
16
- backbone: A `keras_hub.models.ResNetBackbone` instance.
17
- num_classes: int. The number of classes to predict.
18
- activation: `None`, str or callable. The activation function to use on
19
- the `Dense` layer. Set `activation=None` to return the output
20
- logits. Defaults to `"softmax"`.
21
- head_dtype: `None` or str or `keras.mixed_precision.DTypePolicy`. The
22
- dtype to use for the classification head's computations and weights.
23
-
24
- To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)`
25
- where `x` is a tensor and `y` is a integer from `[0, num_classes)`.
26
- All `ImageClassifier` tasks include a `from_preset()` constructor which can
27
- be used to load a pre-trained config and weights.
28
-
29
- Examples:
30
-
31
- Call `predict()` to run inference.
32
- ```python
33
- # Load preset and train
34
- images = np.ones((2, 224, 224, 3), dtype="float32")
35
- classifier = keras_hub.models.ResNetImageClassifier.from_preset(
36
- "resnet_50_imagenet"
37
- )
38
- classifier.predict(images)
39
- ```
40
-
41
- Call `fit()` on a single batch.
42
- ```python
43
- # Load preset and train
44
- images = np.ones((2, 224, 224, 3), dtype="float32")
45
- labels = [0, 3]
46
- classifier = keras_hub.models.ResNetImageClassifier.from_preset(
47
- "resnet_50_imagenet"
48
- )
49
- classifier.fit(x=images, y=labels, batch_size=2)
50
- ```
51
-
52
- Call `fit()` with custom loss, optimizer and backbone.
53
- ```python
54
- classifier = keras_hub.models.ResNetImageClassifier.from_preset(
55
- "resnet_50_imagenet"
56
- )
57
- classifier.compile(
58
- loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
59
- optimizer=keras.optimizers.Adam(5e-5),
60
- )
61
- classifier.backbone.trainable = False
62
- classifier.fit(x=images, y=labels, batch_size=2)
63
- ```
64
-
65
- Custom backbone.
66
- ```python
67
- images = np.ones((2, 224, 224, 3), dtype="float32")
68
- labels = [0, 3]
69
- backbone = keras_hub.models.ResNetBackbone(
70
- stackwise_num_filters=[64, 64, 64],
71
- stackwise_num_blocks=[2, 2, 2],
72
- stackwise_num_strides=[1, 2, 2],
73
- block_type="basic_block",
74
- use_pre_activation=True,
75
- pooling="avg",
76
- )
77
- classifier = keras_hub.models.ResNetImageClassifier(
78
- backbone=backbone,
79
- num_classes=4,
80
- )
81
- classifier.fit(x=images, y=labels, batch_size=2)
82
- ```
83
- """
84
-
85
11
  backbone_cls = ResNetBackbone
86
12
  preprocessor_cls = ResNetImageClassifierPreprocessor
87
-
88
- def __init__(
89
- self,
90
- backbone,
91
- num_classes,
92
- preprocessor=None,
93
- pooling="avg",
94
- activation=None,
95
- head_dtype=None,
96
- **kwargs,
97
- ):
98
- head_dtype = head_dtype or backbone.dtype_policy
99
-
100
- # === Layers ===
101
- self.backbone = backbone
102
- self.preprocessor = preprocessor
103
- if pooling == "avg":
104
- self.pooler = keras.layers.GlobalAveragePooling2D(
105
- data_format=backbone.data_format, dtype=head_dtype
106
- )
107
- elif pooling == "max":
108
- self.pooler = keras.layers.GlobalAveragePooling2D(
109
- data_format=backbone.data_format, dtype=head_dtype
110
- )
111
- else:
112
- raise ValueError(
113
- "Unknown `pooling` type. Polling should be either `'avg'` or "
114
- f"`'max'`. Received: pooling={pooling}."
115
- )
116
- self.output_dense = keras.layers.Dense(
117
- num_classes,
118
- activation=activation,
119
- dtype=head_dtype,
120
- name="predictions",
121
- )
122
-
123
- # === Functional Model ===
124
- inputs = self.backbone.input
125
- x = self.backbone(inputs)
126
- x = self.pooler(x)
127
- outputs = self.output_dense(x)
128
- super().__init__(
129
- inputs=inputs,
130
- outputs=outputs,
131
- **kwargs,
132
- )
133
-
134
- # === Config ===
135
- self.num_classes = num_classes
136
- self.activation = activation
137
- self.pooling = pooling
138
-
139
- def get_config(self):
140
- # Backbone serialized in `super`
141
- config = super().get_config()
142
- config.update(
143
- {
144
- "num_classes": self.num_classes,
145
- "pooling": self.pooling,
146
- "activation": self.activation,
147
- }
148
- )
149
- return config
@@ -1,10 +1,8 @@
1
1
  from keras_hub.src.api_export import keras_hub_export
2
- from keras_hub.src.layers.preprocessing.resizing_image_converter import (
3
- ResizingImageConverter,
4
- )
2
+ from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
5
3
  from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone
6
4
 
7
5
 
8
6
  @keras_hub_export("keras_hub.layers.ResNetImageConverter")
9
- class ResNetImageConverter(ResizingImageConverter):
7
+ class ResNetImageConverter(ImageConverter):
10
8
  backbone_cls = ResNetBackbone
@@ -0,0 +1,5 @@
1
+ from keras_hub.src.models.sam.sam_backbone import SAMBackbone
2
+ from keras_hub.src.models.sam.sam_presets import backbone_presets
3
+ from keras_hub.src.utils.preset_utils import register_presets
4
+
5
+ register_presets(backbone_presets, SAMBackbone)
@@ -1,10 +1,8 @@
1
1
  from keras_hub.src.api_export import keras_hub_export
2
- from keras_hub.src.layers.preprocessing.resizing_image_converter import (
3
- ResizingImageConverter,
4
- )
2
+ from keras_hub.src.layers.preprocessing.image_converter import ImageConverter
5
3
  from keras_hub.src.models.sam.sam_backbone import SAMBackbone
6
4
 
7
5
 
8
6
  @keras_hub_export("keras_hub.layers.SAMImageConverter")
9
- class SAMImageConverter(ResizingImageConverter):
7
+ class SAMImageConverter(ImageConverter):
10
8
  backbone_cls = SAMBackbone
@@ -1,12 +1,22 @@
1
+ import keras
2
+
1
3
  from keras_hub.src.api_export import keras_hub_export
2
4
  from keras_hub.src.models.image_segmenter_preprocessor import (
3
5
  ImageSegmenterPreprocessor,
4
6
  )
5
7
  from keras_hub.src.models.sam.sam_backbone import SAMBackbone
6
8
  from keras_hub.src.models.sam.sam_image_converter import SAMImageConverter
9
+ from keras_hub.src.utils.tensor_utils import preprocessing_function
7
10
 
8
11
 
9
- @keras_hub_export("keras_hub.models.SamImageSegmenterPreprocessor")
12
+ @keras_hub_export("keras_hub.models.SAMImageSegmenterPreprocessor")
10
13
  class SAMImageSegmenterPreprocessor(ImageSegmenterPreprocessor):
11
14
  backbone_cls = SAMBackbone
12
15
  image_converter_cls = SAMImageConverter
16
+
17
+ @preprocessing_function
18
+ def call(self, x, y=None, sample_weight=None):
19
+ images = x["images"]
20
+ if self.image_converter:
21
+ x["images"] = self.image_converter(images)
22
+ return keras.utils.pack_x_y_sample_weight(x, y, sample_weight)