{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "FjDGwlCJYO2m" }, "source": [ "### Model Inferences" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "DUXRPvLRxpJe" }, "outputs": [], "source": [ "import pandas as pd \n", "import numpy as np\n", "from tensorflow import keras\n", "import tensorflow as tf\n", "import pickle\n", "from keras.models import load_model\n", "from tensorflow.keras.preprocessing import image \n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WOLLnPnAuzKm", "outputId": "644f852b-b695-40d8-db1c-3a8e502ca316" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mounted at /content/drive\n" ] } ], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9nTgKQUKu61s", "outputId": "3105e278-99eb-468d-8890-4079916391b2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content/drive/MyDrive/cnn_model\n" ] } ], "source": [ "%cd /content/drive/MyDrive/cnn_model" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "ij3PCeoCyU23" }, "outputs": [], "source": [ "def prediction(file):\n", " img = tf.keras.utils.load_img(file, target_size=(224, 224))\n", " x = tf.keras.utils.img_to_array(img)\n", " x = np.expand_dims(x, axis=0)\n", "\n", " # Load the saved model\n", " loaded_model = load_model('cnn_model.h5')\n", "\n", " # Predict the class probabilities\n", " classes = loaded_model.predict(x)\n", "\n", " # Get the predicted class label\n", " classes = np.ravel(classes) # convert to 1D array\n", " idx = np.argmax(classes)\n", " clas = ['adenocarcinoma', 'large.cell.carcinoma', 'normal', 'squamous.cell.carcinoma']\n", "\n", " # Print the predicted class label\n", " print('Prediction is a {}'.format(clas[idx]))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "h4xpRFtUw-OR", "outputId": "6af2e2d1-3f78-4a9a-a7b2-b8e83db72e6b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 2s 2s/step\n", "Prediction is a normal\n" ] } ], "source": [ "prediction('Adenocarcinoma-in-situ-Axial-contrast-enhanced-chest-CT-scan-with-lung-window.png')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "VG4syGUcYjSK" }, "source": [ "Prediction wrong, the model should maintained for next utilization" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python", "version": "3.7.16" } }, "nbformat": 4, "nbformat_minor": 0 }