Minbz commited on
Commit
ed2ebb9
·
verified ·
1 Parent(s): f62107e

Changed code to load TF model with tf.keras.models.load_model() and finished the rest implementation

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,38 +1,44 @@
1
  import gradio as gr
2
- from transformers import TFAutoModel
 
3
 
4
- PATH = "./4-15_ViT-B16_Lung3_v1"
5
- model = TFAutoModel.from_pretrained(PATH)
6
- tokenizer = AutoTokenizer.from_pretrained(PATH)
7
 
8
  # Define preprocessing function
9
  def preprocess_image(image):
10
  transform_test = transforms.Compose([
11
  transforms.Resize((224, 224)),
 
 
12
  ])
13
 
14
- scaled_img = transform_test(image)
15
- return scaled_img
16
 
17
  # Define inference function
18
  def predict(image):
19
  # Preprocess the image
20
  preprocessed_image = preprocess_image(image)
21
 
22
- # Convert preprocessed image to input format expected by the model
23
- inputs = tokenizer(preprocessed_image, return_tensors="tf")
24
 
25
  # Perform model inference
26
- outputs = model(**inputs)
 
 
 
 
 
 
27
 
28
- # Process model outputs as needed
29
- # For example, extract predicted labels
30
- predicted_labels = np.argmax(outputs.logits.numpy(), axis=1)
31
 
32
- # Return predicted labels
33
- return predicted_labels
34
 
35
- image_component = gr.Image(type="numpy", label="Chest X-ray Image")
36
  label_component = gr.Label(label="Classification result", num_top_classes=1)
37
  description = ""
38
  title = "Pneumonia Classifier"
 
1
  import gradio as gr
2
+ # from transformers import TFAutoModel
3
+ import tensorflow as tf
4
 
5
+ model_path = "./4-15_ViT-B16_Lung3_v1"
6
+ model = tf.keras.models.load_model(model_path)
 
7
 
8
  # Define preprocessing function
9
  def preprocess_image(image):
10
  transform_test = transforms.Compose([
11
  transforms.Resize((224, 224)),
12
+ transforms.ToTensor(),
13
+ transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
14
  ])
15
 
16
+ preprocessed_img = transform_test(image)
17
+ return preprocessed_img
18
 
19
  # Define inference function
20
  def predict(image):
21
  # Preprocess the image
22
  preprocessed_image = preprocess_image(image)
23
 
24
+ # # Convert preprocessed image to input format expected by the model
25
+ # inputs = tokenizer(preprocessed_image, return_tensors="tf")
26
 
27
  # Perform model inference
28
+ # outputs = model(**inputs)
29
+
30
+ # Add batch dimension to input image
31
+ input_tensor = tf.expand_dims(preprocessed_image, axis=0)
32
+
33
+ # Perform inference
34
+ predictions = model.predict(input_tensor)
35
 
36
+ # Extract the predicted class label with the highest probability
37
+ predicted_label = np.argmax(predictions[0])
 
38
 
39
+ return predicted_label
 
40
 
41
+ image_component = gr.Image(type="piL", label="Chest X-ray Image")
42
  label_component = gr.Label(label="Classification result", num_top_classes=1)
43
  description = ""
44
  title = "Pneumonia Classifier"