DeepActionPotential commited on
Commit
9a52777
·
verified ·
1 Parent(s): f52350c

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/brain_tumor_demo.mp4 filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,13 +1,109 @@
1
- ---
2
- title: TumrioAI
3
- emoji: 🐠
4
- colorFrom: green
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.33.0
8
- app_file: app.py
9
- pinned: false
10
- short_description: TumrioAI is a brain MRI classifier app.
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Brain Tumor MRI Classification
2
+
3
+ ## About the Project
4
+
5
+ This project aims to automate the classification of brain tumors from MRI images using deep learning techniques. Early and accurate detection of brain tumors is critical for effective treatment and improved patient outcomes. By leveraging Convolutional Neural Networks (CNNs), this project provides a robust solution for distinguishing between different types of brain tumors and healthy brain scans. The solution is designed to assist clinicians and researchers by providing fast, reliable, and reproducible results, reducing the burden of manual image analysis and supporting clinical decision-making.
6
+
7
+ The project encompasses the entire machine learning pipeline, including data preprocessing, model development, training, evaluation, and deployment. The codebase is organized for clarity and reproducibility, making it easy for other researchers and practitioners to adapt or extend the work for related medical imaging tasks.
8
+
9
+ ## About the Dataset
10
+
11
+ The dataset used in this project is sourced from Kaggle: [Brain Tumor MRI Dataset](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset/data).
12
+
13
+ - **Total Images:** 7,023 MRI scans
14
+ - **Classes:** Glioma Tumor, Meningioma Tumor, Pituitary Tumor, No Tumor
15
+ - **Data Sources:** The dataset is a combination of multiple sources, including Figshare, SARTAJ, and Br35H datasets, ensuring diversity in imaging conditions and patient demographics.
16
+ - **Image Characteristics:** Images vary in size, resolution, and margin. Some classes, such as Glioma, required relabeling due to inconsistencies in the original datasets.
17
+ - **Data Structure:** The dataset is organized into separate folders for training and testing, with subfolders for each class.
18
+
19
+ This dataset presents real-world challenges such as class imbalance, varying image quality, and potential mislabeling, making it suitable for developing robust and generalizable models.
20
+
21
+ ## Notebook Summary
22
+
23
+ The provided Jupyter notebook walks through the complete workflow for brain tumor classification:
24
+
25
+ 1. **Problem Definition:** Outlines the clinical motivation and the importance of automated tumor detection.
26
+ 2. **Data Preparation:** Details the loading, augmentation, and normalization of MRI images to ensure consistency and improve model generalization.
27
+ 3. **Model Building:** Implements a custom CNN architecture tailored for multi-class classification of MRI images.
28
+ 4. **Training:** Trains the model using the prepared dataset, tracks performance metrics, and saves the best-performing model.
29
+ 5. **Evaluation:** Evaluates the trained model using accuracy, precision, recall, F1-score, and confusion matrix. Visualizes both the training process and the results.
30
+ 6. **Results and Discussion:** Summarizes findings and suggests potential improvements, such as increasing training epochs or experimenting with different architectures.
31
+
32
+ The notebook is modular, with clear separation between data handling, model logic, and evaluation, making it easy to follow and modify.
33
+
34
+ ## Model Results
35
+
36
+ ### Preprocessing
37
+
38
+ - **Resizing:** All images are resized to 224x224 pixels to standardize input dimensions for the CNN.
39
+ - **Augmentation:** Random horizontal flips and rotations (up to 10 degrees) are applied to increase data diversity and reduce overfitting.
40
+ - **Normalization:** Images are normalized using ImageNet mean and standard deviation values to facilitate transfer learning and stable training.
41
+
42
+ ### Training
43
+
44
+ - **Architecture:** The model is a custom CNN with two convolutional layers followed by fully connected layers. It is designed to balance complexity and computational efficiency.
45
+ - **Loss Function:** Cross-entropy loss is used for multi-class classification.
46
+ - **Optimizer:** Adam optimizer is employed for efficient gradient-based optimization.
47
+ - **Training Regimen:** The model is trained for 15 epochs (with suggestions to increase to 20-25 for improved results), using separate loaders for training and validation data.
48
+ - **Checkpointing:** The best model (based on validation accuracy) is saved for later evaluation.
49
+
50
+ ### Evaluation
51
+
52
+ - **Metrics:** The model is evaluated using accuracy, precision, recall, and F1-score (macro-averaged for multi-class).
53
+ - **Confusion Matrix:** A confusion matrix is plotted to visualize class-wise performance and identify potential misclassifications.
54
+ - **Training History:** Loss and accuracy curves are plotted for both training and validation sets to monitor learning dynamics and detect overfitting or underfitting.
55
+
56
+ The model achieves a macro F1-score of approximately 0.97, demonstrating strong performance across all classes. Further improvements can be made by tuning hyperparameters, increasing training epochs, or experimenting with deeper architectures.
57
+
58
+ ## How to Install
59
+
60
+ Follow these steps to set up the environment using Python's built-in `venv` module:
61
+
62
+ ```bash
63
+ # Clone the repository
64
+ git clone https://github.com/DeepActionPotential/TumrioAI
65
+ cd TumrioAI
66
+
67
+
68
+ # Create a virtual environment
69
+ python -m venv venv
70
+
71
+ # Activate the virtual environment
72
+ # On Windows:
73
+ venv\Scripts\activate
74
+ # On macOS/Linux:
75
+ source venv/bin/activate
76
+
77
+
78
+ # Install required dependencies
79
+ pip install -r requirements.txt
80
+ ```
81
+
82
+
83
+
84
+ ## How to Use the Software
85
+
86
+
87
+
88
+ 1. **Demo:**
89
+ ## [demo-video](assets/brain_tumor_demo.mp4)
90
+ ![Demo-screenshot](assets/1.jpg)
91
+
92
+
93
+ ## Technologies Used
94
+
95
+
96
+ - **PyTorch:** Used for building, training, and evaluating the deep learning model. PyTorch provides flexibility and ease of use for custom model development.
97
+ - **Torchvision:** Supplies datasets, model architectures, and image transformation utilities, streamlining the data preprocessing and augmentation process.
98
+ - **NumPy:** Facilitates efficient numerical computations and array manipulations.
99
+ - **Matplotlib & Seaborn:** Used for data visualization, including plotting images, training curves, and confusion matrices.
100
+ - **scikit-learn:** Provides metrics for model evaluation, such as precision, recall, and F1-score.
101
+ - **Jupyter Notebook / VS Code:** Interactive development environments for running and documenting experiments.
102
+
103
+ These technologies were chosen for their robustness, community support, and suitability for rapid prototyping and research in deep learning and computer vision.
104
+
105
+ ## License
106
+
107
+ This project is licensed under the MIT License. You are free to use, modify, and distribute this software for personal, academic, or commercial purposes, provided that you include the original copyright and license notice.
108
+
109
+ See the [LICENSE](LICENSE) file for more details.
__pycache__/ui.cpython-311.pyc ADDED
Binary file (967 Bytes). View file
 
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from ui import interface
2
+
3
+ if __name__ == "__main__":
4
+ interface.launch()
assets/brain_tumor_demo.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bbad40b42c2df7c18f29ebf0f01d0988027bfb1a07f6bf35e9613c16c2e2f55f
3
+ size 1601581
brain-tumor-mri-classification-f1-score-97.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
models/model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:04ef0fc495f157cf70c396c7d6817b4314a897961803ce760ee9a7d87351056d
3
+ size 51409572
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch==2.2.2
2
+ torchvision==0.17.2
3
+ Pillow==10.3.0
4
+ gradio==4.31.2
ui.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import load_model, predict
3
+
4
+ # Load model once globally
5
+ model = load_model("./models/model.pth")
6
+
7
+ def classify_image(image):
8
+ """
9
+ Gradio wrapper for prediction.
10
+ """
11
+ return predict(model, image)
12
+
13
+ # Build the interface
14
+ interface = gr.Interface(
15
+ fn=classify_image,
16
+ inputs=gr.Image(type="pil"),
17
+ outputs=gr.Label(num_top_classes=4),
18
+ title="TumrioAi - Brain Tumor Classifier",
19
+ description="Upload a brain MRI image and the model will predict: glioma, meningioma, pituitary tumor, or no tumor."
20
+ )
utils.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.transforms as transforms
4
+ from PIL import Image
5
+ import __main__ # we’ll use this to “register” BrainTumorNet under __main__
6
+
7
+ # --------------------------
8
+ # 1. Define your custom model class
9
+ # --------------------------
10
+ class BrainTumorNet(nn.Module):
11
+ def __init__(self, num_classes):
12
+ super(BrainTumorNet, self).__init__()
13
+ self.conv_layers = nn.Sequential(
14
+ nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1),
15
+ nn.ReLU(inplace=True),
16
+ nn.MaxPool2d(kernel_size=2, stride=2),
17
+ nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1),
18
+ nn.ReLU(inplace=True),
19
+ nn.MaxPool2d(kernel_size=2, stride=2)
20
+ )
21
+ self.fc_layers = nn.Sequential(
22
+ nn.Linear(in_features=32 * 56 * 56, out_features=128),
23
+ nn.ReLU(inplace=True),
24
+ nn.Linear(in_features=128, out_features=num_classes)
25
+ )
26
+
27
+ def forward(self, input_tensor):
28
+ x = self.conv_layers(input_tensor)
29
+ x = x.view(x.size(0), -1)
30
+ x = self.fc_layers(x)
31
+ return x
32
+
33
+ # --------------------------
34
+ # 2. Define label mapping
35
+ # --------------------------
36
+ LABELS = {0: 'glioma', 1: 'meningioma', 2: 'notumor', 3: 'pituitary'}
37
+
38
+ # --------------------------
39
+ # 3. Define transform pipeline
40
+ # --------------------------
41
+ transform_pipeline = transforms.Compose([
42
+ transforms.Resize((224, 224)),
43
+ transforms.RandomHorizontalFlip(),
44
+ transforms.RandomRotation(degrees=10),
45
+ transforms.ToTensor(),
46
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
47
+ std=[0.229, 0.224, 0.225])
48
+ ])
49
+
50
+ # --------------------------
51
+ # 4. Full model loader
52
+ # --------------------------
53
+ def load_model(model_path: str):
54
+ """
55
+ Load the entire saved model (architecture + weights).
56
+ We must register BrainTumorNet under __main__ so that torch.load can unpickle it.
57
+ """
58
+ # 1) “Alias” BrainTumorNet into __main__ so that pickle.find_class("__main__", "BrainTumorNet") works:
59
+ __main__.BrainTumorNet = BrainTumorNet
60
+
61
+ # 2) Now load the model (saved via torch.save(model))
62
+ model = torch.load(model_path, weights_only=False, map_location=torch.device('cpu'))
63
+ model.eval()
64
+ return model
65
+
66
+ # --------------------------
67
+ # 5. Prediction function
68
+ # --------------------------
69
+ def predict(model, image: Image.Image):
70
+ """
71
+ Preprocess the PIL image and run inference.
72
+ Returns the predicted label string.
73
+ """
74
+ img_tensor = transform_pipeline(image).unsqueeze(0) # add batch dimension
75
+ with torch.no_grad():
76
+ output = model(img_tensor)
77
+ _, predicted = torch.max(output, 1)
78
+ return LABELS[predicted.item()]