How do I use it, torch::nn::module forward

21fa417e3fb06e56040c: <p>Hi.</p>
<p>I’m making IntermediateLayerGetter,<br>
This is torchvision Similar to the IntermediateLayerGetter<br>
cpp version.</p>
<p>Can’t use forward code in other ways?</p>
<p>The code is below.</p>
<pre><code class="lang-auto">#pragma once

#include &lt;torch/torch.h&gt;

class IntermediateLayerGetterImpl : public torch::nn::Module
{
public:

	template &lt;typename Net&gt;
	IntermediateLayerGetterImpl(Net  Module, std::vector&lt;std::string&gt; return_layers)
	{
		//this-&gt;to(Module-&gt;b);

		for (auto children : Module-&gt;named_children())
		{
			_module.insert(children.key(), std::move(children.value()));
			register_module(children.key(), _module[children.key()]);
		}

		_return_layers.swap(return_layers);
	}

	~IntermediateLayerGetterImpl();

	std::vector&lt;torch::Tensor&gt;  forward(torch::Tensor x);

private:
	torch::OrderedDict&lt;std::string, std::shared_ptr&lt;Module&gt;&gt; _module;
	std::vector&lt;std::string&gt; _return_layers;
};

TORCH_MODULE(IntermediateLayerGetter);

....

forward code

std::vector&lt;torch::Tensor&gt; IntermediateLayerGetterImpl::forward(torch::Tensor x)
{
	std::vector&lt;torch::Tensor&gt; results;

	x = _module["conv1"]-&gt;as&lt;torch::nn::Conv2d&gt;()-&gt;forward(x);	
	x = _module["bn1"]-&gt;as&lt;torch::nn::BatchNorm&gt;()-&gt;forward(x);
	x = _module["relu1"]-&gt;as&lt;torch::nn::Functional&gt;()-&gt;forward(x);
	x = _module["max_pool1"]-&gt;as&lt;torch::nn::Functional&gt;()-&gt;forward(x);

	x = _module["layer1"]-&gt;as&lt;torch::nn::Sequential&gt;()-&gt;forward(x);
	x = _module["layer2"]-&gt;as&lt;torch::nn::Sequential&gt;()-&gt;forward(x);
	x = _module["layer3"]-&gt;as&lt;torch::nn::Sequential&gt;()-&gt;forward(x);
	results.push_back(x);
	x = _module["layer4"]-&gt;as&lt;torch::nn::Sequential&gt;()-&gt;forward(x);
	results.push_back(x);
	
	return results;
}


</code></pre>

tom: <p>Cool, will you submit it to TorchVision for its C++ interface?</p>
<p>I think you need <a href="https://pytorch.org/cppdocs/api/classtorch_1_1nn_1_1_any_module.html#_CPPv4N5torch2nn9AnyModuleE" rel="nofollow noopener"><code>AnyModule</code></a>, look at what <a href="https://github.com/pytorch/pytorch/blob/master/torch/csrc/api/include/torch/nn/modules/container/sequential.h" rel="nofollow noopener"><code>nn::Sequential</code> does</a>.</p>
<p>Best regards</p>
<p>Thomas</p>

21fa417e3fb06e56040c: <p>thanks, I’ll finish it and submit it</p>

21fa417e3fb06e56040c: <p>Hi Thomas I’m Created Libtorch Example,</p>
<p>Can you see my code and give some advice?<br>
Thanks.</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/kerry-Cho/SemanticSegmentation-Libtorch" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/20694053?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/kerry-Cho/SemanticSegmentation-Libtorch" target="_blank" rel="nofollow noopener">kerry-Cho/SemanticSegmentation-Libtorch</a></h3>

<p>Libtorch Examples. Contribute to kerry-Cho/SemanticSegmentation-Libtorch development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>