-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fbsync] Deprecate the C++ vision::models namespace (#4375)
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
1 parent
ff5c5b3
commit c7cd85d
Showing
15 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters