Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of Libtorch in DeepDetect for image classification #611

Merged
merged 1 commit into from
Sep 6, 2019

Conversation

Bycob
Copy link
Collaborator

@Bycob Bycob commented Jul 22, 2019

  • Libtorch backend
  • Libtorch image input connector
  • Image classification using pytorch traced models
  • Downloading and compiling libtorch automatically during DeepDetect compilation
  • Unit tests on libtorch backend

Building instructions:

cmake .. -DUSE_TORCH=ON

src/backends/torch/torchinputconns.h Outdated Show resolved Hide resolved
src/backends/torch/torchinputconns.h Outdated Show resolved Hide resolved
src/backends/torch/torchinputconns.h Outdated Show resolved Hide resolved
src/backends/torch/torchinputconns.h Outdated Show resolved Hide resolved
src/backends/torch/torchinputconns.h Outdated Show resolved Hide resolved
src/backends/torch/torchlib.cc Show resolved Hide resolved

_device = gpu ? torch::Device("cuda") : torch::Device("cpu");

_traced = torch::jit::load(this->_mlmodel._model_file);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code only works for traced model, right ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, only traced models are currently supported.

src/backends/torch/torchlib.cc Outdated Show resolved Hide resolved
src/backends/torch/torchlib.cc Outdated Show resolved Hide resolved
src/backends/torch/torchlib.cc Outdated Show resolved Hide resolved
@beniz
Copy link
Collaborator

beniz commented Aug 22, 2019

FYI, a build on a CPU only machine with cmake -DUSE_CAFFE=OFF -DUSE_TORCH=ON .. fails with the error below:

[ 32%] Built target pytorch
[ 92%] Built target ddetect
make[2]: *** No rule to make target 'pytorch/src/pytorch/torch/lib/libc10_cuda.so', needed by 'main/dede'.  Stop.
CMakeFiles/Makefile2:177: recipe for target 'main/CMakeFiles/dede.dir/all' failed

However, the build works with cmake -DUSE_CAFFE=OFF -DUSE_TORCH=ON -DUSE_CPU_ONLY=ON ...

I believe that if there's no CUDA automatically detected, USE_CPU_ONLY should be forced ON automatically.

@beniz
Copy link
Collaborator

beniz commented Aug 22, 2019

FYI, to work on CPU at least, it is required to install libiomp5(This is a transitional package on Ubuntu 18.04). So we should add it to the README I believe. However, Pytorch appers to come with its own version of that lib, in pytorch/src/pytorch/torch/lib/libiomp5.so.

I get this error when starting dede:

./dede: error while loading shared libraries: libiomp5.so: cannot open shared object file: No such file or directory

@Bycob
Copy link
Collaborator Author

Bycob commented Aug 23, 2019

Fixed everything mentioned above, except the libiomp loading error.

@Bycob
Copy link
Collaborator Author

Bycob commented Aug 23, 2019

Script to trace resnet and other models.

Requirements:

pip3 install --user pytorch torchvision

@Bycob
Copy link
Collaborator Author

Bycob commented Aug 23, 2019

<!> A segfault may occur when running a model with CUDA

@beniz
Copy link
Collaborator

beniz commented Aug 28, 2019

@BynaryCobweb Confirmed that the final linking is fixed on CPU builds!

@Bycob
Copy link
Collaborator Author

Bycob commented Sep 5, 2019

Example: Image classification

  • Download the model
wget https://www.deepdetect.com/dd/examples/torch/resnet50_torch.tar.gz
tar xvf resnet50_torch.tar.gz
  • Run dede
  • Start the service
curl -X PUT "http://localhost:8080/services/torch_resnet" -d '{
    "description": "image classification service",
    "mllib": "torch",
    "model": {
        "repository": "./resnet50_torch/"
    },
    "parameters": {
        "input": {
            "connector": "image"
        }
    },
    "type": "supervised"
}
'
  • Run predict to guess the class of an image
curl -X POST "http://localhost:8080/predict" -d '{
    "service": "torch_resnet",
    "parameters": {
        "input": {
            "width":224,
            "height":224,
            "rgb":true,
            "std":255.0
        },
        "output": {
            "best":3
        }
    },
    "data":["./resnet50_torch/cat.jpg"]
}
'

@Bycob
Copy link
Collaborator Author

Bycob commented Sep 5, 2019

  • squashed all commits in one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants