diff --git a/crop/SimData.ipynb b/crop/SimData.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..25d8be86fc3e1ed7a98de283fb136c3019f73424 --- /dev/null +++ b/crop/SimData.ipynb @@ -0,0 +1,219 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "import glob\n", + "from PIL import Image\n", + "import pillow_heif\n", + "import pandas as pd\n", + "import numpy as np\n", + "from torchvision.utils import save_image\n", + "import torchvision.transforms.functional as fn" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# TODO: overlap?\n", + "def generate_data(bg: list[Image], beetle_set: list[Image], num_beetles_arr: list[int]):\n", + " set_size = len(beetle_set)\n", + " set_bg_size = len(bg)\n", + " sim_arr, coords_arr = [],[]\n", + "\n", + " for num_beetles in num_beetles_arr:\n", + " bg_id = np.random.randint(0, set_bg_size)\n", + " bg_temp = bg[bg_id].copy()\n", + " width, height = bg_temp.size\n", + " beetle_coords = []\n", + " for _ in range(num_beetles):\n", + " # get random beetle image\n", + " beetle_id = np.random.randint(0, set_size)\n", + " beetle_img = beetle_set[beetle_id]\n", + " beetle_width, beetle_height = beetle_img.size\n", + "\n", + " # get random x,y coords to paste beetle\n", + " x = randint(0, width - beetle_width)\n", + " y = randint(0, height - beetle_height)\n", + "\n", + " # get random beetle rotation\n", + " angle = randint(0, 359)\n", + " beetle_img = beetle_img.rotate(angle, resample=Image.BICUBIC)\n", + "\n", + " bg_temp.paste(beetle_img, box=(x,y), mask=beetle_img)\n", + " beetle_coords.append((beetle_id, x, y, beetle_width, beetle_height, angle))\n", + " sim_arr.append(bg_temp)\n", + " coords_arr.append(beetle_coords)\n", + " \n", + " return sim_arr, coords_arr" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "def find_coeffs(source_coords, target_coords):\n", + " matrix = []\n", + " for s, t in zip(source_coords, target_coords):\n", + " matrix.append([t[0], t[1], 1, 0, 0, 0, -s[0]*t[0], -s[0]*t[1]])\n", + " matrix.append([0, 0, 0, t[0], t[1], 1, -s[1]*t[0], -s[1]*t[1]])\n", + " A = np.matrix(matrix, dtype=float)\n", + " B = np.array(source_coords).reshape(8)\n", + " res = np.dot(np.linalg.inv(A.T * A) * A.T, B)\n", + " return np.array(res).reshape(8)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "bg = Image.open(\"bg.png\")\n", + "beetles = []\n", + "for file in glob.glob(r\"/raid/projects/akhot2/group-01-phys371-sp2023/crop/beetles/*\"):\n", + " b0 = Image.open(file)\n", + " beetles.append(b0);\n", + "\n", + "# map corners of trap to corners of image\n", + "coeffs = find_coeffs([(128,6), (1904,62), (2113,3137), (3,3228)], \n", + " [(0,0), (bg.size[0], 0), (bg.size[0], bg.size[1]), (0, bg.size[1])])\n", + "bg_flat = bg.transform(bg.size, Image.PERSPECTIVE, coeffs, Image.BICUBIC)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "beetle_counts = np.random.randint(0, 6, size=1)\n", + "sim_img_arr, coords_arr = generate_data([bg_flat], beetles, beetle_counts)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "def export(img_arr, coords_arr):\n", + " transform = transforms.Compose([transforms.ToTensor()])\n", + " s = \"\"\n", + " image_set = \"train\"\n", + " change = len(img_arr)*0.8\n", + " for i in range(len(img_arr)):\n", + " s = \"\"\n", + " for coord in coords_arr[i]:\n", + " c, x, y, w, h, a = coord\n", + " img = img_arr[i]\n", + " \n", + " s += f\"{0} {x/img.size[0]} {y/img.size[1]} {w/img.size[0]} {h/img.size[1]}\\n\"\n", + " if i > change:\n", + " image_set = \"test\"\n", + " with open(\"data/\" +image_set+ f\"/labels/sim{i}.txt\", \"w\") as f:\n", + " f.write(s)\n", + " save_image(img, \"data/\" +image_set+ f\"/images/sim{i}.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tensor or list of tensors expected, got <class 'PIL.Image.Image'>", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [51]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mexport\u001b[49m\u001b[43m(\u001b[49m\u001b[43msim_img_arr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcoords_arr\u001b[49m\u001b[43m)\u001b[49m\n", + "Input \u001b[0;32mIn [50]\u001b[0m, in \u001b[0;36mexport\u001b[0;34m(img_arr, coords_arr)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata/\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39mimage_set\u001b[38;5;241m+\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/labels/sim\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m 14\u001b[0m f\u001b[38;5;241m.\u001b[39mwrite(s)\n\u001b[0;32m---> 15\u001b[0m \u001b[43msave_image\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdata/\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43mimage_set\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/images/sim\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mi\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m.png\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/raid/projects/akhot2/conda/envs/akhot2/lib/python3.9/site-packages/torch/autograd/grad_mode.py:27\u001b[0m, in \u001b[0;36m_DecoratorContextManager.__call__.<locals>.decorate_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(func)\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecorate_context\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclone():\n\u001b[0;32m---> 27\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/raid/projects/akhot2/conda/envs/akhot2/lib/python3.9/site-packages/torchvision/utils.py:152\u001b[0m, in \u001b[0;36msave_image\u001b[0;34m(tensor, fp, format, **kwargs)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mjit\u001b[38;5;241m.\u001b[39mis_scripting() \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mjit\u001b[38;5;241m.\u001b[39mis_tracing():\n\u001b[1;32m 151\u001b[0m _log_api_usage_once(save_image)\n\u001b[0;32m--> 152\u001b[0m grid \u001b[38;5;241m=\u001b[39m \u001b[43mmake_grid\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtensor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;66;03m# Add 0.5 after unnormalizing to [0, 255] to round to nearest integer\u001b[39;00m\n\u001b[1;32m 154\u001b[0m ndarr \u001b[38;5;241m=\u001b[39m grid\u001b[38;5;241m.\u001b[39mmul(\u001b[38;5;241m255\u001b[39m)\u001b[38;5;241m.\u001b[39madd_(\u001b[38;5;241m0.5\u001b[39m)\u001b[38;5;241m.\u001b[39mclamp_(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m)\u001b[38;5;241m.\u001b[39mpermute(\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m.\u001b[39mto(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcpu\u001b[39m\u001b[38;5;124m\"\u001b[39m, torch\u001b[38;5;241m.\u001b[39muint8)\u001b[38;5;241m.\u001b[39mnumpy()\n", + "File \u001b[0;32m/raid/projects/akhot2/conda/envs/akhot2/lib/python3.9/site-packages/torch/autograd/grad_mode.py:27\u001b[0m, in \u001b[0;36m_DecoratorContextManager.__call__.<locals>.decorate_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(func)\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecorate_context\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclone():\n\u001b[0;32m---> 27\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/raid/projects/akhot2/conda/envs/akhot2/lib/python3.9/site-packages/torchvision/utils.py:60\u001b[0m, in \u001b[0;36mmake_grid\u001b[0;34m(tensor, nrow, padding, normalize, value_range, scale_each, pad_value, **kwargs)\u001b[0m\n\u001b[1;32m 58\u001b[0m _log_api_usage_once(make_grid)\n\u001b[1;32m 59\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (torch\u001b[38;5;241m.\u001b[39mis_tensor(tensor) \u001b[38;5;129;01mor\u001b[39;00m (\u001b[38;5;28misinstance\u001b[39m(tensor, \u001b[38;5;28mlist\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mall\u001b[39m(torch\u001b[38;5;241m.\u001b[39mis_tensor(t) \u001b[38;5;28;01mfor\u001b[39;00m t \u001b[38;5;129;01min\u001b[39;00m tensor))):\n\u001b[0;32m---> 60\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtensor or list of tensors expected, got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(tensor)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 62\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrange\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[1;32m 63\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 64\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe parameter \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrange\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m is deprecated since 0.12 and will be removed in 0.14. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease use \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue_range\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 66\u001b[0m )\n", + "\u001b[0;31mTypeError\u001b[0m: tensor or list of tensors expected, got <class 'PIL.Image.Image'>" + ] + } + ], + "source": [ + "export(sim_img_arr, coords_arr)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "format\n", + "0 x y width height (each value normalized to size of image)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(beetle_counts)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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" + }, + "vscode": { + "interpreter": { + "hash": "5501471bf458387d76cdb9f487659a1abd30d9114d99ffb68b67c5dd3feef292" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}