Yolov7 docker image size exceeding 6GB /issue

37 Views Asked by At

I am building a docker image which I want to store on ECR(AWS) . My expections with the yolomodel is to just take an image as an input and get predicted set of pose-keypoints as output using YOLOv7-pose.

Following are in requirements.txt in docker :

boto3==1.20.23
boto3-stubs == 1.21.0
aws_lambda_typing==2.9.2
requests==2.25.1
protobuf==4.25.3
pytz == 2023.3
torch == 2.0.1
opencv-python == 4.9.0.80
dateTime == 5.4
pybase64 ==1.3.2
ultralytics ==8.1.0
typing-extensions == 3.7.4.3

and this is how I am importing it in application.py:

import cv2
import numpy as np
import boto3
import base64
import json
import torch
import math 
import pytz
from ultralytics import YOLO
from datetime import datetime

Earlier I used mediapipe to do same which led me to docker size of only 500MB.

1

There are 1 best solutions below

2
datawookie On

The image is being inflated by the Python packages:

234076  ./nvidia/cusparse/lib
234416  ./nvidia/cusparse
235296  ./nvidia/nccl/lib
235344  ./nvidia/nccl
274220  ./nvidia/cufft/lib
274300  ./nvidia/cufft
368104  ./nvidia/cusolver/lib
368384  ./nvidia/cusolver
473492  ./nvidia/cublas/lib
473908  ./nvidia/cublas
880240  ./nvidia/cudnn/lib
880696  ./nvidia/cudnn
1252564 ./torch/lib
1359700 ./torch
2647496 ./nvidia

Dockerfile

FROM python:3.8.0-slim

COPY requirements.txt .

RUN pip3 install -r requirements.txt

The size of the image is 7.7 GB and the /usr/local/lib/python3.8/site-packages directory alone is 4.8 GB.

I'm checking the image size with:

docker inspect  -f "{{ .Size }}" <image-name> | numfmt --to=si

Using --no-cache-dir will reduce the size of the image.

Dockerfile

FROM python:3.8.0-slim

COPY requirements.txt .

RUN pip3 install --no-cache-dir -r requirements.txt

The size of the image is now 5.3 GB.

However, when I tried to load the packages using the code from the original question I had a problem with a missing dependency.

So I went back to first principles and simplified requirements.txt:

boto3==1.34.55
ultralytics==8.1.0

With those dependencies the image builds and packages load.

enter image description here

Since we are not specifying particular versions for all of the other packages, they are simply selected to be compatible with boto3 and ultralytics, which seems to result in consistently larger footprints. For example, we get version 2.2.1 of torch.

94432   ./nvidia/curand/lib
96536   ./nvidia/curand
189712  ./nvidia/cusolver/lib
189796  ./nvidia/cufft/lib
189876  ./nvidia/cufft
189992  ./nvidia/cusolver
214328  ./nvidia/nccl/lib
214392  ./nvidia/nccl
258688  ./nvidia/cusparse/lib
259000  ./nvidia/cusparse
347436  ./triton/_C
428852  ./triton
608716  ./nvidia/cublas/lib
609300  ./nvidia/cublas
1155416 ./nvidia/cudnn/lib
1155880 ./nvidia/cudnn
1442728 ./torch/lib
1556592 ./torch
2877384 ./nvidia

You can probably fiddle around with specific versions of those packages but it seems unlikely that you will get the image down to 500 MB.

Sorry, I know that this is not really an "answer". But maybe there is something useful.