Skip to content

Instantly share code, notes, and snippets.

@nogawanogawa
Created February 11, 2021 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nogawanogawa/226c8763c4348283b8eddae6da827190 to your computer and use it in GitHub Desktop.
Save nogawanogawa/226c8763c4348283b8eddae6da827190 to your computer and use it in GitHub Desktop.
MS_Vision_Resnet_sample.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "MS_Vision_Resnet_sample.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNOlLcDkUFaVxH1F8Fb+FrH",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nogawanogawa/226c8763c4348283b8eddae6da827190/ms_vision_resnet_sample.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "6CNDnDWZEWew"
},
"source": [
"!wget https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz\n",
"!tar zxvf images.tar.gz"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WvmgJ3WjnrQn"
},
"source": [
"!pip install microsoftvision"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6OvBj1iBEYjL"
},
"source": [
"import os\n",
"import glob\n",
"import re \n",
"import pandas as pd\n",
"from PIL import Image\n",
"from torch.utils.data import Dataset\n",
"import pandas as pd\n",
"import os\n",
"import torch\n",
"import torchvision.transforms as transforms\n",
"from torchvision.models import resnet34\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"\n",
"from sklearn.metrics import classification_report\n",
"from sklearn import preprocessing\n",
"import datetime\n",
"import microsoftvision"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "0KgnpwqNIbVd"
},
"source": [
"class MyDataSet(Dataset):\n",
" def __init__(self):\n",
" \n",
" l = glob.glob('images/*.jpg')\n",
" self.train_df = pd.DataFrame()\n",
" self.images = []\n",
" self.labels = []\n",
" self.le = preprocessing.LabelEncoder()\n",
"\n",
" for path in l:\n",
" self.images.append(path)\n",
" self.labels.append(re.split('[/_.]', path)[1])\n",
"\n",
" self.le.fit(self.labels)\n",
" self.labels_id = self.le.transform(self.labels)\n",
" self.transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])\n",
"\n",
" def __len__(self):\n",
" return len(self.images)\n",
" \n",
" def __getitem__(self, idx):\n",
" image = Image.open(self.images[idx])\n",
" image = image.convert('RGB')\n",
" label = self.labels_id[idx]\n",
" return self.transform(image), int(label)\n",
"\n",
"dataset = MyDataSet()\n",
"\n",
"n_samples = len(dataset)\n",
"train_size = int(len(dataset) * 0.7)\n",
"val_size = n_samples - train_size\n",
"\n",
"train_dataset, val_dataset = torch.utils.data.random_split(dataset, [train_size, val_size])\n",
"\n",
"train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)\n",
"val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=True)\n"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ZlttsB5aI-Uz",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "65a2fa8b-7c3a-4a66-8192-0ae96bc25a81"
},
"source": [
"model = microsoftvision.models.resnet50(pretrained=True)\n",
"model.fc = nn.Linear(2048,35)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"Loading Microsoft Vision pretrained model\n",
"Model already downloaded.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "BENWkuCFSaXC"
},
"source": [
"#device=torch.device('cpu')\n",
"device=torch.device('cuda')\n",
"model.cuda()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tuDtAND6S0PF"
},
"source": [
"criterion = nn.CrossEntropyLoss()\n",
"optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)"
],
"execution_count": 11,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OLhm0E4vVQyk",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "578e04ae-bfd5-4743-a882-0b378290662b"
},
"source": [
"def train(epoch):\n",
" total_loss = 0\n",
" total_size = 0\n",
" model.train()\n",
" for batch_idx, (data, target) in enumerate(train_loader):\n",
" data, target = data.to(device), target.to(device)\n",
" optimizer.zero_grad()\n",
" output = model(data)\n",
" loss = criterion(output, target)\n",
" total_loss += loss.item()\n",
" total_size += data.size(0)\n",
" loss.backward()\n",
" optimizer.step()\n",
" if batch_idx % 1000 == 0:\n",
" now = datetime.datetime.now()\n",
" print('[{}] Train Epoch: {} [{}/{} ({:.0f}%)]\\tAverage loss: {:.6f}'.format(\n",
" now,\n",
" epoch, batch_idx * len(data), len(train_loader.dataset),\n",
" 100. * batch_idx / len(train_loader), total_loss / total_size))\n",
"\n",
"for epoch in range(50):\n",
" train(epoch)\n"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"[2021-02-11 12:20:50.082160] Train Epoch: 0 [0/5173 (0%)]\tAverage loss: 0.429286\n",
"[2021-02-11 12:21:42.474456] Train Epoch: 1 [0/5173 (0%)]\tAverage loss: 0.188184\n",
"[2021-02-11 12:22:35.147930] Train Epoch: 2 [0/5173 (0%)]\tAverage loss: 0.126276\n",
"[2021-02-11 12:23:27.917702] Train Epoch: 3 [0/5173 (0%)]\tAverage loss: 0.092502\n",
"[2021-02-11 12:24:20.621052] Train Epoch: 4 [0/5173 (0%)]\tAverage loss: 0.054432\n",
"[2021-02-11 12:25:13.421302] Train Epoch: 5 [0/5173 (0%)]\tAverage loss: 0.046507\n",
"[2021-02-11 12:26:06.234125] Train Epoch: 6 [0/5173 (0%)]\tAverage loss: 0.043850\n",
"[2021-02-11 12:26:59.052813] Train Epoch: 7 [0/5173 (0%)]\tAverage loss: 0.032390\n",
"[2021-02-11 12:27:51.727936] Train Epoch: 8 [0/5173 (0%)]\tAverage loss: 0.018607\n",
"[2021-02-11 12:28:44.505783] Train Epoch: 9 [0/5173 (0%)]\tAverage loss: 0.018987\n",
"[2021-02-11 12:29:37.318523] Train Epoch: 10 [0/5173 (0%)]\tAverage loss: 0.016293\n",
"[2021-02-11 12:30:30.022737] Train Epoch: 11 [0/5173 (0%)]\tAverage loss: 0.006906\n",
"[2021-02-11 12:31:22.802585] Train Epoch: 12 [0/5173 (0%)]\tAverage loss: 0.015156\n",
"[2021-02-11 12:32:15.518767] Train Epoch: 13 [0/5173 (0%)]\tAverage loss: 0.005094\n",
"[2021-02-11 12:33:08.192396] Train Epoch: 14 [0/5173 (0%)]\tAverage loss: 0.010325\n",
"[2021-02-11 12:34:00.942385] Train Epoch: 15 [0/5173 (0%)]\tAverage loss: 0.001631\n",
"[2021-02-11 12:34:53.649993] Train Epoch: 16 [0/5173 (0%)]\tAverage loss: 0.011991\n",
"[2021-02-11 12:35:46.359606] Train Epoch: 17 [0/5173 (0%)]\tAverage loss: 0.004325\n",
"[2021-02-11 12:36:39.037524] Train Epoch: 18 [0/5173 (0%)]\tAverage loss: 0.001233\n",
"[2021-02-11 12:37:31.721078] Train Epoch: 19 [0/5173 (0%)]\tAverage loss: 0.000575\n",
"[2021-02-11 12:38:24.517161] Train Epoch: 20 [0/5173 (0%)]\tAverage loss: 0.001976\n",
"[2021-02-11 12:39:17.250756] Train Epoch: 21 [0/5173 (0%)]\tAverage loss: 0.000661\n",
"[2021-02-11 12:40:09.979922] Train Epoch: 22 [0/5173 (0%)]\tAverage loss: 0.000495\n",
"[2021-02-11 12:41:02.701932] Train Epoch: 23 [0/5173 (0%)]\tAverage loss: 0.000386\n",
"[2021-02-11 12:41:55.362256] Train Epoch: 24 [0/5173 (0%)]\tAverage loss: 0.005637\n",
"[2021-02-11 12:42:48.102639] Train Epoch: 25 [0/5173 (0%)]\tAverage loss: 0.000495\n",
"[2021-02-11 12:43:40.899139] Train Epoch: 26 [0/5173 (0%)]\tAverage loss: 0.000210\n",
"[2021-02-11 12:44:33.646666] Train Epoch: 27 [0/5173 (0%)]\tAverage loss: 0.000720\n",
"[2021-02-11 12:45:26.393464] Train Epoch: 28 [0/5173 (0%)]\tAverage loss: 0.000057\n",
"[2021-02-11 12:46:19.051402] Train Epoch: 29 [0/5173 (0%)]\tAverage loss: 0.000147\n",
"[2021-02-11 12:47:11.831183] Train Epoch: 30 [0/5173 (0%)]\tAverage loss: 0.000151\n",
"[2021-02-11 12:48:04.623109] Train Epoch: 31 [0/5173 (0%)]\tAverage loss: 0.000162\n",
"[2021-02-11 12:48:57.470191] Train Epoch: 32 [0/5173 (0%)]\tAverage loss: 0.000036\n",
"[2021-02-11 12:49:50.160272] Train Epoch: 33 [0/5173 (0%)]\tAverage loss: 0.000027\n",
"[2021-02-11 12:50:42.838386] Train Epoch: 34 [0/5173 (0%)]\tAverage loss: 0.000257\n",
"[2021-02-11 12:51:35.579987] Train Epoch: 35 [0/5173 (0%)]\tAverage loss: 0.000079\n",
"[2021-02-11 12:52:28.290507] Train Epoch: 36 [0/5173 (0%)]\tAverage loss: 0.000037\n",
"[2021-02-11 12:53:21.051955] Train Epoch: 37 [0/5173 (0%)]\tAverage loss: 0.000086\n",
"[2021-02-11 12:54:13.774259] Train Epoch: 38 [0/5173 (0%)]\tAverage loss: 0.000041\n",
"[2021-02-11 12:55:06.608123] Train Epoch: 39 [0/5173 (0%)]\tAverage loss: 0.000138\n",
"[2021-02-11 12:55:59.348568] Train Epoch: 40 [0/5173 (0%)]\tAverage loss: 0.000048\n",
"[2021-02-11 12:56:52.089244] Train Epoch: 41 [0/5173 (0%)]\tAverage loss: 0.000058\n",
"[2021-02-11 12:57:44.844513] Train Epoch: 42 [0/5173 (0%)]\tAverage loss: 0.000085\n",
"[2021-02-11 12:58:37.597057] Train Epoch: 43 [0/5173 (0%)]\tAverage loss: 0.000126\n",
"[2021-02-11 12:59:30.326310] Train Epoch: 44 [0/5173 (0%)]\tAverage loss: 0.000009\n",
"[2021-02-11 13:00:23.054153] Train Epoch: 45 [0/5173 (0%)]\tAverage loss: 0.000020\n",
"[2021-02-11 13:01:15.816782] Train Epoch: 46 [0/5173 (0%)]\tAverage loss: 0.000027\n",
"[2021-02-11 13:02:08.578656] Train Epoch: 47 [0/5173 (0%)]\tAverage loss: 0.000233\n",
"[2021-02-11 13:03:01.368873] Train Epoch: 48 [0/5173 (0%)]\tAverage loss: 0.000029\n",
"[2021-02-11 13:03:54.200946] Train Epoch: 49 [0/5173 (0%)]\tAverage loss: 0.000027\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "nqQOVSmLVVDO",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7be21139-c913-4644-c166-c4cc016bb957"
},
"source": [
"pred = []\n",
"Y = []\n",
"for i, (data, target) in enumerate(val_loader):\n",
" with torch.no_grad():\n",
" data, target = data.to(device), target.to(device)\n",
" output = model(data)\n",
" pred += [int(l.argmax()) for l in output]\n",
" Y += [int(l) for l in target]\n",
"\n",
"print(classification_report(Y, pred))"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" 0 0.71 0.80 0.75 65\n",
" 1 0.72 0.68 0.70 63\n",
" 2 0.70 0.78 0.74 63\n",
" 3 0.81 0.90 0.85 68\n",
" 4 0.90 0.76 0.82 74\n",
" 5 0.78 0.85 0.81 54\n",
" 6 0.79 0.75 0.77 61\n",
" 7 0.69 0.69 0.69 55\n",
" 8 0.57 0.52 0.55 69\n",
" 9 0.74 0.84 0.78 67\n",
" 10 0.75 0.65 0.70 46\n",
" 11 0.76 0.76 0.76 55\n",
" 12 0.66 0.60 0.63 119\n",
" 13 0.69 0.65 0.67 51\n",
" 14 0.56 0.68 0.62 59\n",
" 15 0.78 0.62 0.69 52\n",
" 16 0.66 0.61 0.64 57\n",
" 17 0.74 0.80 0.77 124\n",
" 18 0.78 0.68 0.73 66\n",
" 19 0.84 0.76 0.80 63\n",
" 20 0.72 0.67 0.70 58\n",
" 21 0.84 0.83 0.83 58\n",
" 22 0.82 0.80 0.81 56\n",
" 23 0.84 0.81 0.82 67\n",
" 24 0.69 0.64 0.66 66\n",
" 25 0.75 0.79 0.77 67\n",
" 26 0.74 0.81 0.77 52\n",
" 27 0.81 0.89 0.85 64\n",
" 28 0.79 0.86 0.82 51\n",
" 29 0.79 0.97 0.87 60\n",
" 30 0.78 0.81 0.79 47\n",
" 31 0.85 0.70 0.77 63\n",
" 32 0.58 0.55 0.56 64\n",
" 33 0.73 0.73 0.73 59\n",
" 34 0.72 0.85 0.78 54\n",
"\n",
" accuracy 0.74 2217\n",
" macro avg 0.75 0.75 0.74 2217\n",
"weighted avg 0.74 0.74 0.74 2217\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "CX7d6lD7oNhJ"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment