Minbz commited on
Commit
fdcfae8
·
verified ·
1 Parent(s): e95e282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -61
app.py CHANGED
@@ -3,68 +3,13 @@ os.system('pip install tf_keras')
3
 
4
  import torch
5
  from torchvision import transforms
6
-
7
  import tensorflow as tf
8
- import tensorflow_hub as hub
9
  import tf_keras
10
- from tf_keras.layers import Input, Dense, Dropout, GlobalAveragePooling2D
11
- from tf_keras.models import Model, Sequential
12
- from tf_keras.optimizers import Adam
13
- from tf_keras.preprocessing import image
14
- from tf_keras.applications.vgg16 import preprocess_input
15
-
16
  import numpy as np
17
- # from tensorflow.keras.layers import Input, Dense, Dropout, GlobalAveragePooling2D
18
- # from tensorflow.keras.models import Model, Sequential
19
- # from tensorflow.keras.optimizers import Adam
20
- # from tensorflow.keras.preprocessing import image
21
- # from tensorflow.keras.applications.vgg16 import preprocess_input
22
- # from transformers import TFAutoModelForSequenceClassification
23
-
24
  import gradio as gr
25
 
26
- model_pb_path = "./4-15_ViT-B16_Lung3_v1/"
27
- model_keras_path = "./model.keras"
28
- model_h5_path = "./model.h5"
29
-
30
- # tf.config.experimental._could_use_gpu_kernel = False
31
-
32
- # model = TFAutoModelForSequenceClassification.from_pretrained(model_pb_path)
33
- model = tf_keras.models.load_model(model_pb_path)
34
- # model = tf.keras.models.load_model(model_keras_path)
35
- # model = tf.keras.models.load_model(model_h5_path)
36
- # model.compile(optimizer=Adam(learning_rate=0.0001),
37
- # loss='binary_crossentropy',
38
- # metrics=['accuracy'])
39
-
40
- #------------------------------------------------------------
41
- # model_url = "https://tfhub.dev/sayakpaul/vit_b16_classification/1"
42
-
43
- # # Load the ViT-B16 model pre-trained on ImageNet
44
- # try:
45
- # vit_b16 = hub.KerasLayer(model_url, trainable=False, input_shape=(224, 224, 3))
46
- # except Exception as e:
47
- # print(f"Failed to load the model from {model_url}. Error: {e}")
48
- # # Handle the error if the model cannot be loaded
49
- # raise
50
-
51
- # # Create a Sequential model
52
- # model = Sequential([
53
- # vit_b16,
54
- # Dense(1024, activation='relu'),
55
- # Dropout(0.5),
56
- # Dense(1, activation='sigmoid')
57
- # ])
58
-
59
- # # Compile the model
60
- # model.compile(optimizer=Adam(learning_rate=0.0001),
61
- # loss='binary_crossentropy',
62
- # metrics=['accuracy'])
63
-
64
- # weights_path = "./best_model_weights.h5"
65
- # model.load_weights(weights_path)
66
- #------------------------------------------------------------
67
-
68
 
69
  # Define preprocessing function
70
  def preprocess_image(image):
@@ -90,15 +35,28 @@ def predict(image):
90
  predictions = model.predict(input_tensor)
91
 
92
  # Extract the predicted class label with the highest probability
93
- predicted_label = np.argmax(predictions[0])
94
-
95
- return str(predicted_label)
 
 
 
 
96
 
97
  image_component = gr.Image(type="pil", label="Chest X-ray Image")
98
  label_component = gr.Label(label="Classification result", num_top_classes=1)
99
  description = ""
100
  title = "Pneumonia Classifier"
101
 
102
- demo = gr.Interface(fn=predict, inputs=image_component, outputs=label_component, description=description, title=title)
 
 
 
 
 
 
 
 
 
103
 
104
  demo.launch()
 
3
 
4
  import torch
5
  from torchvision import transforms
 
6
  import tensorflow as tf
 
7
  import tf_keras
 
 
 
 
 
 
8
  import numpy as np
 
 
 
 
 
 
 
9
  import gradio as gr
10
 
11
+ model_path = "./4-15_ViT-B16_Lung3_v1/"
12
+ model = tf_keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Define preprocessing function
15
  def preprocess_image(image):
 
35
  predictions = model.predict(input_tensor)
36
 
37
  # Extract the predicted class label with the highest probability
38
+ prediction = np.argmax(predictions[0])
39
+ predicted_classes = (prediction > 0.5).astype(int) # 0.5 threshold
40
+
41
+ if(prediction > 0.5):
42
+ return "Pneumonia"
43
+ else:
44
+ return "Normal"
45
 
46
  image_component = gr.Image(type="pil", label="Chest X-ray Image")
47
  label_component = gr.Label(label="Classification result", num_top_classes=1)
48
  description = ""
49
  title = "Pneumonia Classifier"
50
 
51
+ demo = gr.Interface(fn=predict,
52
+ inputs=image_component,
53
+ outputs=label_component,
54
+ description=description,
55
+ title=title,
56
+ examples=["demo_example_imgs/NORMAL-0001-0001.jpeg",
57
+ "demo_example_imgs/NORMAL2-IM-0337-0001.jpeg",
58
+ "demo_example_imgs/PNEUMONIA_bacteria_434.jpeg",
59
+ "demo_example_imgs/PNEUMONIA_virus_140.jpeg"
60
+ ])
61
 
62
  demo.launch()