{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "827e9aae-17df-4df6-9441-d9a6486d4b84", "metadata": { "tags": [] }, "outputs": [], "source": [ "import rastereasy" ] }, { "cell_type": "markdown", "id": "8903a54f-410d-487e-b9ab-bc455cc9259d", "metadata": {}, "source": [ "# Dempster-Shafer fusion for 2 hypotheses\n", "\n", "It is possible to fuse belief masses issued from images for two hypotheses A and B.\n", "For this, we assume that each image is composed of 3 spectral bands : \n", "- 1st band : belief mass $m(A)$ for hypothesis $A$\n", "- 2nd band : belief mass $m(B)$ for hypothesis $B$\n", "- 3rd band : belief mass $m(A \\cup B)$ for hypothesis $A \\cup B$ (uncertainty)\n", "\n", "For consistent mass functions, the sum along bands must be $1$ for each pixel of each source" ] }, { "cell_type": "markdown", "id": "ed1ea3b8-0564-4280-8c37-0b45a4570986", "metadata": {}, "source": [ "## 1) Create 5 toy images" ] }, { "cell_type": "code", "execution_count": 2, "id": "b488fb18-aed9-4ddd-a22d-a0795587b4b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1000, 1000, 3)\n", "[[1. 1. 1. ... 1. 1. 1.]\n", " [1. 1. 1. ... 1. 1. 1.]\n", " [1. 1. 1. ... 1. 1. 1.]\n", " ...\n", " [1. 1. 1. ... 1. 1. 1.]\n", " [1. 1. 1. ... 1. 1. 1.]\n", " [1. 1. 1. ... 1. 1. 1.]]\n" ] } ], "source": [ "# 1. Read image (just to have meta informations)\n", "name_im='./data/demo/sentinel.tif'\n", "image=rastereasy.Geoimage(name_im)\n", "# 2. Create a 3 band image consistent with mass functions (i.e. positive and sum to 1)\n", "import numpy as np\n", "raw = np.random.rand(image.shape[0], image.shape[1], 3)\n", "# 3. Normalize mass functions such that m(A) + m(B) + m(A∪B) = 1 in each pixel\n", "mass_functions = raw / np.sum(raw, axis=2, keepdims=True)\n", "print(mass_functions.shape)\n", "# 4. Upload the table \n", "mass_function1 = image.upload_image(mass_functions,channel_first=False)\n", "# 5 verify that the sum is 1 along bands\n", "print(mass_function1.sum(axis='band'))\n", "\n", "## Repeat this operation 5 times to create 5 sources\n", "raw = np.random.rand(image.shape[0], image.shape[1], 3)\n", "mass_functions = raw / np.sum(raw, axis=2, keepdims=True)\n", "mass_function2 = image.upload_image(mass_functions,channel_first=False)\n", "\n", "raw = np.random.rand(image.shape[0], image.shape[1], 3)\n", "mass_functions = raw / np.sum(raw, axis=2, keepdims=True)\n", "mass_function3 = image.upload_image(mass_functions,channel_first=False)\n", "\n", "raw = np.random.rand(image.shape[0], image.shape[1], 3)\n", "mass_functions = raw / np.sum(raw, axis=2, keepdims=True)\n", "mass_function4 = image.upload_image(mass_functions,channel_first=False)\n", "\n", "raw = np.random.rand(image.shape[0], image.shape[1], 3)\n", "mass_functions = raw / np.sum(raw, axis=2, keepdims=True)\n", "mass_function5 = image.upload_image(mass_functions,channel_first=False)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "288729da-9320-4917-aeb9-4c389f2fe16b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0.12571802, 0.25086056, 0.18328983, ..., 0.09160784,\n", " 0.16420958, 0.22534334],\n", " [0.22449783, 0.05369949, 0.13427468, ..., 0.48883442,\n", " 0.55197949, 0.61125815],\n", " [0.41689285, 0.04157854, 0.50268653, ..., 0.07765427,\n", " 0.41123555, 0.33504538],\n", " ...,\n", " [0.05546719, 0.75829898, 0.50423343, ..., 0.38172759,\n", " 0.25669968, 0.37609214],\n", " [0.64042402, 0.29924155, 0.1708952 , ..., 0.2388334 ,\n", " 0.48679624, 0.20806528],\n", " [0.23922514, 0.53796906, 0.44941302, ..., 0.35755458,\n", " 0.25504246, 0.446345 ]],\n", "\n", " [[0.4546271 , 0.10578535, 0.49255176, ..., 0.472167 ,\n", " 0.59570854, 0.38831502],\n", " [0.27936 , 0.69792962, 0.194582 , ..., 0.18611645,\n", " 0.36901982, 0.23794597],\n", " [0.27687108, 0.46032741, 0.0916974 , ..., 0.60872449,\n", " 0.07072767, 0.32069137],\n", " ...,\n", " [0.44288423, 0.12847884, 0.15907349, ..., 0.12155445,\n", " 0.46674052, 0.56966528],\n", " [0.34087475, 0.69525966, 0.70591245, ..., 0.30560047,\n", " 0.32621901, 0.73158923],\n", " [0.71419186, 0.05437057, 0.1414912 , ..., 0.61960515,\n", " 0.35577265, 0.40328638]],\n", "\n", " [[0.41965488, 0.64335409, 0.32415841, ..., 0.43622516,\n", " 0.24008188, 0.38634163],\n", " [0.49614217, 0.24837089, 0.67114331, ..., 0.32504913,\n", " 0.07900069, 0.15079588],\n", " [0.30623606, 0.49809405, 0.40561608, ..., 0.31362124,\n", " 0.51803679, 0.34426325],\n", " ...,\n", " [0.50164858, 0.11322218, 0.33669308, ..., 0.49671796,\n", " 0.27655981, 0.05424259],\n", " [0.01870123, 0.00549879, 0.12319235, ..., 0.45556613,\n", " 0.18698475, 0.06034549],\n", " [0.046583 , 0.40766037, 0.40909578, ..., 0.02284027,\n", " 0.38918489, 0.15036862]]], shape=(3, 1000, 1000))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mass_function1.image" ] }, { "cell_type": "markdown", "id": "61797809-5aef-45e7-b0df-6ef1a81c61ab", "metadata": {}, "source": [ "## 2) Perform the fusion" ] }, { "cell_type": "markdown", "id": "12c32490-36a4-4bc6-8516-986e59c82d11", "metadata": {}, "source": [ "### 1) With function `fuse_dempster_shafer_2` of class `Geoimage`" ] }, { "cell_type": "code", "execution_count": 4, "id": "62a3af07-8dc5-4ca4-a54e-56259c68ea85", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method fuse_dempster_shafer_2 in module rastereasy.rastereasy:\n", "\n", "fuse_dempster_shafer_2(*images) method of rastereasy.rastereasy.Geoimage instance\n", " Fuse the 3 band image (associated with mass functions) from multiple\n", " sources using Dempster-Shafer theory with two hypotheses: A and B.\n", " \n", " Parameters\n", " ----------\n", " *images : Geoimage\n", " Each input is a 3-band Geoimage.\n", " \n", " - Band 1: mass function m(A)\n", " \n", " - Band 2: mass function m(B)\n", " \n", " - Band 3: mass function m(A ∪ B)\n", " \n", " Returns\n", " -------\n", " Geoimage\n", " A new Geoimage with 3 bands containing the fused mass functions:\n", " m(A), m(B), and m(A ∪ B).\n", " Geoimage\n", " A new Geoimage with 1 band containing the conflict values.\n", " \n", " Examples\n", " --------\n", " >>> fused, conflict = im1.fuse_dempster_shafer_2(im2)\n", " >>> fused, conflict = im1.fuse_dempster_shafer_2(im1, im2, im3, im4)\n", "\n" ] } ], "source": [ "help(mass_function1.fuse_dempster_shafer_2)" ] }, { "cell_type": "code", "execution_count": 5, "id": "7b7cce2c-402b-44ff-9e7e-e19ca84ec83b", "metadata": {}, "outputs": [], "source": [ "fusion, conflict = mass_function2.fuse_dempster_shafer_2(mass_function5,mass_function4,mass_function1,mass_function3)" ] }, { "cell_type": "markdown", "id": "03eea926-6037-405f-9d27-d43b74cab5e9", "metadata": {}, "source": [ "### 2) With function `fuse_dempster_shafer_2hypotheses` of class `InferenceTools`" ] }, { "cell_type": "code", "execution_count": 6, "id": "ae530d16-5d8c-482e-90fd-4b863275c7a9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function fuse_dempster_shafer_2hypotheses in module rastereasy.rastereasy:\n", "\n", "fuse_dempster_shafer_2hypotheses(*images)\n", " Fuse mass functions from multiple sources using Dempster-Shafer theory\n", " with two hypotheses: A and B.\n", " \n", " Parameters\n", " ----------\n", " *images : Geoimage\n", " Each input is a 3-band Geoimage.\n", " \n", " - Band 1: mass function m(A)\n", " \n", " - Band 2: mass function m(B)\n", " \n", " - Band 3: mass function m(A ∪ B)\n", " \n", " Returns\n", " -------\n", " Geoimage\n", " A new Geoimage with 3 bands containing the fused mass functions:\n", " m(A), m(B), and m(A ∪ B).\n", " Geoimage\n", " A new Geoimage with 1 band containing the conflict values.\n", " \n", " Examples\n", " --------\n", " >>> fused, conflict = fuse_dempster_shafer_2hypotheses(im1, im2, im3)\n", " >>> fused, conflict = fuse_dempster_shafer_2hypotheses(im1, im2, im3, im4)\n", " >>> fused, conflict = fuse_dempster_shafer_2hypotheses(im1, im2)\n", "\n" ] } ], "source": [ "help(rastereasy.InferenceTools.fuse_dempster_shafer_2hypotheses)" ] }, { "cell_type": "code", "execution_count": 7, "id": "34f12558-e609-4e5d-a738-1c5a62ce6876", "metadata": {}, "outputs": [], "source": [ "fusion, conflict = rastereasy.InferenceTools.fuse_dempster_shafer_2hypotheses(\n", " mass_function5,\n", " mass_function4,\n", " mass_function3,\n", " mass_function2,\n", " mass_function1)" ] }, { "cell_type": "markdown", "id": "97823c02-919f-4681-ab70-e3e1fbcc8ce2", "metadata": {}, "source": [ "Verify that the fused masses sum to $1$" ] }, { "cell_type": "code", "execution_count": 8, "id": "eb63c253-061d-4e48-ac4c-f5ce52331595", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., ..., 1., 1., 1.],\n", " [1., 1., 1., ..., 1., 1., 1.],\n", " [1., 1., 1., ..., 1., 1., 1.],\n", " ...,\n", " [1., 1., 1., ..., 1., 1., 1.],\n", " [1., 1., 1., ..., 1., 1., 1.],\n", " [1., 1., 1., ..., 1., 1., 1.]], shape=(1000, 1000))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fusion.sum(axis='band')" ] } ], "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.10.19" } }, "nbformat": 4, "nbformat_minor": 5 }