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

[DO_NOT_MERGE] Remove the C++ vision::models namespace #4374

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 0 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,6 @@ def get_extensions():
sources = main_file + source_cpu
extension = CppExtension

compile_cpp_tests = os.getenv('WITH_CPP_MODELS_TEST', '0') == '1'
if compile_cpp_tests:
test_dir = os.path.join(this_dir, 'test')
models_dir = os.path.join(this_dir, 'torchvision', 'csrc', 'models')
test_file = glob.glob(os.path.join(test_dir, '*.cpp'))
source_models = glob.glob(os.path.join(models_dir, '*.cpp'))

test_file = [os.path.join(test_dir, s) for s in test_file]
source_models = [os.path.join(models_dir, s) for s in source_models]
tests = test_file + source_models
tests_include_dirs = [test_dir, models_dir]

define_macros = []

extra_compile_args = {'cxx': []}
Expand Down Expand Up @@ -238,16 +226,6 @@ def get_extensions():
extra_compile_args=extra_compile_args,
)
]
if compile_cpp_tests:
ext_modules.append(
extension(
'torchvision._C_tests',
tests,
include_dirs=tests_include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
)

# ------------------- Torchvision extra extensions ------------------------
vision_include = os.environ.get('TORCHVISION_INCLUDE', None)
Expand Down
152 changes: 0 additions & 152 deletions test/test_cpp_models.py

This file was deleted.

Loading