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):
		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))

	def forward(self,xs: List[torch.Tensor]):
		xs:list of 之前的featuremap list
		retrun: loc_preds: [N,21824,4]
		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_conf = self.conf_layers[i](x)
			y_conf = y_conf.permute(0,2,3,1).contiguous()
			y_conf = y_conf.view(N,-1,2)
		loc_preds =,1)
		conf_preds =,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/”, line 1, in
from networks import FaceBox
File “D:\Desktop\Deep Learning\faceboxes\”, line 6, in
from multibox_layer import MultiBoxLayer
File “D:\Desktop\Deep Learning\faceboxes\”, line 11, in
class MultiBoxLayer(torch.jit.ScriptModule):
File “D:\Desktop\Deep Learning\faceboxes\”, line 26, in MultiBoxLayer
def forward(self,xs: List[torch.Tensor]):
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\”, 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\”, 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\”, line 182, in build_def
build_stmts(ctx, body))
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, 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\”, line 124, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, line 163, in call
return method(ctx, node)
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, line 245, in build_Assign
rhs = build_expr(ctx, stmt.value)
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, line 163, in call
return method(ctx, node)
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, line 384, in build_Call
func = build_expr(ctx, expr.func)
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, line 163, in call
return method(ctx, node)
File “D:\SoftWare\Anaconda3\envs\Pytorch1_0\lib\site-packages\torch\jit\”, 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\”, 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