Spaces:
Sleeping
Sleeping
Tanusree88
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,24 @@ import os
|
|
2 |
import zipfile
|
3 |
import numpy as np
|
4 |
import torch
|
5 |
-
import requests
|
6 |
from transformers import ViTForImageClassification, AdamW
|
7 |
import nibabel as nib
|
8 |
from PIL import Image
|
9 |
from torch.utils.data import Dataset, DataLoader
|
10 |
import streamlit as st
|
11 |
-
|
|
|
12 |
|
13 |
-
# Function to download
|
14 |
-
def
|
15 |
response = requests.get(url)
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
# Preprocess images
|
23 |
def preprocess_image(image_path):
|
@@ -45,13 +47,21 @@ def preprocess_image(image_path):
|
|
45 |
def prepare_dataset(extracted_folder):
|
46 |
image_paths = []
|
47 |
labels = []
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
for img_file in os.listdir(folder_path):
|
52 |
if img_file.endswith(('.nii', '.jpg', '.jpeg')):
|
53 |
image_paths.append(os.path.join(folder_path, img_file))
|
54 |
labels.append(label)
|
|
|
55 |
return image_paths, labels
|
56 |
|
57 |
# Custom Dataset class
|
@@ -92,23 +102,29 @@ def fine_tune_model(train_loader):
|
|
92 |
# Streamlit UI for Fine-tuning
|
93 |
st.title("Fine-tune ViT on MRI/CT Scans for MS & Neurodegenerative Diseases")
|
94 |
|
95 |
-
|
96 |
-
|
97 |
if st.button("Start Training"):
|
98 |
-
extraction_dir = "
|
99 |
os.makedirs(extraction_dir, exist_ok=True)
|
100 |
|
101 |
-
#
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# Prepare dataset
|
105 |
image_paths, labels = prepare_dataset(extraction_dir)
|
106 |
dataset = CustomImageDataset(image_paths, labels)
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
|
|
|
|
|
|
114 |
|
|
|
2 |
import zipfile
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
5 |
from transformers import ViTForImageClassification, AdamW
|
6 |
import nibabel as nib
|
7 |
from PIL import Image
|
8 |
from torch.utils.data import Dataset, DataLoader
|
9 |
import streamlit as st
|
10 |
+
import requests
|
11 |
+
import tempfile
|
12 |
|
13 |
+
# Function to download zip files from URL
|
14 |
+
def download_zip(url, download_path):
|
15 |
response = requests.get(url)
|
16 |
+
with open(download_path, 'wb') as file:
|
17 |
+
file.write(response.content)
|
18 |
+
|
19 |
+
# Function to extract zip files
|
20 |
+
def extract_zip(zip_file, extract_to):
|
21 |
+
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
22 |
+
zip_ref.extractall(extract_to)
|
23 |
|
24 |
# Preprocess images
|
25 |
def preprocess_image(image_path):
|
|
|
47 |
def prepare_dataset(extracted_folder):
|
48 |
image_paths = []
|
49 |
labels = []
|
50 |
+
|
51 |
+
# Define the paths for each disease dataset
|
52 |
+
datasets = {
|
53 |
+
'alzheimer_datasets': 0,
|
54 |
+
'parkinson_datasets': 1,
|
55 |
+
'MSjpg': 2
|
56 |
+
}
|
57 |
+
|
58 |
+
for disease_folder, label in datasets.items():
|
59 |
+
folder_path = os.path.join(extracted_folder, 'neuroniiimages', disease_folder)
|
60 |
for img_file in os.listdir(folder_path):
|
61 |
if img_file.endswith(('.nii', '.jpg', '.jpeg')):
|
62 |
image_paths.append(os.path.join(folder_path, img_file))
|
63 |
labels.append(label)
|
64 |
+
|
65 |
return image_paths, labels
|
66 |
|
67 |
# Custom Dataset class
|
|
|
102 |
# Streamlit UI for Fine-tuning
|
103 |
st.title("Fine-tune ViT on MRI/CT Scans for MS & Neurodegenerative Diseases")
|
104 |
|
105 |
+
zip_url = "https://huggingface.co/spaces/Tanusree88/ViT-MRI-FineTuning/resolve/main/neuroniiimages.zip"
|
106 |
+
|
107 |
if st.button("Start Training"):
|
108 |
+
extraction_dir = "extracted_files"
|
109 |
os.makedirs(extraction_dir, exist_ok=True)
|
110 |
|
111 |
+
# Download the zip file to a temporary file
|
112 |
+
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp_file:
|
113 |
+
download_zip(zip_url, tmp_file.name)
|
114 |
+
|
115 |
+
# Extract the zip file
|
116 |
+
extract_zip(tmp_file.name, extraction_dir)
|
117 |
|
118 |
# Prepare dataset
|
119 |
image_paths, labels = prepare_dataset(extraction_dir)
|
120 |
dataset = CustomImageDataset(image_paths, labels)
|
121 |
+
|
122 |
+
if len(image_paths) == 0:
|
123 |
+
st.error("No images found in the specified directory. Please check the folder structure.")
|
124 |
+
else:
|
125 |
+
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
|
|
|
126 |
|
127 |
+
# Fine-tune the model
|
128 |
+
final_loss = fine_tune_model(train_loader)
|
129 |
+
st.write(f"Training Complete with Final Loss: {final_loss}")
|
130 |
|