{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "dc7ab4859abf4fdaa598729ec421129a": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_5441781ef8f046dd979a8c389a8e7587", "IPY_MODEL_999c37c9cce448a792df58915545135f", "IPY_MODEL_db66afed001a4324bdd44f51efeaafa1" ], "layout": "IPY_MODEL_11cb47a0455044af83969ae1994af22f" } }, "5441781ef8f046dd979a8c389a8e7587": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_f884e42910e14b5aa9dc9919558cf75f", "placeholder": "​", "style": "IPY_MODEL_53cc94f964d54d71923bc295d2882216", "value": "config.json: 100%" } }, "999c37c9cce448a792df58915545135f": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_b0a3d7e1d2ea4e3bb7e3264c8089b26d", "max": 71813, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_279f9349c7334713bb760bbc4bff3d3c", "value": 71813 } }, "db66afed001a4324bdd44f51efeaafa1": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_865cd86f5627433e9d92c1de46917f08", "placeholder": "​", "style": "IPY_MODEL_73db46b258794cfe922424d06689e44e", "value": " 71.8k/71.8k [00:00<00:00, 3.22MB/s]" } }, "11cb47a0455044af83969ae1994af22f": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "f884e42910e14b5aa9dc9919558cf75f": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "53cc94f964d54d71923bc295d2882216": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "b0a3d7e1d2ea4e3bb7e3264c8089b26d": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "279f9349c7334713bb760bbc4bff3d3c": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "865cd86f5627433e9d92c1de46917f08": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "73db46b258794cfe922424d06689e44e": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "cd77cd0c2d1741c39b9ca6f36ec562c3": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_4f28a143f3cd438b87a74a62d9c5bea4", "IPY_MODEL_256dc2937cb2449f84ec14b9dae2e51f", "IPY_MODEL_a60bc29c8c1044f690a6cd4f74ed7c2c" ], "layout": "IPY_MODEL_a33d503834a741a9a23a26c03c1c3aaf" } }, "4f28a143f3cd438b87a74a62d9c5bea4": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_a2f0b441f346498faabf97d5d8eabd3a", "placeholder": "​", "style": "IPY_MODEL_3243191638d14067a1565ae2f03f48de", "value": "model.safetensors: 100%" } }, "256dc2937cb2449f84ec14b9dae2e51f": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_f0b74718119e4f3a9bb6295bc7567f6d", "max": 113412768, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_fd6d8576cbc444e1b22111ddf4fa1dc2", "value": 113412768 } }, "a60bc29c8c1044f690a6cd4f74ed7c2c": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_c469ed61c6b946b78bb0308f36cb915b", "placeholder": "​", "style": "IPY_MODEL_e0db11f2d42744e982e2206a1640344a", "value": " 113M/113M [00:01<00:00, 130MB/s]" } }, "a33d503834a741a9a23a26c03c1c3aaf": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "a2f0b441f346498faabf97d5d8eabd3a": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "3243191638d14067a1565ae2f03f48de": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "f0b74718119e4f3a9bb6295bc7567f6d": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "fd6d8576cbc444e1b22111ddf4fa1dc2": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "c469ed61c6b946b78bb0308f36cb915b": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "e0db11f2d42744e982e2206a1640344a": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "cells": [ { "cell_type": "markdown", "source": [ "## using pre-trained swin transformer to train the model" ], "metadata": { "id": "kuFuGmlMnSSF" } }, { "cell_type": "code", "source": [ "import torch\n", "from torchvision import datasets, transforms\n", "from torch.utils.data import DataLoader\n", "\n", "# Define transformation pipeline\n", "transform = transforms.Compose([\n", " transforms.Resize((224, 224)), # Resize to 224x224 for Swin Transformer\n", " transforms.ToTensor(),\n", "])\n", "\n", "# Load dataset\n", "train_dataset = datasets.ImageFolder(root='/content/drive/MyDrive/data/brain_tumor_dataset/train', transform=transform)\n", "val_dataset = datasets.ImageFolder(root='/content/drive/MyDrive/data/brain_tumor_dataset/test', transform=transform)\n", "\n", "# Create dataloaders\n", "train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\n", "val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n" ], "metadata": { "id": "dacVmRJWSB-7" }, "execution_count": 1, "outputs": [] }, { "cell_type": "code", "source": [ "from transformers import SwinForImageClassification\n", "\n", "# Load the pre-trained Swin Transformer model with 4 output classes\n", "model = SwinForImageClassification.from_pretrained(\n", " 'microsoft/swin-tiny-patch4-window7-224',\n", " num_labels=2, # Number of tumor types\n", " ignore_mismatched_sizes=True # Ignore size mismatch for the classifier layer\n", ")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 170, "referenced_widgets": [ "dc7ab4859abf4fdaa598729ec421129a", "5441781ef8f046dd979a8c389a8e7587", "999c37c9cce448a792df58915545135f", "db66afed001a4324bdd44f51efeaafa1", "11cb47a0455044af83969ae1994af22f", "f884e42910e14b5aa9dc9919558cf75f", "53cc94f964d54d71923bc295d2882216", "b0a3d7e1d2ea4e3bb7e3264c8089b26d", "279f9349c7334713bb760bbc4bff3d3c", "865cd86f5627433e9d92c1de46917f08", "73db46b258794cfe922424d06689e44e", "cd77cd0c2d1741c39b9ca6f36ec562c3", "4f28a143f3cd438b87a74a62d9c5bea4", "256dc2937cb2449f84ec14b9dae2e51f", "a60bc29c8c1044f690a6cd4f74ed7c2c", "a33d503834a741a9a23a26c03c1c3aaf", "a2f0b441f346498faabf97d5d8eabd3a", "3243191638d14067a1565ae2f03f48de", "f0b74718119e4f3a9bb6295bc7567f6d", "fd6d8576cbc444e1b22111ddf4fa1dc2", "c469ed61c6b946b78bb0308f36cb915b", "e0db11f2d42744e982e2206a1640344a" ] }, "id": "WQb3MZ2oSTLr", "outputId": "f159d174-0444-44de-dfe0-b77a20694146" }, "execution_count": 2, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "config.json: 0%| | 0.00/71.8k [00:00:2: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " model.load_state_dict(torch.load('swin_brain_tumor_classifier.pth'))\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Predicted class: 0\n" ] } ] }, { "cell_type": "code", "source": [ "#healthy tumor\n", "path = '/content/drive/MyDrive/data/brain_tumor_dataset/test/healthy'\n" ], "metadata": { "id": "gzK2C_4-k8F1" }, "execution_count": 21, "outputs": [] }, { "cell_type": "code", "source": [ "import os" ], "metadata": { "id": "6EDJd38AxZFv" }, "execution_count": 22, "outputs": [] }, { "cell_type": "code", "source": [ "files = os.listdir(path)\n", "\n", "for f in files:\n", " try:\n", " img = Image.open(os.path.join(path,f))\n", " img = transform(img).unsqueeze(0).to(device)\n", " output = model(img).logits\n", " _, predicted = torch.max(output, 1)\n", " print(f'predicted class: {predicted.item()} filename: {f} actual class: 0')\n", " except Exception as e:\n", " print(e)\n", " continue" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1jTDGblkxdAn", "outputId": "5610b6fc-ac47-4a6b-9314-f5398ea2787d" }, "execution_count": 25, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "predicted class: 0 filename: 0796.jpg actual class: 0\n", "predicted class: 0 filename: 0676.jpg actual class: 0\n", "predicted class: 1 filename: 0698.jpg actual class: 0\n", "predicted class: 1 filename: 0601.jpg actual class: 0\n", "predicted class: 0 filename: 0861.jpg actual class: 0\n", "predicted class: 1 filename: 0615.jpg actual class: 0\n", "predicted class: 0 filename: 0874.jpg actual class: 0\n", "predicted class: 0 filename: 0820.jpg actual class: 0\n", "predicted class: 0 filename: 0785.jpg actual class: 0\n", "predicted class: 0 filename: 0792.jpg actual class: 0\n", "predicted class: 0 filename: 0731.jpg actual class: 0\n", "predicted class: 0 filename: 0762.jpg actual class: 0\n", "predicted class: 1 filename: 0710.jpg actual class: 0\n", "predicted class: 0 filename: 0858.jpg actual class: 0\n", "predicted class: 0 filename: 0691.jpg actual class: 0\n", "predicted class: 0 filename: 0791.jpg actual class: 0\n", "predicted class: 1 filename: 0639.jpg actual class: 0\n", "predicted class: 1 filename: 0596.jpg actual class: 0\n", "predicted class: 1 filename: 0591.jpg actual class: 0\n", "predicted class: 0 filename: 0730.jpg actual class: 0\n", "predicted class: 0 filename: 0638.jpg actual class: 0\n", "predicted class: 0 filename: 0566.jpg actual class: 0\n", "predicted class: 1 filename: 0645.jpg actual class: 0\n", "predicted class: 0 filename: 0565.jpg actual class: 0\n", "predicted class: 1 filename: 0778.jpg actual class: 0\n", "predicted class: 0 filename: 0697.jpg actual class: 0\n", "predicted class: 1 filename: 0800.jpg actual class: 0\n", "predicted class: 0 filename: 0857.jpg actual class: 0\n", "predicted class: 0 filename: 0879.jpg actual class: 0\n", "predicted class: 0 filename: 0765.jpg actual class: 0\n", "predicted class: 0 filename: 0562.jpg actual class: 0\n", "predicted class: 0 filename: 0719.jpg actual class: 0\n", "predicted class: 0 filename: 0740.jpg actual class: 0\n", "predicted class: 0 filename: 0607.jpg actual class: 0\n", "predicted class: 0 filename: 0580.jpg actual class: 0\n", "predicted class: 1 filename: 0839.jpg actual class: 0\n", "predicted class: 0 filename: 0860.jpg actual class: 0\n", "predicted class: 0 filename: 0718.jpg actual class: 0\n", "predicted class: 0 filename: 0793.jpg actual class: 0\n", "predicted class: 0 filename: 0881.jpg actual class: 0\n", "predicted class: 0 filename: 0864.jpg actual class: 0\n", "predicted class: 0 filename: 0696.jpg actual class: 0\n", "predicted class: 0 filename: 0724.jpg actual class: 0\n", "predicted class: 0 filename: 0703.jpg actual class: 0\n", "predicted class: 0 filename: 0721.jpg actual class: 0\n", "predicted class: 1 filename: 0652.jpg actual class: 0\n", "predicted class: 0 filename: 0551.jpg actual class: 0\n", "predicted class: 0 filename: 0720.jpg actual class: 0\n", "predicted class: 0 filename: 0689.jpg actual class: 0\n", "predicted class: 0 filename: 0795.jpg actual class: 0\n", "predicted class: 1 filename: 0571.jpg actual class: 0\n", "predicted class: 0 filename: 0640.jpg actual class: 0\n", "predicted class: 0 filename: 0806.jpg actual class: 0\n", "predicted class: 0 filename: 0761.jpg actual class: 0\n", "predicted class: 1 filename: 0715.jpg actual class: 0\n", "predicted class: 0 filename: 0884.jpg actual class: 0\n", "predicted class: 1 filename: 0684.jpg actual class: 0\n", "predicted class: 0 filename: 0846.jpg actual class: 0\n", "predicted class: 0 filename: 0805.jpg actual class: 0\n", "predicted class: 0 filename: 0872.jpg actual class: 0\n", "predicted class: 1 filename: 0707.jpg actual class: 0\n", "predicted class: 0 filename: 0868.jpg actual class: 0\n", "predicted class: 1 filename: 0863.jpg actual class: 0\n", "predicted class: 1 filename: 0871.jpg actual class: 0\n", "predicted class: 0 filename: 0859.jpg actual class: 0\n", "predicted class: 1 filename: 0769.jpg actual class: 0\n", "predicted class: 0 filename: 0888.jpg actual class: 0\n", "predicted class: 0 filename: 0733.jpg actual class: 0\n", "predicted class: 0 filename: 0835.jpg actual class: 0\n", "predicted class: 1 filename: 0841.jpg actual class: 0\n", "predicted class: 0 filename: 0768.jpg actual class: 0\n", "predicted class: 1 filename: 0878.jpg actual class: 0\n", "[Errno 21] Is a directory: '/content/drive/MyDrive/data/brain_tumor_dataset/test/healthy/.ipynb_checkpoints'\n" ] } ] }, { "cell_type": "code", "source": [ "#calculating the accuracy\n", "def calculate_accuracy(model, img_path, img_files, actual_class):\n", " total_images = len(img_files)\n", " predicted_ones = 0\n", " for i in img_files:\n", " try:\n", " img = Image.open(os.path.join(img_path,i))\n", " img = transform(img).unsqueeze(0).to(device)\n", " output = model(img).logits\n", " _, predicted = torch.max(output, 1)\n", " if int(predicted.item()) == int(actual_class):\n", " predicted_ones += 1\n", " except Exception as e:\n", " continue\n", " accuracy_score = (predicted_ones/total_images)*100\n", " return accuracy_score" ], "metadata": { "id": "j92hyOmB15e0" }, "execution_count": 39, "outputs": [] }, { "cell_type": "code", "source": [ "img_path = '/content/drive/MyDrive/data/brain_tumor_dataset/train/healthy'\n", "img_files = os.listdir(img_path)\n", "print(\"Accuracy score:\",calculate_accuracy(model, img_path, img_files, 0))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mAMy1XYG5DCw", "outputId": "5ff76b58-7972-4b05-ea88-5758e0c17483" }, "execution_count": 40, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy score: 62.02830188679245\n" ] } ] }, { "cell_type": "code", "source": [ "img_path = '/content/drive/MyDrive/data/brain_tumor_dataset/train/tumor'\n", "img_files = os.listdir(img_path)\n", "print(\"Accuracy score:\",calculate_accuracy(model, img_path, img_files, 1))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "I4y62G1K5lXF", "outputId": "fb1f6ebe-0862-4f62-e268-8bc42fd803f7" }, "execution_count": 41, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy score: 85.65310492505354\n" ] } ] }, { "cell_type": "markdown", "source": [ "### For healthy class model accuracy score is 62%\n", "### For tumor images model accuracy score is 85%" ], "metadata": { "id": "oLFI0ASV7YKJ" } } ] }