{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from torch import nn\n", "import torch.nn.functional as F\n", "from datasets import load_dataset\n", "import fastcore.all as fc\n", "import torchvision.transforms.functional as TF\n", "from torch.utils.data import default_collate, DataLoader\n", "import torch.optim as optim" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "exclude" ] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import matplotlib as mpl" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "exclude" ] }, "outputs": [], "source": [ "%matplotlib inline\n", "plt.rcParams['figure.figsize'] = [2, 2]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset mnist (/Users/arun/.cache/huggingface/datasets/mnist/mnist/1.0.0/9d494b7f466d6931c64fb39d58bb1249a4d85c9eb9865d9bc20960b999e2a332)\n", "100%|██████████| 2/2 [00:00<00:00, 75.69it/s]\n" ] } ], "source": [ "dataset_nm = 'mnist'\n", "x,y = 'image', 'label'\n", "ds = load_dataset(dataset_nm)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def transform_ds(b):\n", " b[x] = [TF.to_tensor(ele) for ele in b[x]]\n", " return b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAI4AAACOCAYAAADn/TAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAIsUlEQVR4nO3df2yU9R0H8PfHtrQroFJBVrGjHVRAweHWCASCJBuumiXOLAyYWTbjQiYy58Y2fmzZ5oILJgsJMjSRrCsmig7mAjFsZBIlLkNGdeBgrOWnWqnF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FTl1wcfGuY/nDGIRcGh1wYHHIZVGucy1/sOh9y+Xv2pxBXT9xp6ts/YX8eeqCaE5dMPXfHclNP/sm/Ou+XnLdrmPZQex4cOOOQC4NDLoPqUHXqy105b5i2dUCv3Xh+gqnX77GtRCRhz3FOXnPS1JXN9rLb3P8NvL5xxiEXBodcGBxyYSs36hNbuVFaMTjkwuCQC4NDLgwOuTA45MLgkAuDQy4MDrkwOOTC4JBLpJ9Vich7AN4EMBqAv09IZnFs1nhVHZO6MdLgdO5UpK6nD87igGMLhocqcmFwyCVbwXkyS/sNgmMLICtrHBr8eKgil0iDIyLVIlIvIsdEJKvtbUWkRkTOiMihbtti0bt5MPSWjiw4IpIHYCOAOwDcCGBxsl9yttQCqE7ZFpfezfHvLa2qkfwDMAvArm71KgCrotp/L2MqB3CoW10PoDR5vxRAfTbH121c2wHMj9P4ojxUjQPwdre6MbktTmLXuzmuvaWjDE5PfQT5lq4P3t7SUYgyOI0AyrrV1wM4HeH+gwjUuzkKYXpLRyHK4OwHUCkiFSIyDMAidPRKjpOPezcDWezdHKC3NJDt3tIRL/LuBNAA4DiAH2d5wbkFHT9u0oqO2fA+ANeg493K0eRtSZbGNgcdh/E3ABxI/rszLuNTVZ45Jh+eOSYXBodcGBxyYXDIhcEhFwaHXBgccmFwyOV/atVD7hyCzrEAAAAASUVORK5CYII=", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "dst = ds.with_transform(transform_ds)\n", "plt.imshow(dst['train'][0]['image'].permute(1,2,0));" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "bs = 1024\n", "class DataLoaders:\n", " def __init__(self, train_ds, valid_ds, bs, collate_fn, **kwargs):\n", " self.train = DataLoader(train_ds, batch_size=bs, shuffle=True, collate_fn=collate_fn, **kwargs)\n", " self.valid = DataLoader(valid_ds, batch_size=bs, shuffle=False, collate_fn=collate_fn, **kwargs)\n", "\n", "def collate_fn(b):\n", " collate = default_collate(b)\n", " return (collate[x], collate[y])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "data": { "text/plain": [ "(torch.Size([1024, 1, 28, 28]), torch.Size([1024]))" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dls = DataLoaders(dst['train'], dst['test'], bs=bs, collate_fn=collate_fn)\n", "xb,yb = next(iter(dls.train))\n", "xb.shape, yb.shape" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class Reshape(nn.Module):\n", " def __init__(self, dim):\n", " super().__init__()\n", " self.dim = dim\n", " \n", " def forward(self, x):\n", " return x.reshape(self.dim)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "def conv(ni, nf, ks=3, s=2, act=nn.ReLU, norm=None):\n", " layers = [nn.Conv2d(ni, nf, kernel_size=ks, stride=s, padding=ks//2)]\n", " if norm:\n", " layers.append(norm)\n", " if act:\n", " layers.append(act())\n", " return nn.Sequential(*layers)\n", "\n", "def _conv_block(ni, nf, ks=3, s=2, act=nn.ReLU, norm=None):\n", " return nn.Sequential(\n", " conv(ni, nf, ks=ks, s=1, norm=None, act=act),\n", " conv(nf, nf, ks=ks, s=s, norm=norm, act=act),\n", " )\n", "\n", "class ResBlock(nn.Module):\n", " def __init__(self, ni, nf, s=2, ks=3, act=nn.ReLU, norm=None):\n", " super().__init__()\n", " self.convs = _conv_block(ni, nf, s=s, ks=ks, act=act, norm=norm)\n", " self.idconv = fc.noop if ni==nf else conv(ni, nf, ks=1, s=1, act=None)\n", " self.pool = fc.noop if s==1 else nn.AvgPool2d(2, ceil_mode=True)\n", " self.act = act()\n", " \n", " def forward(self, x):\n", " return self.act(self.convs(x) + self.idconv(self.pool(x)))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "def cnn_classifier():\n", " return nn.Sequential(\n", " ResBlock(1, 8, norm=nn.LayerNorm([8, 14, 14])),\n", " ResBlock(8, 16, norm=nn.LayerNorm([16, 7, 7])),\n", " ResBlock(16, 32, norm=nn.LayerNorm([32, 4, 4])),\n", " ResBlock(32, 64, norm=nn.LayerNorm([64, 2, 2])),\n", " ResBlock(64, 64, norm=nn.LayerNorm([64, 1, 1])),\n", " conv(64, 10, act=False),\n", " nn.Flatten(),\n", " )" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "def kaiming_init(m):\n", " if isinstance(m, (nn.Conv1d, nn.Conv2d, nn.Conv3d)):\n", " nn.init.kaiming_normal_(m.weight) " ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "train, epoch:1, loss: 1.8902, accuracy: 0.3183\n", "eval, epoch:1, loss: 1.0976, accuracy: 0.6274\n", "train, epoch:2, loss: 0.5929, accuracy: 0.8003\n", "eval, epoch:2, loss: 0.2895, accuracy: 0.9102\n", "train, epoch:3, loss: 0.2396, accuracy: 0.9264\n", "eval, epoch:3, loss: 0.1343, accuracy: 0.9597\n", "train, epoch:4, loss: 0.1139, accuracy: 0.9651\n", "eval, epoch:4, loss: 0.0801, accuracy: 0.9763\n", "train, epoch:5, loss: 0.1368, accuracy: 0.9582\n", "eval, epoch:5, loss: 0.0882, accuracy: 0.9722\n" ] } ], "source": [ "model = cnn_classifier()\n", "model.apply(kaiming_init)\n", "lr = 0.1\n", "max_lr = 0.3\n", "epochs = 5\n", "opt = optim.AdamW(model.parameters(), lr=lr)\n", "sched = optim.lr_scheduler.OneCycleLR(opt, max_lr, total_steps=len(dls.train), epochs=epochs)\n", "for epoch in range(epochs):\n", " for train in (True, False):\n", " accuracy = 0\n", " total_loss = 0\n", " dl = dls.train if train else dls.valid\n", " for xb,yb in dl:\n", " preds = model(xb)\n", " loss = F.cross_entropy(preds, yb)\n", " if train:\n", " loss.backward()\n", " opt.step()\n", " opt.zero_grad()\n", " with torch.no_grad():\n", " accuracy += (preds.argmax(1).detach().cpu() == yb).float().mean()\n", " total_loss += loss.item()\n", " if train:\n", " sched.step()\n", " accuracy /= len(dl)\n", " total_loss /= len(dl)\n", " print(f\"{'train' if train else 'eval'}, epoch:{epoch+1}, loss: {total_loss:.4f}, accuracy: {accuracy:.4f}\")" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eval, epoch:1, loss: 0.0882, accuracy: 0.9722\n", "eval, epoch:2, loss: 0.0882, accuracy: 0.9722\n", "eval, epoch:3, loss: 0.0882, accuracy: 0.9722\n", "eval, epoch:4, loss: 0.0882, accuracy: 0.9722\n", "eval, epoch:5, loss: 0.0882, accuracy: 0.9722\n" ] } ], "source": [ "for epoch in range(epochs):\n", " train = False\n", " accuracy = 0\n", " total_loss = 0\n", " dl = dls.valid\n", " for xb,yb in dl:\n", " preds = model(xb)\n", " loss = F.cross_entropy(preds, yb)\n", " with torch.no_grad():\n", " accuracy += (preds.argmax(1).detach().cpu() == yb).float().mean()\n", " total_loss += loss.item()\n", " accuracy /= len(dl)\n", " total_loss /= len(dl)\n", " print(f\"{'train' if train else 'eval'}, epoch:{epoch+1}, loss: {total_loss:.4f}, accuracy: {accuracy:.4f}\")" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAj8AAAB+CAYAAADLN3DXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAc5klEQVR4nO3dd3yV1RkH8N+ThL0CGCDISBDCFEQUQaVOtG7UOhARq1itiijWWm21rlZxIVKUqihacWCduGitG1GGIgioTBkyIwHCkiSnf7yXc97nmje8SW6Sm7y/7+fDh+fkvOvek5ucnCnGGBARERFFRUpVPwARERF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7Nc2npLzospremd9lNYeo3+TXbv2MBu/Mb+XyssZ/7ON282qHj+T2fJDREREkcLKDxEREUUKKz9EREQUKVU25ufn/7pxOPm79fbQ8u/moa6xsZ/uk2zfwe0IfVDz1Ta+r9WzgddIgaj0Zt8wg5xpl6u8Lg/vsHGHuXrMAcf5JNaie93U6EvTvrJx+9c3qeP4vide4Sa38/LLtw9UeYMfmGfj/VKDdwWP9+BPbmn8x+YeGXicyXPLS3R55CeVl7EoeAp0yqdzbczvieIV5W1R6cPmuG1AvujzXMLvN3DB2Tauf4HeXiF7kytLjvEpm6J536r0brfaCi7oeYnKW3FmMxuPGaqXr6gre2z8249/a+O2r6eq4+q95pZw6YQ5Kq86liFbfoiIiChSWPkhIiKiSKmybq/3ur1q46L4RrPe4a4R32Xlv87ygl02vn7t0eq4adMOsXHmpwUqr8HcVTbOWTs77vpUUeJ3EB599BQb3/bYhTZuvbB6TKOsKRq+pFdnvfil4C6rsDriq30fBHZfJZrZrVfFzrzSrcp7bF+9Wvdlf3e7dA9uFLwi8KH3jLBxozW6xBp/tc7GBb6uVKp48V1i7VxvNcbc3jXwvJy47qyajC0/REREFCms/BAREVGksPJDREREkVJlY36Ou8xNIy+op+tgG87ZaeNmjXeovIuz3BTJB+cdp/JSFzW0cfu33dRKM2u+Oi4LwVNmCwJzqCKtvlIvl96l9v9s3PZtN72d40CIEqNg9Rob1/fFADD5lTYuRhsEaYngMXj8WUrJjC0/REREFCms/BAREVGkVFm3V5233U6+deLysl9GoFeR4Y7DvMDjquOKk1E26cqHVPo3L1xn4+yFwd2UREREpcWWHyIiIooUVn6IiIgoUlj5ISIiokipsjE/RH43Z/dV6ewSliMgIiIqD7b8EBERUaSw8kNERESRIsZwUjgRERFFB1t+iIiIKFJY+SEiIqJIYeWHiIiIIoWVHyIiIooUVn6IiIgoUlj5ISIiokj5P0TtsIzRFepPAAAAAElFTkSuQmCC", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "xbv,ybv = next(iter(dls.train))\n", "logits = model(xbv)\n", "probs = F.softmax(logits, dim=1)\n", "idx = 5\n", "_,axs = plt.subplots(1, idx, figsize=(10, 10))\n", "for actual, pred, im, ax in zip(ybv[:idx], probs[:idx],xbv.permute(0,2,3,1)[:idx], axs.flat):\n", " ax.imshow(im)\n", " ax.set_axis_off()\n", " ax.set_title(f'pred: {pred.argmax(0).item()}, actual:{actual.item()}')\n", " " ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "tags": [ "exclude" ] }, "outputs": [], "source": [ "torch.save(model.state_dict(), 'classifier.pth')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "loaded_model = cnn_classifier()\n", "loaded_model.load_state_dict(torch.load('classifier.pth'));\n", "loaded_model.eval();" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAj8AAAB+CAYAAADLN3DXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAaPklEQVR4nO3dd3hVRfoH8O+bRopEOpEiCCSAiiKIgg1RdBXrsmtDEVQUxMK6Cuy6P8W26C6yrqhrxQa6CIrYXRFsKAKiSBMQpKj0SO9J5vfHOZk5c5/ccJLcfr6f58nzvJM55565d3Jv5k47opQCERERUVCkxbsARERERLHExg8REREFChs/REREFChs/BAREVGgsPFDREREgcLGDxEREQVK3Bs/IrJKRHrGuxyxIiL3iMj4eJcjWlifqYN1mVpYn6mDdVlzcW/8VIWI/ElEfhKR7SKyVkQeEZGMKF+zv4jMiOY1PNdqICJfikixiGwVkZkicnIsrh0PItJDRD4RkW0isipG14xZfbrXe0ZElopImYj0j9V140VEskRkiYj8EoNrxbQuPdftJyJKRAbE+tqxIiIfiMhOz89+EVkQ5WvG8rP21J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Hybh7cRMev/fFRK29k42/CPubIizx5F4U9zLejPre3kc2fbobnGn220cprsMx0F7JLPnae7OvdJ2F+3MpB/qXn51vpEaPH6jhTzHD14P/1t44rXBzdbS7ISG9odtpOf8ke4q9sqOuXErPOefiqS3Sc0ZPDXJQ82PNDREREgcLGDxEREQUKGz9EREQUKFGd8+Plnf8DAK2HmvQd026y8lZdaO4eO+uCR6y8UZtO0XFlc4M6ze6r453r7OW07f9drOMjfgyZQ6LM3Wo5rycxpO01y2lDb4dACaqgoZWsnbZXx5tLzfySptNiViIKsfx2c5ufBW3GVHKk7bIHhuq4/rOJeesCooNhzw8REREFChs/REREFCgxG/aqTNaH9t1/i8wGzOg7+OSQo82wVGW7UTbB4rB5HM5KDKWbzfBju4n20GdZnqml9j8tjVmZKDJKl62w0utLDtXxw7+aO4bnTubS9ljZeYm9c/70PqM8qVphz7t/Uycr3XDCQh1zGJqSFXt+iIiIKFDY+CEiIqJAYeOHiIiIAiUh5vxQMKl9Zpv8Nrd9HfY4ztFKfk//7mwdq23b41iSYCk7paOOH/zH01Zew/Tw83yOef5WHbceZ9/mp2zHitDDiZIOe36IiIgoUNj4ISIiokDhsBcRRV3JT6viXYRAylq7VceTtxxv5Z1YYHZnHlXcwcpr/epmHYduW0CUCtjzQ0RERIHCxg8REREFChs/REREFCic80NElKK8c61+CLkb0IXoUsmZP0alPESJgj0/REREFChs/BAREVGgiFLq4EcRERERpQj2/BAREVGgsPFDREREgcLGDxEREQUKGz9EREQUKGz8EBERUaCw8UNERESB8v9D4PCrWQgE7QAAAABJRU5ErkJggg==", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "with torch.no_grad():\n", " xbv,ybv = next(iter(dls.train))\n", " logits = loaded_model(xbv)\n", " probs = F.softmax(logits, dim=1)\n", " idx = 5\n", " _,axs = plt.subplots(1, idx, figsize=(10, 10))\n", " for actual, pred, im, ax in zip(ybv[:idx], probs[:idx],xbv.permute(0,2,3,1)[:idx], axs.flat):\n", " ax.imshow(im)\n", " ax.set_axis_off()\n", " ax.set_title(f'pred: {pred.argmax(0).item()}, actual:{actual.item()}')\n", " " ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAj8AAAB+CAYAAADLN3DXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAaPklEQVR4nO3dd3hVRfoH8O+bRopEOpEiCCSAiiKIgg1RdBXrsmtDEVQUxMK6Cuy6P8W26C6yrqhrxQa6CIrYXRFsKAKiSBMQpKj0SO9J5vfHOZk5c5/ccJLcfr6f58nzvJM55565d3Jv5k47opQCERERUVCkxbsARERERLHExg8REREFChs/REREFChs/BAREVGgsPFDREREgcLGDxEREQVK3Bs/IrJKRHrGuxyxIiL3iMj4eJcjWlifqYN1mVpYn6mDdVlzcW/8VIWI/ElEfhKR7SKyVkQeEZGMKF+zv4jMiOY1PNdqICJfikixiGwVkZkicnIsrh0PItJDRD4RkW0isipG14xZfbrXe0ZElopImYj0j9V140VEskRkiYj8EoNrxbQuPdftJyJKRAbE+tqxIiIfiMhOz89+EVkQ5WvG8rP21JDnt9Ot0z/E4vqxJiKdRORz93luEJEhUb5erD9n00XkAbddsENEvhOROpWdE9HGT7QbIgDeAdBJKZUP4GgAxwK4NcrXjKWdAK4F0BBAXQD/APBODF7XCsXgursAPA9gaJSvE0/fAxgM4Nt4FiKGf0NDAWyM0bViTkTqAvgrgEVxLkdU61Mpda5S6pDyHwBfAZgUzWvGklLqi5Dndz6cz98PY12WGHyBbwDneT0NoD6ANgA+iuY14+BeACcB6AYgH0BfAHsrO+GgjR+3e+2vIrJYRLaIyAsiku3mnS4iv4jIcBFZD+AFEUkTkb+IyAq3B2OiiNTzPF5fEVnt5v2tKs9OKbVCK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REFCgxG/aqTNaH9t1/i8wGzOg7+OSQo82wVGW7UTbB4rB5HM5KDKWbzfBju4n20GdZnqml9j8tjVmZKDJKl62w0utLDtXxw7+aO4bnTubS9ljZeYm9c/70PqM8qVphz7t/Uycr3XDCQh1zGJqSFXt+iIiIKFDY+CEiIqJAYeOHiIiIAiUh5vxQMKl9Zpv8Nrd9HfY4ztFKfk//7mwdq23b41iSYCk7paOOH/zH01Zew/Tw83yOef5WHbceZ9/mp2zHitDDiZIOe36IiIgoUNj4ISIiokDhsBcRRV3JT6viXYRAylq7VceTtxxv5Z1YYHZnHlXcwcpr/epmHYduW0CUCtjzQ0RERIHCxg8REREFChs/REREFCic80NElKK8c61+CLkb0IXoUsmZP0alPESJgj0/REREFChs/BAREVGgiFLq4EcRERERpQj2/BAREVGgsPFDREREgcLGDxEREQUKGz9EREQUKGz8EBERUaCw8UNERESB8v9D4PCrWQgE7QAAAABJRU5ErkJggg==", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "logits = model(xbv)\n", "probs = F.softmax(logits, dim=1)\n", "idx = 5\n", "_,axs = plt.subplots(1, idx, figsize=(10, 10))\n", "for actual, pred, im, ax in zip(ybv[:idx], probs[:idx],xbv.permute(0,2,3,1)[:idx], axs.flat):\n", " ax.imshow(im)\n", " ax.set_axis_off()\n", " ax.set_title(f'pred: {pred.argmax(0).item()}, actual:{actual.item()}')\n", " " ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "def predict(img):\n", " with torch.no_grad():\n", " img = img[None,]\n", " pred = loaded_model(img)[0]\n", " pred_probs = F.softmax(pred, dim=0)\n", " pred = [{\"digit\": i, \"prob\": f'{prob*100:.2f}%', 'logits': pred[i]} for i, prob in enumerate(pred_probs)]\n", " pred = sorted(pred, key=lambda ele: ele['digit'], reverse=False)\n", " return pred" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor(3)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[W NNPACK.cpp:64] Could not initialize NNPACK! Reason: Unsupported hardware.\n" ] }, { "data": { "text/plain": [ "[{'digit': 0, 'prob': '0.00%', 'logits': tensor(-5.5980)},\n", " {'digit': 1, 'prob': '0.00%', 'logits': tensor(-0.4972)},\n", " {'digit': 2, 'prob': '0.02%', 'logits': tensor(1.2516)},\n", " {'digit': 3, 'prob': '99.95%', 'logits': tensor(9.9263)},\n", " {'digit': 4, 'prob': '0.00%', 'logits': tensor(-5.5094)},\n", " {'digit': 5, 'prob': '0.01%', 'logits': tensor(0.2367)},\n", " {'digit': 6, 'prob': '0.00%', 'logits': tensor(-9.4633)},\n", " {'digit': 7, 'prob': '0.00%', 'logits': tensor(-2.4315)},\n", " {'digit': 8, 'prob': '0.02%', 'logits': tensor(1.4733)},\n", " {'digit': 9, 'prob': '0.00%', 'logits': tensor(-0.0205)}]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img = xb[1].reshape(1, 28, 28)\n", "print(yb[1])\n", "predict(img)" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "exclude" ] }, "source": [ "#### commit to .py file for deployment" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[NbConvertApp] Converting notebook mnist_classifier.ipynb to script\n", "[NbConvertApp] Writing 2920 bytes to mnist_classifier.py\n" ] } ], "source": [ "!jupyter nbconvert --to script --TagRemovePreprocessor.remove_cell_tags=\"exclude\" --TemplateExporter.exclude_input_prompt=True mnist_classifier.ipynb\n" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "tags": [ "exclude" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor(0.1046) tensor(1.) tensor(0.) tensor(0.3062)\n", "tensor(0.1435) tensor(1.) tensor(0.) tensor(0.3220)\n" ] }, { "data": { "image/png": "iVBOR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Fa5ySqnxfp/e907DmccWxFAfHUnypGoLeS08bvobqimMpDo6lODiW4jVO0mK31P3WPNNwe+6KYykOjqU4OJbi4FiKg2MpDo6lODiW4n2ckiZxr2WUXHEsxcGxFAfHUhwcS3FwLMXBsRQHx1K8j1PwPs1gXHEspUx/nPWSvpJ0XtI5SW8U425Z22JlKs5t4K2IeAx4CnhN0mbcsrbV+gYnIq5FxA/F85vAeWAtnZa1R4pfOwK8NKI5WgMNtMYp+h0/AXyPW9a2WungSLoX+BR4MyL+HuB9eyWdlnT6Fv9k5mgNVCo4kpbSCc2HEfFZMVyqZa3b1U6nMndVAj4AzkfEe10vuWVti5XZAHwGeBX4SdKZYuwdOi1qPy7a1/4GvDySGVojlWlX+y2gBV52y9qW8s6xpTg4luLgWIqDYykOjqU4OJbi4FiKg2Mp/uroCLhdrdkCHBxLcXAsxWucMZiGP8VxxbEUB8dSHBxL8RpnCPrt20zDmqaXK46lODiW4uBYitc4SW34PGoxrjiW4uBYioNjKYqI+k4m/QlcBh4AbtR24sF4bv/1SESs7h2sNTj/nlQ6HRFbaj9xCZ5bOb5UWYqDYynjCs7BMZ23DM+thLGscWzy+VJlKbUGR9KMpFlJlySNtb2tpEOSrks62zXWiN7Nk9BburbgSFoCvA+8CGwGdhX9ksflMDDTM9aU3s3N7y0dEbX8AE8DJ7qO9wP76zr/AnPaAJztOp4F1hTP1wCz45xf17yOAduaNL86L1VrgStdx3PFWJM0rndzU3tL1xmc+foI+pZuEdne0nWoMzhzwPqu43XA1RrPX0ap3s11qNJbug51BucUsEnSRknLgJ10eiU3SSN6N09Eb+maF3nbgQvAL8C7Y15wHgWuAbfoVMM9wP107lYuFo+rxjS3Z+lcxn8EzhQ/25syv4jwzrHleOfYUhwcS3FwLMXBsRQHx1IcHEtxcCzFwbGUO+5QVpdhOxaLAAAAAElFTkSuQmCC", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "from PIL import Image\n", "import numpy as np\n", "import glob\n", "import os\n", "png_files = glob.glob(os.path.join('./uploads', '*png'))\n", "img = png_files[1]\n", "image = Image.open(img)\n", "image = TF.to_tensor(image).permute(1, 2, 0)\n", "print(image.mean(), image.max(), image.min(), image.std())\n", "print(xb[0].mean(), xb[0].max(), xb[0].min(), xb[0].std())\n", "plt.imshow(image);" ] } ], "metadata": { "kernelspec": { "display_name": "python_main", "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.9.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }