fernandoperlar/preprocessing_image
0
1{2 "cells": [3 {4 "cell_type": "markdown",5 "metadata": {},6 "source": [7 "# Breast cancer detection from thermal imaging"8 ]9 },10 {11 "cell_type": "markdown",12 "metadata": {},13 "source": [14 "The main purpose of this project is to develop a comprehensive decision support system for breast cancer screening."15 ]16 },17 {18 "cell_type": "markdown",19 "metadata": {},20 "source": [21 "## Library import\n",22 "In this section we will try to import the libraries that will be used throughout this model. Note that some of the libraries used in this program are declared in the files found in `src/scripts/*.py`."23 ]24 },25 {26 "cell_type": "code",27 "execution_count": null,28 "metadata": {},29 "outputs": [],30 "source": [31 "%reload_ext autoreload\n",32 "%autoreload 2"33 ]34 },35 {36 "cell_type": "code",37 "execution_count": null,38 "metadata": {},39 "outputs": [],40 "source": [41 "from scripts import *"42 ]43 },44 {45 "cell_type": "code",46 "execution_count": null,47 "metadata": {},48 "outputs": [],49 "source": [50 "computer.check_available_devices() # Check available devices"51 ]52 },53 {54 "cell_type": "markdown",55 "metadata": {},56 "source": [57 "## Data selection\n",58 "To make this model work correctly it will be necessary to extract and save the images found in the `data` folder.\n",59 "\n",60 "In this folder there are two labeled folders that contain all the images to be used:\n",61 "```\n",62 "data\n",63 "├── healthy\n",64 "└── sick\n",65 "```"66 ]67 },68 {69 "cell_type": "code",70 "execution_count": null,71 "metadata": {},72 "outputs": [],73 "source": [74 "data = Data(\"./data/\") # Data imported into a table\n",75 "\n",76 "data.images.head(3) # Display first 3 rows"77 ]78 },79 {80 "cell_type": "markdown",81 "metadata": {},82 "source": [83 "## Transformation\n",84 "In the transformation stage, the data is adapted to find the solution to the problem to be solved."85 ]86 },87 {88 "cell_type": "markdown",89 "metadata": {},90 "source": [91 "First of all, the data obtained previously will be divided to be able to use it for training and to check the results."92 ]93 },94 {95 "cell_type": "code",96 "execution_count": null,97 "metadata": {},98 "outputs": [],99 "source": [100 "data.training, data.test = data.train_test_split(test_size=0.15, shuffle=True, stratify=True) # Split data into train and test"101 ]102 },103 {104 "cell_type": "code",105 "execution_count": null,106 "metadata": {},107 "outputs": [],108 "source": [109 "# The category distribution is shown for the original, training, and test data\n",110 "data.count_labels(data.images, \"Original\")\n",111 "data.count_labels(data.training, \"Training\")\n",112 "data.count_labels(data.test, \"Test\")"113 ]114 },115 {116 "cell_type": "markdown",117 "metadata": {},118 "source": [119 "Once the data is divided, different transformation techniques are applied on it to expand the size of the dataset in real time while training the model."120 ]121 },122 {123 "cell_type": "code",124 "execution_count": null,125 "metadata": {},126 "outputs": [],127 "source": [128 "train_generator, validation_generator, test_generator = data.image_generator(shuffle=False) # Image genearation"129 ]130 },131 {132 "cell_type": "code",133 "execution_count": null,134 "metadata": {},135 "outputs": [],136 "source": [137 "filters = {\n",138 "\t\"original\": lambda x: x,\n",139 "\t\"red\": lambda x: data.getImageTensor(x, (330, 0, 0), (360, 255, 255)) + data.getImageTensor(x, (0, 0, 0), (50, 255, 255)),\n",140 "\t\"green\": lambda x: data.getImageTensor(x, (60, 0, 0), (130, 255, 255)),\n",141 "\t\"blue\": lambda x: data.getImageTensor(x, (180, 0, 0), (270, 255, 255)),\n",142 "}\n",143 "\n",144 "data.show_images(train_generator, filters, \"Training\") # Show some images from the training generator"145 ]146 },147 {148 "cell_type": "markdown",149 "metadata": {},150 "source": [151 "## Data Mining\n",152 "This section seeks to apply techniques that are capable of extracting useful patterns and then evaluate them."153 ]154 },155 {156 "cell_type": "markdown",157 "metadata": {},158 "source": [159 "### Model creation\n",160 "The model to be used for the next training is created."161 ]162 },163 {164 "cell_type": "code",165 "execution_count": null,166 "metadata": {},167 "outputs": [],168 "source": [169 "red_model = Model(\"red\", filter=filters[\"red\"], new=True, summary=False, plot=False) # Red model creation\n",170 "green_model = Model(\"green\", filter=filters[\"green\"], new=True, summary=False, plot=False) # Green model creation\n",171 "blue_model = Model(\"blue\", filter=filters[\"blue\"], new=True, summary=False, plot=False) # Blue model creation"172 ]173 },174 {175 "cell_type": "code",176 "execution_count": null,177 "metadata": {},178 "outputs": [],179 "source": [180 "red_model.compile() # Compile the red model\n",181 "green_model.compile() # Compile the green model\n",182 "blue_model.compile() # Compile the blue model"183 ]184 },185 {186 "cell_type": "markdown",187 "metadata": {},188 "source": [189 "### Model training\n",190 "The created model is trained indicating the times that are going to be used."191 ]192 },193 {194 "cell_type": "code",195 "execution_count": null,196 "metadata": {},197 "outputs": [],198 "source": [199 "red_model.fit(train_generator, validation_generator, epochs=600, verbose=False, plot=False) # Train the red model\n",200 "green_model.fit(train_generator, validation_generator, epochs=600, verbose=False, plot=False) # Train the green model\n",201 "blue_model.fit(train_generator, validation_generator, epochs=600, verbose=False, plot=False) # Train the blue model"202 ]203 },204 {205 "cell_type": "markdown",206 "metadata": {},207 "source": [208 "### Model evaluation\n",209 "The trained model is evaluated using the generators created before. In this case, the best weight matrix obtained in the training will be used."210 ]211 },212 {213 "cell_type": "code",214 "execution_count": null,215 "metadata": {},216 "outputs": [],217 "source": [218 "red_model.evaluate(test_generator, path=None) # Evaluate the red model\n",219 "green_model.evaluate(test_generator, path=None) # Evaluate the green model\n",220 "blue_model.evaluate(test_generator, path=None) # Evaluate the blue model"221 ]222 },223 {224 "cell_type": "markdown",225 "metadata": {},226 "source": [227 "### Grad-CAM\n",228 "An activation map of the predictions obtained by the convolutional network is displayed."229 ]230 },231 {232 "cell_type": "code",233 "execution_count": null,234 "metadata": {},235 "outputs": [],236 "source": [237 "join_models = Join(red_model, green_model, blue_model)\n",238 "\n",239 "# The activation map is displayed\n",240 "for index, image in data.test.iterrows():\n",241 "\tjoin_models.visualize_heatmap(image)"242 ]243 }244 ],245 "metadata": {246 "interpreter": {247 "hash": "a8beb786faeb05718f0030b63e3f9a213e4fc874985a90138c9f4beb79c05e3b"248 },249 "kernelspec": {250 "display_name": "Python 3.8.11 64-bit ('hinton': conda)",251 "name": "python3"252 },253 "language_info": {254 "codemirror_mode": {255 "name": "ipython",256 "version": 3257 },258 "file_extension": ".py",259 "mimetype": "text/x-python",260 "name": "python",261 "nbconvert_exporter": "python",262 "pygments_lexer": "ipython3",263 "version": "3.8.11"264 },265 "orig_nbformat": 4266 },267 "nbformat": 4,268 "nbformat_minor": 2269}270 