How to vision::models polymorphism?

ingbeeeded: <p>I’m using torchvision c++ API. Vision::Model namespace there a way to implement polymorphism in the upper class? I can’t even feel it when I look at the code</p>
<p>I want to point to a parent class pointer.</p>
<pre><code class="lang-cpp">vision::models::AlexNet         alexnet;
vision::models::VGG19           vgg19;
vision::models::ResNet18        resnet18;
vision::models::InceptionV3     inceptionv3; // 299
vision::models::MobileNetV2     mobilenet;
vision::models::ResNext50_32x4d resnext50;
vision::models::WideResNet50_2  wide_resnet50;
</code></pre>

tom: <p>Would the ModuleHolder mechanism from LibTorch work?</p>
<p>Best regards</p>
<p>Thomas</p>

ingbeeeded: <p>I didn’t use it <code>torch::nn::ModuleHolder</code>, but the torch::nn::ModuleHolder class have any templates (model dependency)? Could you write down the code you thought of?</p>

tom: <p>Maybe I don’t understand well enough what you want to achieve.</p>

ingbeeeded: <p>It is difficult to return the auto from the code below, but I would like to solve it with unique_pointer.</p>
<p>Or if there’s another way,</p>
<pre><code class="lang-cpp">auto create_model(const std::string model_type, int64_t num_classes)
{
	// std::unique_ptr&lt;torch::nn::Module&gt; res;
	
	vision::models::AlexNet         alexnet;
	vision::models::VGG19           vgg19;
	vision::models::ResNet18        resnet18;
	vision::models::InceptionV3     inceptionv3; // 299
	vision::models::MobileNetV2     mobilenet;
	vision::models::ResNext50_32x4d resnext50;
	vision::models::WideResNet50_2  wide_resnet50;

	if (model_type.find("alexnet") &gt;= 0) {
		alexnet = vision::models::AlexNet();

		// load imagenet weight
		torch::load(alexnet, model_type);

		// unregister "fc"
		alexnet-&gt;unregister_module("classifier");

		// https://github.com/pytorch/vision/blob/main/torchvision/csrc/models/alexnet.cpp
		alexnet-&gt;classifier = torch::nn::Sequential(
			torch::nn::Dropout(),
			torch::nn::Linear(256 * 6 * 6, 4096),
			torch::nn::Functional(torch::relu),
			torch::nn::Dropout(),
			torch::nn::Linear(4096, 4096),
			torch::nn::Functional(torch::relu),
			torch::nn::Linear(4096, num_classes)
		);

		// register "fc"
		alexnet-&gt;register_module("classifier", alexnet-&gt;classifier);

		return alexnet;
	}
	else if (model_type.find("vgg") &gt;= 0) {
		vgg19 = vision::models::VGG19();

		// load imagenet weight
		torch::load(vgg19, model_type);

		// unregister "fc"
		vgg19-&gt;unregister_module("classifier");

		// https://github.com/pytorch/vision/blob/main/torchvision/csrc/models/vgg.cpp
		vgg19-&gt;classifier = torch::nn::Sequential(
			torch::nn::Linear(512 * 7 * 7, 4096),
			torch::nn::Functional(vision::models::modelsimpl::relu_),
			torch::nn::Dropout(),
			torch::nn::Linear(4096, 4096),
			torch::nn::Functional(vision::models::modelsimpl::relu_),
			torch::nn::Dropout(),
			torch::nn::Linear(512, num_classes)
		);

		// register "fc"
		vgg19-&gt;register_module("classifier", vgg19-&gt;classifier);

		return vgg19;
	}

</code></pre>

tom: <p>Ah, sorry, <code>ModuleHolder</code> isn’t the solution, but maybe <a href="https://pytorch.org/cppdocs/api/classtorch_1_1nn_1_1_any_module.html" rel="noopener nofollow ugc">AnyModule</a> can help.</p>
<p>Best regards</p>
<p>Thomas</p>

ingbeeeded: <p>Thank you! I solve it</p>

ingbeeeded: <p>Is there a class that is supported by a similar case <code>torch:: optim</code>?</p>

tom: <p>I’m not aware of any but wouldn’t the <code>Optimizer</code> base class work there?<br>
As far as I understand, the trouble with modules comes mainly from the fact that it is not a proper subclassing because the signatures of <code>forward</code> are incompatible. I would naively expect that this should be better with optimizers.</p>
<p>Best regards</p>
<p>Thomas</p>