Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,67 +13,67 @@ from tensorflow.keras.applications.vgg16 import preprocess_input
|
|
| 13 |
model_pb_path = "./4-15_ViT-B16_Lung3_v1/saved_model.pb"
|
| 14 |
model_keras_path = "./model.keras"
|
| 15 |
model_h5_path = "./model.h5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
# model_url
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
# model = Sequential([
|
| 36 |
-
# vit_b16,
|
| 37 |
-
# Dense(1024, activation='relu'),
|
| 38 |
-
# Dropout(0.5),
|
| 39 |
-
# Dense(1, activation='sigmoid')
|
| 40 |
-
# ])
|
| 41 |
|
| 42 |
-
# # Load the saved weights
|
| 43 |
-
# model.load_weights("model.h5")
|
| 44 |
|
| 45 |
-
# Define preprocessing function
|
| 46 |
-
def preprocess_image(image):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
# Define inference function
|
| 57 |
-
def predict(image):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
predicted_label = np.argmax(predictions[0])
|
| 69 |
-
|
| 70 |
-
return predicted_label
|
| 71 |
|
| 72 |
-
image_component = gr.Image(type="pil", label="Chest X-ray Image")
|
| 73 |
-
label_component = gr.Label(label="Classification result", num_top_classes=1)
|
| 74 |
-
description = ""
|
| 75 |
-
title = "Pneumonia Classifier"
|
| 76 |
|
| 77 |
-
demo = gr.Interface(fn=predict, inputs=image_component, outputs=label_component, description=description, title=title)
|
| 78 |
-
|
| 79 |
-
demo.launch()
|
|
|
|
| 13 |
model_pb_path = "./4-15_ViT-B16_Lung3_v1/saved_model.pb"
|
| 14 |
model_keras_path = "./model.keras"
|
| 15 |
model_h5_path = "./model.h5"
|
| 16 |
+
with custom_object_scope({"KerasLayer": hub.KerasLayer}):
|
| 17 |
+
# model = tf.keras.models.load_model(model_keras_path, custom_objects={'KerasLayer': hub.KerasLayer})
|
| 18 |
+
model = tf.keras.models.load_model(model_h5_path, custom_objects={})
|
| 19 |
+
# model.compile(optimizer=Adam(learning_rate=0.0001),
|
| 20 |
+
# loss='binary_crossentropy',
|
| 21 |
+
# metrics=['accuracy'])
|
| 22 |
|
| 23 |
+
# # Load the saved TensorFlow Hub KerasLayer
|
| 24 |
+
# model_url = "https://tfhub.dev/sayakpaul/vit_b16_classification/1"
|
| 25 |
+
|
| 26 |
+
# # Load the ViT-B16 model pre-trained on ImageNet
|
| 27 |
+
# try:
|
| 28 |
+
# vit_b16 = hub.KerasLayer(model_url, trainable=False, input_shape=(224, 224, 3))
|
| 29 |
+
# except Exception as e:
|
| 30 |
+
# print(f"Failed to load the model from {model_url}. Error: {e}")
|
| 31 |
+
# # Handle the error if the model cannot be loaded
|
| 32 |
+
# raise
|
| 33 |
+
|
| 34 |
+
# # Define the rest of the model architecture
|
| 35 |
+
# model = Sequential([
|
| 36 |
+
# vit_b16,
|
| 37 |
+
# Dense(1024, activation='relu'),
|
| 38 |
+
# Dropout(0.5),
|
| 39 |
+
# Dense(1, activation='sigmoid')
|
| 40 |
+
# ])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# # Load the saved weights
|
| 43 |
+
# model.load_weights("model.h5")
|
| 44 |
|
| 45 |
+
# Define preprocessing function
|
| 46 |
+
def preprocess_image(image):
|
| 47 |
+
transform_test = transforms.Compose([
|
| 48 |
+
transforms.Resize((224, 224)),
|
| 49 |
+
transforms.ToTensor(),
|
| 50 |
+
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
|
| 51 |
+
])
|
| 52 |
|
| 53 |
+
preprocessed_img = transform_test(image)
|
| 54 |
+
return preprocessed_img
|
| 55 |
|
| 56 |
+
# Define inference function
|
| 57 |
+
def predict(image):
|
| 58 |
+
# Preprocess the image
|
| 59 |
+
preprocessed_image = preprocess_image(image)
|
| 60 |
|
| 61 |
+
# Add batch dimension to input image
|
| 62 |
+
input_tensor = tf.expand_dims(preprocessed_image, axis=0)
|
| 63 |
|
| 64 |
+
# Perform inference
|
| 65 |
+
predictions = model.predict(input_tensor)
|
| 66 |
+
|
| 67 |
+
# Extract the predicted class label with the highest probability
|
| 68 |
+
predicted_label = np.argmax(predictions[0])
|
| 69 |
|
| 70 |
+
return predicted_label
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
image_component = gr.Image(type="pil", label="Chest X-ray Image")
|
| 73 |
+
label_component = gr.Label(label="Classification result", num_top_classes=1)
|
| 74 |
+
description = ""
|
| 75 |
+
title = "Pneumonia Classifier"
|
| 76 |
|
| 77 |
+
demo = gr.Interface(fn=predict, inputs=image_component, outputs=label_component, description=description, title=title)
|
| 78 |
+
|
| 79 |
+
demo.launch()
|