Overview
DCGAN_FashionGen_Handler is a TorchServe handler for serving a DCGAN-based Fashion Image Generator. It extends BaseHandler to load a DCGAN checkpoint from a ZIP archive and generate fashion images from noise tensors with constraint labels. The handler implements the full preprocess -> inference -> postprocess pipeline, converting generated tensors to JPEG images for the response.
Description
The ModelHandler class is the inference handler for the DCGAN Fashion Generator example. It is responsible for extracting the model archive, loading a pretrained DCGAN checkpoint, constructing noise vectors with fashion category constraints, running the generator forward pass, and encoding the resulting image tensors as JPEG byte arrays.
Key Responsibilities
- Model Loading: Extracts a ZIP archive (
MODELSZIP) containing the DCGAN checkpoint and loads it via torch.load()
- Noise Construction: Builds random noise tensors paired with constraint labels for conditional generation
- Image Generation: Runs the DCGAN generator forward pass to produce synthetic fashion images
- JPEG Encoding: Converts output tensors to JPEG-encoded byte arrays for HTTP response
Constants
| Constant |
Description
|
MODELSZIP |
Name of the ZIP archive containing model files
|
CHECKPOINT |
Filename of the DCGAN checkpoint within the archive
|
default_number_of_images |
Default number of images to generate per request
|
Code Reference
Source Location
| File |
Lines |
Repository
|
examples/dcgan_fashiongen/dcgan_fashiongen_handler.py |
L1-86 |
pytorch/serve
|
Key Class
class ModelHandler(BaseHandler):
"""
DCGAN Fashion Generator handler extending BaseHandler.
Lines 12-86.
"""
def initialize(self, context):
"""
Extract the ZIP archive and load the DCGAN checkpoint.
Extracts MODELSZIP into the model directory, then loads
the CHECKPOINT file using torch.load().
Parameters:
context: TorchServe context with system_properties and manifest.
"""
...
def preprocess(self, data):
"""
Build noise tensors with constraint labels.
Constructs random latent vectors and pairs them with
fashion category constraint labels from the request.
Parameters:
data (list): List of request input dicts.
Returns:
tuple: (noise_tensor, constraint_labels) on device.
"""
...
def inference(self, data, *args, **kwargs):
"""
Run the DCGAN generator forward pass.
Parameters:
data (tuple): (noise_tensor, constraint_labels).
Returns:
torch.Tensor: Generated image tensors.
"""
...
def postprocess(self, data):
"""
Convert generated image tensors to JPEG byte arrays.
Parameters:
data (torch.Tensor): Generated images from the DCGAN.
Returns:
list: JPEG-encoded byte arrays, one per generated image.
"""
...
Import
from ts.torch_handler.base_handler import BaseHandler
I/O Contract
| Method |
Input |
Output |
Notes
|
initialize(context) |
Context with system_properties, manifest |
None (sets self.model) |
Extracts ZIP, loads DCGAN checkpoint
|
preprocess(data) |
list of request dicts with constraint labels |
tuple: (noise_tensor, constraint_labels) |
Builds noise vectors on self.device
|
inference(data) |
tuple: (noise_tensor, constraint_labels) |
torch.Tensor of generated images |
DCGAN forward pass
|
postprocess(data) |
torch.Tensor of images |
list of JPEG byte arrays |
Encodes each image as JPEG
|
Usage Examples
Example 1: Handler Initialization
# During model archive extraction, the handler:
# 1. Extracts the ZIP containing DCGAN model files
# 2. Loads the checkpoint into the generator model
handler = ModelHandler()
handler.initialize(context)
# self.model is now the loaded DCGAN generator
Example 2: Generating Fashion Images
# Request body specifies the number of images and constraint category
data = [{"body": {"number_of_images": 4, "label": 2}}]
# Pipeline execution:
noise_and_labels = handler.preprocess(data)
generated_images = handler.inference(noise_and_labels)
jpeg_results = handler.postprocess(generated_images)
# jpeg_results is a list of 4 JPEG byte arrays
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.