From fd5623732267a54bd01dd0b34613dac693f56471 Mon Sep 17 00:00:00 2001 From: Jing Zhang Date: Sun, 5 Nov 2023 19:22:30 +0100 Subject: [PATCH] update --- code/dnn_marathon.ipynb | 912 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 882 insertions(+), 30 deletions(-) diff --git a/code/dnn_marathon.ipynb b/code/dnn_marathon.ipynb index a1dbe3c..feedfde 100644 --- a/code/dnn_marathon.ipynb +++ b/code/dnn_marathon.ipynb @@ -11,65 +11,66 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [ - "# import lib\n", - "import torch" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "torch version 2.1.0+cu121\n", - "number of gpu 1\n", + "torch version 2.1.0+cu121 number of gpu 1\n", "gpu info _CudaDeviceProperties(name='NVIDIA GeForce RTX 3050 4GB Laptop GPU', major=8, minor=6, total_memory=4095MB, multi_processor_count=20)\n" ] } ], "source": [ - "# torch version\n", - "print('torch version',torch.__version__)\n", - "# check gpu\n", - "print('number of gpu',torch.cuda.device_count())\n", - "for id in range(torch.cuda.device_count()):\n", - " gpu = torch.cuda.get_device_properties(id)\n", - " print('gpu info',gpu)" + "# torch version and gpu info\n", + "import torch\n", + "\n", + "print('torch version',torch.__version__, 'number of gpu',torch.cuda.device_count())\n", + "for id in range(torch.cuda.device_count()): print('gpu info', torch.cuda.get_device_properties(id))" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "#alexnet\n", + "# alexnet\n", "\n", "import torch\n", "import torch.nn as nn\n", "import torch.nn.functional as F\n", "\n", - "class AlexNet(nn.Module):\n", + "\n", + "class AlexNet(nn.Module):# 这行代码定义了一个名为 AlexNet 的 PyTorch 模型类,它继承自 nn.Module 类。nn.Module 是 PyTorch 中所有模型类的基类。\n", + " ##super() 函数用于获取父类的实例。在本例中,父类是 nn.Module。\n", + " ##(AlexNet, self) 是元组,用于指定子类和当前实例。\n", + " ##__init__() 是 Python 中类的构造函数。它在类实例化时被调用,用于初始化类的属性和方法。\n", + " ##self 是 Python 中的自我引用,表示当前类的实例。num_classes 是参数,用于指定模型的类别数。\n", " def __init__(self, num_classes=10):\n", - " super(AlexNet, self).__init__()\n", - " self.conv1 = nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2)\n", - " self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)\n", + " super(AlexNet, self).__init__() \n", + " #卷积层用于提取图像中的特征。\n", + " #该层将输入图像 (3 通道 RGB 图像) 转换为 96 通道特征图。卷积核大小为 11,步长为 4,填充为 2。\n", + " self.conv1 = nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2) \n", + " ##池化层用于减少特征图的大小。第一个池化层使用 3x3 的最大池化核,步长为 2。 \n", + " ##这意味着它会在输入特征图上移动 2 个像素,并取每个池化核覆盖区域内的最大值。这样可以减少特征图的大小,并保留最重要的信息。\n", + " self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2) \n", " self.conv2 = nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)\n", " self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)\n", " self.conv3 = nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1)\n", " self.conv4 = nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1)\n", " self.pool4 = nn.MaxPool2d(kernel_size=3, stride=2)\n", - " self.fc1 = nn.Linear(256 * 6 * 6, 4096)\n", + " ##全连接层用于将提取到的特征进行分类。第一个全连接层有 4096 个神经元。这意味着它会将提取到的特征向量投影到一个 4096 维度的空间中。\n", + " ##第二个全连接层也有 4096 个神经元。这意味着它会进一步将特征向量投影到一个 4096 维度的空间中。第三个全连接层有 10 个神经元,对应于 10 个类别。\n", + " self.fc1 = nn.Linear(256 * 6 * 6, 4096) \n", " self.fc2 = nn.Linear(4096, 4096)\n", " self.fc3 = nn.Linear(4096, num_classes)\n", "\n", + " # forward() 方法是模型的前向传播函数。它用于将输入数据转换为输出数据。\n", " def forward(self, x):\n", " x = self.conv1(x)\n", + " ##ReLU 激活函数用于卷积层和全连接层。ReLU 激活函数是一种非线性激活函数,它可以将输入值映射到 0 和正无穷之间。\n", + " ##ReLU 激活函数可以加速模型的收敛速度,并防止梯度消失问题。\n", " x = F.relu(x)\n", " x = self.pool1(x)\n", " x = self.conv2(x)\n", @@ -80,7 +81,7 @@ " x = self.conv4(x)\n", " x = F.relu(x)\n", " x = self.pool4(x)\n", - " x = x.view(x.size(0), -1)\n", + " #x = x.view(x.size(0), -1)\n", " x = self.fc1(x)\n", " x = F.relu(x)\n", " x = self.fc2(x)\n", @@ -91,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -111,16 +112,36 @@ " (fc3): Linear(in_features=4096, out_features=10, bias=True)\n", ")\n" ] + }, + { + "ename": "RuntimeError", + "evalue": "mat1 and mat2 shapes cannot be multiplied (5376x7 and 9216x4096)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32md:\\GitHubRepo\\Code\\code\\dnn_marathon.ipynb Cell 4\u001b[0m line \u001b[0;36m5\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(model)\n\u001b[0;32m 4\u001b[0m x \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mrandn(\u001b[39m3\u001b[39m,\u001b[39m3\u001b[39m, \u001b[39m256\u001b[39m, \u001b[39m256\u001b[39m) \u001b[39m# 随机生成一个张量\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m out \u001b[39m=\u001b[39m model(x)\n", + "File \u001b[1;32mc:\\MyPrograms\\Miniconda3\\Lib\\site-packages\\torch\\nn\\modules\\module.py:1518\u001b[0m, in \u001b[0;36mModule._wrapped_call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1516\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_compiled_call_impl(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs) \u001b[39m# type: ignore[misc]\u001b[39;00m\n\u001b[0;32m 1517\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m-> 1518\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call_impl(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n", + "File \u001b[1;32mc:\\MyPrograms\\Miniconda3\\Lib\\site-packages\\torch\\nn\\modules\\module.py:1527\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1522\u001b[0m \u001b[39m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[0;32m 1523\u001b[0m \u001b[39m# this function, and just call forward.\u001b[39;00m\n\u001b[0;32m 1524\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m (\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_pre_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_pre_hooks\n\u001b[0;32m 1525\u001b[0m \u001b[39mor\u001b[39;00m _global_backward_pre_hooks \u001b[39mor\u001b[39;00m _global_backward_hooks\n\u001b[0;32m 1526\u001b[0m \u001b[39mor\u001b[39;00m _global_forward_hooks \u001b[39mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[1;32m-> 1527\u001b[0m \u001b[39mreturn\u001b[39;00m forward_call(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 1529\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 1530\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n", + "\u001b[1;32md:\\GitHubRepo\\Code\\code\\dnn_marathon.ipynb Cell 4\u001b[0m line \u001b[0;36m4\n\u001b[0;32m 46\u001b[0m x \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mpool4(x)\n\u001b[0;32m 47\u001b[0m \u001b[39m#x = x.view(x.size(0), -1)\u001b[39;00m\n\u001b[1;32m---> 48\u001b[0m x \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfc1(x)\n\u001b[0;32m 49\u001b[0m x \u001b[39m=\u001b[39m F\u001b[39m.\u001b[39mrelu(x)\n\u001b[0;32m 50\u001b[0m x \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfc2(x)\n", + "File \u001b[1;32mc:\\MyPrograms\\Miniconda3\\Lib\\site-packages\\torch\\nn\\modules\\module.py:1518\u001b[0m, in \u001b[0;36mModule._wrapped_call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1516\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_compiled_call_impl(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs) \u001b[39m# type: ignore[misc]\u001b[39;00m\n\u001b[0;32m 1517\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m-> 1518\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call_impl(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n", + "File \u001b[1;32mc:\\MyPrograms\\Miniconda3\\Lib\\site-packages\\torch\\nn\\modules\\module.py:1527\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1522\u001b[0m \u001b[39m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[0;32m 1523\u001b[0m \u001b[39m# this function, and just call forward.\u001b[39;00m\n\u001b[0;32m 1524\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m (\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_pre_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_pre_hooks\n\u001b[0;32m 1525\u001b[0m \u001b[39mor\u001b[39;00m _global_backward_pre_hooks \u001b[39mor\u001b[39;00m _global_backward_hooks\n\u001b[0;32m 1526\u001b[0m \u001b[39mor\u001b[39;00m _global_forward_hooks \u001b[39mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[1;32m-> 1527\u001b[0m \u001b[39mreturn\u001b[39;00m forward_call(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 1529\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 1530\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n", + "File \u001b[1;32mc:\\MyPrograms\\Miniconda3\\Lib\\site-packages\\torch\\nn\\modules\\linear.py:114\u001b[0m, in \u001b[0;36mLinear.forward\u001b[1;34m(self, input)\u001b[0m\n\u001b[0;32m 113\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mforward\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39minput\u001b[39m: Tensor) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Tensor:\n\u001b[1;32m--> 114\u001b[0m \u001b[39mreturn\u001b[39;00m F\u001b[39m.\u001b[39mlinear(\u001b[39minput\u001b[39m, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mweight, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mbias)\n", + "\u001b[1;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (5376x7 and 9216x4096)" + ] } ], "source": [ + "# test model\n", "model = AlexNet(num_classes=10)\n", - "print(model)" + "print(model)\n", + "x = torch.randn(3,3, 256, 256) # 随机生成一个张量\n", + "out = model(x) " ] }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -159,6 +180,837 @@ "summary(model=AlexNet(), input_size=(3,224,224), batch_size=1, device='cpu')" ] }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652615632\n", + "\n", + " (1, 1000)\n", + "\n", + "\n", + "\n", + "2176627251888\n", + "\n", + "AddmmBackward0\n", + "\n", + "\n", + "\n", + "2176627251888->2176652615632\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176627257120\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176627257120->2176627251888\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652854384\n", + "\n", + "classifier.6.bias\n", + " (1000)\n", + "\n", + "\n", + "\n", + "2176652854384->2176627257120\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176627259712\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176627259712->2176627251888\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176627254912\n", + "\n", + "AddmmBackward0\n", + "\n", + "\n", + "\n", + "2176627254912->2176627259712\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2175846863712\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2175846863712->2176627254912\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655198352\n", + "\n", + "classifier.4.bias\n", + " (4096)\n", + "\n", + "\n", + "\n", + "2176655198352->2175846863712\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2175846872736\n", + "\n", + "MulBackward0\n", + "\n", + "\n", + "\n", + "2175846872736->2176627254912\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615412880\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176615412880->2175846872736\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615412688\n", + "\n", + "AddmmBackward0\n", + "\n", + "\n", + "\n", + "2176615412688->2176615412880\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615414752\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176615414752->2176615412688\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655198160\n", + "\n", + "classifier.1.bias\n", + " (4096)\n", + "\n", + "\n", + "\n", + "2176655198160->2176615414752\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615413696\n", + "\n", + "MulBackward0\n", + "\n", + "\n", + "\n", + "2176615413696->2176615412688\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652354192\n", + "\n", + "ViewBackward0\n", + "\n", + "\n", + "\n", + "2176652354192->2176615413696\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652354144\n", + "\n", + "AdaptiveAvgPool2DBackward0\n", + "\n", + "\n", + "\n", + "2176652354144->2176652354192\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652354624\n", + "\n", + "MaxPool2DWithIndicesBackward0\n", + "\n", + "\n", + "\n", + "2176652354624->2176652354144\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176654822128\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176654822128->2176652354624\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655058848\n", + "\n", + "ConvolutionBackward0\n", + "\n", + "\n", + "\n", + "2176655058848->2176654822128\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655056736\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176655056736->2176655058848\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655053952\n", + "\n", + "ConvolutionBackward0\n", + "\n", + "\n", + "\n", + "2176655053952->2176655056736\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655053856\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176655053856->2176655053952\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655057840\n", + "\n", + "ConvolutionBackward0\n", + "\n", + "\n", + "\n", + "2176655057840->2176655053856\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059712\n", + "\n", + "MaxPool2DWithIndicesBackward0\n", + "\n", + "\n", + "\n", + "2176655059712->2176655057840\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655055680\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176655055680->2176655059712\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655053568\n", + "\n", + "ConvolutionBackward0\n", + "\n", + "\n", + "\n", + "2176655053568->2176655055680\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655053616\n", + "\n", + "MaxPool2DWithIndicesBackward0\n", + "\n", + "\n", + "\n", + "2176655053616->2176655053568\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059424\n", + "\n", + "ReluBackward0\n", + "\n", + "\n", + "\n", + "2176655059424->2176655053616\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059280\n", + "\n", + "ConvolutionBackward0\n", + "\n", + "\n", + "\n", + "2176655059280->2176655059424\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059136\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655059136->2176655059280\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652853424\n", + "\n", + "x\n", + " (1, 3, 227, 227)\n", + "\n", + "\n", + "\n", + "2176652853424->2176655059136\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059184\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655059184->2176655059280\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655183792\n", + "\n", + "features.0.weight\n", + " (64, 3, 11, 11)\n", + "\n", + "\n", + "\n", + "2176655183792->2176655059184\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059568\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655059568->2176655059280\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655183600\n", + "\n", + "features.0.bias\n", + " (64)\n", + "\n", + "\n", + "\n", + "2176655183600->2176655059568\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655059664\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655059664->2176655053568\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655196912\n", + "\n", + "features.3.weight\n", + " (192, 64, 5, 5)\n", + "\n", + "\n", + "\n", + "2176655196912->2176655059664\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655055728\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655055728->2176655053568\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655196816\n", + "\n", + "features.3.bias\n", + " (192)\n", + "\n", + "\n", + "\n", + "2176655196816->2176655055728\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655055776\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655055776->2176655057840\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655196528\n", + "\n", + "features.6.weight\n", + " (384, 192, 3, 3)\n", + "\n", + "\n", + "\n", + "2176655196528->2176655055776\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655057888\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655057888->2176655057840\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655197008\n", + "\n", + "features.6.bias\n", + " (384)\n", + "\n", + "\n", + "\n", + "2176655197008->2176655057888\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655053904\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655053904->2176655053952\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655197104\n", + "\n", + "features.8.weight\n", + " (256, 384, 3, 3)\n", + "\n", + "\n", + "\n", + "2176655197104->2176655053904\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655054096\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655054096->2176655053952\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655197776\n", + "\n", + "features.8.bias\n", + " (256)\n", + "\n", + "\n", + "\n", + "2176655197776->2176655054096\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655058800\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655058800->2176655058848\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655197872\n", + "\n", + "features.10.weight\n", + " (256, 256, 3, 3)\n", + "\n", + "\n", + "\n", + "2176655197872->2176655058800\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655063552\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176655063552->2176655058848\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655197968\n", + "\n", + "features.10.bias\n", + " (256)\n", + "\n", + "\n", + "\n", + "2176655197968->2176655063552\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615413168\n", + "\n", + "TBackward0\n", + "\n", + "\n", + "\n", + "2176615413168->2176615412688\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176652354096\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176652354096->2176615413168\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655198064\n", + "\n", + "classifier.1.weight\n", + " (4096, 9216)\n", + "\n", + "\n", + "\n", + "2176655198064->2176652354096\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2175846872640\n", + "\n", + "TBackward0\n", + "\n", + "\n", + "\n", + "2175846872640->2176627254912\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176615414704\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176615414704->2175846872640\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176655198256\n", + "\n", + "classifier.4.weight\n", + " (4096, 4096)\n", + "\n", + "\n", + "\n", + "2176655198256->2176615414704\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176627257744\n", + "\n", + "TBackward0\n", + "\n", + "\n", + "\n", + "2176627257744->2176627251888\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176627258080\n", + "\n", + "AccumulateGrad\n", + "\n", + "\n", + "\n", + "2176627258080->2176627257744\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2176623211056\n", + "\n", + "classifier.6.weight\n", + " (1000, 4096)\n", + "\n", + "\n", + "\n", + "2176623211056->2176627258080\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# visualize a model method 1\n", + "'''\n", + "install graphviz.exe + torchviz\n", + "如果你的电脑还是无法画图,并且出现了下面的报错:\n", + "CalledProcessError: Command '['dot', '-Tpdf', '-O', iitH.gv']' returned non-zero exit status 1. [st\n", + "那么我们这个时候就可以按照下面步骤完成画图:\n", + "1.打开cmd(使用管理员身份,要不然没有权限)\n", + "2.运行:dot-v\n", + "3.出现问题:There is no layout engine support for \"dot\" ...\n", + "4.运行:dot-c\n", + "5.没有提示(成功)\n", + "6.再次运行dot-v\n", + "7.结果就会画图成功\n", + "'''\n", + "from torchviz import make_dot, make_dot_from_trace\n", + "from torchvision.models import AlexNet\n", + "model = AlexNet()\n", + "\n", + "x = torch.randn(1, 3, 227, 227).requires_grad_(True)\n", + "y = model(x)\n", + "make_dot(y, params=dict(list(model.named_parameters()) + [('x', x)]))\n", + "#g = make_dot(y, params=dict(list(model.named_parameters()) + [('x', x)]))\n", + "#g.view() # 直接在当前路径下保存 pdf 并打开\n", + "# g.render(filename='netStructure/myNetModel', view=False, format='pdf') # 保存 pdf 到指定路径不打开" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Serving './demo.pth' at http://localhost:8080\n" + ] + }, + { + "data": { + "text/plain": [ + "('localhost', 8080)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# visualize a model method 2\n", + "from torchvision.models import AlexNet\n", + "import netron\n", + "import torch.onnx\n", + "\n", + "model = AlexNet()\n", + "\n", + "x = torch.randn(1, 3, 227, 227).requires_grad_(True)\n", + "y = model(x)\n", + "\n", + "modelData = \"./demo.pth\" # 定义模型数据保存的路径\n", + "torch.onnx.export(model, x, modelData) # 将 pytorch 模型以 onnx 格式导出并保存\n", + "netron.start(modelData) # 输出网络结构\n", + "\n", + "# 针对已经存在网络模型 .pth 文件的情况\n", + "#modelData = \"./demo.pth\" # 定义模型数据保存的路径\n", + "#netron.start(modelData) # 输出网络结构" + ] + }, { "cell_type": "code", "execution_count": null,