Run ERROR when convert my own model to TorchScript file

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

then run error , i cant know what happened , i follow the methods on the introduction…

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

I can’t reproduce your exact error locally by instantiating a MultiBoxLayer, but you’re using a couple features that we don’t support yet in TorchScript: enumerate() isn’t supported (you can workaround this with for i in range(len(xs)) and getting x with the index), and indexing a nn.ModuleList isn’t supported.

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 tracing API instead of writing TorchScript directly).

OK, I see, look forward to solving this problem as soon as possible!@driazati

When you use string_object.index(substring), it looks for the occurrence of substring in the string_object. If substring is present, the method returns the index at which the substring is present, otherwise, it throws ValueError: substring not found.

Using Python’s “in” operator

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 .

str="Hello, World!"
print("World" in str)//output is  True

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.