Skip to content

Instantly share code, notes, and snippets.

@BeniaminK
Last active January 10, 2020 02:15
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 BeniaminK/d21ebbb47931924ff64756df1071c3aa to your computer and use it in GitHub Desktop.
Save BeniaminK/d21ebbb47931924ff64756df1071c3aa to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch\n",
"import matplotlib.pyplot as plt\n",
"import torch.nn.functional as F"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N = 13\n",
"C = 3\n",
"Hin = 512\n",
"Win = 256\n",
"Hout = 255\n",
"Wout = 128\n",
"# input = np.arange(0, N * C * Hin * Win, dtype=np.float32).reshape(N, C, Win, Hin).transpose([0, 1, 3, 2])\n",
"input = np.arange(0, N * C * Hin * Win, dtype=np.float32).reshape(N, C, Hin, Win)\n",
"input.shape, input[0, 0, :, 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Sxx = input[0, 0, :, :]\n",
"plt.pcolormesh(np.arange(Win), np.arange(Hin), Sxx)\n",
"plt.ylabel('Frequency [samples]')\n",
"plt.xlabel('Time [samples]')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(-1, 1, Wout)\n",
"y = np.linspace(-1, 1, Hout)\n",
"pow_y = (np.power(21, (y + 1) / 2) - 11) / 10\n",
"log_y = 2 * np.log(10 * y + 11) / np.log(21) - 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(y, pow_y, \"b\", y, log_y , \"g\")\n",
"plt.show()\n",
"plt.plot(x, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# xx, yy = np.meshgrid(np.arange(0, 512), np.arange(0, 256), indexing='xy')\n",
"xx, yy = np.meshgrid(x, pow_y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grid = np.empty(shape=(N, Hout, Wout, 2), dtype=np.float32)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grid[0, :, :, 0] = xx\n",
"grid[0, :, :, 1] = yy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grid[0, :, 0, 1] # changes in H axis\n",
"# grid[0, 0, :, 0] # changes in W axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out = F.grid_sample(torch.as_tensor(input), torch.as_tensor(grid), mode='nearest')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out[0, 0, :, 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Sxx = out[0, 0, :, :]\n",
"plt.pcolormesh(np.arange(Wout), np.arange(Hout), Sxx)\n",
"plt.ylabel('Frequency [samples]')\n",
"plt.xlabel('Time [samples]')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out = F.grid_sample(torch.as_tensor(input), torch.as_tensor(grid), mode='bilinear')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(out[0, 0, :, 2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# normalize ix, iy from [-1, 1] to [0, IH-1] & [0, IW-1]\n",
"\n",
"Nval = 0\n",
"Cval = 0\n",
"Hval = 10\n",
"Wval = 2\n",
"print(grid[0, Hval, Wval, 1])\n",
"print(grid[0, Hval, Wval, 0])\n",
"\n",
"iy = grid[0, Hval, Wval, 1]\n",
"ix = grid[0, Hval, Wval, 0]\n",
"ix = ((ix + 1) / 2) * (Win-1);\n",
"iy = ((iy + 1) / 2) * (Hin-1);\n",
"\n",
"print(iy)\n",
"print(ix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get NE, NW, SE, SW pixel values from (x, y)\n",
"ix_nw = int(np.floor(ix))\n",
"iy_nw = int(np.floor(iy))\n",
"ix_ne = int(ix_nw + 1)\n",
"iy_ne = int(iy_nw)\n",
"ix_sw = int(ix_nw)\n",
"iy_sw = int(iy_nw + 1)\n",
"ix_se = int(ix_nw + 1)\n",
"iy_se = int(iy_nw + 1)\n",
"\n",
"print((ix_nw, iy_nw), (ix_ne, iy_ne), (ix_sw, iy_sw), (ix_se, iy_se))\n",
"\n",
"pts = [(ix_nw, iy_nw), (ix_ne, iy_ne), (ix_sw, iy_sw), (ix_se, iy_se)]\n",
"plt.scatter(*zip(*pts))\n",
"plt.scatter([ix], [iy], c=\"r\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get surfaces to each neighbor:\n",
"nw = (ix_se - ix) * (iy_se - iy);\n",
"ne = (ix - ix_sw) * (iy_sw - iy);\n",
"sw = (ix_ne - ix) * (iy - iy_ne);\n",
"se = (ix - ix_nw) * (iy - iy_nw);\n",
"\n",
"print(nw, ne, sw, se)\n",
"print(np.sum([nw, ne, sw, se]))\n",
"\n",
"# clip coordinates when border mode else pad with zeros"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate bilinear weighted pixel value and set output pixel\n",
"\n",
"# this works for each C in the input independently\n",
"nw_val = input[Nval, Cval, iy_nw, ix_nw]\n",
"ne_val = input[Nval, Cval, iy_ne, ix_ne]\n",
"sw_val = input[Nval, Cval, iy_sw, ix_sw]\n",
"se_val = input[Nval, Cval, iy_se, ix_se]\n",
"print(\"Neighbour vals: \", nw_val, ne_val, sw_val, se_val)\n",
"out_val = nw_val * nw + ne_val * ne + sw_val * sw + se_val * se;\n",
"print(out_val)\n",
"print(out[Nval, Cval, Hval, Wval])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Sxx = out[0, 0, :, :]\n",
"plt.pcolormesh(np.arange(Wout), np.arange(Hout), Sxx)\n",
"plt.ylabel('Frequency [samples]')\n",
"plt.xlabel('Time [samples]')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment