Suphawan commited on
Commit
b6a5415
·
verified ·
1 Parent(s): 073d3c4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +38 -36
  2. 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
- model = tf.keras.models.load_model('best_model_InceptionV3.keras')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Function for prediction
11
  def predict(img):
12
- try:
13
- img_resized = img.resize((224, 224)) # Resize image to the target size
14
- img_array = image.img_to_array(img_resized) # Convert image to array
15
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
16
- img_array = preprocess_input(img_array) # Preprocess image according to model requirements
17
-
18
- predictions = model.predict(img_array)
19
- class_idx = np.argmax(predictions, axis=1)[0]
20
- class_labels = ['Benign', 'Malignant'] # Update according to your class labels
21
- class_label = class_labels[class_idx]
22
- confidence = float(predictions[0][class_idx])
23
-
24
- return f"Class: {class_label}, Confidence: {confidence:.2f}"
25
- except Exception as e:
26
- return f"Error in prediction: {e}"
27
-
28
- # Define the Gradio app
29
- with gr.Blocks() as demo:
30
- gr.Markdown("Image Classification with InceptionV2")
31
-
32
- with gr.Row():
33
- with gr.Column():
34
- classify_input = gr.Image(type="pil", label="Upload an Image")
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