Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345678956117891011131415161213141516171819202118192324202621282223302425263327282936303132393334353637443839464748495051525354404142434445464756484950515253545556571 unmodified line596061646263641 unmodified lineimport osimport cv2import gradio as grimport tensorflow as tffrom tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input as inception_preprocess_inputfrom tensorflow.keras.applications.vgg16 import VGG16, preprocess_input as vgg_preprocess_inputfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input as resnet_preprocess_inputfrom tensorflow.keras.applications.efficientnet import EfficientNetB0, preprocess_input as efficientnet_preprocess_inputimport onnx
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'onnx_inception_path = './Models_onnx/inceptionv3.onnx'onnx_vggnet_path = './Models_onnx/vggnet.onnx'onnx_resnet_path = './Models_onnx/resnet.onnx'onnx_efficientnet_path = './Models_onnx/efficientnet.onnx'
inception_model_path = './inceptionv3_-saved-model-97-loss-0.00.hdf5'vggnet_model_path = './resnet50-saved-model-79-val_acc-1.00.hdf5'resnet_model_path = './vgg_16_-saved-model-89-acc-0.93.hdf5'efficientnet_model_path = './efficientnet-saved-model-06-val_acc-0.98.hdf5'# Load ONNX modelsonnx_inception_model = onnx.load(onnx_inception_path)onnx_vggnet_model = onnx.load(onnx_vggnet_path)onnx_resnet_model = onnx.load(onnx_resnet_path)onnx_efficientnet_model = onnx.load(onnx_efficientnet_path)
inception_model = tf.keras.models.load_model(inception_model_path)vggnet_model = tf.keras.models.load_model(vggnet_model_path)resnet_model = tf.keras.models.load_model(resnet_model_path)efficientnet_model = tf.keras.models.load_model(efficientnet_model_path)class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
# Define class names corresponding to your datasetclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names
# Define the classify_image functiondef classify_image(img, model_choice): # Resize the input image based on the selected model's expected input shape if model_choice == "ResNet": img = tf.image.resize(img, (224, 224)) # ResNet50 input size img = tf.image.resize(img, (150, 150)) img = resnet_preprocess_input(img) elif model_choice == "EfficientNet": img = tf.image.resize(img, (224, 224)) img = tf.image.resize(img, (150, 150)) img = efficientnet_preprocess_input(img) elif model_choice == "VGGNet": img = tf.image.resize(img, (224, 224)) img = tf.image.resize(img, (150, 150)) img = vgg_preprocess_input(img) elif model_choice == "InceptionV3": img = tf.image.resize(img, (299, 299)) # InceptionV3 input size img = tf.image.resize(img, (150, 150)) img = inception_preprocess_input(img) else: return "Invalid model choice"
img = tf.expand_dims(img, 0) # Add batch dimension img = tf.expand_dims(img, 0)
# Make predictions based on the selected model if model_choice == "ResNet": predictions = resnet_model.predict(img) elif model_choice == "EfficientNet": predictions = efficientnet_model.predict(img) elif model_choice == "VGGNet": predictions = vggnet_model.predict(img) elif model_choice == "InceptionV3": predictions = inception_model.predict(img) # Use gr.to_onnx to convert ONNX models to Gradio-compatible format onnx_models = { "ResNet": gr.to_onnx(onnx_resnet_model), "EfficientNet": gr.to_onnx(onnx_efficientnet_model), "VGGNet": gr.to_onnx(onnx_vggnet_model), "InceptionV3": gr.to_onnx(onnx_inception_model), }
class_idx = tf.argmax(predictions[0]) onnx_model = onnx_models.get(model_choice) if onnx_model is None: return "Invalid model choice"
# Perform inference using the ONNX model predictions = gr.run(onnx_model, {"input": img}) class_idx = np.argmax(predictions["output"])
# Map class index to class name class_name = class_names[class_idx]1 unmodified line # Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
image_input = gr.Image()
# Create a Gradio interface with a dropdown for model selectionapp.py+31-34