andromeda01111 commited on
Commit
5c48a95
·
verified ·
1 Parent(s): e022727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -1,34 +1,47 @@
1
  import torch
2
  import torchvision.transforms as transforms
 
3
  import gradio as gr
4
  import numpy as np
5
  import tensorflow as tf
6
  from PIL import Image
7
  from sklearn.preprocessing import StandardScaler
8
  import joblib
9
- from transformers import ViTForImageClassification, ViTImageProcessor
10
 
11
  # Set device
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
 
14
- # Load ViT model and processor from Hugging Face
15
- model_path = "andromeda01111/ViT_BCC"
 
16
 
17
- vit_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
18
- vit_model = ViTForImageClassification.from_pretrained(model_path).to(device)
 
 
 
19
  vit_model.eval()
20
- vit_processor = ViTImageProcessor.from_pretrained(model_path)
21
 
22
- # Load Neural Network model
23
- nn_model = tf.keras.models.load_model("andromeda01111/NN_BC")
24
-
25
- # Load scaler
26
- scaler = joblib.load("scaler.pkl")
 
27
 
28
  # Class labels
29
  class_names = ["Benign", "Malignant"]
30
 
31
- # Define feature names
 
 
 
 
 
 
 
 
32
  feature_names = [
33
  "Mean Radius", "Mean Texture", "Mean Perimeter", "Mean Area", "Mean Smoothness",
34
  "Mean Compactness", "Mean Concavity", "Mean Concave Points", "Mean Symmetry", "Mean Fractal Dimension",
@@ -39,15 +52,16 @@ feature_names = [
39
  ]
40
 
41
  def classify(model_choice, image=None, *features):
 
42
  if model_choice == "ViT":
43
  if image is None:
44
- return "Please upload an image."
45
  image = image.convert("RGB")
46
- inputs = vit_processor(images=image, return_tensors="pt").to(device)
47
 
48
  with torch.no_grad():
49
- outputs = vit_model(**inputs)
50
- predicted_class = torch.argmax(outputs.logits, dim=1).item()
51
 
52
  return class_names[predicted_class]
53
 
@@ -56,8 +70,8 @@ def classify(model_choice, image=None, *features):
56
  return "Please enter all 30 numerical features."
57
 
58
  input_data = np.array(features).reshape(1, -1)
59
- input_data_std = scaler.transform(input_data)
60
- prediction = nn_model.predict(input_data_std)
61
  predicted_class = np.argmax(prediction)
62
 
63
  return class_names[predicted_class]
@@ -72,7 +86,7 @@ iface = gr.Interface(
72
  inputs=[model_selector, image_input] + feature_inputs,
73
  outputs="text",
74
  title="Breast Cancer Classification",
75
- description="Choose ViT (image-based) or Neural Network (feature-based) classification."
76
  )
77
 
78
- iface.launch()
 
1
  import torch
2
  import torchvision.transforms as transforms
3
+ import torchvision.models as models
4
  import gradio as gr
5
  import numpy as np
6
  import tensorflow as tf
7
  from PIL import Image
8
  from sklearn.preprocessing import StandardScaler
9
  import joblib
10
+ import os
11
 
12
  # Set device
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
 
15
+ # Load trained ViT model (PyTorch)
16
+ vit_model = models.vit_b_16(pretrained=False)
17
+ vit_model.heads = torch.nn.Linear(in_features=768, out_features=2) # Binary classification
18
 
19
+ # Load ViT model weights
20
+ vit_model_path = "vit_bc.pth" # Update with uploaded model path
21
+ if os.path.exists(vit_model_path):
22
+ vit_model.load_state_dict(torch.load(vit_model_path, map_location=device))
23
+ vit_model.to(device)
24
  vit_model.eval()
 
25
 
26
+ # Define ViT image transformations
27
+ transform = transforms.Compose([
28
+ transforms.Resize((224, 224)),
29
+ transforms.ToTensor(),
30
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
31
+ ])
32
 
33
  # Class labels
34
  class_names = ["Benign", "Malignant"]
35
 
36
+ # Load trained Neural Network model (TensorFlow/Keras)
37
+ nn_model_path = "my_NN_BC_model.keras" # Update with uploaded model path
38
+ nn_model = tf.keras.models.load_model(nn_model_path) if os.path.exists(nn_model_path) else None
39
+
40
+ # Load scaler for feature normalization
41
+ scaler_path = "nn_bc_scaler.pkl" # Update with uploaded model path
42
+ scaler = joblib.load(scaler_path) if os.path.exists(scaler_path) else None
43
+
44
+ # Feature names
45
  feature_names = [
46
  "Mean Radius", "Mean Texture", "Mean Perimeter", "Mean Area", "Mean Smoothness",
47
  "Mean Compactness", "Mean Concavity", "Mean Concave Points", "Mean Symmetry", "Mean Fractal Dimension",
 
52
  ]
53
 
54
  def classify(model_choice, image=None, *features):
55
+ """Classify using ViT (image) or NN (features)."""
56
  if model_choice == "ViT":
57
  if image is None:
58
+ return "Please upload an image for ViT classification."
59
  image = image.convert("RGB")
60
+ input_tensor = transform(image).unsqueeze(0).to(device)
61
 
62
  with torch.no_grad():
63
+ output = vit_model(input_tensor)
64
+ predicted_class = torch.argmax(output, dim=1).item()
65
 
66
  return class_names[predicted_class]
67
 
 
70
  return "Please enter all 30 numerical features."
71
 
72
  input_data = np.array(features).reshape(1, -1)
73
+ input_data_std = scaler.transform(input_data) if scaler else input_data
74
+ prediction = nn_model.predict(input_data_std) if nn_model else [[0, 1]]
75
  predicted_class = np.argmax(prediction)
76
 
77
  return class_names[predicted_class]
 
86
  inputs=[model_selector, image_input] + feature_inputs,
87
  outputs="text",
88
  title="Breast Cancer Classification",
89
+ description="Choose between ViT (image-based) and Neural Network (feature-based) classification."
90
  )
91
 
92
+ iface.launch()