This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/AIandML/e3_deep_learning/e3.3_alexnet_classification.ipynb

636 lines
1.2 MiB
Text
Raw Normal View History

2023-01-04 14:33:43 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 实验3-4 骨干网络基于AlexNet的图像分类\n",
"\n",
"实验目标:\n",
"* 深化对基于骨干网络的拓扑结构认识\n",
"* 了解图像分类应用\n",
"\n",
"实验内容:\n",
"\n",
"* 建立骨干网络\n",
"* 载入网络权重\n",
"* 给与网络输入,获取输出结果\n",
"* 解析结果含义\n",
"\n",
"参考: https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"import torch, torchvision"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 创建网络,载入权重"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ir/Code/justhomework/AIandML/.venv/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
" warnings.warn(\n",
"/home/ir/Code/justhomework/AIandML/.venv/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=AlexNet_Weights.IMAGENET1K_V1`. You can also use `weights=AlexNet_Weights.DEFAULT` to get the most up-to-date weights.\n",
" warnings.warn(msg)\n"
]
}
],
"source": [
"# 载入AlexNet\n",
"alexnet = torchvision.models.alexnet(pretrained=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AlexNet(\n",
" (features): Sequential(\n",
" (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))\n",
" (1): ReLU(inplace=True)\n",
" (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
" (4): ReLU(inplace=True)\n",
" (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (7): ReLU(inplace=True)\n",
" (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (9): ReLU(inplace=True)\n",
" (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (11): ReLU(inplace=True)\n",
" (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))\n",
" (classifier): Sequential(\n",
" (0): Dropout(p=0.5, inplace=False)\n",
" (1): Linear(in_features=9216, out_features=4096, bias=True)\n",
" (2): ReLU(inplace=True)\n",
" (3): Dropout(p=0.5, inplace=False)\n",
" (4): Linear(in_features=4096, out_features=4096, bias=True)\n",
" (5): ReLU(inplace=True)\n",
" (6): Linear(in_features=4096, out_features=1000, bias=True)\n",
" )\n",
")\n"
]
}
],
"source": [
"print(alexnet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 请回答:\n",
"> 1. 第一次和第二次调用`torchvision.models.alexnet(pretrained=True)`有何区别?参数`pretrained=True`是何含义?\n",
"> 2. 解释`print(alexnet)`的输出信息。\n",
"> 3. 请参考文档试着载入AlexNet之外的其他网络。"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 1000])\n"
]
}
],
"source": [
"# 用伪输入测试一下\n",
"x = torch.rand(1, 3, 224, 224)\n",
"y = alexnet(x)\n",
"print(y.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 请回答,\n",
"> 1. 以上代码中`x`的尺寸可否修改?\n",
"> 2. `y`的尺寸是什么,是何含义?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 准备输入数据\n",
"\n",
"将图像转化为torch.tensor数据类型且尺寸为 [B, C, H, W],并且做若干随机变换。对后文的变换说明如下:\n",
"* Line [1]: 定义图像转换的类.\n",
"* Line [2]: 图像尺寸变换 to 256×256 pixels.\n",
"* Line [3]: 随机裁剪 224×224 pixels.\n",
"* Line [4]: 转化PIL为PyTorch的Tensor.\n",
"* Line [5-7]: 通过均值方差,转化为规范化数据."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# 准备PIL图像转为Tensor的变换模块\n",
"from torchvision import transforms\n",
"transform = transforms.Compose([ #[1]\n",
" transforms.Resize(256), #[2]\n",
" transforms.CenterCrop(224), #[3]\n",
" transforms.ToTensor(), #[4]\n",
" transforms.Normalize( #[5]\n",
" mean=[0.485, 0.456, 0.406], #[6]\n",
" std=[0.229, 0.224, 0.225] #[7]\n",
" )\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image # PIL 是python的标注模块用于图像读取\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"img = Image.open(\"dog.jpg\") "
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(371, 474, 3)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhAAAAGiCAYAAAC26v9qAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQG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AhGtRrFCoYEoKQ6MY2YcD4QAURIhgonQtFCWymIzOWckXE9m6Sf4tk+UAqU0TBVfKopqxYpBY48P9iF+YDk08AUX/ybGkMAejCDivxCgKd/31D8kzeM2gLgNJLYg4u3H7m/XA423//5Fz3n73/aF1J97+3oeM5kjKT3okuCbhDb2Dbe1SraBOMQe8ECIkHJgyAMB2TeKYIEQIiZ+iEb8IF/XlVILTbf3V+I0EGIgGFS1PatBlVKKH54B8pBBgt8aCYS+ETWtrFVZV58EQQRigLBdJ8WicBgHTjGiBNamzG1B2+qbNf4ZtRVabb5ZiSESOtKgnJ/mPQCLMXI4HJimEymO2FLI5puABqGYgSlRYIxCMghNOOSB+zxymg6kmFhb5f7uxGfzwvcuF2Yt1HWlrCtjyzRtPJ6fKLqSxoQYlGVF1MhxpJlRtEIQQkoUVYI0GCLpkKnNqKWRQkSCUE3BGrVVamsgRsgBNXrw1DCUEDpaFhNko7bSp70HbCFERLaA2FGCIEIKkZyMFLWjj/0wpcft2g+Jpp7F9t+rR1tEUQTxz9OxPSF2IGkL/j3Iub4wSBIkRiIR65u9BSCCxOjzicaW6NMUtZUGtOCHIVE6YuEHqYggIRIRYsyUUljX1Q/VeEUpaq2OlIjvSbdDzBAzWi3kYeT+cGAQ4XJZ/Nqtfu9EjdA
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(img)\n",
"im = np.array(img)\n",
"im.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"转化数据类型,并且将其转为批量形式(batch_size=1)。\n",
"\n",
"需注意网络模型的输入总是成批次的若只有一个输入则使批次数量为1即可。"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([3, 224, 224]) tensor(-2.1179)\n",
"torch.Size([1, 3, 224, 224])\n"
]
}
],
"source": [
"img_t = transform(img)\n",
"print(img_t.shape, img_t.min())\n",
"batch_t = torch.unsqueeze(img_t, 0)\n",
"print(batch_t.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 请回答:\n",
"> 1. 观测变换前后图像`im`和`img_t`的尺寸是何变化?\n",
"> 2. `torch.unsqueeze`是何功能?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 图像分类\n",
"\n",
"用模型进行分类之前,需将模型设置为`eval`模式。"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AlexNet(\n",
" (features): Sequential(\n",
" (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))\n",
" (1): ReLU(inplace=True)\n",
" (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
" (4): ReLU(inplace=True)\n",
" (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (7): ReLU(inplace=True)\n",
" (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (9): ReLU(inplace=True)\n",
" (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (11): ReLU(inplace=True)\n",
" (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))\n",
" (classifier): Sequential(\n",
" (0): Dropout(p=0.5, inplace=False)\n",
" (1): Linear(in_features=9216, out_features=4096, bias=True)\n",
" (2): ReLU(inplace=True)\n",
" (3): Dropout(p=0.5, inplace=False)\n",
" (4): Linear(in_features=4096, out_features=4096, bias=True)\n",
" (5): ReLU(inplace=True)\n",
" (6): Linear(in_features=4096, out_features=1000, bias=True)\n",
" )\n",
")"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alexnet.eval()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 1000])\n"
]
}
],
"source": [
"out = alexnet(batch_t)\n",
"print(out.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 请回答:\n",
"> 1. 请对以下分类功能的代码进行注释,以详细解释输出变量`out`到其类别标签的过程。"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([258])\n"
]
}
],
"source": [
"_, index = torch.max(out, 1)\n",
"print(index)\n",
"percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 读取标签: int => str\n",
"\n",
"查看分类结果到底是什么"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"with open('imagenet1000_clsidx_to_labels.txt', 'r') as fp:\n",
" label = fp.read()\n",
" label = eval(label)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Samoyed, Samoyede\n",
"63.1154670715332\n"
]
}
],
"source": [
"print(label[int(index[0])])\n",
"print(percentage[index[0]].item())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"若是萨摩亚,分类正确"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. 更多示例\n",
"\n",
"使用更多图片进行分类尝试。"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7fd7d76a6ca0>"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAakAAAGiCAYAAABd6zm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8PEmwuXrfTPGyjvjfsvKNIarce7C1Vhfvs5RkH496bvXT5DR57O6q0Wzn+zfbdef7AO48iMRWve1g8Vwixr4p37vV4tlvP8F1JUvfKTL84oO/0movfs8e72TFvR3npux03mz3vdR7E7WIvCar4Hdsf250obRc76YNbef52m/B34zhb7btXE87dHDvuSpK6VzCYyXmnvu3iTPZmD9FBWaW+HzF4v+6km+cg4yBYXMPu7UFo937EhKQOKIZZPDt9CYYNnlthmCtjN1/Au/ll3g8ENZlo3Dns5WLeO32svcI9QVIH4UbcCgaDtTuJSW2XqEYNYru5mn6nMbX9jOJ1bXcyMAy7da17lZVlr3En+2xc3M5aVTvBfmrLVrgnSOpuhB30ilbUTolinMFzZ7WHJrC4/ZbUqIFy2D0UI7bbz27l/PufAPcak/dmZ7jrSGpUoPVgPyDDZr29z8zvAiHMav+dWSL2O9v57rB9DnL/3j4URQI7wVaz8+IxLRHZZyLbOvCvWWYgBDiOi5AKrTUqVSgFUgocx0EpTZqmVCpV1tbWqVarpGkKul90g7AEXFSw9UhKSmdH13yrGPd92M7+Ox1PthIUbL5/u4ODPfbdhSQ1CgfBlTEawwem4iVtJq3xjy8EaL2dpLB35qE/yPdwKzfpIG51UOlZSsMnHsVTSimQUiKloN1p4XkeQRAgkERRTBiGVKs1wjCk3e5QKpWyQ0qKllevyaPPe7dgLwlqr4jqdmOSu28LTBRTBx+T+7e30Frn63Ucx0FrTRz
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"img = Image.open(\"cat.jpg\")\n",
"plt.imshow(img)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Egyptian cat\n",
"41.64906311035156\n"
]
}
],
"source": [
"img_t = transform(img)\n",
"batch_t = torch.unsqueeze(img_t, 0)\n",
"\n",
"out = alexnet(batch_t)\n",
"\n",
"_, index = torch.max(out, 1)\n",
"percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100\n",
"\n",
"print(label[int(index[0])])\n",
"print(percentage[index[0]].item())"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7fd7d76e8a60>"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAakAAAGiCAYAAABd6zmYAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9WYxlW3bXjf7GbNZau4uI7E9bzSlXFS539eGiCn+GK1n4Ysz3QHclLPFgGQlLSGUJ1QPCEmBZQrIEPBgDAokXQAKJJ5B4sYSMrtCVLANGXN1P2Fy41GeXXafJLiJ2s5rZjPsw194ZmRmRJyNPnnMyz9l/aWfu2Huvfs455hzjP/5DVFXZY4899thjjxcQ5uM+gT322GOPPfa4CHsjtccee+yxxwuLvZHaY4899tjjhcXeSO2xxx577PHCYm+k9thjjz32eGGxN1J77LHHHnu8sNgbqT322GOPPV5Y7I3UHnvsscceLyz2RmqPPfbYY48XFnsjtccee+yxxwuLj81I/cN/+A/53Oc+R9M0fOMb3+A//sf/+HGdyh577LHHHi8oPhYj9a/+1b/iW9/6Fr/wC7/Af/kv/4Uf+qEf4id+4id47733Po7T2WOPPfbY4wWFfBwCs9/4xjf4Q3/oD/EP/sE/ACDnzJtvvsnP/dzP8df+2l/7qE9njz322GOPFxTuoz7gMAz85m/+Jj//8z+/+8wYw4//+I/z67/+6+du0/c9fd/v/s45c+/ePa5du4aIfOjnvMcee+yxx/OFqrJcLnnttdcw5mKn3kdupO7cuUNKiVu3bj30+a1bt/jt3/7tc7f5pV/6JX7xF3/xozi9PfbYY489PkJ85zvf4Y033rjw+4/cSD0Lfv7nf55vfetbu79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baQebWg7quXDT7d05XPcGfLwb87+v+2ElyP3yfnvz/t7h8tQCC4+mQsD1g9f0IuDZzmd5xXOu8DF/lERvc43jxddmj7y2iI98ptH35/32dP89v3xINxqzzlpOfM6+xmja/vxldFjz0LOvrmkv0+3WeKPThAfvdazRvRy/Up3E+NLndglf385COxu7VlD8rgxKu/lzGfyyA9UUzF4D01cympK9PHxSi6YYJVYFlsr9uBMFUSePpjykhupZ8ClB7kPe3B/wYzHHns8FZ48uJ8bk/ooXG7nHuJJx3wBXQzPAn22keTibc41O5eaYF14Z4WdS/dp1lKfPiO1xx4vFT6qWIpcsJS84PNLs72e5Tqecdg9c25nT/38U/7YipN/qlHs1NM9372R2mOPlwIf9mB6NuZ1BnrRPPlZ8eFeh8iD/T8e69l7LV4UFJfxp4E4sccee+zxELbRen3AGnjednaPD44nCBg/ik+OkVK4UCPvg+z2IiXol0zyZo+H8XLJ4XwULr+LjvH45+WTMwbgoS8/5n7xEFHoUTbf5c5tl8iquiupURJdP75rfF9l+qf8PfBwdRbhqcbO59VvPj0rqTEAt9PuMwZNzzE59jkJtu6xx/vh/eWEHsfzNbTnD77nsiB3p6SFUWtt6Svb9xfi+br6LmbePfL9M/ZXEUHHZNYdRzBnkPNrQH0kuLAcyLPxDR+Q987u4MWawL3cRmqPPT4NuGjM0Oe4wrrsvnaLE8GMRiq/b12zZzivZ0pH+uD0cFUlDQPGe6wx5JxJO+26/ST1o8TeSO2xx0uBy2VXXR6X3dc4sBuzEyTNKe2SPy/G80uCvXj/H/wY2yRgzbmsMUVw3gOQ0ou10vikY8+/3GOPPZ4d71PF9+OBsJMoe+LrCcZMhPnhIdZacow7hXfdqjDs8ZFhv5LaY4+XAh9BUvllxt5tTEqVtK1MqPpAvfzJGz4lPqL8sAuOJSLIeD151LHTnMFYLluJeo9nx95IPSsu6j8vWh98QfFMl/4
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"img = Image.open(\"gold_fish.jpg\")\n",
"plt.imshow(img)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"goldfish, Carassius auratus\n",
"99.99734497070312\n"
]
}
],
"source": [
"img_t = transform(img)\n",
"batch_t = torch.unsqueeze(img_t, 0)\n",
"\n",
"out = alexnet(batch_t)\n",
"\n",
"_, index = torch.max(out, 1)\n",
"percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100\n",
"\n",
"print(label[int(index[0])])\n",
"print(percentage[index[0]].item())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 请实验:\n",
"> 1. 自行收集图片,做分类测试,查看分类是否正确?"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7fd7d7654820>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiUAAAGhCAYAAAC+pMS4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9SbBl23amBX6zWNUuT+Xu1/2Wr5T0IhIpIlQYlhAGZJjRpYfRwmgjGqhFdMBo0aUBPbqZZtEgW9nAMJSWmQZEEEQonqRQ8cpbul93P9UuVzWrbIy59vH3JESQifEk05nXjvk9++yzz95rzWKMf/z/P1RKKfE4HsfjeByP43E8jsfxCx76F/0GHsfjeByP43E8jsfxOOAxKHkcj+NxPI7H8Tgex1+S8RiUPI7H8Tgex+N4HI/jL8V4DEoex+N4HI/jcTyOx/GXYjwGJY/jcTyOx/E4Hsfj+EsxHoOSx/E4HsfjeByP43H8pRiPQcnjeByP43E8jsfxOP5SjMeg5HE8jsfxOB7H43gcfynGY1DyOB7H43gcj+NxP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QymNMha0URakpK4Mb3Wlf1woKFDHofKb+xZSLX0hQcnV1hTGGN2/e/Mzjb9684b333vszz68qyTj+7NCAARJakw+NCaVIxHQ62wA50OSn6p1g489eoJT+bDDyMN5FRf48Oo76c/7/3ceSIDpFjSksRSpJyROjR6uSokg09ZLF3LNcnGGt4Rvf/IQYI19+8QpSorCW2WzOJx9/g8ViybNnz7i/3/DFF19yPB55/foVJHm
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 请在此给出自行收集图片做分类测试的结果,并简要分析。\n",
"img = Image.open(\"bike.jpg\")\n",
"plt.imshow(img)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mountain bike, all-terrain bike, off-roader\n",
"54.07283401489258\n"
]
}
],
"source": [
"img_t = transform(img)\n",
"batch_t = torch.unsqueeze(img_t, 0)\n",
"\n",
"out = alexnet(batch_t)\n",
"\n",
"_, index = torch.max(out, 1)\n",
"percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100\n",
"\n",
"print(label[int(index[0])])\n",
"print(percentage[index[0]].item())"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 分析\n",
"\n",
"分类基本正确,但此车似乎是共享单车,并不是山地车/越野车。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.16"
},
"vscode": {
"interpreter": {
"hash": "0733c54d9044ea299f7b7f48049f3576c8ad4e6ff5a97e2c60d8a9e3bff0bc54"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}