|  |  | @ -1,4 +1,4 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | """Exports a YOLOv5 *.pt model to *.onnx and *.torchscript formats |  |  |  | """Exports a YOLOv5 *.pt model to ONNX and TorchScript formats | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | Usage: |  |  |  | Usage: | 
			
		
	
		
		
			
				
					
					|  |  |  |     $ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1 |  |  |  |     $ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1 | 
			
		
	
	
		
		
			
				
					|  |  | @ -30,20 +30,20 @@ if __name__ == '__main__': | 
			
		
	
		
		
			
				
					
					|  |  |  |     model.model[-1].export = True  # set Detect() layer export=True |  |  |  |     model.model[-1].export = True  # set Detect() layer export=True | 
			
		
	
		
		
			
				
					
					|  |  |  |     _ = model(img)  # dry run |  |  |  |     _ = model(img)  # dry run | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     # Export to torchscript |  |  |  |     # Export to TorchScript | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     try: |  |  |  |     try: | 
			
		
	
		
		
			
				
					
					|  |  |  |         f = opt.weights.replace('.pt', '.torchscript')  # filename |  |  |  |         f = opt.weights.replace('.pt', '.torchscript')  # filename | 
			
		
	
		
		
			
				
					
					|  |  |  |         ts = torch.jit.trace(model, img) |  |  |  |         ts = torch.jit.trace(model, img) | 
			
		
	
		
		
			
				
					
					|  |  |  |         ts.save(f) |  |  |  |         ts.save(f) | 
			
		
	
		
		
			
				
					
					|  |  |  |         print('Torchscript export success, saved as %s' % f) |  |  |  |         print('TorchScript export success, saved as %s' % f) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     except: |  |  |  |     except Exception as e: | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         print('Torchscript export failed.') |  |  |  |         print('TorchScript export failed: %s' % e) | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     # Export to ONNX |  |  |  |     # Export to ONNX | 
			
		
	
		
		
			
				
					
					|  |  |  |     try: |  |  |  |     try: | 
			
		
	
		
		
			
				
					
					|  |  |  |         f = opt.weights.replace('.pt', '.onnx')  # filename |  |  |  |         f = opt.weights.replace('.pt', '.onnx')  # filename | 
			
		
	
		
		
			
				
					
					|  |  |  |         model.fuse()  # only for ONNX |  |  |  |         model.fuse()  # only for ONNX | 
			
		
	
		
		
			
				
					
					|  |  |  |         torch.onnx.export(model, img, f, verbose=False, opset_version=11, input_names=['images'], |  |  |  |         torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'], | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                           output_names=['output'])  # output_names=['classes', 'boxes'] |  |  |  |                           output_names=['output'])  # output_names=['classes', 'boxes'] | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         # Checks |  |  |  |         # Checks | 
			
		
	
	
		
		
			
				
					|  |  | @ -51,5 +51,5 @@ if __name__ == '__main__': | 
			
		
	
		
		
			
				
					
					|  |  |  |         onnx.checker.check_model(onnx_model)  # check onnx model |  |  |  |         onnx.checker.check_model(onnx_model)  # check onnx model | 
			
		
	
		
		
			
				
					
					|  |  |  |         print(onnx.helper.printable_graph(onnx_model.graph))  # print a human readable representation of the graph |  |  |  |         print(onnx.helper.printable_graph(onnx_model.graph))  # print a human readable representation of the graph | 
			
		
	
		
		
			
				
					
					|  |  |  |         print('ONNX export success, saved as %s\nView with https://github.com/lutzroeder/netron' % f) |  |  |  |         print('ONNX export success, saved as %s\nView with https://github.com/lutzroeder/netron' % f) | 
			
		
	
		
		
			
				
					
					|  |  |  |     except: |  |  |  |     except Exception as e: | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         print('ONNX export failed.') |  |  |  |         print('ONNX export failed: %s' % e) | 
			
				
				
			
		
	
		
		
	
		
		
	
	
		
		
			
				
					|  |  | 
 |