Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +38 -36
- jo33_model_v222.h5 +3 -0
app.py
CHANGED
@@ -3,43 +3,45 @@ import tensorflow as tf
|
|
3 |
from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
import numpy as np
|
6 |
-
from PIL import Image
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Function for prediction
|
11 |
def predict(img):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
classify_button = gr.Button("Classify!")
|
36 |
-
with gr.Column():
|
37 |
-
classify_output = gr.Textbox(label="Classification Result")
|
38 |
-
|
39 |
-
classify_button.click(
|
40 |
-
predict,
|
41 |
-
inputs=[classify_input],
|
42 |
-
outputs=[classify_output]
|
43 |
-
)
|
44 |
-
|
45 |
-
demo.launch()
|
|
|
3 |
from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
import numpy as np
|
|
|
6 |
|
7 |
+
# กำหนดเลเยอร์ที่กำหนดเอง (CustomScaleLayer)
|
8 |
+
class CustomScaleLayer(tf.keras.layers.Layer):
|
9 |
+
def __init__(self, scale=1.0, **kwargs):
|
10 |
+
super(CustomScaleLayer, self).__init__(**kwargs)
|
11 |
+
self.scale = scale
|
12 |
+
|
13 |
+
def call(self, inputs, *args, **kwargs):
|
14 |
+
if isinstance(inputs, list):
|
15 |
+
return [input_tensor * self.scale for input_tensor in inputs]
|
16 |
+
else:
|
17 |
+
return inputs * self.scale
|
18 |
+
|
19 |
+
# ใช้ custom_object_scope เพื่อทำให้เลเยอร์ที่กำหนดเองสามารถใช้งานได้
|
20 |
+
with tf.keras.utils.custom_object_scope({'CustomScaleLayer': CustomScaleLayer}):
|
21 |
+
model = tf.keras.models.load_model("jo33_model_v222.h5")
|
22 |
|
23 |
# Function for prediction
|
24 |
def predict(img):
|
25 |
+
img = img.resize((224, 224)) # Resize image to the target size
|
26 |
+
img_array = image.img_to_array(img) # Convert image to array
|
27 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
28 |
+
img_array = preprocess_input(img_array) # Preprocess image according to model requirements
|
29 |
+
|
30 |
+
predictions = model.predict(img_array)
|
31 |
+
class_idx = np.argmax(predictions, axis=1)[0]
|
32 |
+
class_label = list(train_generator.class_indices.keys())[class_idx]
|
33 |
+
confidence = predictions[0][class_idx]
|
34 |
+
|
35 |
+
return {class_label: confidence}
|
36 |
+
|
37 |
+
# Create Gradio Interface
|
38 |
+
interface = gr.Interface(
|
39 |
+
fn=predict,
|
40 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
41 |
+
outputs=gr.Label(num_top_classes=2, label="Predicted Class"),
|
42 |
+
title="Image Classification with InceptionResNetV2",
|
43 |
+
description="Upload an image to classify it into one of the classes."
|
44 |
+
)
|
45 |
+
|
46 |
+
# Launch the interface
|
47 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jo33_model_v222.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0b0cac9d667bdc51caba249e3ac5325ae2acd0cd91e57a058770fafc5bbbab30
|
3 |
+
size 656724656
|