diff --git a/artifacts/k8s-deployment-manifest-templates/neuron/pytorch-inference-resnet50.yml b/artifacts/k8s-deployment-manifest-templates/neuron/pytorch-inference-resnet50.yml new file mode 100644 index 0000000..d5870d9 --- /dev/null +++ b/artifacts/k8s-deployment-manifest-templates/neuron/pytorch-inference-resnet50.yml @@ -0,0 +1,96 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: pytorch-resnet50-script +data: + pytorch-resnet50.sh: | + #!/usr/bin/env python + import torch + import numpy as np + import os + import torch_neuron + from torchvision import models, transforms, datasets + import time + from urllib import request + import json + + image = torch.zeros([1, 3, 224, 224], dtype=torch.float32) + + ## Load a pretrained ResNet50 model + model = models.resnet50(pretrained=True) + + ## Tell the model we are using it for evaluation (not training) + model.eval() + model_neuron = torch.neuron.trace(model, example_inputs=[image]) + + ## Create an image directory containing a small kitten + os.makedirs("./torch_neuron_test/images", exist_ok=True) + request.urlretrieve("https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg", + "./torch_neuron_test/images/kitten_small.jpg") + + ## Fetch labels to output the top classifications + request.urlretrieve("https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json","imagenet_class_index.json") + idx2label = [] + + with open("imagenet_class_index.json", "r") as read_file: + class_idx = json.load(read_file) + idx2label = [class_idx[str(k)][1] for k in range(len(class_idx))] + + ## Import a sample image and normalize it into a tensor + normalize = transforms.Normalize( + mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225]) + + eval_dataset = datasets.ImageFolder( + os.path.dirname("./torch_neuron_test/"), + transforms.Compose([ + transforms.Resize([224, 224]), + transforms.ToTensor(), + normalize, + ]) + ) + + image, _ = eval_dataset[0] + image = torch.tensor(image.numpy()[np.newaxis, ...]) + + ## Predict + while True: + results = model_neuron( image ) + time.sleep(0.1) +--- +apiVersion: v1 +kind: Pod +metadata: + name: pytorch-inference-resnet50 +spec: + tolerations: + - key: node-role.kubernetes.io/control-plane + operator: Exists + effect: NoSchedule + - key: node-role.kubernetes.io/master + operator: Exists + effect: NoSchedule + containers: + - name: pytorch + image: 763104351884.dkr.ecr.us-west-2.amazonaws.com/pytorch-inference-neuron:1.13.1-neuron-py310-sdk2.13.2-ubuntu20.04 + command: ["/script/pytorch-resnet50.sh"] + volumeMounts: + - name: script + mountPath: "/script" + resources: + limits: + cpu: 4 + memory: 4Gi + aws.amazon.com/neuron: 1 + requests: + cpu: "1" + memory: 1Gi + securityContext: + capabilities: + add: + - IPC_LOCK + volumes: + - name: script + configMap: + name: pytorch-resnet50-script + defaultMode: 0777