Update app.py
Browse files
app.py
CHANGED
|
@@ -7,25 +7,11 @@ from transformers import ResNetForImageClassification, AdamW
|
|
| 7 |
from PIL import Image
|
| 8 |
from torch.utils.data import Dataset, DataLoader
|
| 9 |
import streamlit as st
|
| 10 |
-
import gradio as gr
|
| 11 |
-
import os
|
| 12 |
-
import zipfile
|
| 13 |
-
import numpy as np
|
| 14 |
-
import torch
|
| 15 |
-
from transformers import SegformerForSemanticSegmentation, SegformerFeatureExtractor
|
| 16 |
-
from transformers import ResNetForImageClassification, AdamW
|
| 17 |
-
from PIL import Image
|
| 18 |
-
from torch.utils.data import Dataset, DataLoader
|
| 19 |
-
import streamlit as st
|
| 20 |
-
import gradio as gr
|
| 21 |
|
| 22 |
# Load feature extractor and model
|
| 23 |
feature_extractor = SegformerFeatureExtractor.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512')
|
| 24 |
segformer_model = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512')
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
# Function to extract zip files
|
| 30 |
def extract_zip(zip_file, extract_to):
|
| 31 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
|
@@ -34,7 +20,7 @@ def extract_zip(zip_file, extract_to):
|
|
| 34 |
# Preprocess images
|
| 35 |
def preprocess_image(image_path):
|
| 36 |
ext = os.path.splitext(image_path)[-1].lower()
|
| 37 |
-
|
| 38 |
if ext == '.npy':
|
| 39 |
image_data = np.load(image_path)
|
| 40 |
image_tensor = torch.tensor(image_data).float()
|
|
@@ -62,13 +48,19 @@ def prepare_dataset(extracted_folder):
|
|
| 62 |
image_paths = []
|
| 63 |
labels = []
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
folder_path = os.path.join(neuronii_path, disease_folder)
|
| 67 |
|
| 68 |
if not os.path.exists(folder_path):
|
| 69 |
print(f"Folder not found: {folder_path}")
|
| 70 |
continue
|
| 71 |
-
label = {'alzheimers_dataset': 0, 'parkinsons_dataset': 1, 'MSjpg': 2}[disease_folder]
|
| 72 |
|
| 73 |
for img_file in os.listdir(folder_path):
|
| 74 |
if img_file.endswith(('.npy', '.jpg', '.jpeg')):
|
|
@@ -97,9 +89,7 @@ class CustomImageDataset(Dataset):
|
|
| 97 |
def fine_tune_classification_model(train_loader):
|
| 98 |
# Load the ResNet model with ignore_mismatched_sizes
|
| 99 |
model = ResNetForImageClassification.from_pretrained('microsoft/resnet-50', num_labels=3, ignore_mismatched_sizes=True)
|
| 100 |
-
|
| 101 |
-
print(model) # Inspect the model structure
|
| 102 |
-
|
| 103 |
# Update the classifier layer to match the number of labels
|
| 104 |
if hasattr(model, 'classifier'):
|
| 105 |
if isinstance(model.classifier, torch.nn.Sequential):
|
|
@@ -116,7 +106,7 @@ def fine_tune_classification_model(train_loader):
|
|
| 116 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 117 |
model.to(device)
|
| 118 |
|
| 119 |
-
for epoch in range(10):
|
| 120 |
running_loss = 0.0
|
| 121 |
for images, labels in train_loader:
|
| 122 |
images, labels = images.to(device), labels.to(device)
|
|
@@ -164,7 +154,7 @@ def fine_tune_segmentation_model(train_loader):
|
|
| 164 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 165 |
model.to(device)
|
| 166 |
|
| 167 |
-
for epoch in range(10):
|
| 168 |
running_loss = 0.0
|
| 169 |
for images, labels in train_loader:
|
| 170 |
images, labels = images.to(device), labels.to(device)
|
|
@@ -185,3 +175,4 @@ if st.button("Start Segmentation Training"):
|
|
| 185 |
# Fine-tune the segmentation model
|
| 186 |
final_loss_seg = fine_tune_segmentation_model(seg_train_loader)
|
| 187 |
st.write(f"Segmentation Training Complete with Final Loss: {final_loss_seg}")
|
|
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from torch.utils.data import Dataset, DataLoader
|
| 9 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Load feature extractor and model
|
| 12 |
feature_extractor = SegformerFeatureExtractor.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512')
|
| 13 |
segformer_model = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to extract zip files
|
| 16 |
def extract_zip(zip_file, extract_to):
|
| 17 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
|
|
|
| 20 |
# Preprocess images
|
| 21 |
def preprocess_image(image_path):
|
| 22 |
ext = os.path.splitext(image_path)[-1].lower()
|
| 23 |
+
|
| 24 |
if ext == '.npy':
|
| 25 |
image_data = np.load(image_path)
|
| 26 |
image_tensor = torch.tensor(image_data).float()
|
|
|
|
| 48 |
image_paths = []
|
| 49 |
labels = []
|
| 50 |
|
| 51 |
+
# Define the mapping of folders to labels
|
| 52 |
+
folder_label_mapping = {
|
| 53 |
+
'alzheimers_dataset': 0,
|
| 54 |
+
'parkinsons_dataset': 1,
|
| 55 |
+
'MSjpg': 2
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
for disease_folder, label in folder_label_mapping.items():
|
| 59 |
folder_path = os.path.join(neuronii_path, disease_folder)
|
| 60 |
|
| 61 |
if not os.path.exists(folder_path):
|
| 62 |
print(f"Folder not found: {folder_path}")
|
| 63 |
continue
|
|
|
|
| 64 |
|
| 65 |
for img_file in os.listdir(folder_path):
|
| 66 |
if img_file.endswith(('.npy', '.jpg', '.jpeg')):
|
|
|
|
| 89 |
def fine_tune_classification_model(train_loader):
|
| 90 |
# Load the ResNet model with ignore_mismatched_sizes
|
| 91 |
model = ResNetForImageClassification.from_pretrained('microsoft/resnet-50', num_labels=3, ignore_mismatched_sizes=True)
|
| 92 |
+
|
|
|
|
|
|
|
| 93 |
# Update the classifier layer to match the number of labels
|
| 94 |
if hasattr(model, 'classifier'):
|
| 95 |
if isinstance(model.classifier, torch.nn.Sequential):
|
|
|
|
| 106 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 107 |
model.to(device)
|
| 108 |
|
| 109 |
+
for epoch in range(10): # Adjust epochs as needed
|
| 110 |
running_loss = 0.0
|
| 111 |
for images, labels in train_loader:
|
| 112 |
images, labels = images.to(device), labels.to(device)
|
|
|
|
| 154 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 155 |
model.to(device)
|
| 156 |
|
| 157 |
+
for epoch in range(10): # Adjust epochs as needed
|
| 158 |
running_loss = 0.0
|
| 159 |
for images, labels in train_loader:
|
| 160 |
images, labels = images.to(device), labels.to(device)
|
|
|
|
| 175 |
# Fine-tune the segmentation model
|
| 176 |
final_loss_seg = fine_tune_segmentation_model(seg_train_loader)
|
| 177 |
st.write(f"Segmentation Training Complete with Final Loss: {final_loss_seg}")
|
| 178 |
+
|