Run ERROR when convert my own model to TorchScript file

ZLeopard: <pre><code class="lang-auto">class MultiBoxLayer(torch.jit.ScriptModule):
	num_classes = 2
	num_anchors = [21,1,1]
	in_planes = [128,256,256]

	def __init__(self):
		super(MultiBoxLayer,self).__init__()
		
		self.loc_layers = nn.ModuleList()
		self.conf_layers = nn.ModuleList()
		for i in range(len(self.in_planes)):
			self.loc_layers.append(nn.Conv2d(self.in_planes[i], self.num_anchors[i]*4, kernel_size=3, padding=1))
			self.conf_layers.append(nn.Conv2d(self.in_planes[i],self.num_anchors[i]*2, kernel_size=3, padding=1))

	@torch.jit.script_method
	def forward(self,xs: List[torch.Tensor]):
		'''
		xs:list of 之前的featuremap list
		retrun: loc_preds: [N,21824,4]
				conf_preds:[N,24824,2]
		'''
		y_locs=[]
		y_confs = []
		for i,x in enumerate(xs):
			y_loc = self.loc_layers[i](x) # N,anhors*4,H,W
			N = y_loc.size(0)
			y_loc = y_loc.permute(0,2,3,1).contiguous()
			y_loc = y_loc.view(N,-1,4)
			y_locs.append(y_loc)

			y_conf = self.conf_layers[i](x)
			y_conf = y_conf.permute(0,2,3,1).contiguous()
			y_conf = y_conf.view(N,-1,2)
			y_confs.append(y_conf)
			
		loc_preds = torch.cat(y_locs,1)
		conf_preds = torch.cat(y_confs,1)

		return loc_preds, conf_preds
</code></pre>
<p>then run error , i cant know what happened , i follow the methods on the introduction…</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “D:/Desktop/Deep Learning/faceboxes/inference.py”, line 1, in <br>
from networks import FaceBox<br>
File “D:\Desktop\Deep Learning\faceboxes\networks.py”, line 6, in <br>
from multibox_layer import MultiBoxLayer<br>
File “D:\Desktop\Deep Learning\faceboxes\multibox_layer.py”, line 11, in <br>
class MultiBoxLayer(torch.jit.ScriptModule):<br>
File “D:\Desktop\Deep Learning\faceboxes\multibox_layer.py”, line 26, in MultiBoxLayer<br>
def forward(self,xs: List[torch.Tensor]):<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit_<em>init</em>_.py”, line 716, in script_method<br>
ast = get_jit_ast(fn, is_method=True)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 147, in get_jit_ast<br>
return build_def(ctx, py_ast.body[0], type_line, is_method)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 182, in build_def<br>
build_stmts(ctx, body))<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 124, in build_stmts<br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 124, in <br>
stmts = [build_stmt(ctx, s) for s in stmts]<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 163, in <strong>call</strong><br>
return method(ctx, node)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 245, in build_Assign<br>
rhs = build_expr(ctx, stmt.value)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 163, in <strong>call</strong><br>
return method(ctx, node)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 384, in build_Call<br>
func = build_expr(ctx, expr.func)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 163, in <strong>call</strong><br>
return method(ctx, node)<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 373, in build_Attribute<br>
pos = find_after(ctx, value.range().end, ‘.’).end  # Start with the dot<br>
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\frontend.py”, line 569, in find_after<br>
new_pos = pos + ctx.source[pos:].index(substr)<br>
ValueError: substring not found</p>
</blockquote>

driazati: <p>I can’t reproduce your exact error locally by instantiating a <code>MultiBoxLayer</code>, but you’re using a couple features that we don’t support yet in TorchScript: <code>enumerate()</code> isn’t supported (you can workaround this with <code>for i in range(len(xs))</code> and getting <code>x</code> with the index), and indexing a <code>nn.ModuleList</code> isn’t supported.</p>
<p>From a first glance it looks like you can get around these by re-writing these parts of your model a little bit (or potentially use the <a href="https://pytorch.org/docs/stable/jit.html#torch.jit.ScriptModule" rel="nofollow noopener">tracing API</a> instead of writing TorchScript directly).</p>

ZLeopard: <p>OK， I see, look forward to solving this problem as soon as possible!<a class="mention" href="/u/driazati">@driazati</a></p>

warnerjonn: <aside class="quote no-group" data-username="ZLeopard" data-post="1" data-topic="33148">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zleopard/48/8249_2.png" class="avatar"> ZLeopard:</div>
<blockquote>
<p>ValueError: substring not found</p>
</blockquote>
</aside>
<p>When you use string_object.index(substring), it looks for the occurrence of substring in the string_object. If <a href="http://net-informations.com/python/basics/substring.htm" rel="noopener nofollow ugc">substring</a> is present, the method returns the index at which the substring is present, otherwise, it throws ValueError: substring not found.</p>
<p><strong>Using Python’s “in” operator</strong></p>
<p>The simplest and fastest way to check whether a string contains a substring or not in Python is the “in” operator . This operator returns true if the string contains the characters, otherwise, it returns false .</p>
<pre><code class="lang-auto">str="Hello, World!"
print("World" in str)//output is  True
</code></pre>
<p>Python “in” operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string. It is important to note that the “in” operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.</p>