File size: 4,844 Bytes
abcbc55 00f5f19 abcbc55 bb76005 00f5f19 bb76005 00f5f19 bb76005 00f5f19 bb76005 abcbc55 bb76005 abcbc55 bb76005 abcbc55 bb76005 abcbc55 00f5f19 abcbc55 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7881\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7881/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import gradio as gr\n",
"import tensorflow as tf\n",
"from PIL import Image\n",
"import numpy as np\n",
"\n",
"# Load the model\n",
"model_path = \"best_model.keras\"\n",
"model = tf.keras.models.load_model(model_path)\n",
"\n",
"# Define labels\n",
"labels = ['AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake']\n",
"\n",
"def predict_image(image):\n",
" image = Image.fromarray(image.astype('uint8'), 'RGB')\n",
" image = image.resize((64, 64)) \n",
" image = np.array(image)\n",
"\n",
" prediction = model.predict(np.expand_dims(image, axis=0))\n",
" confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))}\n",
" return confidences\n",
"\n",
"# Gradio interface\n",
"iface = gr.Interface(\n",
" fn=predict_image,\n",
" inputs=gr.Image(),\n",
" outputs=gr.Label(num_top_classes=10),\n",
" title=\"Sentinel Image Classifier\",\n",
" description=\"Upload a satellite image and the classifier will predict the type of land cover or feature.\",\n",
" examples = [\"images/annualcrop.jpg\", \"images/forest.jpg\", \"images/herbaceousvegetation.jpg\", \"images/highway.jpg\", \"images/industrial.jpg\", \"images/pasture.jpg\", \"images/permanentcrop.jpg\", \"images/residential.jpg\", \"images/river.jpg\", \"images/sealake.jpg\"]\n",
")\n",
"# Launch the interface\n",
"iface.launch(share=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 647ms/step\n",
"[[2.98899226e-06 3.38417292e-02 1.58750382e-08 1.03646407e-08\n",
" 3.04554437e-10 3.97204403e-08 7.68960629e-09 1.02308356e-10\n",
" 1.51210475e-06 9.66153681e-01]]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Min and max values: 0.16862745098039217 1.0\n",
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step\n",
"Min and max values: 0.1411764705882353 1.0\n",
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 21ms/step\n",
"Min and max values: 0.16862745098039217 1.0\n",
"\u001b[1m1/1\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 38ms/step\n"
]
}
],
"source": [
"from PIL import Image\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"\n",
"# Load the trained model\n",
"model_path = 'sentinel_classificatiion_model.keras' # Adjust the path as necessary\n",
"model = tf.keras.models.load_model(model_path)\n",
"\n",
"# Load and process an example image\n",
"image_path = 'images/forest.jpg' # Replace with an example image from your dataset\n",
"image = Image.open(image_path)\n",
"\n",
"\n",
"# Predict using the model\n",
"prediction = model.predict(np.expand_dims(image, axis=0)) # Add batch dimension\n",
"print(prediction)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|