Skip to content

Commit

Permalink
final result finished
Browse files Browse the repository at this point in the history
  • Loading branch information
rfgzuid committed Jun 14, 2024
1 parent 28b1952 commit 912168e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 82 deletions.
161 changes: 82 additions & 79 deletions experiments/PIV Test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-06-13T20:18:03.586847500Z",
"start_time": "2024-06-13T20:18:03.436659Z"
"end_time": "2024-06-13T22:15:25.378631Z",
"start_time": "2024-06-13T22:15:25.221900100Z"
}
},
"source": [
Expand Down Expand Up @@ -52,13 +52,13 @@
]
}
],
"execution_count": 55
"execution_count": 123
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T18:53:25.284089300Z",
"start_time": "2024-06-13T18:53:25.141785Z"
"end_time": "2024-06-13T22:15:26.079934900Z",
"start_time": "2024-06-13T22:15:25.931102100Z"
}
},
"cell_type": "code",
Expand All @@ -81,7 +81,7 @@
]
}
],
"execution_count": 2
"execution_count": 124
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -113,12 +113,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T18:54:08.690264600Z",
"start_time": "2024-06-13T18:54:08.468712300Z"
"end_time": "2024-06-13T22:02:03.997281200Z",
"start_time": "2024-06-13T22:02:03.828594600Z"
}
},
"id": "e415162f1d18b88f",
"execution_count": 8
"execution_count": 103
},
{
"cell_type": "code",
Expand Down Expand Up @@ -189,12 +189,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T19:19:55.659681500Z",
"start_time": "2024-06-13T19:19:55.484374Z"
"end_time": "2024-06-13T22:15:32.299508800Z",
"start_time": "2024-06-13T22:15:32.130185500Z"
}
},
"id": "dbd62634a9d3b488",
"execution_count": 24
"execution_count": 125
},
{
"cell_type": "code",
Expand Down Expand Up @@ -245,12 +245,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T20:17:37.844020500Z",
"start_time": "2024-06-13T20:17:37.706941200Z"
"end_time": "2024-06-13T22:15:36.257416600Z",
"start_time": "2024-06-13T22:15:36.115107300Z"
}
},
"id": "c5af121528197517",
"execution_count": 52
"execution_count": 126
},
{
"cell_type": "code",
Expand All @@ -259,21 +259,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 4/2675 [00:16<2:59:32, 4.03s/it]\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mKeyboardInterrupt\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[53], line 4\u001B[0m\n\u001B[0;32m 1\u001B[0m idx \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m0\u001B[39m\n\u001B[0;32m 2\u001B[0m u3_avg \u001B[38;5;241m=\u001B[39m torch\u001B[38;5;241m.\u001B[39mzeros((\u001B[38;5;28mlen\u001B[39m(ctf_gen\u001B[38;5;241m.\u001B[39mdataset),))\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28;43;01mfor\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mres\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;129;43;01min\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mctf_gen\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m:\u001B[49m\n\u001B[0;32m 5\u001B[0m \u001B[43m \u001B[49m\u001B[43mx\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43my\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mu\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mv\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mres\u001B[49m\n\u001B[0;32m 6\u001B[0m \u001B[43m \u001B[49m\u001B[43mu3_avg\u001B[49m\u001B[43m[\u001B[49m\u001B[43midx\u001B[49m\u001B[43m]\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mtorch\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmean\u001B[49m\u001B[43m(\u001B[49m\u001B[43mu\u001B[49m\u001B[43m)\u001B[49m\n",
"File \u001B[1;32m~\\PycharmProjects\\LowCostPIV\\src\\SIV_library\\advanced.py:81\u001B[0m, in \u001B[0;36mCTF.__call__\u001B[1;34m(self)\u001B[0m\n\u001B[0;32m 78\u001B[0m a \u001B[38;5;241m=\u001B[39m t(a[\u001B[38;5;28;01mNone\u001B[39;00m, :, :, :])\u001B[38;5;241m.\u001B[39msqueeze(\u001B[38;5;241m0\u001B[39m) \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124ma\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;129;01min\u001B[39;00m t\u001B[38;5;241m.\u001B[39mapply_to \u001B[38;5;28;01melse\u001B[39;00m a\n\u001B[0;32m 79\u001B[0m b \u001B[38;5;241m=\u001B[39m t(b[\u001B[38;5;28;01mNone\u001B[39;00m, :, :, :])\u001B[38;5;241m.\u001B[39msqueeze(\u001B[38;5;241m0\u001B[39m) \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mb\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;129;01min\u001B[39;00m t\u001B[38;5;241m.\u001B[39mapply_to \u001B[38;5;28;01melse\u001B[39;00m b\n\u001B[1;32m---> 81\u001B[0m du, dv \u001B[38;5;241m=\u001B[39m \u001B[43moptical_flow\u001B[49m\u001B[43m(\u001B[49m\u001B[43ma\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mb\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43malpha\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mnum_iter\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43meps\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 82\u001B[0m u, v \u001B[38;5;241m=\u001B[39m (u \u001B[38;5;241m+\u001B[39m du, v \u001B[38;5;241m+\u001B[39m dv) \u001B[38;5;28;01mif\u001B[39;00m idx \u001B[38;5;241m!=\u001B[39m \u001B[38;5;241m0\u001B[39m \u001B[38;5;28;01melse\u001B[39;00m (du, dv)\n\u001B[0;32m 84\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m idx \u001B[38;5;241m<\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mnum_passes \u001B[38;5;241m-\u001B[39m \u001B[38;5;241m1\u001B[39m:\n",
"File \u001B[1;32m~\\PycharmProjects\\LowCostPIV\\src\\SIV_library\\optical_flow.py:31\u001B[0m, in \u001B[0;36moptical_flow\u001B[1;34m(img1, img2, alpha, num_iter, eps)\u001B[0m\n\u001B[0;32m 28\u001B[0m u_avg \u001B[38;5;241m=\u001B[39m conv2d(u, avg_kernel, padding\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m)\n\u001B[0;32m 29\u001B[0m v_avg \u001B[38;5;241m=\u001B[39m conv2d(v, avg_kernel, padding\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m)\n\u001B[1;32m---> 31\u001B[0m der \u001B[38;5;241m=\u001B[39m (Ix \u001B[38;5;241m*\u001B[39m u_avg \u001B[38;5;241m+\u001B[39m Iy \u001B[38;5;241m*\u001B[39m v_avg \u001B[38;5;241m+\u001B[39m It) \u001B[38;5;241m/\u001B[39m (alpha \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39m \u001B[38;5;241m2\u001B[39m \u001B[38;5;241m+\u001B[39m \u001B[43mIx\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43m \u001B[49m\u001B[38;5;241;43m2\u001B[39;49m \u001B[38;5;241m+\u001B[39m Iy \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39m \u001B[38;5;241m2\u001B[39m)\n\u001B[0;32m 33\u001B[0m u_new \u001B[38;5;241m=\u001B[39m u_avg \u001B[38;5;241m-\u001B[39m Ix \u001B[38;5;241m*\u001B[39m der\n\u001B[0;32m 34\u001B[0m v_new \u001B[38;5;241m=\u001B[39m v_avg \u001B[38;5;241m-\u001B[39m Iy \u001B[38;5;241m*\u001B[39m der\n",
"File \u001B[1;32m~\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\torch\\_tensor.py:34\u001B[0m, in \u001B[0;36m_handle_torch_function_and_wrap_type_error_to_not_implemented.<locals>.wrapped\u001B[1;34m(*args, **kwargs)\u001B[0m\n\u001B[0;32m 31\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_handle_torch_function_and_wrap_type_error_to_not_implemented\u001B[39m(f):\n\u001B[0;32m 32\u001B[0m assigned \u001B[38;5;241m=\u001B[39m functools\u001B[38;5;241m.\u001B[39mWRAPPER_ASSIGNMENTS\n\u001B[1;32m---> 34\u001B[0m \u001B[38;5;129m@functools\u001B[39m\u001B[38;5;241m.\u001B[39mwraps(f, assigned\u001B[38;5;241m=\u001B[39massigned)\n\u001B[0;32m 35\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mwrapped\u001B[39m(\u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs):\n\u001B[0;32m 36\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m 37\u001B[0m \u001B[38;5;66;03m# See https://github.com/pytorch/pytorch/issues/75462\u001B[39;00m\n\u001B[0;32m 38\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m has_torch_function(args):\n",
"\u001B[1;31mKeyboardInterrupt\u001B[0m: "
"100%|██████████| 2675/2675 [2:11:50<00:00, 2.96s/it] \n"
]
}
],
Expand All @@ -289,12 +275,29 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T20:17:56.659018300Z",
"start_time": "2024-06-13T20:17:40.336726200Z"
"end_time": "2024-06-14T00:30:07.351571700Z",
"start_time": "2024-06-13T22:18:16.769419200Z"
}
},
"id": "9c7adcc2a153f094",
"execution_count": 53
"execution_count": 128
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import numpy as np\n",
"np.save(\"../experiments/ctf.npy\", u3_avg.numpy())"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-14T06:08:21.008669300Z",
"start_time": "2024-06-14T06:08:20.866403900Z"
}
},
"id": "253f31033c7d4bf4",
"execution_count": 138
},
{
"cell_type": "markdown",
Expand All @@ -315,12 +318,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T20:18:59.018229500Z",
"start_time": "2024-06-13T20:18:58.195499Z"
"end_time": "2024-06-13T22:02:13.462268Z",
"start_time": "2024-06-13T22:02:13.313259800Z"
}
},
"id": "d4328ad8b24e27eb",
"execution_count": 59
"execution_count": 104
},
{
"cell_type": "code",
Expand All @@ -329,7 +332,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 2675/2675 [57:10<00:00, 1.28s/it] \n"
"100%|██████████| 19/19 [00:22<00:00, 1.18s/it]\n"
]
}
],
Expand All @@ -345,12 +348,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T21:33:10.573589100Z",
"start_time": "2024-06-13T20:35:55.882428700Z"
"end_time": "2024-06-13T22:03:39.360211500Z",
"start_time": "2024-06-13T22:03:12.005716500Z"
}
},
"id": "4e85e0ae2eac11fe",
"execution_count": 79
"execution_count": 107
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -446,12 +449,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T21:33:19.887244500Z",
"start_time": "2024-06-13T21:33:19.747186Z"
"end_time": "2024-06-14T06:00:46.979169200Z",
"start_time": "2024-06-14T06:00:46.794205700Z"
}
},
"id": "43dd9a881d3bbd1b",
"execution_count": 82
"execution_count": 130
},
{
"cell_type": "code",
Expand All @@ -476,18 +479,18 @@
"cell_type": "code",
"outputs": [],
"source": [
"u1, u2, u4 = process(-u1_avg), process(-u2_avg), process(-u4_avg) # u3 = process(-u3_avg)\n",
"u1, u2, u4 = u1 * factor, u2 * factor, u4 * factor # u3 = u3 * factor"
"u1, u2, u3, u4 = process(-u1_avg), process(-u2_avg), process(-u3_avg), process(-u4_avg)\n",
"u1, u2, u3, u4 = u1 * factor, u2 * factor, u3 * factor, u4 * factor"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T21:33:22.713779600Z",
"start_time": "2024-06-13T21:33:22.510490900Z"
"end_time": "2024-06-14T06:01:21.405193500Z",
"start_time": "2024-06-14T06:01:21.198587Z"
}
},
"id": "5522ed9c105eca27",
"execution_count": 84
"execution_count": 131
},
{
"cell_type": "code",
Expand All @@ -503,7 +506,7 @@
{
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": "<div id='12a7d088-a144-4a08-bf16-45e68f0e34fc'></div>"
"text/html": "<div id='c2b518ed-7700-4e3d-8274-8b38427aeece'></div>"
},
"metadata": {},
"output_type": "display_data"
Expand All @@ -519,7 +522,8 @@
"# plt.plot(ds[::2], pair_averages, label='measurements')\n",
"plt.plot(ds[::2], u1, 'b-', label='SAD')\n",
"plt.plot(ds[::2], u2, 'g-', label='Optical')\n",
"plt.plot(ds[::2], u4, 'b--', label='Refine')\n",
"plt.plot(ds[::2], u3, 'b--', label='CTF')\n",
"# plt.plot(ds[::2], u4, 'b--', label='Refine')\n",
"plt.plot(times, velocities, 'r--', label='Testo', linewidth=1)\n",
"\n",
"plt.xlabel('Time (s)')\n",
Expand All @@ -535,12 +539,37 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T21:33:55.332850100Z",
"start_time": "2024-06-13T21:33:55.137492600Z"
"end_time": "2024-06-14T06:05:05.456452Z",
"start_time": "2024-06-14T06:05:05.263118700Z"
}
},
"id": "afd5755c1eeba3d4",
"execution_count": 86
"execution_count": 136
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "torch.Size([2675])"
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u3_avg.shape"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-14T06:05:06.407491500Z",
"start_time": "2024-06-14T06:05:06.255928700Z"
}
},
"id": "7a14746af79b6187",
"execution_count": 137
},
{
"cell_type": "code",
Expand Down Expand Up @@ -588,32 +617,6 @@
"id": "9eb582797e9463cb",
"execution_count": 87
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "tensor([-17.8040, -38.4342, -17.1759, ..., -11.6538, -35.1645, -8.6250])"
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u1_avg = torch.tensor(np.load(\"../experiments/SAD.npy\"))\n",
"u1_avg"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-06-13T21:35:51.462629200Z",
"start_time": "2024-06-13T21:35:51.319489200Z"
}
},
"id": "6a43e472d4be94c2",
"execution_count": 89
},
{
"cell_type": "code",
"outputs": [],
Expand Down
Binary file added experiments/ctf.npy
Binary file not shown.
6 changes: 3 additions & 3 deletions src/SIV_library/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def forward(self, image: torch.Tensor) -> torch.Tensor:
grid = torch.stack((x, y), dim=-1).to(x.device)
v_grid = grid + torch.stack((-self.u / (cols / 2), self.v / (rows / 2)), dim=-1)

img_new = grid_sample(image.float(), v_grid, mode='bicubic').to(torch.uint8)
img_new = grid_sample(image.float(), v_grid[None, :, :, :], mode='bicubic').to(torch.uint8)
return img_new

def interpolate_field(self, img_shape) -> None:
if self.u.shape[-2:] == img_shape:
return

self.u = interpolate(self.u[None, None, :, :], img_shape, mode='bicubic').squeeze(dim=0)
self.v = interpolate(self.v[None, None, :, :], img_shape, mode='bicubic').squeeze(dim=0)
self.u = interpolate(self.u[None, None, :, :], img_shape, mode='bicubic').squeeze()
self.v = interpolate(self.v[None, None, :, :], img_shape, mode='bicubic').squeeze()

y, x = torch.meshgrid(torch.arange(0, img_shape[0], 1), torch.arange(0, img_shape[1], 1))
self.x, self.y = x.to(self.x.device), y.to(self.y.device)
Expand Down

0 comments on commit 912168e

Please sign in to comment.