From e839a36503ee49ff6bf67b565df7cf92647428e1 Mon Sep 17 00:00:00 2001 From: Loreto Parisi Date: Mon, 15 Nov 2021 20:11:46 +0100 Subject: [PATCH 1/6] Added Dockerfile --- Dockerfile | 36 ++++++++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ lama_cleaner/helper.py | 3 ++- main.py | 6 +++++- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6630fe4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# +# Lama Cleaner Dockerfile +# @author Loreto Parisi (loretoparisi at gmail dot com) +# + +FROM python:3.7.4-slim-buster + +LABEL maintainer Loreto Parisi loretoparisi@gmail.com + +WORKDIR app + +RUN apt-get update && apt-get install -y --no-install-recommends \ + software-properties-common \ + libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx \ + curl \ + npm + +# python requirements +COPY . . +COPY requirements.txt /etc/tmp/requirements.txt +RUN pip install -r /etc/tmp/requirements.txt + +# nodejs +RUN npm install n -g && \ + n lts +# yarn +RUN npm install -g yarn + +# webapp +RUN cd lama_cleaner/app/ && \ + yarn && \ + yarn build + +EXPOSE 8080 + +CMD ["bash"] \ No newline at end of file diff --git a/README.md b/README.md index c5a7f0b..330d747 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,17 @@ You can experience their great online services [here](https://cleanup.pictures/) - Install dependencies:`cd lama_cleaner/app/ && yarn` - Start development server: `yarn dev` - Build: `yarn build` + +## Docker (cpu) +``` +docker build -f Dockerfile -t lamacleaner . +docker run -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cpu --port=8080 +``` + +## Docker (gpu) +``` +docker build -f Dockerfile -t lamacleaner . +docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=gpu --port=8080 +``` + +Then open [http://localhost:8080](http://localhost:8080) \ No newline at end of file diff --git a/lama_cleaner/helper.py b/lama_cleaner/helper.py index 9f54599..75fd85a 100644 --- a/lama_cleaner/helper.py +++ b/lama_cleaner/helper.py @@ -17,8 +17,9 @@ def download_model(url=LAMA_MODEL_URL): parts = urlparse(url) hub_dir = get_dir() model_dir = os.path.join(hub_dir, "checkpoints") + if not os.path.isdir(model_dir): + os.makedirs(os.path.join(model_dir, "hub", "checkpoints")) filename = os.path.basename(parts.path) - cached_file = os.path.join(model_dir, filename) if not os.path.exists(cached_file): sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file)) diff --git a/main.py b/main.py index 5dc995e..b52ac21 100644 --- a/main.py +++ b/main.py @@ -17,12 +17,16 @@ from lama_cleaner.helper import ( pad_img_to_modulo, ) -NUM_THREADS = "4" +import multiprocessing + +NUM_THREADS = str(multiprocessing.cpu_count()) + os.environ["OMP_NUM_THREADS"] = NUM_THREADS os.environ["OPENBLAS_NUM_THREADS"] = NUM_THREADS os.environ["MKL_NUM_THREADS"] = NUM_THREADS os.environ["VECLIB_MAXIMUM_THREADS"] = NUM_THREADS os.environ["NUMEXPR_NUM_THREADS"] = NUM_THREADS +os.environ["TORCH_HOME"] = os.environ["cache_dir"] BUILD_DIR = os.environ.get("LAMA_CLEANER_BUILD_DIR", "./lama_cleaner/app/build") From c8e3604f7104484b706e34602e8bda6635e93827 Mon Sep 17 00:00:00 2001 From: loretoparisi Date: Mon, 15 Nov 2021 23:24:07 +0100 Subject: [PATCH 2/6] Updated docker run gpu --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 330d747..55b0ab4 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ docker run -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd) ## Docker (gpu) ``` docker build -f Dockerfile -t lamacleaner . -docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=gpu --port=8080 +docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cuda --port=8080 ``` Then open [http://localhost:8080](http://localhost:8080) \ No newline at end of file From 7114f988b4cefaa66f55dbaf12e7e700d32954d1 Mon Sep 17 00:00:00 2001 From: loretoparisi Date: Mon, 15 Nov 2021 23:37:26 +0100 Subject: [PATCH 3/6] Set TORCH_HOME --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index b52ac21..780e999 100644 --- a/main.py +++ b/main.py @@ -26,7 +26,8 @@ os.environ["OPENBLAS_NUM_THREADS"] = NUM_THREADS os.environ["MKL_NUM_THREADS"] = NUM_THREADS os.environ["VECLIB_MAXIMUM_THREADS"] = NUM_THREADS os.environ["NUMEXPR_NUM_THREADS"] = NUM_THREADS -os.environ["TORCH_HOME"] = os.environ["cache_dir"] +if os.environ["cache_dir"]: + os.environ["TORCH_HOME"] = os.environ["cache_dir"] BUILD_DIR = os.environ.get("LAMA_CLEANER_BUILD_DIR", "./lama_cleaner/app/build") From 7c877af84653fd5f0735174c6a2eaf8a9d6f63bc Mon Sep 17 00:00:00 2001 From: loretoparisi Date: Mon, 15 Nov 2021 23:41:59 +0100 Subject: [PATCH 4/6] Docker run options --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 55b0ab4..d3f6e66 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,17 @@ You can experience their great online services [here](https://cleanup.pictures/) - Start development server: `yarn dev` - Build: `yarn build` -## Docker (cpu) +## Docker +Run within a Docker container. Set the `cache_dir` to models location path. +Optionally add a `-d` option to the `docker run` command below to run as a daemon. + +### Docker (cpu) ``` docker build -f Dockerfile -t lamacleaner . docker run -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cpu --port=8080 ``` -## Docker (gpu) +### Docker (gpu) ``` docker build -f Dockerfile -t lamacleaner . docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cuda --port=8080 From 9c9f67882c0aece228f0c172ae6ec1b7b4ef02b6 Mon Sep 17 00:00:00 2001 From: loretoparisi Date: Mon, 15 Nov 2021 23:51:27 +0100 Subject: [PATCH 5/6] Docker commands --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d3f6e66..a659ed8 100644 --- a/README.md +++ b/README.md @@ -22,15 +22,18 @@ You can experience their great online services [here](https://cleanup.pictures/) Run within a Docker container. Set the `cache_dir` to models location path. Optionally add a `-d` option to the `docker run` command below to run as a daemon. -### Docker (cpu) +### Build Docker image ``` docker build -f Dockerfile -t lamacleaner . +``` + +### Run Docker (cpu) +``` docker run -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cpu --port=8080 ``` -### Docker (gpu) +### Run Docker (gpu) ``` -docker build -f Dockerfile -t lamacleaner . docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cuda --port=8080 ``` From 796eff96c8fff646afe7b4e10bab3d125a53ebc1 Mon Sep 17 00:00:00 2001 From: loretoparisi Date: Mon, 15 Nov 2021 23:54:55 +0100 Subject: [PATCH 6/6] Docker commands --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a659ed8..51ca646 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ docker build -f Dockerfile -t lamacleaner . ### Run Docker (cpu) ``` -docker run -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cpu --port=8080 +docker run -p 8080:8080 -e cache_dir=/app/models -v $(pwd)/models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cpu --port=8080 ``` ### Run Docker (gpu) ``` -docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cuda --port=8080 +docker run --gpus all -p 8080:8080 -e cache_dir=/app/models -v $(pwd)/models:/app/models -v $(pwd):/app --rm lamacleaner python3 main.py --device=cuda --port=8080 ``` Then open [http://localhost:8080](http://localhost:8080) \ No newline at end of file