Lars2000 commited on
Commit
abcbc55
Β·
1 Parent(s): 06b4322

initital commit

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