Boltuzamaki commited on
Commit
a49489d
·
1 Parent(s): 9bf8244
Files changed (6) hide show
  1. .gitignore +3 -0
  2. app.py +89 -0
  3. final.h5 +3 -0
  4. images/test_10158.jpg +0 -0
  5. images/train_10004.jpg +0 -0
  6. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ venv
2
+ .env
3
+ .venv
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from tensorflow.keras import backend as K
5
+
6
+
7
+ def fbeta(y_true, y_pred, threshold_shift=0):
8
+ beta = 2
9
+ y_pred = K.clip(y_pred, 0, 1)
10
+ y_pred_bin = K.round(y_pred + threshold_shift)
11
+ tp = K.sum(K.round(y_true * y_pred_bin)) + K.epsilon()
12
+ fp = K.sum(K.round(K.clip(y_pred_bin - y_true, 0, 1)))
13
+ fn = K.sum(K.round(K.clip(y_true - y_pred, 0, 1)))
14
+ precision = tp / (tp + fp)
15
+ recall = tp / (tp + fn)
16
+ beta_squared = beta**2
17
+ return (
18
+ (beta_squared + 1)
19
+ * (precision * recall)
20
+ / (beta_squared * precision + recall + K.epsilon())
21
+ )
22
+
23
+
24
+ def accuracy_score(y_true, y_pred, epsilon=1e-4):
25
+ y_true = tf.cast(y_true, tf.float32)
26
+ y_pred = tf.cast(
27
+ tf.greater(tf.cast(y_pred, tf.float32), tf.constant(0.5)), tf.float32
28
+ )
29
+ tp = tf.reduce_sum(y_true * y_pred, axis=1)
30
+ fp = tf.reduce_sum(y_pred, axis=1) - tp
31
+ fn = tf.reduce_sum(y_true, axis=1) - tp
32
+ y_true = tf.cast(y_true, tf.bool)
33
+ y_pred = tf.cast(y_pred, tf.bool)
34
+ tn = tf.reduce_sum(
35
+ tf.cast(tf.logical_not(y_true), tf.float32)
36
+ * tf.cast(tf.logical_not(y_pred), tf.float32),
37
+ axis=1,
38
+ )
39
+ return (tp + tn) / (tp + tn + fp + fn + epsilon)
40
+
41
+
42
+ model = tf.keras.models.load_model(
43
+ "final.h5", custom_objects={"fbeta": fbeta, "accuracy_score": accuracy_score}
44
+ )
45
+
46
+ class_names = [
47
+ "clear",
48
+ "agriculture",
49
+ "selective_logging",
50
+ "haze",
51
+ "bare_ground",
52
+ "blooming",
53
+ "habitation",
54
+ "artisinal_mine",
55
+ "blow_down",
56
+ "road",
57
+ "slash_burn",
58
+ "primary",
59
+ "cultivation",
60
+ "water",
61
+ "conventional_mine",
62
+ "cloudy",
63
+ "partly_cloudy",
64
+ ]
65
+
66
+
67
+ def predict(image):
68
+ image = tf.image.resize(image, [64, 64])
69
+ image = image / 255.0
70
+ image = np.expand_dims(image, axis=0)
71
+ predictions = model.predict(image)[0]
72
+ print(predictions)
73
+ results = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
74
+ return results
75
+
76
+
77
+ test_examples = [
78
+ "./images/test_10158.jpg", # Path to Test Image 1
79
+ "./images/train_10004.jpg", # Path to Test Image 2
80
+ ]
81
+
82
+ iface = gr.Interface(
83
+ fn=predict,
84
+ inputs=gr.Image(type="numpy"),
85
+ outputs=gr.Label(num_top_classes=17),
86
+ examples=test_examples,
87
+ )
88
+
89
+ iface.launch()
final.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:199df7c1cae0f24c23a58d21b17e286cb99ab577963013065d7866ff3c92d624
3
+ size 78038628
images/test_10158.jpg ADDED
images/train_10004.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ tensorflow==2.9.0
2
+ gradio
3
+ numpy==1.22