{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Model comparison\n", "from pathlib import Path\n", "import sys\n", "\n", "sys.path.append(str(Path(sys.path[0]).parent))\n", "\n", "from isegm.model.is_hrnet_model import HRNetModel\n", "\n", "def params_hrnet_18s(**kwargs):\n", " model = HRNetModel(width=18, ocr_width=48, small=True, with_aux_output=True, use_rgb_conv=True, \n", " use_leaky_relu=True, use_disks=True, norm_radius=5, with_prev_mask=True)\n", " return model\n", "\n", "\n", "def params_hrnet_18(**kwargs):\n", " model = HRNetModel(width=18, ocr_width=64, with_aux_output=True, use_rgb_conv=True, use_leaky_relu=True,\n", " use_disks=True, norm_radius=5, with_prev_mask=True)\n", " return model\n", "\n", "\n", "def params_hrnet_32(**kwargs):\n", " model = HRNetModel(width=32, ocr_width=128, with_aux_output=True, use_disks=True, use_rgb_conv=True, \n", " use_leaky_relu=True, norm_radius=5, with_prev_mask=True)\n", " return model" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INFO] Register count_convNd() for .\n", "[INFO] Register count_relu() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register count_normalization() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register zero_ops() for .\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/playpen-raid/qinliu/tools/anaconda3/lib/python3.8/site-packages/torch/functional.py:568: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /opt/conda/conda-bld/pytorch_1646755903507/work/aten/src/ATen/native/TensorShape.cpp:2228.)\n", " return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "17.938G 4.220M\n", "[INFO] Register count_convNd() for .\n", "[INFO] Register count_relu() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register count_normalization() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register zero_ops() for .\n", "30.985G 10.032M\n", "[INFO] Register count_convNd() for .\n", "[INFO] Register count_relu() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register count_normalization() for .\n", "[INFO] Register zero_ops() for .\n", "[INFO] Register zero_ops() for .\n", "83.118G 30.951M\n" ] } ], "source": [ "import torch\n", "from thop import profile\n", "from thop import clever_format\n", "\n", "\n", "input = torch.randn(1, 4, 400, 400)\n", "point = torch.randn(1, 2, 3)\n", "\n", "models = params_hrnet_18s(), params_hrnet_18(), params_hrnet_32()\n", "for model in models:\n", " model.eval()\n", " macs, params = profile(model, inputs=(input, point))\n", " gflops, params = clever_format([macs*2, params], \"%.3f\")\n", "\n", " print(gflops, params)\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "__init__() got an unexpected keyword argument 'use_naive_concat'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/playpen-raid2/qinliu/projects/SimpleClick/notebooks/analyze_effiency.ipynb Cell 3\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[39minput\u001b[39m \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mrandn(\u001b[39m1\u001b[39m, \u001b[39m4\u001b[39m, \u001b[39m400\u001b[39m, \u001b[39m400\u001b[39m)\n\u001b[1;32m 42\u001b[0m point \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mrandn(\u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m, \u001b[39m3\u001b[39m)\n\u001b[0;32m---> 44\u001b[0m models \u001b[39m=\u001b[39m params_swin_b(), params_swin_l()\n\u001b[1;32m 45\u001b[0m \u001b[39mfor\u001b[39;00m model \u001b[39min\u001b[39;00m models:\n\u001b[1;32m 46\u001b[0m model\u001b[39m.\u001b[39meval()\n", "\u001b[1;32m/playpen-raid2/qinliu/projects/SimpleClick/notebooks/analyze_effiency.ipynb Cell 3\u001b[0m in \u001b[0;36mparams_swin_b\u001b[0;34m(**kwargs)\u001b[0m\n\u001b[1;32m 11\u001b[0m backbone\u001b[39m=\u001b[39m\u001b[39mdict\u001b[39m(in_chans\u001b[39m=\u001b[39m\u001b[39m3\u001b[39m, in_coord_chans\u001b[39m=\u001b[39m\u001b[39m3\u001b[39m, embed_dim\u001b[39m=\u001b[39m\u001b[39m128\u001b[39m, depths\u001b[39m=\u001b[39m[\u001b[39m2\u001b[39m, \u001b[39m2\u001b[39m, \u001b[39m18\u001b[39m, \u001b[39m2\u001b[39m], num_heads\u001b[39m=\u001b[39m[\u001b[39m4\u001b[39m, \u001b[39m8\u001b[39m, \u001b[39m16\u001b[39m, \u001b[39m32\u001b[39m],\n\u001b[1;32m 12\u001b[0m window_size\u001b[39m=\u001b[39m\u001b[39m12\u001b[39m, ape\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m, drop_path_rate\u001b[39m=\u001b[39m\u001b[39m0.3\u001b[39m, patch_norm\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m, use_checkpoint\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m,)\n\u001b[1;32m 14\u001b[0m head \u001b[39m=\u001b[39m \u001b[39mdict\u001b[39m(in_channels\u001b[39m=\u001b[39m[\u001b[39m128\u001b[39m, \u001b[39m256\u001b[39m, \u001b[39m512\u001b[39m, \u001b[39m1024\u001b[39m], in_index\u001b[39m=\u001b[39m[\u001b[39m0\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m, \u001b[39m3\u001b[39m], channels\u001b[39m=\u001b[39m\u001b[39m256\u001b[39m, dropout_ratio\u001b[39m=\u001b[39m\u001b[39m0.1\u001b[39m,\n\u001b[1;32m 15\u001b[0m num_classes\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m, loss_decode\u001b[39m=\u001b[39mCrossEntropyLoss(), align_corners\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m,)\n\u001b[0;32m---> 17\u001b[0m model \u001b[39m=\u001b[39m SwinformerModel(backbone_params\u001b[39m=\u001b[39;49mbackbone, head_params\u001b[39m=\u001b[39;49mhead, use_naive_concat\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m, use_rgb_conv\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m,\n\u001b[1;32m 18\u001b[0m use_deep_fusion\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m, use_disks\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m, norm_radius\u001b[39m=\u001b[39;49m\u001b[39m5\u001b[39;49m, with_prev_mask\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m,)\n\u001b[1;32m 20\u001b[0m \u001b[39mreturn\u001b[39;00m model\n", "File \u001b[0;32m/playpen-raid2/qinliu/projects/SimpleClick/isegm/utils/serialization.py:39\u001b[0m, in \u001b[0;36mserialize..new_init\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 32\u001b[0m config[\u001b[39m'\u001b[39m\u001b[39mparams\u001b[39m\u001b[39m'\u001b[39m][name] \u001b[39m=\u001b[39m {\n\u001b[1;32m 33\u001b[0m \u001b[39m'\u001b[39m\u001b[39mtype\u001b[39m\u001b[39m'\u001b[39m: param_type,\n\u001b[1;32m 34\u001b[0m \u001b[39m'\u001b[39m\u001b[39mvalue\u001b[39m\u001b[39m'\u001b[39m: value,\n\u001b[1;32m 35\u001b[0m \u001b[39m'\u001b[39m\u001b[39mspecified\u001b[39m\u001b[39m'\u001b[39m: name \u001b[39min\u001b[39;00m specified_params\n\u001b[1;32m 36\u001b[0m }\n\u001b[1;32m 38\u001b[0m \u001b[39msetattr\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39m'\u001b[39m\u001b[39m_config\u001b[39m\u001b[39m'\u001b[39m, config)\n\u001b[0;32m---> 39\u001b[0m init(\u001b[39mself\u001b[39;49m, \u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", "File \u001b[0;32m/playpen-raid2/qinliu/projects/SimpleClick/isegm/model/is_swinformer_model.py:14\u001b[0m, in \u001b[0;36mSwinformerModel.__init__\u001b[0;34m(self, backbone_params, head_params, **kwargs)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[39m@serialize\u001b[39m\n\u001b[1;32m 7\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__init__\u001b[39m(\n\u001b[1;32m 8\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs\n\u001b[1;32m 12\u001b[0m ):\n\u001b[0;32m---> 14\u001b[0m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49m\u001b[39m__init__\u001b[39;49m(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 16\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mbackbone \u001b[39m=\u001b[39m SwinTransformer(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mbackbone_params)\n\u001b[1;32m 17\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhead \u001b[39m=\u001b[39m SwinTransfomerSegHead(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mhead_params)\n", "\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'use_naive_concat'" ] } ], "source": [ "# Analysis for Swin-B and Swin-L\n", "from pathlib import Path\n", "import sys\n", "\n", "sys.path.append(str(Path(sys.path[0]).parent))\n", "\n", "from isegm.model.modeling.transformer_helper.cross_entropy_loss import CrossEntropyLoss\n", "from isegm.model.is_swinformer_model import SwinformerModel\n", "\n", "def params_swin_b(**kwargs):\n", " backbone=dict(in_chans=3, in_coord_chans=3, embed_dim=128, depths=[2, 2, 18, 2], num_heads=[4, 8, 16, 32],\n", " window_size=12, ape=False, drop_path_rate=0.3, patch_norm=True, use_checkpoint=False,)\n", "\n", " head = dict(in_channels=[128, 256, 512, 1024], in_index=[0, 1, 2, 3], channels=256, dropout_ratio=0.1,\n", " num_classes=1, loss_decode=CrossEntropyLoss(), align_corners=False,)\n", "\n", " model = SwinformerModel(backbone_params=backbone, head_params=head, use_naive_concat=False, use_rgb_conv=False,\n", " use_deep_fusion=True, use_disks=True, norm_radius=5, with_prev_mask=True,)\n", "\n", " return model \n", "\n", "\n", "def params_swin_l(**kwargs):\n", " backbone=dict(in_chans=3, in_coord_chans=3, embed_dim=192, depths=[2, 2, 18, 2], num_heads=[6, 12, 24, 48],\n", " window_size=12, ape=False, drop_path_rate=0.3, patch_norm=True, use_checkpoint=False,)\n", "\n", " head = dict(in_channels=[192, 384, 768, 1536], in_index=[0, 1, 2, 3], channels=256, dropout_ratio=0.1,\n", " num_classes=1, loss_decode=CrossEntropyLoss(), align_corners=False,)\n", "\n", " model = SwinformerModel(backbone_params=backbone, head_params=head, use_naive_concat=False,\n", " use_rgb_conv=False, use_deep_fusion=True, use_disks=True, norm_radius=5, with_prev_mask=True,)\n", "\n", " return model \n", "\n", "\n", "import torch\n", "from thop import profile\n", "from thop import clever_format\n", "\n", "\n", "input = torch.randn(1, 4, 400, 400)\n", "point = torch.randn(1, 2, 3)\n", "\n", "models = params_swin_b(), params_swin_l()\n", "for model in models:\n", " model.eval()\n", " macs, params = profile(model, inputs=(input, point))\n", " gflops, params = clever_format([macs*2, params], \"%.3f\")\n", "\n", " print(gflops, params)\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.8 ('base')", "language": "python", "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.8 (default, Apr 13 2021, 19:58:26) \n[GCC 7.3.0]" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e89c9d8a920cc6bbfac98998034c8a15ed2e75dae1678af787f4c4e38e5be518" } } }, "nbformat": 4, "nbformat_minor": 2 }