Skip to content

Instantly share code, notes, and snippets.

@chilin0525
Last active May 19, 2023 06:32
Show Gist options
  • Save chilin0525/f5e2ddbca60ef154c182d0c77d591ca4 to your computer and use it in GitHub Desktop.
Save chilin0525/f5e2ddbca60ef154c182d0c77d591ca4 to your computer and use it in GitHub Desktop.
EfficientNet-PyTorch to ONNX
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.onnx\n",
"from efficientnet_pytorch import EfficientNet"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image size: 240\n",
"Loaded pretrained weights for efficientnet-b1\n",
"Model image size: 240\n"
]
}
],
"source": [
"# Specify which model to use\n",
"model_name = 'efficientnet-b1'\n",
"image_size = EfficientNet.get_image_size(model_name)\n",
"print('Image size: ', image_size)\n",
"\n",
"# Load model\n",
"model = EfficientNet.from_pretrained(model_name)\n",
"model.set_swish(memory_efficient=False)\n",
"model.eval()\n",
"print('Model image size: ', model._global_params.image_size)\n",
"\n",
"# Dummy input for ONNX\n",
"dummy_input = torch.randn(10, 3, 224, 224)\n",
"\n",
"# Export with ONNX\n",
"torch.onnx.export(model, dummy_input, \"efficientnet-b1.onnx\", verbose=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"metadata": {
"interpreter": {
"hash": "df0893f56f349688326838aaeea0de204df53a132722cbd565e54b24a8fec5f6"
}
},
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 2
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment