|
| 1 | +#include <torch/script.h> |
| 2 | +#include <torch/torch.h> |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#include "../torchvision/csrc/models/models.h" |
| 6 | + |
| 7 | +using namespace vision::models; |
| 8 | + |
| 9 | +template <typename Model> |
| 10 | +torch::Tensor forward_model(const std::string& input_path, torch::Tensor x) { |
| 11 | + Model network; |
| 12 | + torch::load(network, input_path); |
| 13 | + network->eval(); |
| 14 | + return network->forward(x); |
| 15 | +} |
| 16 | + |
| 17 | +torch::Tensor forward_alexnet(const std::string& input_path, torch::Tensor x) { |
| 18 | + return forward_model<AlexNet>(input_path, x); |
| 19 | +} |
| 20 | + |
| 21 | +torch::Tensor forward_vgg11(const std::string& input_path, torch::Tensor x) { |
| 22 | + return forward_model<VGG11>(input_path, x); |
| 23 | +} |
| 24 | +torch::Tensor forward_vgg13(const std::string& input_path, torch::Tensor x) { |
| 25 | + return forward_model<VGG13>(input_path, x); |
| 26 | +} |
| 27 | +torch::Tensor forward_vgg16(const std::string& input_path, torch::Tensor x) { |
| 28 | + return forward_model<VGG16>(input_path, x); |
| 29 | +} |
| 30 | +torch::Tensor forward_vgg19(const std::string& input_path, torch::Tensor x) { |
| 31 | + return forward_model<VGG19>(input_path, x); |
| 32 | +} |
| 33 | + |
| 34 | +torch::Tensor forward_vgg11bn(const std::string& input_path, torch::Tensor x) { |
| 35 | + return forward_model<VGG11BN>(input_path, x); |
| 36 | +} |
| 37 | +torch::Tensor forward_vgg13bn(const std::string& input_path, torch::Tensor x) { |
| 38 | + return forward_model<VGG13BN>(input_path, x); |
| 39 | +} |
| 40 | +torch::Tensor forward_vgg16bn(const std::string& input_path, torch::Tensor x) { |
| 41 | + return forward_model<VGG16BN>(input_path, x); |
| 42 | +} |
| 43 | +torch::Tensor forward_vgg19bn(const std::string& input_path, torch::Tensor x) { |
| 44 | + return forward_model<VGG19BN>(input_path, x); |
| 45 | +} |
| 46 | + |
| 47 | +torch::Tensor forward_resnet18(const std::string& input_path, torch::Tensor x) { |
| 48 | + return forward_model<ResNet18>(input_path, x); |
| 49 | +} |
| 50 | +torch::Tensor forward_resnet34(const std::string& input_path, torch::Tensor x) { |
| 51 | + return forward_model<ResNet34>(input_path, x); |
| 52 | +} |
| 53 | +torch::Tensor forward_resnet50(const std::string& input_path, torch::Tensor x) { |
| 54 | + return forward_model<ResNet50>(input_path, x); |
| 55 | +} |
| 56 | +torch::Tensor forward_resnet101( |
| 57 | + const std::string& input_path, |
| 58 | + torch::Tensor x) { |
| 59 | + return forward_model<ResNet101>(input_path, x); |
| 60 | +} |
| 61 | +torch::Tensor forward_resnet152( |
| 62 | + const std::string& input_path, |
| 63 | + torch::Tensor x) { |
| 64 | + return forward_model<ResNet152>(input_path, x); |
| 65 | +} |
| 66 | +torch::Tensor forward_resnext50_32x4d( |
| 67 | + const std::string& input_path, |
| 68 | + torch::Tensor x) { |
| 69 | + return forward_model<ResNext50_32x4d>(input_path, x); |
| 70 | +} |
| 71 | +torch::Tensor forward_resnext101_32x8d( |
| 72 | + const std::string& input_path, |
| 73 | + torch::Tensor x) { |
| 74 | + return forward_model<ResNext101_32x8d>(input_path, x); |
| 75 | +} |
| 76 | + |
| 77 | +torch::Tensor forward_squeezenet1_0( |
| 78 | + const std::string& input_path, |
| 79 | + torch::Tensor x) { |
| 80 | + return forward_model<SqueezeNet1_0>(input_path, x); |
| 81 | +} |
| 82 | +torch::Tensor forward_squeezenet1_1( |
| 83 | + const std::string& input_path, |
| 84 | + torch::Tensor x) { |
| 85 | + return forward_model<SqueezeNet1_1>(input_path, x); |
| 86 | +} |
| 87 | + |
| 88 | +torch::Tensor forward_densenet121( |
| 89 | + const std::string& input_path, |
| 90 | + torch::Tensor x) { |
| 91 | + return forward_model<DenseNet121>(input_path, x); |
| 92 | +} |
| 93 | +torch::Tensor forward_densenet169( |
| 94 | + const std::string& input_path, |
| 95 | + torch::Tensor x) { |
| 96 | + return forward_model<DenseNet169>(input_path, x); |
| 97 | +} |
| 98 | +torch::Tensor forward_densenet201( |
| 99 | + const std::string& input_path, |
| 100 | + torch::Tensor x) { |
| 101 | + return forward_model<DenseNet201>(input_path, x); |
| 102 | +} |
| 103 | +torch::Tensor forward_densenet161( |
| 104 | + const std::string& input_path, |
| 105 | + torch::Tensor x) { |
| 106 | + return forward_model<DenseNet161>(input_path, x); |
| 107 | +} |
| 108 | + |
| 109 | +torch::Tensor forward_mobilenetv2( |
| 110 | + const std::string& input_path, |
| 111 | + torch::Tensor x) { |
| 112 | + return forward_model<MobileNetV2>(input_path, x); |
| 113 | +} |
| 114 | + |
| 115 | +torch::Tensor forward_googlenet( |
| 116 | + const std::string& input_path, |
| 117 | + torch::Tensor x) { |
| 118 | + GoogLeNet network; |
| 119 | + torch::load(network, input_path); |
| 120 | + network->eval(); |
| 121 | + return network->forward(x).output; |
| 122 | +} |
| 123 | +torch::Tensor forward_inceptionv3( |
| 124 | + const std::string& input_path, |
| 125 | + torch::Tensor x) { |
| 126 | + InceptionV3 network; |
| 127 | + torch::load(network, input_path); |
| 128 | + network->eval(); |
| 129 | + return network->forward(x).output; |
| 130 | +} |
| 131 | + |
| 132 | +PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { |
| 133 | + m.def("forward_alexnet", &forward_alexnet, "forward_alexnet"); |
| 134 | + |
| 135 | + m.def("forward_vgg11", &forward_vgg11, "forward_vgg11"); |
| 136 | + m.def("forward_vgg13", &forward_vgg13, "forward_vgg13"); |
| 137 | + m.def("forward_vgg16", &forward_vgg16, "forward_vgg16"); |
| 138 | + m.def("forward_vgg19", &forward_vgg19, "forward_vgg19"); |
| 139 | + |
| 140 | + m.def("forward_vgg11bn", &forward_vgg11bn, "forward_vgg11bn"); |
| 141 | + m.def("forward_vgg13bn", &forward_vgg13bn, "forward_vgg13bn"); |
| 142 | + m.def("forward_vgg16bn", &forward_vgg16bn, "forward_vgg16bn"); |
| 143 | + m.def("forward_vgg19bn", &forward_vgg19bn, "forward_vgg19bn"); |
| 144 | + |
| 145 | + m.def("forward_resnet18", &forward_resnet18, "forward_resnet18"); |
| 146 | + m.def("forward_resnet34", &forward_resnet34, "forward_resnet34"); |
| 147 | + m.def("forward_resnet50", &forward_resnet50, "forward_resnet50"); |
| 148 | + m.def("forward_resnet101", &forward_resnet101, "forward_resnet101"); |
| 149 | + m.def("forward_resnet152", &forward_resnet152, "forward_resnet152"); |
| 150 | + m.def( |
| 151 | + "forward_resnext50_32x4d", |
| 152 | + &forward_resnext50_32x4d, |
| 153 | + "forward_resnext50_32x4d"); |
| 154 | + m.def( |
| 155 | + "forward_resnext101_32x8d", |
| 156 | + &forward_resnext101_32x8d, |
| 157 | + "forward_resnext101_32x8d"); |
| 158 | + |
| 159 | + m.def( |
| 160 | + "forward_squeezenet1_0", &forward_squeezenet1_0, "forward_squeezenet1_0"); |
| 161 | + m.def( |
| 162 | + "forward_squeezenet1_1", &forward_squeezenet1_1, "forward_squeezenet1_1"); |
| 163 | + |
| 164 | + m.def("forward_densenet121", &forward_densenet121, "forward_densenet121"); |
| 165 | + m.def("forward_densenet169", &forward_densenet169, "forward_densenet169"); |
| 166 | + m.def("forward_densenet201", &forward_densenet201, "forward_densenet201"); |
| 167 | + m.def("forward_densenet161", &forward_densenet161, "forward_densenet161"); |
| 168 | + |
| 169 | + m.def("forward_mobilenetv2", &forward_mobilenetv2, "forward_mobilenetv2"); |
| 170 | + |
| 171 | + m.def("forward_googlenet", &forward_googlenet, "forward_googlenet"); |
| 172 | + m.def("forward_inceptionv3", &forward_inceptionv3, "forward_inceptionv3"); |
| 173 | +} |
0 commit comments