vision: [C++ Frontend] Simple example with VGG gives memory error

🐛 Bug

Hello! I have compiled the master branch of torchvision and used the pre-built libtorch lib. I manage to run the simple HelloWorld example using the ResNet18 but I get “Unhandled exception at 0x00007FFDB7D2A799” error when using the VGG16 network. It fails both for Release and Debug configurations (while ResNet works for both of them). Any ideas? Thanks.

To Reproduce

#include <iostream>
#include <torchvision/models/vgg.h>
//#include <torchvision/models/resnet.h>

int main()
{

	auto model = vision::models::VGG16();
	//auto model = vision::models::ResNet18();

	model->eval();

	// Create a random input tensor and run it through the model.
	//auto in = torch::rand({ 1, 3, 10, 10 });
	auto in = torch::rand({ 10, 3, 224, 224 });
	auto out = model->forward(in);

	std::cout << out;

	system("pause");
}

By the way as a sanity check, the equivalent python code works:

import torch
import torchvision.models as models

vgg16 = models.vgg16(pretrained=False)

vgg16.eval()

in_tensor = torch.rand(size=(10, 3, 224, 224))
out_tensor = vgg16.forward(in_tensor)

print(out_tensor)

Environment

Windows 10 Visual Studio 2017 pre-build libtorch Torchvision build from master repo CMake 3.16

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I have implemented VGG19 model from torchvision VGG code to try to find the error by debugging.

#ifndef VGGCUSTOM_H
#define VGGCUSTOM_H

#include <torch/torch.h>

		
struct VGG19CustomImpl: torch::nn::Module {
	
	// Neural network model consisting of layers proposed by VGG19 model.

	// N(int) : number of classes to predict with this model
	// input size should be : (b x 3 x 224 x 224)

	VGG19CustomImpl(int64_t N=1000)
		: conv1(register_module("conv1", torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 64, 3).padding(1).stride(1)))),
		conv2(register_module("conv2", torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 64, 3).padding(1).stride(1)))),
		conv3(register_module("conv3", torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 128, 3).padding(1)))),
		conv4(register_module("conv4", torch::nn::Conv2d(torch::nn::Conv2dOptions(128, 128, 3).padding(1)))),
		conv5(register_module("conv5", torch::nn::Conv2d(torch::nn::Conv2dOptions(128, 256, 3).padding(1)))),
		conv6(register_module("conv6", torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, 3).padding(1)))),
		conv7(register_module("conv7", torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, 3).padding(1)))),
		conv8(register_module("conv8", torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, 3).padding(1)))),
		conv9(register_module("conv9", torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 512, 3).padding(1)))),
		conv10(register_module("conv10", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv11(register_module("conv11", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv12(register_module("conv12", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv13(register_module("conv13", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv14(register_module("conv14", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv15(register_module("conv15", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		conv16(register_module("conv16", torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, 3).padding(1)))),
		linear1(register_module("linear1", torch::nn::Linear(512 * 7  * 7, 4096))),
		linear2(register_module("linear2", torch::nn::Linear(4096, 4096))),
		linear3(register_module("linear3", torch::nn::Linear(4096, N))),
		dropout1(register_module("dropout1", torch::nn::Dropout(torch::nn::DropoutOptions(0.5)))),
		dropout2(register_module("dropout2", torch::nn::Dropout(torch::nn::DropoutOptions(0.5)))) 
               {
		      _initialize_weights();
	       }

	torch::Tensor forward(const torch::Tensor& input) {
		
		auto x = torch::relu(conv1(input));
		x = torch::relu(conv2(x));
		x = torch::max_pool2d(x, 2, 2);
		
		x = torch::relu(conv3(x));
		x = torch::relu(conv4(x));
		x = torch::max_pool2d(x, 2, 2);
		
		x = torch::relu(conv5(x));
		x = torch::relu(conv6(x));
		x = torch::relu(conv7(x));
		x = torch::relu(conv8(x));
		x = torch::max_pool2d(x, 2, 2);
		
		x = torch::relu(conv9(x));
		x = torch::relu(conv10(x));
		x = torch::relu(conv11(x));
		x = torch::relu(conv12(x));
		x = torch::max_pool2d(x, 2, 2);


		x = torch::relu(conv13(x));
		x = torch::relu(conv14(x));
		x = torch::relu(conv15(x));
		x = torch::relu(conv16(x));
		x = torch::max_pool2d(x, 2, 2);


		// Classifier, 512 * 7 * 7 = 25088
		x = x.view({ x.size(0), 25088 });
		x = relu(linear1(x));
		x = dropout1(x);
	
		x = relu(linear2(x));
		x = dropout2(x);
		
		x = linear3(x);
		return x;
	}
	torch::nn::Linear linear1, linear2, linear3;
	torch::nn::Dropout dropout1,dropout2;
	torch::nn::Conv2d conv1, conv2, conv3, conv4, conv5,conv6, conv7, conv8, conv9, conv10,conv11,conv12,conv13,conv14,conv15,conv16;
	
	void _initialize_weights() {
			for (auto& module : modules(/*include_self=*/false)) {
				if (auto M = dynamic_cast<torch::nn::Conv2dImpl*>(module.get())) {
					torch::nn::init::kaiming_normal_(
						M->weight,
						/*a=*/0,
						torch::kFanOut,
						torch::kReLU);
					torch::nn::init::constant_(M->bias, 0);
				}
				else if (
					auto M = dynamic_cast<torch::nn::BatchNorm2dImpl*>(module.get())) {
					torch::nn::init::constant_(M->weight, 1);
					torch::nn::init::constant_(M->bias, 0);
				}
				else if (auto M = dynamic_cast<torch::nn::LinearImpl*>(module.get())) {
					torch::nn::init::normal_(M->weight, 0, 0.01);
					torch::nn::init::constant_(M->bias, 0);
				}
			}
		}

	
};

TORCH_MODULE_IMPL(VGG19Custom, VGG19CustomImpl);

#endif // VGG_H

But there is no problem with this model when executing this code

VGG19Custom modelvggcustom;
auto in = torch::rand({ 10, 3, 224, 224 });
auto out = modelvggcustom->forward(in);

So I still don’t know why using torchvision model fails. I would say that the model is the same isn’t it?