From 5a33ab042067fc0eda195a6e17c3e0e3c7b319fa Mon Sep 17 00:00:00 2001 From: patlevin Date: Tue, 31 May 2022 11:09:15 +0200 Subject: [PATCH 1/2] add histogram refactoring notes and tests --- Histogram_refactoring.ipynb | 1069 +++++++++++++++++++++++++++++++++++ 1 file changed, 1069 insertions(+) create mode 100644 Histogram_refactoring.ipynb diff --git a/Histogram_refactoring.ipynb b/Histogram_refactoring.ipynb new file mode 100644 index 0000000..7fbc10b --- /dev/null +++ b/Histogram_refactoring.ipynb @@ -0,0 +1,1069 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Refactoring HistoGAN: Validation and Benchmark\n", + "\n", + "The refactored histogram blocks extracted common functionality and -data into a separate base class.\n", + "Here we test, whether the results match the original implementation and assess potential performance benefits.\n", + "\n", + "## Reference Implementation\n", + "\n", + "Below are the reference implemenations of all three histogram blocks: RGB-uv, rg-chroma, and Lab." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torch.nn.functional as F\n", + "import numpy as np\n", + "\n", + "EPS = 1e-6\n", + "\n", + "class OriginalRGBuvHistBlock(nn.Module):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=True,\n", + " hist_boundary=None, green_only=False, device='cuda'):\n", + " super().__init__()\n", + " self.h = h\n", + " self.insz = insz\n", + " self.device = device\n", + " self.resizing = resizing\n", + " self.method = method\n", + " self.intensity_scale = intensity_scale\n", + " self.green_only = green_only\n", + " if hist_boundary is None:\n", + " hist_boundary = [-3, 3]\n", + " hist_boundary.sort()\n", + " self.hist_boundary = hist_boundary\n", + " if self.method == 'thresholding':\n", + " self.eps = (abs(hist_boundary[0]) + abs(hist_boundary[1])) / h\n", + " else:\n", + " self.sigma = sigma\n", + "\n", + " def forward(self, x):\n", + " x = torch.clamp(x, 0, 1)\n", + " if x.shape[2] > self.insz or x.shape[3] > self.insz:\n", + " if self.resizing == 'interpolation':\n", + " x_sampled = F.interpolate(x, size=(self.insz, self.insz),\n", + " mode='bilinear', align_corners=False)\n", + " elif self.resizing == 'sampling':\n", + " inds_1 = torch.LongTensor(\n", + " np.linspace(0, x.shape[2], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " inds_2 = torch.LongTensor(\n", + " np.linspace(0, x.shape[3], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " x_sampled = x.index_select(2, inds_1)\n", + " x_sampled = x_sampled.index_select(3, inds_2)\n", + " else:\n", + " raise Exception(\n", + " f'Wrong resizing method. It should be: interpolation or sampling. '\n", + " f'But the given value is {self.resizing}.')\n", + " else:\n", + " x_sampled = x\n", + "\n", + " L = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " hists = torch.zeros((x_sampled.shape[0], 1 + int(not self.green_only) * 2,\n", + " self.h, self.h)).to(device=self.device)\n", + " for l in range(L):\n", + " I = torch.t(torch.reshape(X[l], (3, -1)))\n", + " II = torch.pow(I, 2)\n", + " if self.intensity_scale:\n", + " Iy = torch.unsqueeze(torch.sqrt(II[:, 0] + II[:, 1] + II[:, 2] + EPS),\n", + " dim=1)\n", + " else:\n", + " Iy = 1\n", + " if not self.green_only:\n", + " Iu0 = torch.unsqueeze(torch.log(I[:, 0] + EPS) - torch.log(I[:, 1] +\n", + " EPS), dim=1)\n", + " Iv0 = torch.unsqueeze(torch.log(I[:, 0] + EPS) - torch.log(I[:, 2] +\n", + " EPS), dim=1)\n", + " diff_u0 = abs(\n", + " Iu0 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " diff_v0 = abs(\n", + " Iv0 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " if self.method == 'thresholding':\n", + " diff_u0 = torch.reshape(diff_u0, (-1, self.h)) <= self.eps / 2\n", + " diff_v0 = torch.reshape(diff_v0, (-1, self.h)) <= self.eps / 2\n", + " elif self.method == 'RBF':\n", + " diff_u0 = torch.pow(torch.reshape(diff_u0, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v0 = torch.pow(torch.reshape(diff_v0, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u0 = torch.exp(-diff_u0) # Radial basis function\n", + " diff_v0 = torch.exp(-diff_v0)\n", + " elif self.method == 'inverse-quadratic':\n", + " diff_u0 = torch.pow(torch.reshape(diff_u0, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v0 = torch.pow(torch.reshape(diff_v0, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u0 = 1 / (1 + diff_u0) # Inverse quadratic\n", + " diff_v0 = 1 / (1 + diff_v0)\n", + " else:\n", + " raise Exception(\n", + " f'Wrong kernel method. It should be either thresholding, RBF,'\n", + " f' inverse-quadratic. But the given value is {self.method}.')\n", + " diff_u0 = diff_u0.type(torch.float32)\n", + " diff_v0 = diff_v0.type(torch.float32)\n", + " a = torch.t(Iy * diff_u0)\n", + " hists[l, 0, :, :] = torch.mm(a, diff_v0)\n", + "\n", + " Iu1 = torch.unsqueeze(torch.log(I[:, 1] + EPS) - torch.log(I[:, 0] + EPS),\n", + " dim=1)\n", + " Iv1 = torch.unsqueeze(torch.log(I[:, 1] + EPS) - torch.log(I[:, 2] + EPS),\n", + " dim=1)\n", + " diff_u1 = abs(\n", + " Iu1 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " diff_v1 = abs(\n", + " Iv1 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + "\n", + " if self.method == 'thresholding':\n", + " diff_u1 = torch.reshape(diff_u1, (-1, self.h)) <= self.eps / 2\n", + " diff_v1 = torch.reshape(diff_v1, (-1, self.h)) <= self.eps / 2\n", + " elif self.method == 'RBF':\n", + " diff_u1 = torch.pow(torch.reshape(diff_u1, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v1 = torch.pow(torch.reshape(diff_v1, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u1 = torch.exp(-diff_u1) # Gaussian\n", + " diff_v1 = torch.exp(-diff_v1)\n", + " elif self.method == 'inverse-quadratic':\n", + " diff_u1 = torch.pow(torch.reshape(diff_u1, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v1 = torch.pow(torch.reshape(diff_v1, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u1 = 1 / (1 + diff_u1) # Inverse quadratic\n", + " diff_v1 = 1 / (1 + diff_v1)\n", + "\n", + " diff_u1 = diff_u1.type(torch.float32)\n", + " diff_v1 = diff_v1.type(torch.float32)\n", + " a = torch.t(Iy * diff_u1)\n", + " if not self.green_only:\n", + " hists[l, 1, :, :] = torch.mm(a, diff_v1)\n", + " else:\n", + " hists[l, 0, :, :] = torch.mm(a, diff_v1)\n", + "\n", + " if not self.green_only:\n", + " Iu2 = torch.unsqueeze(torch.log(I[:, 2] + EPS) - torch.log(I[:, 0] +\n", + " EPS), dim=1)\n", + " Iv2 = torch.unsqueeze(torch.log(I[:, 2] + EPS) - torch.log(I[:, 1] +\n", + " EPS), dim=1)\n", + " diff_u2 = abs(\n", + " Iu2 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " diff_v2 = abs(\n", + " Iv2 - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " if self.method == 'thresholding':\n", + " diff_u2 = torch.reshape(diff_u2, (-1, self.h)) <= self.eps / 2\n", + " diff_v2 = torch.reshape(diff_v2, (-1, self.h)) <= self.eps / 2\n", + " elif self.method == 'RBF':\n", + " diff_u2 = torch.pow(torch.reshape(diff_u2, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v2 = torch.pow(torch.reshape(diff_v2, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u2 = torch.exp(-diff_u2) # Gaussian\n", + " diff_v2 = torch.exp(-diff_v2)\n", + " elif self.method == 'inverse-quadratic':\n", + " diff_u2 = torch.pow(torch.reshape(diff_u2, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_v2 = torch.pow(torch.reshape(diff_v2, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_u2 = 1 / (1 + diff_u2) # Inverse quadratic\n", + " diff_v2 = 1 / (1 + diff_v2)\n", + " diff_u2 = diff_u2.type(torch.float32)\n", + " diff_v2 = diff_v2.type(torch.float32)\n", + " a = torch.t(Iy * diff_u2)\n", + " hists[l, 2, :, :] = torch.mm(a, diff_v2)\n", + "\n", + " # normalization\n", + " hists_normalized = hists / (\n", + " ((hists.sum(dim=1)).sum(dim=1)).sum(dim=1).view(-1, 1, 1, 1) + EPS)\n", + "\n", + " return hists_normalized\n", + "\n", + "class OriginalrgChromaHistBlock(nn.Module):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=False,\n", + " hist_boundary=None, device='cuda'):\n", + " super().__init__()\n", + " self.h = h\n", + " self.insz = insz\n", + " self.device = device\n", + " self.resizing = resizing\n", + " self.method = method\n", + " self.intensity_scale = intensity_scale\n", + " if hist_boundary is None:\n", + " hist_boundary = [0, 1]\n", + " hist_boundary.sort()\n", + " self.hist_boundary = hist_boundary\n", + " if self.method == 'thresholding':\n", + " self.eps = (abs(hist_boundary[0]) + abs(hist_boundary[1])) / h\n", + " else:\n", + " self.sigma = sigma\n", + "\n", + " def forward(self, x):\n", + " x = torch.clamp(x, 0, 1)\n", + " if x.shape[2] > self.insz or x.shape[3] > self.insz:\n", + " if self.resizing == 'interpolation':\n", + " x_sampled = F.interpolate(x, size=(self.insz, self.insz),\n", + " mode='bilinear', align_corners=False)\n", + " elif self.resizing == 'sampling':\n", + " inds_1 = torch.LongTensor(\n", + " np.linspace(0, x.shape[2], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " inds_2 = torch.LongTensor(\n", + " np.linspace(0, x.shape[3], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " x_sampled = x.index_select(2, inds_1)\n", + " x_sampled = x_sampled.index_select(3, inds_2)\n", + " else:\n", + " raise Exception(\n", + " f'Wrong resizing method. It should be: interpolation or sampling. '\n", + " f'But the given value is {self.resizing}.')\n", + " else:\n", + " x_sampled = x\n", + "\n", + " L = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " hists = torch.zeros((x_sampled.shape[0], 1, self.h, self.h)).to(\n", + " device=self.device)\n", + " for l in range(L):\n", + " I = torch.t(torch.reshape(X[l], (3, -1)))\n", + " II = torch.pow(I, 2)\n", + " if self.intensity_scale:\n", + " Iy = torch.unsqueeze(torch.sqrt(II[:, 0] + II[:, 1] + II[:, 2] + EPS),\n", + " dim=1)\n", + " else:\n", + " Iy = 1\n", + "\n", + " Ir = torch.unsqueeze(I[:, 0] / (torch.sum(I, dim=-1) + EPS), dim=1)\n", + " Ig = torch.unsqueeze(I[:, 1] / (torch.sum(I, dim=-1) + EPS), dim=1)\n", + "\n", + " diff_r = abs(Ir - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " diff_g = abs(Ig - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + "\n", + " if self.method == 'thresholding':\n", + " diff_r = torch.reshape(diff_r, (-1, self.h)) <= self.eps / 2\n", + " diff_g = torch.reshape(diff_g, (-1, self.h)) <= self.eps / 2\n", + " elif self.method == 'RBF':\n", + " diff_r = torch.pow(torch.reshape(diff_r, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_g = torch.pow(torch.reshape(diff_g, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_r = torch.exp(-diff_r) # Gaussian\n", + " diff_g = torch.exp(-diff_g)\n", + " elif self.method == 'inverse-quadratic':\n", + " diff_r = torch.pow(torch.reshape(diff_r, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_g = torch.pow(torch.reshape(diff_g, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_r = 1 / (1 + diff_r) # Inverse quadratic\n", + " diff_g = 1 / (1 + diff_g)\n", + "\n", + " diff_r = diff_r.type(torch.float32)\n", + " diff_g = diff_g.type(torch.float32)\n", + " a = torch.t(Iy * diff_r)\n", + "\n", + " hists[l, 0, :, :] = torch.mm(a, diff_g)\n", + "\n", + " # normalization\n", + " hists_normalized = hists / (\n", + " ((hists.sum(dim=1)).sum(dim=1)).sum(dim=1).view(-1, 1, 1, 1) + EPS)\n", + "\n", + " return hists_normalized\n", + "\n", + "class OriginalLabHistBlock(nn.Module):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=False,\n", + " hist_boundary=None, device='cuda'):\n", + " super().__init__()\n", + " self.h = h\n", + " self.insz = insz\n", + " self.device = device\n", + " self.resizing = resizing\n", + " self.method = method\n", + " self.intensity_scale = intensity_scale\n", + " if hist_boundary is None:\n", + " hist_boundary = [0, 1]\n", + " hist_boundary.sort()\n", + " self.hist_boundary = hist_boundary\n", + " if self.method == 'thresholding':\n", + " self.eps = (abs(hist_boundary[0]) + abs(hist_boundary[1])) / h\n", + " else:\n", + " self.sigma = sigma\n", + "\n", + " def forward(self, x):\n", + " x = torch.clamp(x, 0, 1)\n", + " if x.shape[2] > self.insz or x.shape[3] > self.insz:\n", + " if self.resizing == 'interpolation':\n", + " x_sampled = F.interpolate(x, size=(self.insz, self.insz),\n", + " mode='bilinear', align_corners=False)\n", + " elif self.resizing == 'sampling':\n", + " inds_1 = torch.LongTensor(\n", + " np.linspace(0, x.shape[2], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " inds_2 = torch.LongTensor(\n", + " np.linspace(0, x.shape[3], self.h, endpoint=False)).to(\n", + " device=self.device)\n", + " x_sampled = x.index_select(2, inds_1)\n", + " x_sampled = x_sampled.index_select(3, inds_2)\n", + " else:\n", + " raise Exception(\n", + " f'Wrong resizing method. It should be: interpolation or sampling. '\n", + " f'But the given value is {self.resizing}.')\n", + " else:\n", + " x_sampled = x\n", + "\n", + " L = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " hists = torch.zeros((x_sampled.shape[0], 1, self.h, self.h)).to(\n", + " device=self.device)\n", + " for l in range(L):\n", + " I = torch.t(torch.reshape(X[l], (3, -1)))\n", + " if self.intensity_scale:\n", + " Il = torch.unsqueeze(I[:, 0], dim=1)\n", + " else:\n", + " Il = 1\n", + "\n", + " Ia = torch.unsqueeze(I[:, 1], dim=1)\n", + " Ib = torch.unsqueeze(I[:, 2], dim=1)\n", + "\n", + " diff_a = abs(Ia - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + " diff_b = abs(Ib - torch.unsqueeze(torch.tensor(np.linspace(\n", + " self.hist_boundary[0], self.hist_boundary[1], num=self.h)),\n", + " dim=0).to(self.device))\n", + "\n", + " if self.method == 'thresholding':\n", + " diff_a = torch.reshape(diff_a, (-1, self.h)) <= self.eps / 2\n", + " diff_b = torch.reshape(diff_b, (-1, self.h)) <= self.eps / 2\n", + " elif self.method == 'RBF':\n", + " diff_a = torch.pow(torch.reshape(diff_a, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_b = torch.pow(torch.reshape(diff_b, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_a = torch.exp(-diff_a) # Gaussian\n", + " diff_b = torch.exp(-diff_b)\n", + " elif self.method == 'inverse-quadratic':\n", + " diff_a = torch.pow(torch.reshape(diff_a, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_b = torch.pow(torch.reshape(diff_b, (-1, self.h)),\n", + " 2) / self.sigma ** 2\n", + " diff_a = 1 / (1 + diff_a) # Inverse quadratic\n", + " diff_b = 1 / (1 + diff_b)\n", + "\n", + " diff_a = diff_a.type(torch.float32)\n", + " diff_b = diff_b.type(torch.float32)\n", + " a = torch.t(Il * diff_a)\n", + "\n", + " hists[l, 0, :, :] = torch.mm(a, diff_b)\n", + "\n", + " # normalization\n", + " hists_normalized = hists / (\n", + " ((hists.sum(dim=1)).sum(dim=1)).sum(dim=1).view(-1, 1, 1, 1) + EPS)\n", + "\n", + " return hists_normalized" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Refactored Implementation\n", + "\n", + "Next, let's define the refactored versions.\n", + "Since pixel sampling, histogram value scaling, and kernel methods are the same across all histogram blocks, it makes sense to extract them into separate functions.\n", + "We can also do some micro-optimizations here, like replacing divisions with multiplications where applicable." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Kernel Functions\n", + "\n", + "We start by defining the kernel functions for intensity scaling, resizing, and pixel counting.\n", + "\n", + "Minor changes: first, squaring the input for intensity scaling has been moved into the intensity scaling function itself.\n", + "This means that when `intensity_scale` is set to `False`, the histogram block's `forward()` method no longer includes this\n", + "calculation, which saves on memory and computation time (only calculate what we need and when we actually need it).\n", + "\n", + "Another change can be found in the sampling method. Here, we no longer use `LongTensor`, which creates 64-bit indexes and\n", + "is slow on many consumer as well as professional GPU devices. It's also unnecessary, since the index values only exceed\n", + "the 32-bit range once we get to image sizes beyond 45k x 45k pixels, i.e. √(2^31) by √(2^31) images. Given that a 3-channel\n", + "image of this size would require ~6GiB or VRAM, it's reasonable to assume that we can limit ourselves to sub-2 gigapixel\n", + "images for the time being." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from typing import Union\n", + "\n", + "Device = Union[str, torch.device]\n", + "\n", + "\n", + "def no_scaling(_: torch.Tensor) -> int:\n", + " return 1\n", + "\n", + "\n", + "def intensity_scaling(X: torch.Tensor) -> torch.Tensor:\n", + " XX = X ** 2\n", + " return (XX[:, 0] + XX[:, 1] + XX[:, 2] + EPS).sqrt().unsqueeze(dim=1)\n", + "\n", + "\n", + "def resizing_interpolate(max_size: int, X: torch.Tensor) -> torch.Tensor:\n", + " H, W = X.shape[2:]\n", + " if H > max_size or W > max_size:\n", + " return F.interpolate(\n", + " X, size=(max_size, max_size), mode='bilinear', align_corners=False\n", + " )\n", + " return X\n", + "\n", + "\n", + "def resizing_sample(\n", + " h: int, max_size: int, device: Device, X: torch.Tensor\n", + ") -> torch.Tensor:\n", + " H, W = X.shape[2:]\n", + " if H > max_size or W > max_size:\n", + " index_H = torch.linspace(0, H - H/h, h, dtype=torch.int32).to(device)\n", + " index_W = torch.linspace(0, W - W/h, h, dtype=torch.int32).to(device)\n", + " sampled = X.index_select(dim=2, index=index_H)\n", + " return sampled.index_select(dim=3, index=index_W)\n", + " return X\n", + "\n", + "\n", + "def thresholding_kernel(h: int, eps: float, X: torch.Tensor) -> torch.Tensor:\n", + " return (X.reshape(-1, h) <= eps).float()\n", + "\n", + "\n", + "def rbf_kernel(h: int, inv_sigma_sq: float, X: torch.Tensor) -> torch.Tensor:\n", + " Y = (X.reshape(-1, h) ** 2) * inv_sigma_sq\n", + " return (-Y).exp()\n", + "\n", + "\n", + "def inverse_quadratic_kernel(\n", + " h: int, inv_sigma_sq: float, X: torch.Tensor\n", + ") -> torch.Tensor:\n", + " Y = (X.reshape(-1, h) ** 2) * inv_sigma_sq\n", + " return 1. / (1. + Y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### HistBlock Base Class\n", + "\n", + "Next we define a base class for all histogram blocks. The base class ctor selects the kernel functions depending on the provided parameter and precalculates tensors that only depend on ctor arguments. This includes the delta-values used for calculating differences.\n", + "\n", + "We can compute these once and upload them onto the device in a suitable data format. Factory functions are used to map function names to actual kernel functions.\n", + "Partial function application helps setting kernel function parameters that don't depend on the input tensor and thus can be precalculated." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from functools import partial\n", + "from typing import Callable, List, Sequence, Union\n", + "\n", + "_KernelMethod = Callable[[torch.Tensor], torch.Tensor]\n", + "_Device = Union[str, torch.device]\n", + "\n", + "def _get_resizing(\n", + " mode: str, h: int, max_size: int, device: Device\n", + ") -> _KernelMethod:\n", + " if mode == 'interpolation':\n", + " return partial(resizing_interpolate, max_size)\n", + " elif mode == 'sampling':\n", + " return partial(resizing_sample, h, max_size, device)\n", + " else:\n", + " raise ValueError(\n", + " f'Unknown resizing method: \"{mode}\". Supported methods are '\n", + " '\"interpolation\" or \"sampling\"'\n", + " )\n", + "\n", + "\n", + "def _get_scaling(intensity_scale: bool):\n", + " return intensity_scaling if intensity_scale else no_scaling \n", + "\n", + "\n", + "def _get_kernel(\n", + " method: str, h: int, sigma: float, boundary: Sequence[int]\n", + ") -> _KernelMethod: \n", + " if method == 'thresholding':\n", + " eps = (boundary[1] - boundary[0]) / (2 * h)\n", + " return partial(thresholding_kernel, h, eps)\n", + " elif method == 'RBF':\n", + " inv_sigma_sq = 1 / sigma ** 2\n", + " return partial(rbf_kernel, h, inv_sigma_sq)\n", + " elif method == 'inverse-quadratic':\n", + " inv_sigma_sq = 1 / sigma ** 2\n", + " return partial(inverse_quadratic_kernel, h, inv_sigma_sq)\n", + " else:\n", + " raise ValueError(\n", + " f'Unknown kernel method: \"{method}\". Supported methods are '\n", + " '\"thresholding\", \"RBF\", or \"inverse-quadratic\".'\n", + " )\n", + "\n", + "class HistBlock(nn.Module):\n", + " def __init__(\n", + " self, h: int, insz: int, resizing: str, method: str, sigma: float,\n", + " intensity_scale: str, hist_boundary: List[int], device: _Device\n", + " ) -> None:\n", + " super().__init__()\n", + " hist_boundary.sort()\n", + " start, end = hist_boundary[:2]\n", + " self.h = h\n", + " self.device = torch.device(device)\n", + " self.resize = _get_resizing(resizing, h, insz, self.device)\n", + " self.kernel = _get_kernel(method, h, sigma, hist_boundary)\n", + " self.scaling = _get_scaling(intensity_scale)\n", + " self.delta = torch.linspace(\n", + " start, end, steps=h, device=self.device, dtype=torch.float32\n", + " ).unsqueeze(dim=0)\n", + "\n", + " def forward(self, _: torch.Tensor) -> torch.Tensor:\n", + " raise NotImplementedError()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Refactored Histogram Blocks\n", + "\n", + "With all the pieces in place, we can modify the original histogram blocks to make use of our common components.\n", + "\n", + "The RGB-uv block can be simplified by observing that the difference calculations for the channels only differ in\n", + "tensor indexing. We can extract the calculation into a function and pass these indexes as arguments to the difference\n", + "calculation.\n", + "\n", + "We can also speed up historgram normalization by only summing elements once. This will cause a little more numeric\n", + "instability due to loss of significance with unsorted tensor values. Performance is a bit better, though, and the\n", + "differences should be minimal, but we get back to later in the validation part.\n", + "\n", + "Just from personal prefence and for sake of consistency, member functions are used on tensors where appropriate\n", + "(type hints would also help a lot, but I didn't want to change too much)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class RGBuvHistBlock(HistBlock):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=True,\n", + " hist_boundary=None, green_only=False, device='cuda'):\n", + " super().__init__(\n", + " h, insz, resizing, method, sigma, intensity_scale,\n", + " hist_boundary or [-3, 3], device\n", + " )\n", + " self.green_only = green_only\n", + "\n", + " def forward(self, x):\n", + " x_sampled = self.resize(x.clamp(0, 1))\n", + "\n", + " N = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " C = 1 + int(not self.green_only) * 2\n", + " hists = torch.zeros(N, C, self.h, self.h, device=self.device)\n", + " for n in range(N):\n", + " Ix = X[n].reshape(3, -1).t()\n", + " Iy = self.scaling(Ix)\n", + " if not self.green_only:\n", + " Du, Dv = self._diff_uv(Ix, i=0, j=1, k=2)\n", + " a = (Iy * Du).t()\n", + " hists[n, 0, :, :] = torch.mm(a, Dv)\n", + "\n", + " Du, Dv = self._diff_uv(Ix, i=1, j=0, k=2)\n", + " a = (Iy * Du).t()\n", + " hists[n, int(not self.green_only), :, :] = torch.mm(a, Dv)\n", + "\n", + " if not self.green_only:\n", + " Du, Dv = self._diff_uv(Ix, i=2, j=0, k=1)\n", + " a = (Iy * Du).t()\n", + " hists[n, 2, :, :] = torch.mm(a, Dv)\n", + "\n", + " # normalization\n", + " norm = hists.view(-1, C * self.h * self.h).sum(dim=1).view(-1, 1, 1, 1)\n", + " hists_normalized = hists / (norm + EPS)\n", + "\n", + " return hists_normalized\n", + "\n", + " def _diff_uv(self, X: torch.Tensor, i: int, j: int, k: int):\n", + " U = ((X[:, i] + EPS).log() - (X[:, j] + EPS).log()).unsqueeze(dim=1)\n", + " V = ((X[:, i] + EPS).log() - (X[:, k] + EPS).log()).unsqueeze(dim=1)\n", + " Du = (U - self.delta).abs()\n", + " Dv = (V - self.delta).abs()\n", + " Du = self.kernel(Du)\n", + " Dv = self.kernel(Dv)\n", + " return Du, Dv\n", + "\n", + "class rgChromaHistBlock(HistBlock):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=False,\n", + " hist_boundary=None, device='cuda'):\n", + " super().__init__(\n", + " h, insz, resizing, method, sigma, intensity_scale, \n", + " hist_boundary or [0, 1], device\n", + " )\n", + "\n", + " def forward(self, x):\n", + " x_sampled = self.resize(x.clamp(0, 1))\n", + "\n", + " N = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " hists = torch.zeros(N, 1, self.h, self.h, device=self.device)\n", + " for n in range(N):\n", + " Ix = X[n].reshape(3, -1).t()\n", + " Inorm = Ix.sum(dim=-1) + EPS\n", + " Ir = (Ix[:, 0] / Inorm).unsqueeze(dim=1)\n", + " Ig = (Ix[:, 1] / Inorm).unsqueeze(dim=1)\n", + "\n", + " diff_r = (Ir - self.delta).abs()\n", + " diff_g = (Ig - self.delta).abs()\n", + " diff_r = self.kernel(diff_r)\n", + " diff_g = self.kernel(diff_g)\n", + " Iy = self.scaling(Ix)\n", + " a = torch.t(Iy * diff_r)\n", + "\n", + " hists[n, 0, :, :] = torch.mm(a, diff_g)\n", + "\n", + " # normalization\n", + " norm = hists.view(-1, self.h * self.h).sum(dim=1).view(-1, 1, 1, 1) + EPS\n", + " hists_normalized = hists / norm\n", + "\n", + " return hists_normalized\n", + "\n", + "class LabHistBlock(HistBlock):\n", + " def __init__(self, h=64, insz=150, resizing='interpolation',\n", + " method='inverse-quadratic', sigma=0.02, intensity_scale=False,\n", + " hist_boundary=None, device='cuda'):\n", + " super().__init__(\n", + " h, insz, resizing, method, sigma, intensity_scale, \n", + " hist_boundary or [0, 1], device\n", + " )\n", + "\n", + " def forward(self, x):\n", + " x_sampled = self.resize(x.clamp(0, 1))\n", + "\n", + " N = x_sampled.shape[0] # size of mini-batch\n", + " if x_sampled.shape[1] > 3:\n", + " x_sampled = x_sampled[:, :3, :, :]\n", + " X = torch.unbind(x_sampled, dim=0)\n", + " hists = torch.zeros(N, 1, self.h, self.h, device=self.device)\n", + " for n in range(N):\n", + " Ix = X[n].reshape(3, -1).t()\n", + "\n", + " Ia = Ix[:, 1].unsqueeze(dim=1)\n", + " Ib = Ix[:, 2].unsqueeze(dim=1)\n", + "\n", + " diff_a = (Ia - self.delta).abs()\n", + " diff_b = (Ib - self.delta).abd()\n", + "\n", + " diff_a = self.kernel(diff_a)\n", + " diff_b = self.kernel(diff_b)\n", + " Iy = self.scaling(Ix)\n", + " a = torch.t(Iy * diff_a)\n", + "\n", + " hists[n, 0, :, :] = torch.mm(a, diff_b)\n", + "\n", + " # normalization\n", + " norm = hists.view(-1, self.h * self.h).sum(dim=1).view(-1, 1, 1, 1) + EPS\n", + " hists_normalized = hists / norm\n", + "\n", + " return hists_normalized" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Validation\n", + "\n", + "In order to validate our work, let's run A-B-tests for each possible parameter combination.\n", + "We can keep most values at their default, but resizing, sampling, and intensity scaling options should be thoroughly tested.\n", + "We can define a `dict` that holds all test cases - histogram block classes and the tested parameters.\n", + "\n", + "Next we run both reference and refactored models with a batch of randomly generated images and compare the results.\n", + "For the comparison, we use the *arctangent absolute percentage error* (AAPE) as proposed in\n", + "\n", + " Sungil Kim, Heeyoung Kim,\n", + " \"A new metric of absolute percentage error for intermittent demand forecasts\",\n", + " International Journal of Forecasting,\n", + " Volume 32, Issue 3,\n", + " 2016,\n", + " Pages 669-679,\n", + " https://doi.org/10.1016/j.ijforecast.2015.12.003\n", + "\n", + "with the AAPE rescaled from its original [0, ½π] range to [0, 100] to obtain more readable percentages." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "DEVICE = 'cuda' # device to run the tests on (e.g. 'cuda' or 'cpu')\n", + "BATCHES = 8 # how many samples per mini-batch\n", + "SAMPLE_SIZE = 256 # sample image size in pixels\n", + "ERR_THRESHOLD = 0.05 # validation error threshold in percent\n", + "RANDOM_SEED = 4793 # for reproducibility we seed the rng, use torch.random.seed() instead to explore\n", + " # the selected seed produces a more colourful output ;)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from dataclasses import dataclass, field\n", + "from itertools import product, repeat\n", + "import matplotlib.pyplot as plt\n", + "\n", + "\n", + "RESIZING_VALS = ['interpolation', 'sampling']\n", + "BOOL_VALS = [False, True]\n", + "METHOD_VALS = ['thresholding', 'RBF', 'inverse-quadratic']\n", + "TESTS = {\n", + " 'RGB-uv': {\n", + " 'A': OriginalRGBuvHistBlock, 'B': RGBuvHistBlock,\n", + " 'params': {'resizing': RESIZING_VALS, 'method': METHOD_VALS, 'intensity_scale': BOOL_VALS, 'green_only': BOOL_VALS}\n", + " },\n", + " 'rg-chroma': {\n", + " 'A': OriginalrgChromaHistBlock, 'B': rgChromaHistBlock,\n", + " 'params': {'resizing': RESIZING_VALS, 'method': METHOD_VALS, 'intensity_scale': BOOL_VALS}\n", + " },\n", + " 'Lab': {\n", + " 'A': OriginalrgChromaHistBlock, 'B': rgChromaHistBlock,\n", + " 'params': {'resizing': RESIZING_VALS, 'method': METHOD_VALS, 'intensity_scale': BOOL_VALS}\n", + " }\n", + "}\n", + "\n", + "def _to_dict(names, values):\n", + " return {key: val for key, val in zip(names, values)}\n", + "\n", + "def _param_info(p):\n", + " if isinstance(p[1], bool):\n", + " return f\"{'+' if p[1] else '-'}{p[0][:3].upper()}\"\n", + " return p[1][:4].upper()\n", + "\n", + "@dataclass\n", + "class Result:\n", + " test: str\n", + " params: str\n", + " min_err: float\n", + " max_err: float\n", + " avg_err: float\n", + " median_err: float\n", + " outcome: str = field(init=False)\n", + "\n", + " def __post_init__(self):\n", + " valid = self.avg_err < ERR_THRESHOLD\n", + " self.outcome = 'PASS' if valid else 'FAILED'\n", + "\n", + "def _to_row(result: Result):\n", + " return [\n", + " result.test, result.params, f'{result.min_err:.6f}', f'{result.max_err:.6f}',\n", + " f'{result.avg_err:.6f}', f'{result.median_err:.6f}', result.outcome\n", + " ]\n", + "\n", + "def _aape(X, Y):\n", + " eps = torch.full_like(input=X, fill_value=1.1921e-7).float()\n", + " phi = ((X - Y).abs() / torch.maximum(X.abs(), eps)).arctan()\n", + " return phi * (2 / torch.pi) * 100\n", + "\n", + "torch.random.manual_seed(RANDOM_SEED)\n", + "samples = torch.randint(low=0, high=256, size=(BATCHES, 3, SAMPLE_SIZE, SAMPLE_SIZE))\n", + "samples = (samples / 255.).float().to(DEVICE)\n", + "\n", + "def _validate(name, A_, B_, model_args):\n", + " A = A_(**model_args).eval().to(DEVICE)\n", + " B = B_(**model_args).eval().to(DEVICE)\n", + " with torch.no_grad():\n", + " Ay = A(samples)\n", + " By = B(samples)\n", + " err = _aape(Ay, By)\n", + " params = ','.join(map(_param_info, model_args.items()))\n", + " return Result(name, params, err.min().item(), err.max().item(), err.mean().item(), err.median().item())\n", + "\n", + "validation_results: List[Result] = []\n", + "for name, args in TESTS.items():\n", + " A, B, params = args['A'], args['B'], args['params']\n", + " test_params = map(_to_dict, repeat(params), product(*params.values()))\n", + " items = map(_validate, repeat(name), repeat(A), repeat(B), test_params)\n", + " validation_results.extend(items)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's visualize the validation results next - a table will do for now.\n", + "\n", + "The tested parameter combinations are listed by their first four letters in caps. Boolean flags are indicated by -*FLAG*\n", + "if the parameter is `False` and +*FLAG* if the parameter is set to `True`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if any(filter(lambda i: i.outcome == 'FAILED', validation_results)):\n", + " print(f'Got some failures; RANDOM_SEED to reproduce: {RANDOM_SEED}')\n", + "\n", + "# plot the results\n", + "fig, ax = plt.subplots(1, 1, figsize=(10, 10))\n", + "col_labels = ['HIST BLOCK', 'PARAMS', 'MIN ERR %', 'MAX ERR %', 'AVG ERR %', 'MEDIAN ERR %', 'RESULT']\n", + "ax.axis('tight')\n", + "ax.axis('off')\n", + "tbl = ax.table(\n", + " cellText=list(map(_to_row, validation_results)), colLabels=col_labels, loc='center',\n", + " colColours=['slategrey']*7\n", + ")\n", + "# format table\n", + "_ = list(map(lambda col: tbl[(0, col)].set_text_props(fontweight='bold'), range(7)))\n", + "for row, item in enumerate(validation_results):\n", + " tbl[(row+1, 0)].set_facecolor('lightsteelblue')\n", + " if item.min_err > ERR_THRESHOLD:\n", + " tbl[(row+1, 2)].set_facecolor('darkorange')\n", + " if item.max_err > ERR_THRESHOLD:\n", + " tbl[(row+1, 3)].set_facecolor('darkorange')\n", + " if item.avg_err > ERR_THRESHOLD:\n", + " tbl[(row+1, 4)].set_facecolor('darkorange')\n", + " if item.median_err > ERR_THRESHOLD:\n", + " tbl[(row+1, 5)].set_facecolor('darkorange')\n", + " tbl[(row+1, 6)].set_facecolor('g' if item.outcome=='PASS' else 'r')\n", + " tbl[(row+1, 6)].set_text_props(fontweight='bold')\n", + "tbl.auto_set_font_size(False)\n", + "tbl.set_fontsize(10)\n", + "tbl.scale(2, 2)\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Validation Result Discussion\n", + "\n", + "The chosen criterion for passing validation is a somewhat arbitrary but low average error threshold\n", + "(across all samples in the mini-batch).\n", + "The reported maximum error might exceed this threshold (I've seen values as high as 10%), but we\n", + "need to keep two things in mind here:\n", + "\n", + "First, the maximum error refers to a single bucket value in the histogram. A single outlier in a\n", + "64x64 histogram shouldn't account for a validation failure.\n", + "\n", + "Secondly, I noticed that the deviations occur in conjunction with interpolation only. Match the\n", + "sample dimensions (`SAMPLE_SIZE`) with the maximum histogram input size (`insz`) and the errors\n", + "go away. Since the resize function is identical to the original version in every way, I'm a bit at a\n", + "loss as to why that is.\n", + "\n", + "## Benchmark\n", + "\n", + "With that out of the way, let's get some performance numbers to see whether we actually improved things.\n", + "We assess the performance differences by running each histogram block a given number of times on a\n", + "mini-batch of random samples. Inference time is measured and results are plotted to a diagram." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ITERATIONS = 100 # number of benchmark passes per model\n", + "BATCHES = 16 # mini-batch size\n", + "SAMPLE_SIZE = 256 # sample image size for benchmarking\n", + "DEVICE = 'cuda' # computation device to run the benchmark on (e.g. 'cuda' or 'cpu')\n", + "RANDOM_SEED = 0 # random seed for producing sample data (again, for reproducibility)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from tqdm import tqdm\n", + "from time import perf_counter\n", + "from typing import Dict\n", + "import numpy as np\n", + "\n", + "\n", + "BASELINE = [(key, val['A']) for key, val in TESTS.items()]\n", + "REFACTORED = [(key, val['B']) for key, val in TESTS.items()]\n", + "\n", + "torch.random.manual_seed(RANDOM_SEED)\n", + "\n", + "\n", + "def _gen_minibatch():\n", + " while True:\n", + " X = torch.randint(low=0, high=256, size=(BATCHES, 3, SAMPLE_SIZE, SAMPLE_SIZE))\n", + " yield (X / 255.).float().to(DEVICE)\n", + "\n", + "\n", + "def _benchmark(model, sample):\n", + " start = perf_counter()\n", + " _ = model(sample)\n", + " return (perf_counter() - start) * 1_000\n", + "\n", + "\n", + "baseline_results: Dict[str, float] = { }\n", + "for name, Model in BASELINE:\n", + " with torch.no_grad():\n", + " models = repeat(Model().eval().to(DEVICE), times=ITERATIONS)\n", + " runs = tqdm(models, total=ITERATIONS, desc=f'Benchmarking baseline {name}')\n", + " baseline_results[name] = list(map(_benchmark, runs, _gen_minibatch()))\n", + "\n", + "refactored_results: Dict[str, float] = { }\n", + "for name, Model in REFACTORED:\n", + " with torch.no_grad():\n", + " models = repeat(Model().eval().to(DEVICE), times=ITERATIONS)\n", + " runs = tqdm(models, total=ITERATIONS, desc=f'Benchmarking refactored {name}')\n", + " refactored_results[name] = list(map(_benchmark, runs, _gen_minibatch()))\n", + "\n", + "a = np.array(list(baseline_results.values()))\n", + "a_mins = a.min(axis=1)\n", + "a_maxes = a.max(axis=1)\n", + "a_means = a.mean(axis=1)\n", + "a_std = a.std(axis=1)\n", + "\n", + "b = np.array(list(refactored_results.values()))\n", + "b_mins = b.min(axis=1)\n", + "b_maxes = b.max(axis=1)\n", + "b_means = b.mean(axis=1)\n", + "b_std = b.std(axis=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With our data collected, let's print the mean relative execution time differences and plot some charts. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from itertools import chain\n", + "\n", + "N = len(baseline_results)\n", + "\n", + "for name, speedup in zip(TESTS, a_means / b_means):\n", + " print(f'Refactored {name}: {speedup:.1f}x faster on average')\n", + "\n", + "fig, ax = plt.subplots(3, 1, figsize=(12, 18))\n", + "\n", + "tick_labels = [(f'{n} (baseline)', f'{n} (refactored)') for n in TESTS]\n", + "tick_labels = list(chain(*tick_labels))\n", + "ax[0].set_xticks(np.arange(2*N), labels=tick_labels, minor=False)\n", + "ax[0].errorbar(np.arange(N) * 2, a_means, a_std, fmt='_k', lw=3, ms=11, capsize=3)\n", + "ax[0].errorbar(np.arange(N) * 2, a_means, [a_means - a_mins, a_maxes - a_means], fmt='.k', ecolor='grey', lw=1, capsize=3)\n", + "ax[0].errorbar(np.arange(N) * 2 + 1, b_means, b_std, fmt='_b', lw=3, ms=11, capsize=3)\n", + "ax[0].errorbar(np.arange(N) * 2 + 1, b_means, [b_means - b_mins, b_maxes - b_means], fmt='.k', ecolor='lightsteelblue', lw=1, capsize=3)\n", + "ax[0].set_title(f'Benchmark results for {ITERATIONS} iterations and mini-batch size of {BATCHES}')\n", + "ax[0].set_ylabel('Iteration time in ms')\n", + "\n", + "labels = [name for name in TESTS]\n", + "width = 0.35\n", + "ax[1].bar(labels, a_means, width, yerr=a_std, label='Baseline', capsize=3)\n", + "ax[1].bar(labels, b_means, width, yerr=b_std, label='Refactored', capsize=3)\n", + "ax[1].set_ylabel('Iteration in ms')\n", + "ax[1].set_title('Execution time difference')\n", + "ax[1].legend()\n", + "\n", + "\n", + "a_mean_its = 1_000 / a_means\n", + "b_mean_its = 1_000 / b_means\n", + "a_it_std = (1_000 / a).std(axis=1)\n", + "b_it_std = (1_000 / b).std(axis=1)\n", + "width = 0.35\n", + "ax[2].bar(labels, a_mean_its, width, yerr=a_it_std, label='Baseline', capsize=3)\n", + "ax[2].bar(labels, b_mean_its, width, yerr=b_it_std, label='Refactored', bottom=a_mean_its, capsize=3)\n", + "ax[2].set_ylabel('Iteration per second')\n", + "ax[2].set_title('Performance difference')\n", + "ax[2].legend()\n", + "\n", + "plt.show()" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "57164f1c0f1b3bb0c5f993d1ba49aa53e928357509b375fbcec01d49bae2dae4" + }, + "kernelspec": { + "display_name": "Python 3.9.12 ('pytorch')", + "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.12" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} From d581d3b6047e31d26d40477e8fe01423edbb3ede Mon Sep 17 00:00:00 2001 From: patlevin Date: Tue, 31 May 2022 11:33:43 +0200 Subject: [PATCH 2/2] add support for outdated matplotlib versions --- Histogram_refactoring.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Histogram_refactoring.ipynb b/Histogram_refactoring.ipynb index 7fbc10b..a1a725d 100644 --- a/Histogram_refactoring.ipynb +++ b/Histogram_refactoring.ipynb @@ -1009,11 +1009,12 @@ "\n", "tick_labels = [(f'{n} (baseline)', f'{n} (refactored)') for n in TESTS]\n", "tick_labels = list(chain(*tick_labels))\n", - "ax[0].set_xticks(np.arange(2*N), labels=tick_labels, minor=False)\n", "ax[0].errorbar(np.arange(N) * 2, a_means, a_std, fmt='_k', lw=3, ms=11, capsize=3)\n", "ax[0].errorbar(np.arange(N) * 2, a_means, [a_means - a_mins, a_maxes - a_means], fmt='.k', ecolor='grey', lw=1, capsize=3)\n", "ax[0].errorbar(np.arange(N) * 2 + 1, b_means, b_std, fmt='_b', lw=3, ms=11, capsize=3)\n", "ax[0].errorbar(np.arange(N) * 2 + 1, b_means, [b_means - b_mins, b_maxes - b_means], fmt='.k', ecolor='lightsteelblue', lw=1, capsize=3)\n", + "ax[0].set_xticks(np.arange(2*N), minor=False)\n", + "ax[0].set_xtick_labels(tick_labels)\n", "ax[0].set_title(f'Benchmark results for {ITERATIONS} iterations and mini-batch size of {BATCHES}')\n", "ax[0].set_ylabel('Iteration time in ms')\n", "\n",