top of page

Using Amazon's SageMaker image-classifier in standalone applications.

Amazon's SageMaker environment provides an end-to-end managed machine learning (ML) framework. Using it's integrated Jupyter environment, distributed training, and scalable deployment you can quickly develop large ML models. This is particularly true if you utilize one of the provided built-in algorithms, such as the MxNet image-classifier.


The image-classifier model is easy to train and deploy as a SageMaker EndPoint, skipping some of the setup, it basically looks like this:

########################################
# Prepare the model
training_image = get_image_uri(
    boto3.Session().region_name,
    'image-classification',
    repo_version='latest'
)

model = sagemaker.estimator.Estimator(
    training_image,
    role=role,
    train_instance_count=1,
    train_instance_type='ml.p3.2xlarge',
    train_volume_size=100, # GB
    train_max_run=36000, # seconds
    input_mode='File',
    output_path=f's3://{bucket_name}/output',
    sagemaker_sessions=sess
)

model.set_hyperparameters(
    num_layers=18,
    use_pretrained_model=1,
    image_shape='3,244,244',
    num_classes=len(classes),
    mini_batch_size=32,
    resize=244,
    epochs=10,
    learning_rate=0.001,
    num_training_samples=train_count,
    augmentation_type='crop_color_transform'
)

########################################
# Train the model
model.fit(
    inputs=data_channels,
    logs=True
)

########################################
# Deploy the model
deployed_model = model.deploy(
    initial_instance_count=1,
    instance_type='ml.m4.xlarge',
    serializer=sagemaker.serializers.IdentitySerializer("image/jpeg"),
    deserializer=sagemaker.deserializers.JSONDeserializer()
)

However, if you want to use the trained model outside of SageMaker (i.e. not using a SageMaker EndPoint) it's takes a few extra steps.


First, you need to download the trained model produced by SageMaker:

s3.download_file(
    bucket-name,
    'output/image-classification-2023-02-18-23-26-22-023/output/model.tar.gz",
    'model.tar.gz'
)

Inside the model.tar.gz will be two files that are required to reconstruct the model: image-classification-symbol.json and image-classification-0010.params. These can be loaded into MXNet to instantiate the model.


ctx = mx.gpu() if mx.context.num_gpus() else mx.cpu()
deserialized_net = gluon.nn.SymbolBlock.imports(
    "image-classification-symbol.json",
    ['data','softmax_label'],
    "image-classification-0010.params",
    ctx=ctx
)
deserialized_net.hybridize(static_alloc=True, static_shape=True)

From this instantiated model, you can now directly make predictions:

img = mx.image.imread(image_path)
img = mx.image.imresize(img, 224, 224)
img = img.transpose((2, 0, 1)) # Channel first
img = img.expand_dims(axis=0) # batchify
img = img.astype('float32') # for gpu context

l = mx.nd.zeros(shape=(1))
inputs = [img, l]
outputs = deserialized_net(*inputs)
result = classes[np.argmax(outputs.asnumpy())]


83 views
bottom of page