Tanusree88 commited on
Commit
4b003fd
·
verified ·
1 Parent(s): bb90c09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -32
app.py CHANGED
@@ -7,14 +7,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):
@@ -45,18 +37,24 @@ def preprocess_image(image_path):
45
 
46
  # Prepare dataset
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))
@@ -102,29 +100,24 @@ def fine_tune_model(train_loader):
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
 
 
 
 
 
7
  from PIL import Image
8
  from torch.utils.data import Dataset, DataLoader
9
  import streamlit as st
 
 
 
 
 
 
 
 
10
 
11
  # Function to extract zip files
12
  def extract_zip(zip_file, extract_to):
 
37
 
38
  # Prepare dataset
39
  def prepare_dataset(extracted_folder):
40
+ # Ensure the path exists
41
+ neuronii_path = os.path.join(extracted_folder, "neuroniiimages")
42
+
43
+ if not os.path.exists(neuronii_path):
44
+ raise FileNotFoundError(f"The folder neuroniiimages does not exist in the extracted folder: {neuronii_path}")
45
+
46
  image_paths = []
47
  labels = []
48
 
49
+ for disease_folder in ['alzheimers_datasets', 'parkinson_datasets', 'MSjpg']:
50
+ folder_path = os.path.join(neuronii_path, disease_folder)
51
+
52
+ # Check if the subfolder exists
53
+ if not os.path.exists(folder_path):
54
+ raise FileNotFoundError(f"The folder {disease_folder} does not exist at path: {folder_path}")
55
+
56
+ label = {'alzheimers_datasets': 0, 'parkinson_datasets': 1, 'MSjpg': 2}[disease_folder]
57
+
58
  for img_file in os.listdir(folder_path):
59
  if img_file.endswith(('.nii', '.jpg', '.jpeg')):
60
  image_paths.append(os.path.join(folder_path, img_file))
 
100
  # Streamlit UI for Fine-tuning
101
  st.title("Fine-tune ViT on MRI/CT Scans for MS & Neurodegenerative Diseases")
102
 
103
+ # Provide the correct zip file URL
104
+ zip_file_url = "https://huggingface.co/spaces/Tanusree88/ViT-MRI-FineTuning/resolve/main/neuroniiimages.zip"
105
 
106
  if st.button("Start Training"):
107
  extraction_dir = "extracted_files"
108
  os.makedirs(extraction_dir, exist_ok=True)
109
 
110
+ # Download the zip file (this is a placeholder; use requests or any other method to download the zip file)
111
+ zip_file = "neuroniiimages.zip" # Assuming you downloaded it with this name
 
112
 
113
+ # Extract zip file
114
+ extract_zip(zip_file, extraction_dir)
115
 
116
  # Prepare dataset
117
  image_paths, labels = prepare_dataset(extraction_dir)
118
  dataset = CustomImageDataset(image_paths, labels)
119
+ train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
 
 
 
 
 
 
 
 
120
 
121
+ # Fine-tune the model
122
+ final_loss = fine_tune_model(train_loader)
123
+ st.write(f"Training Complete with Final Loss: {final_loss}")