Skip to content

Commit

Permalink
Add inference function for darknet image classification model
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 582028410
  • Loading branch information
tensorflower-gardener committed Nov 13, 2023
1 parent d9e9f47 commit 5a5a403
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions official/projects/yolo/dataloaders/classification_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

"""Classification decoder and parser."""
from typing import List

import tensorflow as tf, tf_keras
from official.vision.dataloaders import classification_input
from official.vision.ops import preprocess_ops
Expand Down Expand Up @@ -90,3 +92,19 @@ def _parse_eval_image(self, decoded_tensors):
image = tf.image.convert_image_dtype(image, self._dtype)
image = image / 255.0
return image

@classmethod
def inference_fn(
cls, image: tf.Tensor, input_image_size: List[int], num_channels: int = 3
) -> tf.Tensor:
"""Builds image model inputs for serving."""

image = tf.cast(image, dtype=tf.float32)
image = preprocess_ops.center_crop_image(image)
image = tf.image.resize(
image, input_image_size, method=tf.image.ResizeMethod.BILINEAR
)

image.set_shape(input_image_size + [num_channels])
image = image / 255.0
return image
2 changes: 1 addition & 1 deletion official/projects/yolo/serving/export_module_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
from official.projects.yolo.configs import darknet_classification
from official.projects.yolo.configs import yolo
from official.projects.yolo.configs import yolov7
from official.projects.yolo.dataloaders import classification_input
from official.projects.yolo.modeling import factory as yolo_factory
from official.projects.yolo.modeling.backbones import darknet # pylint: disable=unused-import
from official.projects.yolo.modeling.decoders import yolo_decoder # pylint: disable=unused-import
from official.projects.yolo.serving import model_fn as yolo_model_fn
from official.vision.dataloaders import classification_input
from official.vision.modeling import factory
from official.vision.serving import export_utils

Expand Down

0 comments on commit 5a5a403

Please sign in to comment.