Skip to content

Commit

Permalink
fix: add in MODEL_WEIGHTS, MODEL_LABELS to cdk stack
Browse files Browse the repository at this point in the history
  • Loading branch information
danellecline committed Aug 14, 2023
1 parent fc2ee43 commit 8571930
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
[![Python](https://img.shields.io/badge/language-Python-blue.svg)](https://www.python.org/downloads/)

**fastapi-yolov5** code deploys and runs the YOLOv5 model in the Python web framework [FastAPI](https://fastapi.tiangolo.com/) either locally or in AWS.
In AWS, the model is deployed with ECS Fargate and exposed with an Application Load Balancer.

It is currently live at https://deepsea-ai.mbari.org/megadetector/docs

![Image link ](api_example.png)

# Requirements

- [Docker](https://docs.docker.com/get-docker/)
Expand Down Expand Up @@ -43,15 +46,15 @@ docker-compose down
# Running

## Health Check
Check the health of the server by going to `http://localhost:3000/health`. You should see the following response:
Check the health of the server by going to `http://localhost:8000/health`. You should see the following response:

```json
{"status":"ok"}
```

## Predict to JSON

Send a POST request to `http://localhost:3000/predict` with an image file in the body to get a prediction returned in JSON format.
Send a POST request to `http://localhost:8000/predict` with an image file in the body to get a prediction returned in JSON format.
By default, predictions greater than 0.01 are posted.

```shell
Expand Down Expand Up @@ -120,7 +123,7 @@ app:
environment:
- MODEL_WEIGHTS=/app/models/best/best.pt
- MODEL_LABELS=/app/models/best/labels.txt
- MODEL_DESCRIPTION="Megadetector"
- MODEL_DESCRIPTION=Megadetector
```
The labels file should be in the format of one label per line.
Expand All @@ -141,16 +144,22 @@ docker-compose up

## Deploying a custom model in AWS

To override the default model, upload the `best.pt` file and the labels file `labels.txt` for that model to an S3 bucket.
Specify the S3 bucket in the `MODELPATH` environment variable in the `config.yaml` file.
The S3 bucket must be in the same region as the ECS cluster
Specify any custom configuration on scaling in `config.yaml` file for the stack resources, e.g. min and max capacity.

```yaml
app:
environment:
- MODEL_WEIGHTS=s3://901103-models-deploy/megadetector/best.pt
- MODEL_LABELS=s3://901103-models-deploy/megadetector/best.pt
- MODEL_DESCRIPTION="Megadetector"
MinCapacity: 1
MaxCapacity: 5
```
To override the default model, upload yor model, e.g. `best.pt` file and the labels file `labels.txt` for that model to an S3 bucket.
The S3 bucket must be in the same region as the ECS cluster. Define the S3 bucket and the model path in the `config.yml` file.
These can be overridden as needed in the AWS console through the ECS task definition as environment variables.

```yaml
MODEL_WEIGHTS: s3://901103-models-deploy/midwatervars102/best.pt
MODEL_LABELS: s3://901103-models-deploy/midwatervars102/labels.txt
MODEL_DESCRIPTION: Megadetector
MODEL_INPUT_SIZE: 1280
```

Deploy the stack with the new configuration
Expand Down
Binary file added api_example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions cdk/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ def __init__(
with open("config.yml", 'r') as stream:
config = yaml.safe_load(stream)

# Get the model path, and capacity from the config file
model_path = config['ModelPath']
# Get the model information from environment variables
model_weights = config['ModelWeights']
model_labels = config['ModelLabels']

# Cluster capacity
min_capacity = config['MinCapacity']
max_capacity = config['MaxCapacity']
model_description = config['ModelDescription']
model_input_size = config['ModelInputSize']

# Create VPC
vpc = ec2.Vpc(self, "FastAPIYOLOVv5VPC", max_azs=2)
Expand Down Expand Up @@ -74,8 +75,6 @@ def __init__(
connection=ec2.Port.tcp(80)
)

# Retrieve the secret value from AWS Secrets Manager

# Retrieve the AWS access key ID secret value from AWS Secrets Manager
secret = secrets_manager.Secret.from_secret_name_v2(self, "MySecretID", secret_name="prod/s3download")

Expand All @@ -87,9 +86,10 @@ def __init__(
image=docker_image,
container_port=80,
environment={
"MODEL_PATH": model_path,
"MODEL_DESCRIPTION": model_description,
"MODEL_INPUT_SIZE": str(model_input_size),
"MODEL_WEIGHTS": config['MODEL_WEIGHTS'],
"MODEL_LABELS": config['MODEL_LABELS'],
"MODEL_DESCRIPTION": config['MODEL_DESCRIPTION'],
"MODEL_INPUT_SIZE": str(config['MODEL_INPUT_SIZE']),
},
secrets={
"AWS_ACCESS_KEY_ID": ecs.Secret.from_secrets_manager(secret, "AWS_ACCESS_KEY_ID"),
Expand Down
8 changes: 4 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Account: 975513124282
ProjectName: FastAPI-YOLOv5
ProjectNumber: 901103-yv5
Description: This stack deploys a Fargate managed ELB environment for creating localized detections from a YOLOv5 model using the FastAPI framework with a general underwater megadetector
DockerURI: public.ecr.aws/e5z9d1o0/mbari/pypampsd:444bb3b486087df80ff38bdf05683ec52fd180c1
ModelPath: s3://901103-models-deploy/megadetector/
ModelDescription: Megadetector
ModelInputSize: 1280
MODEL_WEIGHTS: s3://901103-models-deploy/midwatervars102/best.pt
MODEL_LABELS: s3://901103-models-deploy/midwatervars102/labels.txt
MODEL_DESCRIPTION: Megadetector
MODEL_INPUT_SIZE: 1280

0 comments on commit 8571930

Please sign in to comment.