<jupyter_start><jupyter_text>TensorRTへの変換PyTorchから、TensorRTへの変換 train_modelで生成した学習済みモデルである'best_steering_model_xy.pth'を読み込みます。<jupyter_code>import torchvision
import torch

model = torchvision.models.resnet18(pretrained=False)
model.fc = torch.nn.Linear(512, 2)
model.load_state_dict(torch.load('best_steering_model_xy.pth'))<jupyter_output><empty_output><jupyter_text>以下のコードを実行してGPUデバイスに転送します。<jupyter_code>device = torch.device('cuda')
model = model.to(device)
model = model.cuda().eval().half()<jupyter_output><empty_output><jupyter_text>TorchからTensorRTに変換します。<jupyter_code>from torch2trt import torch2trt 
data = torch.randn((1, 3, 224, 224)).cuda().half()
model_trt = torch2trt(model, [data], fp16_mode=True)<jupyter_output><empty_output><jupyter_text>変換が正しく行われたか、変換前と変換後の比較をおこないます。<jupyter_code>output_trt = model_trt(data)
output = model(data)

print(output.flatten()[0:10])
print(output_trt.flatten()[0:10])
print('max error: %f' % float(torch.max(torch.abs(output - output_trt))))<jupyter_output><empty_output><jupyter_text>TensorRTのモデルとして保存。<jupyter_code>torch.save(model_trt.state_dict(), 'road_following_model_xy_trt.pth')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14175257731958762, 'fraction_numerical': 0.02147766323024055, 'mean_word_length': 13.935897435897436, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '589870', 'license': 'MIT', 'n_tokens_mistral': 553, 'n_tokens_neox': 533, 'n_words': 39, 'path': 'notebooks/road_following/convert_trt.ipynb', 'repo_name': 'katebrighteyes/jetbot', 'size': '1164'}