Skip to content

Commit

Permalink
TLDR-516 GPU in docker (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
Travvy88 authored Dec 25, 2023
1 parent 98c6ec2 commit bf668d2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,25 @@ class ColumnsOrientationClassifier(object):
Class Classifier for work with Orientation Network. This class set device,
preprocessing (transform) input data, weights of model
"""

_nets = {}

def __init__(self, on_gpu: bool, checkpoint_path: Optional[str], *, config: dict) -> None:
self.logger = config.get("logger", logging.getLogger())
self._set_device(on_gpu)
self._set_transform_image()
self.checkpoint_path = path.abspath(checkpoint_path)
self.classes = [1, 2, 0, 90, 180, 270]
self._net = None

@property
def net(self) -> ClassificationModelTorch:
# lazy loading and net sharing, comrade
if self.checkpoint_path not in self._nets:
if self._net is None:
if self.checkpoint_path is not None:
net = ClassificationModelTorch(path.join(self.checkpoint_path, "scan_orientation_efficient_net_b0.pth"))
self._load_weights(net)
else:
net = ClassificationModelTorch(None)
net.to(self.device)
self._nets[self.checkpoint_path] = net
return self._nets[self.checkpoint_path]
self._net = net
self._net.to(self.device)
return self._net

@staticmethod
def my_resize(image: Image) -> Image:
Expand Down
5 changes: 0 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,3 @@ services:
PYTHONPATH: $PYTHONPATH:/dedoc_root/tests:/dedoc_root
command:
bash dedoc_root/tests/run_tests_in_docker.sh

## By default this config uses default local driver,
## For custom volumes replace with volume driver configuration.
volumes:
data1-1:
24 changes: 24 additions & 0 deletions docker_gpu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ARG REPOSITORY="docker.io"
FROM dedocproject/dedoc_p3.9_base:version_2023_08_28

ENV PYTHONPATH "${PYTHONPATH}:/dedoc_root"
ENV RESOURCES_PATH "/dedoc_root/resources"

ADD requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
RUN pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/torch_stable.html

RUN mkdir /dedoc_root
RUN mkdir /dedoc_root/dedoc
ADD dedoc/config.py /dedoc_root/dedoc/config.py
ADD dedoc/download_models.py /dedoc_root/dedoc/download_models.py
RUN python3 /dedoc_root/dedoc/download_models.py

ADD dedoc /dedoc_root/dedoc
ADD VERSION /dedoc_root
RUN echo "__version__ = \"$(cat /dedoc_root/VERSION)\"" > /dedoc_root/dedoc/version.py

ADD tests /dedoc_root/tests
ADD resources /dedoc_root/resources

CMD ["python3", "/dedoc_root/dedoc/main.py", "-c", "/dedoc_root/dedoc/config.py"]
17 changes: 17 additions & 0 deletions docker_gpu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
To run Dedoc on CUDA with Docker use files from `docker_gpu` directory
([CUDA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
should be installed on your machine):

1. Set `on_gpu` to `True` in `config.py`
2. Run application:
```shell
cd docker_gpu
docker-compose up --build
```

You can change index of CUDA device at `docker-compose.yml`:
```
NVIDIA_VISIBLE_DEVICES: 0
NVIDIA_VISIBLE_DEVICES: 0, 3
NVIDIA_VISIBLE_DEVICES: all
```
33 changes: 33 additions & 0 deletions docker_gpu/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '2.4'

services:
dedoc:
mem_limit: 16G
build:
context: ..
dockerfile: docker_gpu/Dockerfile
restart: always
tty: true
ports:
- 1231:1231
environment:
DOCREADER_PORT: 1231
NVIDIA_VISIBLE_DEVICES: 0
runtime: nvidia

test:
depends_on:
- dedoc
build:
context: ..
dockerfile: docker_gpu/Dockerfile
tty: true
environment:
DOC_READER_HOST: "dedoc"
DOCREADER_PORT: 1231
is_test: $test
PYTHONPATH: $PYTHONPATH:/dedoc_root/tests:/dedoc_root
NVIDIA_VISIBLE_DEVICES: 0
runtime: nvidia
command:
bash dedoc_root/tests/run_tests_in_docker.sh

0 comments on commit bf668d2

Please sign in to comment.