{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "melanoma_classification.ipynb", "version": "0.3.2", "views": {}, "default_view": {}, "provenance": [ { "file_id": "1WB_KFS_v-cUGcQlLCui1zY2yi9_53FRO", "timestamp": 1521599432486 }, { "file_id": "1gxbV3jGpAScBlpDwmunpw9v-KkdDW-Mm", "timestamp": 1519788607959 } ], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "metadata": { "id": "Weolwx_o-XW0", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Installing TuriCreate. This is our main package. TuriCreate is open sourced. \n", "# You can get more info about TuriCreate at https://github.com/apple/turicreate\n", "\n", "!pip install turicreate" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "xZ0sRri2FBZD", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "p6r25vQo_1-8", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 1 } ], "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "cc000212-b4f1-4cf3-ccb9-1204d20654b1", "executionInfo": { "status": "ok", "timestamp": 1521599523242, "user_tz": 300, "elapsed": 515, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "# Finding the directory we are in\n", "!ls" ], "execution_count": 2, "outputs": [ { "output_type": "stream", "text": [ "datalab\r\n" ], "name": "stdout" } ] }, { "metadata": { "id": "mDm-xDqJKXTL", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Next code block will mount your google drive. This will help you to access files stored in your google drive. \n", "Druing installation you see a prompt with a link -Please, open the following URL in a web browser: https://accounts.google.com/o ....\n", "Click the link. Copy and paste the code from the link in the form field and press enter. \n", "Sometime You may have to run this code block twice. I think this is a bug in colab. " ] }, { "metadata": { "id": "RK-IDnlMKWy7", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "MW6q3iD0E_zK", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "!apt-get install -y -qq software-properties-common python-software-properties module-init-tools\n", "!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null\n", "!apt-get update -qq 2>&1 > /dev/null\n", "!apt-get -y install -qq google-drive-ocamlfuse fuse\n", "from google.colab import auth\n", "auth.authenticate_user()\n", "from oauth2client.client import GoogleCredentials\n", "creds = GoogleCredentials.get_application_default()\n", "import getpass\n", "!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL\n", "vcode = getpass.getpass()\n", "!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "QoafsczbH8Wt", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Mount your google drive to access files from it \n", "!mkdir -p drive\n", "!google-drive-ocamlfuse drive" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "TETZgUL1H8F4", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# The image folder that I am using for this tutorial is located in my google drive under 'app' folder\n", "# So we need to change directory to the 'app' folder\n", "\n", "import os\n", "os.chdir(\"drive/app\")" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "bKGEoRTkH7y6", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 1 } ], "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "9694539a-152c-4317-bbcc-5e4af41edb6c", "executionInfo": { "status": "ok", "timestamp": 1521599653818, "user_tz": 300, "elapsed": 696, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "# make sure that we are in the right folder \n", "!ls" ], "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ "cats-dogs.sframe ImageFolder MelanomaImages wisconsin_breast_cancer.csv\r\n" ], "name": "stdout" } ] }, { "metadata": { "id": "WRB_f_VqIyjh", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# importing turicreate as tc\n", "\n", "import turicreate as tc" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "wgIccMR5Fzzd", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Loading the images from MelanomaImages in my google drive. This 'MelanomaImages' has 2 folders\n", "# 'melanoma' and 'benign'\n", "\n", "\n", "data = tc.image_analysis.load_images('MelanomaImages', with_path=True)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "0YNnUEwgGnDk", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Labelling each images in the SFrame using the folder name\n", "\n", "data['label'] = data['path'].apply(lambda path: 'melanoma' if '/melanoma' in path else 'benign')" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "ZuEWrVXfGrfv", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 1 } ], "base_uri": "https://localhost:8080/", "height": 421 }, "outputId": "e0df3b46-8a90-488c-b3ef-a393ee6b72f9", "executionInfo": { "status": "ok", "timestamp": 1521600470404, "user_tz": 300, "elapsed": 645, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "#Displaying the first 5 rows in data to make sure that we got everything right \n", "\n", "data.tail()" ], "execution_count": 12, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
path | \n", "image | \n", "label | \n", "
---|---|---|
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4439 Width: 6708 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4441 Width: 6661 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4401 Width: 6681 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4459 Width: 6627 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4439 Width: 6668 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4419 Width: 6648 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4422 Width: 6661 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4420 Width: 6641 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4441 Width: 6640 | \n", "melanoma | \n", "
/content/drive/app/Melano maImages/melanoma/ISI ... | \n",
" Height: 4459 Width: 6708 | \n", "melanoma | \n", "
WARNING: The number of feature dimensions in this problem is very large in comparison with the number of examples. Unless an appropriate regularization value is set, this model may not provide accurate predictions for a validation/test set." ], "text/plain": [ "WARNING: The number of feature dimensions in this problem is very large in comparison with the number of examples. Unless an appropriate regularization value is set, this model may not provide accurate predictions for a validation/test set." ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
WARNING: Detected extremely low variance for feature(s) '__image_features__' because all entries are nearly the same.\n", "Proceeding with model training using all features. If the model does not provide results of adequate quality, exclude the above mentioned feature(s) from the input dataset." ], "text/plain": [ "WARNING: Detected extremely low variance for feature(s) '__image_features__' because all entries are nearly the same.\n", "Proceeding with model training using all features. If the model does not provide results of adequate quality, exclude the above mentioned feature(s) from the input dataset." ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Logistic regression:" ], "text/plain": [ "Logistic regression:" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
--------------------------------------------------------" ], "text/plain": [ "--------------------------------------------------------" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Number of examples : 699" ], "text/plain": [ "Number of examples : 699" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Number of classes : 2" ], "text/plain": [ "Number of classes : 2" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Number of feature columns : 1" ], "text/plain": [ "Number of feature columns : 1" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Number of unpacked features : 2048" ], "text/plain": [ "Number of unpacked features : 2048" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Number of coefficients : 2049" ], "text/plain": [ "Number of coefficients : 2049" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
Starting L-BFGS" ], "text/plain": [ "Starting L-BFGS" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
--------------------------------------------------------" ], "text/plain": [ "--------------------------------------------------------" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
+-----------+----------+-----------+--------------+-------------------+---------------------+" ], "text/plain": [ "+-----------+----------+-----------+--------------+-------------------+---------------------+" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| Iteration | Passes | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |" ], "text/plain": [ "| Iteration | Passes | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
+-----------+----------+-----------+--------------+-------------------+---------------------+" ], "text/plain": [ "+-----------+----------+-----------+--------------+-------------------+---------------------+" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 1 | 7 | 0.000009 | 1.407496 | 0.589413 | 0.657143 |" ], "text/plain": [ "| 1 | 7 | 0.000009 | 1.407496 | 0.589413 | 0.657143 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 2 | 10 | 5.000000 | 1.614489 | 0.728183 | 0.771429 |" ], "text/plain": [ "| 2 | 10 | 5.000000 | 1.614489 | 0.728183 | 0.771429 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 3 | 11 | 5.000000 | 1.714628 | 0.765379 | 0.714286 |" ], "text/plain": [ "| 3 | 11 | 5.000000 | 1.714628 | 0.765379 | 0.714286 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 4 | 12 | 5.000000 | 1.810076 | 0.589413 | 0.657143 |" ], "text/plain": [ "| 4 | 12 | 5.000000 | 1.810076 | 0.589413 | 0.657143 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 5 | 14 | 1.000000 | 1.960559 | 0.789700 | 0.771429 |" ], "text/plain": [ "| 5 | 14 | 1.000000 | 1.960559 | 0.789700 | 0.771429 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 6 | 15 | 1.000000 | 2.066136 | 0.723891 | 0.685714 |" ], "text/plain": [ "| 6 | 15 | 1.000000 | 2.066136 | 0.723891 | 0.685714 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
| 10 | 20 | 1.000000 | 2.569017 | 0.831187 | 0.828571 |" ], "text/plain": [ "| 10 | 20 | 1.000000 | 2.569017 | 0.831187 | 0.828571 |" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
+-----------+----------+-----------+--------------+-------------------+---------------------+" ], "text/plain": [ "+-----------+----------+-----------+--------------+-------------------+---------------------+" ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
TERMINATED: Iteration limit reached." ], "text/plain": [ "TERMINATED: Iteration limit reached." ] }, "metadata": { "tags": [] } }, { "output_type": "display_data", "data": { "text/html": [ "
This model may not be optimal. To improve it, consider increasing `max_iterations`." ], "text/plain": [ "This model may not be optimal. To improve it, consider increasing `max_iterations`." ] }, "metadata": { "tags": [] } } ] }, { "metadata": { "id": "GjhQrTTzRxjN", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Took only about 7 minutes to do this with GPU!" ] }, { "metadata": { "id": "VzziTY2aG-A5", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# storing the prediction \n", "\n", "predictions = model.predict(test_data)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "clbk_WE7R-q2", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 3 } ], "base_uri": "https://localhost:8080/", "height": 86 }, "outputId": "49fda9a0-359f-428a-d374-3d66a1561e70", "executionInfo": { "status": "ok", "timestamp": 1521601353680, "user_tz": 300, "elapsed": 97321, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "# Evaluating the model \n", "\n", "metrics = model.evaluate(test_data)\n", "print(metrics['accuracy'])" ], "execution_count": 23, "outputs": [ { "output_type": "stream", "text": [ "Resizing images...\n", "Performing feature extraction on resized images...\n", "Completed 183/183\n", "0.825136612021858\n" ], "name": "stdout" } ] }, { "metadata": { "id": "38gNPKZRSrhL", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 1 } ], "base_uri": "https://localhost:8080/", "height": 207 }, "outputId": "1ba507c4-7500-419e-d7cb-ce1a77d52835", "executionInfo": { "status": "ok", "timestamp": 1521601378853, "user_tz": 300, "elapsed": 317, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "# Printing confusion matrix\n", "\n", "print (\"Confusion Matrix : \\n%s\" % metrics['confusion_matrix'])" ], "execution_count": 24, "outputs": [ { "output_type": "stream", "text": [ "Confusion Matrix : \n", "+--------------+-----------------+-------+\n", "| target_label | predicted_label | count |\n", "+--------------+-----------------+-------+\n", "| benign | benign | 102 |\n", "| benign | melanoma | 7 |\n", "| melanoma | melanoma | 49 |\n", "| melanoma | benign | 25 |\n", "+--------------+-----------------+-------+\n", "[4 rows x 3 columns]\n", "\n" ], "name": "stdout" } ] }, { "metadata": { "id": "FXJqEP6ci6gM", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", "```\n", "\n", "> Statistic Value Confidence interval \n", "\n", "\n", "> Sensitivity 66.22%\t 54.28% to 76.81%\n", "\n", "\n", "\n", "> Specificity 93.58 %\t 87.22% to 97.38%\n", "\n", "\n", "> PPV 87.50% \t 77.05% to 93.59%\n", "\n", "\n", "> NPV 80.31 % 74.71% to 84.93%\n", "\n", "\n", "> Accuracy 82.51% \t 76.22% to 87.72%\n", "\n", "\n", "```" ] }, { "metadata": { "id": "Q4GoAum5TP4v", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Save the model for later use in Turi Create\n", "model.save('melanoma.model')" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "XPdqOGADURz2", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Export for use in Core ML\n", "model.export_coreml('MyMelanomaClassifier.mlmodel')" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "6X51lMk0U04m", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "output_extras": [ { "item_id": 1 } ], "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "823d3e54-432f-4e26-b0c1-547cade1ceb8", "executionInfo": { "status": "ok", "timestamp": 1521601450071, "user_tz": 300, "elapsed": 643, "user": { "displayName": "Endocrine Doctor", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "106981773189993416592" } } }, "cell_type": "code", "source": [ "!ls" ], "execution_count": 27, "outputs": [ { "output_type": "stream", "text": [ "MyMelanomaClassifier.mlmodel games lib\t local\t sbin src\r\n", "bin\t\t\t include lib64-nvidia melanoma.model share\r\n" ], "name": "stdout" } ] }, { "metadata": { "id": "O6PUln9hWcBQ", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "# Downloading the model for use in Xcode\n", "\n", "from google.colab import files\n", "\n", "files.download('MyMelanomaClassifier.mlmodel')" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "lKbWi2xqc1J3", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }