Deploying TensorFlow Vision Models in Hugging Face with TF Serving
A Vision Transformer image classifier from Hugging Face Transformers can be served locally with TensorFlow Serving by exporting it as a SavedModel with preprocessing and postprocessing embedded in the graph. A custom serving signature accepts base64-encoded JPEG strings, then decodes, resizes, normalizes and transposes them to channel-first, and returns a class label plus softmax confidence. Building these steps into the model cuts request payload size and avoids training-serving skew. The walkthrough covers launching tensorflow_model_server and querying both the REST endpoint (port 8501) and the gRPC endpoint (port 8500), including how to select a model version.
Related posts
- Deploying 🤗 ViT on Kubernetes with TF Serving
Hugging Face mediumAdeptA Vision Transformer from Hugging Face Transformers, exported as a TensorFlow SavedModel, is packaged into a custom TensorFlow Serving Docker image and deployed to a GKE cluster. The walkthrough covers building the image via docker commit with the versioned model directory layout, pushing it to GCR, and writing Deployment, Service (LoadBalancer exposing gRPC 8500 and REST 8501) and HorizontalPodAutoscaler manifests applied with kubectl or Kustomize. It also explains how CPU requests drive HPA scaling decisions and how to tune TF Serving's inter-op and intra-op parallelism flags.
- Deploying 🤗 ViT on Vertex AI
Hugging Face mediumAdeptA TensorFlow ViT B/16 SavedModel from 🤗 Transformers, with base64 image preprocessing and label postprocessing baked into its signature, can be served on Google Cloud Vertex AI in four SDK steps: upload to the Model Registry, create an Endpoint, deploy with machine/accelerator specs and traffic split, then send prediction requests. Code covers ModelServiceClient, EndpointServiceClient and PredictionServiceClient usage and the required {"b64": ...} request payload format. A Locust load test on an n1-standard-8 with one T4 GPU averaged 646 ms latency over about 17k requests, and hourly pricing is roughly $0.44 for the machine plus $0.40 for the T4.
- My Journey to a serverless transformers pipeline on Google Cloud
Hugging Face mediumAdeptA Hugging Face transformers sentiment-analysis pipeline (DistilBERT fine-tuned on SST-2) was deployed as a low-traffic microservice on Google Cloud Run after AI Platform Prediction (beta instability, checkpoint not a pure TensorFlow model) and App Engine (TensorFlow install failure, PyTorch handling only 2 requests per instance) fell short. The final setup is a Flask app served by gunicorn with one worker and one thread in a Docker image, with Cloud Run memory raised to 4 GB because the model needs most of it. Requests take under five seconds including model loading, plus roughly 10 seconds on cold start; loading the model at startup instead of per request would cut both latency and memory. The image is about 1 GB (PyTorch ~700 MB, model ~250 MB), so storage costs roughly €0.10 per GB per month on top of request costs.
- Timm ❤️ Transformers: Use any timm model with transformers
Hugging Face mediumAdeptTimmWrapper lets any timm vision model load through Hugging Face transformers' Auto classes and pipeline API. That makes architectures with no native transformers implementation, like MobileNetV4, usable with the standard tooling. Code examples cover pipeline inference, a Gradio demo, 8-bit quantization with bitsandbytes (346 MB to 88 MB, about 75% smaller, with near-identical predictions), Trainer-based fine-tuning and LoRA via PEFT (0.77% of parameters trainable), and torch.compile benchmarking. Fine-tuned models round-trip back into timm via the Hub with timm.create_model.
- Getting Started with Hugging Face Inference Endpoints
Hugging Face mediumAdeptHugging Face Inference Endpoints deploys Hub models to managed cloud infrastructure in a few clicks, demonstrated here with a fine-tuned Swin image classifier on a GPU instance in AWS eu-west-1. Endpoints come in three security levels: public (no auth), protected (organization token required) and private, which is reachable only from your own AWS VPC through AWS PrivateLink. The walkthrough calls a protected endpoint from Python with a bearer token, checks the analytics and logs (including errors from a missing Content-Type header), then wires up a private endpoint by creating a VPC endpoint in the AWS console and testing it with curl from EC2.