From 8fa2cde7870b303cc59d4213e224bc3a0b06d90c Mon Sep 17 00:00:00 2001 From: Laurent Fainsin Date: Tue, 12 Jul 2022 16:17:37 +0200 Subject: [PATCH] feat: yay more drafts for filtering output of unet Former-commit-id: 8c46294df7a834e6b67043afbb905b5fcc8194f2 [formerly 79b7ecefd45e16c01afc3e062780147d14da127a] Former-commit-id: e334164ea7c9b6c9093935d7e468efa66b4ccbe5 --- src/notebooks/predict.ipynb | 448 --------------------- src/notebooks/predict.ipynb.REMOVED.git-id | 1 + 2 files changed, 1 insertion(+), 448 deletions(-) delete mode 100644 src/notebooks/predict.ipynb create mode 100644 src/notebooks/predict.ipynb.REMOVED.git-id diff --git a/src/notebooks/predict.ipynb b/src/notebooks/predict.ipynb deleted file mode 100644 index e83106f..0000000 --- a/src/notebooks/predict.ipynb +++ /dev/null @@ -1,448 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import albumentations as A\n", - "import numpy as np\n", - "import onnx\n", - "import onnxruntime\n", - "from albumentations.pytorch import ToTensorV2\n", - "from PIL import Image\n", - "\n", - "%config InlineBackend.figure_formats = ['svg']\n", - "import matplotlib.pyplot as plt\n", - "%matplotlib inline\n", - "\n", - "import torch\n", - "from utils.dice import dice_score\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def sigmoid(x):\n", - " return 1 / (1 + np.exp(-x))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def to_numpy(tensor):\n", - " return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "model_path = \"../../checkpoints/best.onnx\"\n", - "# image_path = \"../../images/SM.png\"\n", - "image_path = \"/home/lilian/data_disk/lfainsin/test/2022_SM/DOS_DETAIL/DSC_0055.jpg\"\n", - "gt_path = \"/home/lilian/data_disk/lfainsin/test/2022_SM/DOS_DETAIL/MASK.PNG\"\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "onnx_model = onnx.load(model_path)\n", - "onnx.checker.check_model(onnx_model)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "session = onnxruntime.InferenceSession(model_path)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "image = Image.open(image_path).convert(\"RGB\")\n", - "\n", - "plt.figure(figsize=(25, 10))\n", - "plt.imshow(image)\n", - "\n", - "print(image.size)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "transform = A.Compose(\n", - " [\n", - " A.LongestMaxSize(1024),\n", - " A.ToFloat(max_value=255), # [0, 255] -> [0.0, 1.0]\n", - " ToTensorV2(), # HWC -> CHW\n", - " ],\n", - ")\n", - "aug = transform(image=np.asarray(image))\n", - "img = aug[\"image\"]\n", - "\n", - "img = img.unsqueeze(0) # -> 1CHW" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "inputs = {\n", - " session.get_inputs()[0].name: to_numpy(img),\n", - "}\n", - "\n", - "outs = session.run(None, inputs)\n", - "\n", - "img_out = outs[0][0][0] # extract HW\n", - "img_out = img_out * 255 # [0.0, 255.0]\n", - "img_out = img_out.clip(0, 255) \n", - "img_out = np.uint8(img_out) # [0, 255]\n", - "img_out = Image.fromarray(img_out, \"L\") # PIL img\n", - "\n", - "# plt.figure(figsize=(25, 10))\n", - "# plt.imshow(img_out, cmap=plt.cm.gray)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "inputs = {\n", - " session.get_inputs()[0].name: to_numpy(img),\n", - "}\n", - "\n", - "outs = session.run(None, inputs)\n", - "\n", - "img_out = outs[0][0][0] # extract HW\n", - "img_out = sigmoid(img_out) # -> [0.0, 1.0]\n", - "img_out = img_out * 255 # [0.0, 255.0]\n", - "img_out = np.uint8(img_out) # [0, 255]\n", - "img_out = Image.fromarray(img_out, \"L\") # PIL img\n", - "\n", - "plt.figure(figsize=(25, 10))\n", - "plt.imshow(img_out, cmap=plt.cm.gray)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from skimage import measure, draw\n", - "\n", - "# Find contours at a constant value of 0.8\n", - "contours = measure.find_contours(np.asarray(img_out))\n", - "\n", - "# Display the image and plot all contours found\n", - "plt.figure(figsize=(19, 18))\n", - "plt.imshow(img_out, cmap=plt.cm.gray)\n", - "\n", - "lenc = [len(c) for c in contours]\n", - "indexs = np.argsort(lenc)\n", - "l = indexs[-1]\n", - "\n", - "# plt.plot(contours[l][:, 1], contours[l][:, 0], linewidth=2, c=\"red\")\n", - "\n", - "# on estime l'ellipse\n", - "ellipse = measure.EllipseModel()\n", - "ellipse.estimate(contours[l])\n", - "\n", - "# on récupère les coords des points de l'ellipse\n", - "cx, cy, a, b, theta = ellipse.params\n", - "ex, ey = draw.ellipse_perimeter(int(cx), int(cy), int(a), int(b), orientation=theta, shape=img_out.size[::-1])\n", - "\n", - "plt.scatter(ey, ex, c=\"green\", s=0.5)\n", - "plt.scatter(cy, cx, c=\"green\", s=0.5)\n", - "\n", - "# # on estime le cercle\n", - "circle = measure.CircleModel()\n", - "circle.estimate(contours[l])\n", - "\n", - "# on récupère les coords des points du cercle\n", - "cx, cy, r = circle.params\n", - "ex, ey = draw.circle_perimeter(int(cx), int(cy), int(r), shape=img_out.size[::-1])\n", - "\n", - "plt.scatter(ey, ex, c=\"blue\", s=0.5)\n", - "plt.scatter(cy, cx, c=\"blue\", s=0.5)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# mutilresolution zoom" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "factor = image.size[0] / img_out.size[0]\n", - "taille = min(a, b) * factor\n", - "percentage = 0.1\n", - "size = taille / percentage\n", - "\n", - "img = image.crop((cy*factor - size, cx*factor - size/1.5, cy*factor + size, cx*factor + size/1.5))\n", - "\n", - "transform = A.Compose(\n", - " [\n", - " A.LongestMaxSize(1024),\n", - " A.ToFloat(max_value=255), # [0, 255] -> [0.0, 1.0]\n", - " ToTensorV2(), # HWC -> CHW\n", - " ],\n", - ")\n", - "aug = transform(image=np.asarray(img))\n", - "img = aug[\"image\"]\n", - "\n", - "img = img.unsqueeze(0) # -> 1CHW" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "inputs = {\n", - " session.get_inputs()[0].name: to_numpy(img),\n", - "}\n", - "\n", - "outs = session.run(None, inputs)\n", - "\n", - "img_out = outs[0][0][0] # extract HW\n", - "img_out = sigmoid(img_out) # -> [0.0, 1.0]\n", - "img_out = img_out * 255 # [0.0, 255.0]\n", - "img_out = np.uint8(img_out) # [0, 255]\n", - "img_out = Image.fromarray(img_out, \"L\") # PIL img\n", - "\n", - "plt.figure(figsize=(25, 10))\n", - "plt.imshow(img_out, cmap=plt.cm.gray)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.356565656565656564\r" - ] - } - ], - "source": [ - "image = Image.open(image_path).convert(\"RGB\")\n", - "ground_truth = Image.open(gt_path).convert(\"L\")\n", - "\n", - "Image.MAX_IMAGE_PIXELS = 5000000000 \n", - "\n", - "dices = []\n", - "percentages = np.linspace(0.01, 0.35, 100)\n", - "\n", - "for p in percentages:\n", - " size = taille / p \n", - " print(p, end=\"\\r\")\n", - "\n", - " img = image.crop((cy * factor - size, cx * factor - size, cy * factor + size, cx * factor + size))\n", - " gt = ground_truth.crop((cy * factor - size, cx * factor - size, cy * factor + size, cx * factor + size))\n", - "\n", - " transform = A.Compose(\n", - " [\n", - " A.LongestMaxSize(1024),\n", - " A.ToFloat(max_value=255), # [0, 255] -> [0.0, 1.0]\n", - " ToTensorV2(), # HWC -> CHW\n", - " ],\n", - " )\n", - " aug = transform(image=np.asarray(img))\n", - " img = aug[\"image\"]\n", - " img = img.unsqueeze(0) # -> 1CHW\n", - "\n", - " inputs = {\n", - " session.get_inputs()[0].name: to_numpy(img),\n", - " }\n", - "\n", - " outs = session.run(None, inputs)\n", - "\n", - " img_out2 = outs[0][0][0] # extract HW\n", - " img_out2 = sigmoid(img_out2) # -> [0.0, 1.0]\n", - " img_out2 = img_out2 > 0.5 # {False, True}\n", - " img_out2 = np.uint8(img_out2) # [0, 1]\n", - " img_out2 = torch.tensor(img_out2)\n", - "\n", - " aug = transform(image=np.array(gt))\n", - " gt = aug[\"image\"]\n", - " gt = gt > 0.1 # {False, True}\n", - " gt = np.uint8(gt) # [0, 1]\n", - " gt = torch.tensor(gt)\n", - "\n", - " dice = dice_score(gt, img_out2, logits=False)\n", - " dices.append(dice)\n", - "\n", - " # plt.figure(figsize=(25, 10))\n", - " # plt.imshow(Image.fromarray(gt.squeeze(0).numpy()), cmap=plt.cm.gray)" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 60, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/svg+xml": "\n\n\n \n \n \n \n 2022-07-12T14:21:04.233003\n image/svg+xml\n \n \n Matplotlib v3.5.2, https://matplotlib.org/\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "plt.figure(figsize=(15, 10))\n", - "plt.plot(percentages, dices)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Dice(resolution)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "image_path = \"/home/lilian/data_disk/lfainsin/test/2022_SM/DOS_DETAIL/DSC_0050.jpg\"\n", - "gt_path = \"/home/lilian/data_disk/lfainsin/test/2022_SM/DOS_DETAIL/MASK.PNG\"\n", - "\n", - "image = Image.open(image_path).convert(\"RGB\")\n", - "image = torch.tensor(np.uint8(image))\n", - "\n", - "ground_truth = Image.open(gt_path).convert(\"L\")\n", - "ground_truth = torch.tensor(np.uint8(ground_truth))\n", - "\n", - "dices = []\n", - "rezs = range(128, 1920+4, 4)\n", - "\n", - "for rez in rezs:\n", - "\n", - " print(rez, end=\"\\r\")\n", - "\n", - " transform = A.Compose(\n", - " [\n", - " A.LongestMaxSize(rez),\n", - " A.ToFloat(max_value=255), # [0, 255] -> [0.0, 1.0]\n", - " ToTensorV2(), # HWC -> CHW\n", - " ],\n", - " )\n", - " aug = transform(image=np.array(image))\n", - " img = aug[\"image\"]\n", - " img = img.unsqueeze(0) # -> 1CHW\n", - "\n", - " inputs = {\n", - " session.get_inputs()[0].name: to_numpy(img),\n", - " }\n", - " outs = session.run(None, inputs)\n", - "\n", - " img_out = outs[0][0][0] # extract HW\n", - " img_out = sigmoid(img_out) # -> [0.0, 1.0]\n", - " img_out = img_out > 0.5 # {False, True}\n", - " img_out = np.uint8(img_out) # [0, 1]\n", - " img_out = torch.tensor(img_out)\n", - "\n", - " aug = transform(image=np.array(ground_truth))\n", - " gt = aug[\"image\"]\n", - " gt = gt > 0.1 # {False, True}\n", - " gt = np.uint8(gt) # [0, 1]\n", - " gt = torch.tensor(gt)\n", - "\n", - " dice = dice_score(gt, img_out, logits=False)\n", - " dices.append(dice)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.figure(figsize=(15, 10))\n", - "plt.plot(rezs, dices)\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.8.0 ('.venv': poetry)", - "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.8.0" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "dc80d2c03865715c8671359a6bf138f6c8ae4e26ae025f2543e0980b8db0ed7e" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/src/notebooks/predict.ipynb.REMOVED.git-id b/src/notebooks/predict.ipynb.REMOVED.git-id new file mode 100644 index 0000000..8f5a3d0 --- /dev/null +++ b/src/notebooks/predict.ipynb.REMOVED.git-id @@ -0,0 +1 @@ +b9209497dc0ffc22fedf1f80bc68d81cdc5acb6a \ No newline at end of file