Spaces:
Sleeping
Sleeping
Commit ·
2a1d0ba
1
Parent(s): 9dc96f5
fix issues
Browse files- .gitignore +4 -0
- README.md +3 -3
- app.py +55 -0
- model.weights.h5 +3 -0
- requirements.txt +6 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
dataset/
|
| 2 |
+
NN.py
|
| 3 |
+
test.py
|
| 4 |
+
.venv
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: FLL Innovation
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.3.0
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
title: FLL Innovation
|
| 3 |
+
emoji: 😻
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.3.0
|
| 8 |
app_file: app.py
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
print("Building model architecture...")
|
| 8 |
+
base_model = tf.keras.applications.MobileNetV2(
|
| 9 |
+
input_shape=(224, 224, 3),
|
| 10 |
+
include_top=False,
|
| 11 |
+
weights="imagenet"
|
| 12 |
+
)
|
| 13 |
+
base_model.trainable = False
|
| 14 |
+
|
| 15 |
+
model = tf.keras.models.Sequential([
|
| 16 |
+
base_model,
|
| 17 |
+
tf.keras.layers.GlobalAveragePooling2D(),
|
| 18 |
+
tf.keras.layers.Dense(128, activation="relu"),
|
| 19 |
+
tf.keras.layers.Dropout(0.3),
|
| 20 |
+
tf.keras.layers.Dense(2, activation="softmax")
|
| 21 |
+
])
|
| 22 |
+
|
| 23 |
+
print("Loading weights...")
|
| 24 |
+
model.load_weights("model.weights.h5")
|
| 25 |
+
print("Model ready!")
|
| 26 |
+
|
| 27 |
+
LABELS = ["preserved", "looted"]
|
| 28 |
+
IMG_SIZE = (224, 224)
|
| 29 |
+
|
| 30 |
+
def preprocess(img: Image.Image):
|
| 31 |
+
img = img.convert("RGB").resize(IMG_SIZE)
|
| 32 |
+
arr = np.array(img) / 255.0
|
| 33 |
+
arr = np.expand_dims(arr, 0).astype(np.float32)
|
| 34 |
+
return arr
|
| 35 |
+
|
| 36 |
+
def predict(image: Image.Image):
|
| 37 |
+
x = preprocess(image)
|
| 38 |
+
probs = model.predict(x)[0]
|
| 39 |
+
|
| 40 |
+
result = {LABELS[0]: float(probs[0]), LABELS[1]: float(probs[1])}
|
| 41 |
+
idx = int(np.argmax(probs))
|
| 42 |
+
summary = f"Prediction: **{LABELS[idx]}** ({float(probs[idx]):.1%} confidence)"
|
| 43 |
+
|
| 44 |
+
return result, summary
|
| 45 |
+
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=predict,
|
| 48 |
+
inputs=gr.Image(type="pil", label="Upload photo"),
|
| 49 |
+
outputs=[gr.Label(num_top_classes=2, label="Prediction"), gr.Textbox(label="Summary")],
|
| 50 |
+
title="Preserved vs Looted Classifier",
|
| 51 |
+
description="Upload a photo to classify as preserved or looted."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
model.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:adbf23137123a4490a24b8f8a78e6035fbccb0f4348b07127c191d58824e2703
|
| 3 |
+
size 11437088
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tensorflow
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
| 5 |
+
huggingface-hub
|
| 6 |
+
|