|
| 1 | + |
| 2 | +**Note: The purpose of the project to explore the libraries and learn how to use them. Not to build a SOTA model.** |
| 3 | + |
| 4 | +## Requirements: |
| 5 | + |
| 6 | +This project uses Python 3.8 |
| 7 | + |
| 8 | +Create a virtual env with the following command: |
| 9 | + |
| 10 | +``` |
| 11 | +conda create --name project-setup python=3.8 |
| 12 | +conda activate project-setup |
| 13 | +``` |
| 14 | + |
| 15 | +Install the requirements: |
| 16 | + |
| 17 | +``` |
| 18 | +pip install -r requirements.txt |
| 19 | +``` |
| 20 | + |
| 21 | +## Running |
| 22 | + |
| 23 | +### Training |
| 24 | + |
| 25 | +After installing the requirements, in order to train the model simply run: |
| 26 | + |
| 27 | +``` |
| 28 | +python train.py |
| 29 | +``` |
| 30 | + |
| 31 | +### Monitoring |
| 32 | + |
| 33 | +Once the training is completed in the end of the logs you will see something like: |
| 34 | + |
| 35 | +``` |
| 36 | +wandb: Synced 5 W&B file(s), 4 media file(s), 3 artifact file(s) and 0 other file(s) |
| 37 | +wandb: |
| 38 | +wandb: Synced proud-mountain-77: https://wandb.ai/raviraja/MLOps%20Basics/runs/3vp1twdc |
| 39 | +``` |
| 40 | + |
| 41 | +Follow the link to see the wandb dashboard which contains all the plots. |
| 42 | + |
| 43 | +### Versioning data |
| 44 | + |
| 45 | +Refer to the blog: [DVC Configuration](https://www.ravirajag.dev/blog/mlops-dvc) |
| 46 | + |
| 47 | +### Exporting model to ONNX |
| 48 | + |
| 49 | +Once the model is trained, convert the model using the following command: |
| 50 | + |
| 51 | +``` |
| 52 | +python convert_model_to_onnx.py |
| 53 | +``` |
| 54 | + |
| 55 | +### Inference |
| 56 | + |
| 57 | +#### Inference using standard pytorch |
| 58 | + |
| 59 | +``` |
| 60 | +python inference.py |
| 61 | +``` |
| 62 | + |
| 63 | +#### Inference using ONNX Runtime |
| 64 | + |
| 65 | +``` |
| 66 | +python inference_onnx.py |
| 67 | +``` |
| 68 | + |
| 69 | +## S3 & ECR |
| 70 | + |
| 71 | +Follow the instructions mentioned in the [blog post](https://www.ravirajag.dev/blog/mlops-container-registry) for creating S3 bucket and ECR repository. |
| 72 | + |
| 73 | +### Configuring dvc |
| 74 | + |
| 75 | +``` |
| 76 | +dvc init (this has to be done at root folder) |
| 77 | +dvc remote add -d model-store s3://models-dvc/trained_models/ |
| 78 | +``` |
| 79 | + |
| 80 | +### AWS credentials |
| 81 | + |
| 82 | +Create the credentials as mentioned in the [blog post](https://www.ravirajag.dev/blog/mlops-container-registry) |
| 83 | + |
| 84 | +**Do not share the secrets with others** |
| 85 | + |
| 86 | +Set the ACCESS key and id values in environment variables. |
| 87 | + |
| 88 | +``` |
| 89 | +export AWS_ACCESS_KEY_ID=<ACCESS KEY ID> |
| 90 | +export AWS_SECRET_ACCESS_KEY=<ACCESS SECRET> |
| 91 | +``` |
| 92 | + |
| 93 | +### Trained model in DVC |
| 94 | + |
| 95 | +Sdd the trained model(onnx) to dvc using the following command: |
| 96 | + |
| 97 | +```shell |
| 98 | +cd dvcfiles |
| 99 | +dvc add ../models/model.onnx --file trained_model.dvc |
| 100 | +``` |
| 101 | + |
| 102 | +Push the model to remote storage |
| 103 | + |
| 104 | +```shell |
| 105 | +dvc push trained_model.dvc |
| 106 | +``` |
| 107 | + |
| 108 | +### Docker |
| 109 | + |
| 110 | +Install the docker using the [instructions here](https://docs.docker.com/engine/install/) |
| 111 | + |
| 112 | +Build the image using the command |
| 113 | + |
| 114 | +```shell |
| 115 | +docker build -t mlops-basics:latest . |
| 116 | +``` |
| 117 | + |
| 118 | +Then run the container using the command |
| 119 | + |
| 120 | +```shell |
| 121 | +docker run -p 8000:8000 --name inference_container mlops-basics:latest |
| 122 | +``` |
| 123 | + |
| 124 | +(or) |
| 125 | + |
| 126 | +Build and run the container using the command |
| 127 | + |
| 128 | +```shell |
| 129 | +docker-compose up |
| 130 | +``` |
| 131 | + |
| 132 | +### Pushing the image to ECR |
| 133 | + |
| 134 | +Follow the instructions mentioned in [blog post](https://www.ravirajag.dev/blog/mlops-container-registry) for creating ECR repository. |
| 135 | + |
| 136 | +- Authenticating docker client to ECR |
| 137 | + |
| 138 | +``` |
| 139 | +aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 246113150184.dkr.ecr.us-west-2.amazonaws.com |
| 140 | +``` |
| 141 | + |
| 142 | +- Tagging the image |
| 143 | + |
| 144 | +``` |
| 145 | +docker tag mlops-basics:latest 246113150184.dkr.ecr.us-west-2.amazonaws.com/mlops-basics:latest |
| 146 | +``` |
| 147 | + |
| 148 | +- Pushing the image |
| 149 | + |
| 150 | +``` |
| 151 | +docker push 246113150184.dkr.ecr.us-west-2.amazonaws.com/mlops-basics:latest |
| 152 | +``` |
| 153 | + |
| 154 | +Refer to `.github/workflows/build_docker_image.yaml` file for automatically creating the docker image with trained model and pushing it to ECR. |
| 155 | + |
| 156 | + |
| 157 | +### Running notebooks |
| 158 | + |
| 159 | +I am using [Jupyter lab](https://jupyter.org/install) to run the notebooks. |
| 160 | + |
| 161 | +Since I am using a virtualenv, when I run the command `jupyter lab` it might or might not use the virtualenv. |
| 162 | + |
| 163 | +To make sure to use the virutalenv, run the following commands before running `jupyter lab` |
| 164 | + |
| 165 | +``` |
| 166 | +conda install ipykernel |
| 167 | +python -m ipykernel install --user --name project-setup |
| 168 | +pip install ipywidgets |
| 169 | +``` |
0 commit comments