Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758591 unmodified line616263646566671 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_inputos.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'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'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)# 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 shapeif model_choice == "ResNet":img = tf.image.resize(img, (224, 224)) # ResNet50 input sizeimg = resnet_preprocess_input(img)elif model_choice == "EfficientNet":img = tf.image.resize(img, (224, 224))img = efficientnet_preprocess_input(img)elif model_choice == "VGGNet":img = tf.image.resize(img, (224, 224))img = vgg_preprocess_input(img)elif model_choice == "InceptionV3":img = tf.image.resize(img, (299, 299)) # InceptionV3 input sizeimg = inception_preprocess_input(img)else:return "Invalid model choice"img = tf.expand_dims(img, 0) # Add batch dimension# Make predictions based on the selected modelif 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)class_idx = tf.argmax(predictions[0])# Map class index to class nameclass_name = class_names[class_idx]1 unmodified line# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"image_input = gr.Image()# Create a Gradio interface with a dropdown for model selection1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556571 unmodified line5960616263641 unmodified lineimport osimport cv2import gradio as grimport onnxonnx_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'# 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)class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def classify_image(img, model_choice):if model_choice == "ResNet":img = tf.image.resize(img, (150, 150))img = resnet_preprocess_input(img)elif model_choice == "EfficientNet":img = tf.image.resize(img, (150, 150))img = efficientnet_preprocess_input(img)elif model_choice == "VGGNet":img = tf.image.resize(img, (150, 150))img = vgg_preprocess_input(img)elif model_choice == "InceptionV3":img = tf.image.resize(img, (150, 150))img = inception_preprocess_input(img)else:return "Invalid model choice"img = tf.expand_dims(img, 0)# Use gr.to_onnx to convert ONNX models to Gradio-compatible formatonnx_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),}onnx_model = onnx_models.get(model_choice)if onnx_model is None:return "Invalid model choice"# Perform inference using the ONNX modelpredictions = gr.run(onnx_model, {"input": img})class_idx = np.argmax(predictions["output"])# Map class index to class nameclass_name = class_names[class_idx]1 unmodified line# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"image_input = gr.Image()# Create a Gradio interface with a dropdown for model selection
app.py+31-34