abdulrahman245 commited on
Commit
98bfa1a
·
verified ·
1 Parent(s): 8ba4a89
Files changed (2) hide show
  1. app.py +72 -0
  2. build_model_1_v2.h5 +3 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from tensorflow.keras.models import load_model as lm
4
+ from PIL import Image
5
+ import plotly.graph_objects as go
6
+
7
+ # Load your trained CIFAR model
8
+ model = lm('models/build_model_1_v2.keras')
9
+
10
+ # Define the CIFAR-10 class names
11
+ class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
12
+
13
+
14
+ # Function to preprocess the image and predict the class
15
+ def classify_image(image):
16
+ # Ensure the image is in the right format
17
+ image = Image.fromarray(image).convert('RGB')
18
+
19
+ # Resize the image to (32, 32) as CIFAR-10 uses 32x32 images
20
+ image = image.resize((32, 32))
21
+
22
+ # Convert the image to an array and preprocess it
23
+ image_array = np.array(image).astype(np.float32) / 255.0 # Normalize to [0, 1]
24
+
25
+ # Expand dimensions to match model input shape (1, 32, 32, 3)
26
+ image_array = np.expand_dims(image_array, axis=0)
27
+
28
+ # Get predictions
29
+ predictions = model.predict(image_array)[0] # Get the prediction array for the first image
30
+
31
+ # Get the predicted class and its confidence
32
+ predicted_class_idx = np.argmax(predictions)
33
+ predicted_class = class_names[predicted_class_idx]
34
+ predicted_confidence = predictions[predicted_class_idx] * 100 # Convert to percentage
35
+
36
+ # Print predicted class and confidence
37
+ predicted_info = f"Predicted Class: {predicted_class} with {predicted_confidence:.2f}% confidence."
38
+
39
+ # Create a Plotly bar chart for class confidence levels
40
+ fig = go.Figure(go.Bar(
41
+ x=predictions * 100, # Convert probabilities to percentages
42
+ y=class_names,
43
+ orientation='h',
44
+ marker=dict(color='skyblue'),
45
+ text=[f"{conf:.1f}%" for conf in predictions * 100], # Show percentage labels
46
+ hoverinfo="text"
47
+ ))
48
+
49
+ # Update layout for better presentation
50
+ fig.update_layout(
51
+ title="Class Confidence Levels",
52
+ xaxis_title="Confidence (%)",
53
+ yaxis_title="Classes",
54
+ xaxis=dict(range=[0, 100]), # Set x-axis to 0-100%
55
+ yaxis=dict(categoryorder='total ascending'), # Sort bars by confidence
56
+ bargap=0.2
57
+ )
58
+
59
+ return predicted_info, fig
60
+
61
+
62
+ # Define the Gradio interface
63
+ interface = gr.Interface(
64
+ fn=classify_image,
65
+ inputs=gr.Image(type="numpy"),
66
+ outputs=["text", gr.Plot()],
67
+ title="CIFAR Image Classification",
68
+ description="Upload an image, and the model will classify it as one of the CIFAR-10 classes."
69
+ )
70
+
71
+ # Launch the interface
72
+ interface.launch()
build_model_1_v2.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:16d160cfb01eefa350299b6db302e13b2040efb574a41963caeb998d88f53a97
3
+ size 2301984