Skip to content

Commit

Permalink
[fbsync] Deprecate the C++ vision::models namespace (#4375)
Browse files Browse the repository at this point in the history
Summary:
* Add deprecation warnings on vision::models

* Change the C++ example.

* Chage readme.

* Update deprecation warning.

Reviewed By: kazhang

Differential Revision: D30898331

fbshipit-source-id: 64edd30d726469111dd33821b4d0befc25c9b4dd
  • Loading branch information
NicolasHug authored and facebook-github-bot committed Sep 13, 2021
1 parent ff5c5b3 commit c7cd85d
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ otherwise, add the include and library paths in the environment variables ``TORC
.. _libjpeg: http://ijg.org/
.. _libjpeg-turbo: https://libjpeg-turbo.org/

C++ API
=======
TorchVision also offers a C++ API that contains C++ equivalent of python models.
Using the models on C++
=======================
TorchVision provides an example project for how to use the models on C++ using JIT Script.

Installation From source:

Expand Down
45 changes: 32 additions & 13 deletions examples/cpp/hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>
#include <torchvision/vision.h>
#include <torchvision/models/resnet.h>

int main()
{
auto model = vision::models::ResNet18();
model->eval();
int main() {
torch::DeviceType device_type;
device_type = torch::kCPU;

// Create a random input tensor and run it through the model.
auto in = torch::rand({1, 3, 10, 10});
auto out = model->forward(in);
torch::jit::script::Module model;
try {
std::cout << "Loading model\n";
// Deserialize the ScriptModule from a file using torch::jit::load().
model = torch::jit::load("resnet18.pt");
std::cout << "Model loaded\n";
} catch (const torch::Error& e) {
std::cout << "error loading the model\n";
return -1;
} catch (const std::exception& e) {
std::cout << "Other error: " << e.what() << "\n";
return -1;
}

std::cout << out.sizes();
// TorchScript models require a List[IValue] as input
std::vector<torch::jit::IValue> inputs;

// Create a random input tensor and run it through the model.
inputs.push_back(torch::rand({1, 3, 10, 10}));
auto out = model.forward(inputs);
std::cout << out << "\n";

if (torch::cuda::is_available()) {
// Move model and inputs to GPU
model->to(torch::kCUDA);
auto gpu_in = in.to(torch::kCUDA);
auto gpu_out = model->forward(gpu_in);
model.to(torch::kCUDA);

// Add GPU inputs
inputs.clear();
torch::TensorOptions options = torch::TensorOptions{torch::kCUDA};
inputs.push_back(torch::rand({1, 3, 10, 10}, options));

std::cout << gpu_out.sizes();
auto gpu_out = model.forward(inputs);
std::cout << gpu_out << "\n";
}
}
13 changes: 13 additions & 0 deletions examples/cpp/hello_world/trace_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os.path as osp

import torch
import torchvision

HERE = osp.dirname(osp.abspath(__file__))
ASSETS = osp.dirname(osp.dirname(HERE))

model = torchvision.models.resnet18(pretrained=False)
model.eval()

traced_model = torch.jit.script(model)
traced_model.save("resnet18.pt")
7 changes: 6 additions & 1 deletion packaging/build_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,18 @@ fi
# Compile and run the CPP example
popd
cd examples/cpp/hello_world

mkdir build

# Trace model
python trace_model.py
cp resnet18.pt build

cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch

if [[ "$OSTYPE" == "msys" ]]; then
"$script_dir/windows/internal/vc_env_helper.bat" "$script_dir/windows/internal/build_cpp_example.bat" $PARALLELISM
mv resnet18.pt Release
cd Release
else
make -j$PARALLELISM
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/alexnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ AlexNetImpl::AlexNetImpl(int64_t num_classes) {

register_module("features", features);
register_module("classifier", classifier);

modelsimpl::deprecation_warning();
}

torch::Tensor AlexNetImpl::forward(torch::Tensor x) {
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/densenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ DenseNetImpl::DenseNetImpl(
} else if (auto M = dynamic_cast<torch::nn::LinearImpl*>(module.get()))
torch::nn::init::constant_(M->bias, 0);
}

modelsimpl::deprecation_warning();
}

torch::Tensor DenseNetImpl::forward(torch::Tensor x) {
Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/models/googlenet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "googlenet.h"

#include "modelsimpl.h"

namespace vision {
namespace models {

Expand Down Expand Up @@ -143,6 +145,8 @@ GoogLeNetImpl::GoogLeNetImpl(

if (init_weights)
_initialize_weights();

modelsimpl::deprecation_warning();
}

void GoogLeNetImpl::_initialize_weights() {
Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/models/inception.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "inception.h"

#include "modelsimpl.h"

namespace vision {
namespace models {

Expand Down Expand Up @@ -297,6 +299,8 @@ InceptionV3Impl::InceptionV3Impl(
register_module("Mixed_7b", Mixed_7b);
register_module("Mixed_7c", Mixed_7c);
register_module("fc", fc);

modelsimpl::deprecation_warning();
}

InceptionV3Output InceptionV3Impl::forward(torch::Tensor x) {
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/mnasnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ MNASNetImpl::MNASNetImpl(double alpha, int64_t num_classes, double dropout) {
register_module("classifier", classifier);

_initialize_weights();

modelsimpl::deprecation_warning();
}

torch::Tensor MNASNetImpl::forward(torch::Tensor x) {
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/mobilenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ MobileNetV2Impl::MobileNetV2Impl(
torch::nn::init::zeros_(M->bias);
}
}

modelsimpl::deprecation_warning();
}

torch::Tensor MobileNetV2Impl::forward(at::Tensor x) {
Expand Down
7 changes: 7 additions & 0 deletions torchvision/csrc/models/modelsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ inline bool double_compare(double a, double b) {
return double(std::abs(a - b)) < std::numeric_limits<double>::epsilon();
};

inline void deprecation_warning() {
TORCH_WARN_ONCE(
"The vision::models namespace is not actively maintained, use at "
"your own discretion. We recommend using Torch Script instead: "
"https://pytorch.org/tutorials/advanced/cpp_export.html");
}

} // namespace modelsimpl
} // namespace models
} // namespace vision
3 changes: 3 additions & 0 deletions torchvision/csrc/models/resnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <torch/nn.h>
#include "../macros.h"
#include "modelsimpl.h"

namespace vision {
namespace models {
Expand Down Expand Up @@ -164,6 +165,8 @@ ResNetImpl<Block>::ResNetImpl(
else if (auto* M = dynamic_cast<_resnetimpl::BasicBlock*>(module.get()))
torch::nn::init::constant_(M->bn2->weight, 0);
}

modelsimpl::deprecation_warning();
}

template <typename Block>
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/shufflenetv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ ShuffleNetV2Impl::ShuffleNetV2Impl(
register_module("stage4", stage4);
register_module("conv2", conv5);
register_module("fc", fc);

modelsimpl::deprecation_warning();
}

torch::Tensor ShuffleNetV2Impl::forward(torch::Tensor x) {
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/squeezenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ SqueezeNetImpl::SqueezeNetImpl(double version, int64_t num_classes)
if (M->options.bias())
torch::nn::init::constant_(M->bias, 0);
}

modelsimpl::deprecation_warning();
}

torch::Tensor SqueezeNetImpl::forward(torch::Tensor x) {
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/models/vgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ VGGImpl::VGGImpl(

if (initialize_weights)
_initialize_weights();

modelsimpl::deprecation_warning();
}

torch::Tensor VGGImpl::forward(torch::Tensor x) {
Expand Down

0 comments on commit c7cd85d

Please sign in to comment.