| |
| """gradio.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1B520JUHmyofueyUqotN2yj6Gad69uavo |
| """ |
|
|
| import gradio as gr |
| import tensorflow as tf |
| from tensorflow import keras |
| from tensorflow.keras import models, layers |
|
|
|
|
| input_shape = (256, 256, 3) |
|
|
| def create_model(): |
| model = models.Sequential([ |
| |
| layers.Input(shape=(256, 256, 3)), |
| layers.Resizing(256, 256), |
| layers.Rescaling(1.0/255), |
| layers.RandomFlip('horizontal_and_vertical'), |
| layers.RandomRotation(0.2), |
| layers.Conv2D(32, (3, 3), activation='relu'), |
| layers.MaxPooling2D((2, 2)), |
| layers.Conv2D(64, (3, 3), activation='relu'), |
| layers.MaxPooling2D((2, 2)), |
| layers.Conv2D(64, (3, 3), activation='relu'), |
| layers.MaxPooling2D((2, 2)), |
| layers.Conv2D(64, (3, 3), activation='relu'), |
| layers.MaxPooling2D((2, 2)), |
| layers.Conv2D(64, (3, 3), activation='relu'), |
| layers.MaxPooling2D((2, 2)), |
| layers.Flatten(), |
| layers.Dense(64, activation='relu'), |
| layers.Dense(3, activation='softmax') |
| |
| ]) |
| |
| return model |
|
|
| model = create_model() |
| model.load_weights('my_model_weights1.weights.h5') |
|
|
| def predict_disease(image): |
| """ |
| Preprocesses the image, performs prediction, and returns the predicted class with confidence score. |
| """ |
| img = tf.image.resize(image, [256, 256]) |
| img = tf.expand_dims(img, axis=0) |
|
|
| |
| prediction = model.predict(img)[0] |
| labels = ['Early Blight', 'Late Blight', 'Healthy'] |
|
|
| |
| class_probs = {label: float(prob) for label, prob in zip(labels, prediction)} |
|
|
| return class_probs |
| |
|
|
| iface = gr.Interface( |
| fn=predict_disease, |
| inputs=gr.Image(), |
| outputs=gr.Label(num_top_classes=3), |
| title="Potato Disease Classifier ππ₯", |
| description=( |
| "Upload an image of a potato leaf, and I'll classify it as one of the following:\n" |
| "- **Early Blight** π±\n" |
| "- **Late Blight** π§οΈ\n" |
| "- **Healthy** π\n\n" |
| "Check out the probability bars for more details!" |
| ), |
| ) |
| iface.launch(share=True) |
|
|