Sjleeiv commited on
Commit
628ac72
·
1 Parent(s): f5c59f3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ import numpy as np
5
+ from tensorflow.keras import layers, models
6
+ from tensorflow.keras.applications.resnet import preprocess_input
7
+
8
+ def identity_block(x, filters):
9
+ x_shortcut = x
10
+
11
+ x = layers.Conv2D(filters[0], (1, 1), strides=(1, 1), padding='valid')(x)
12
+ x = layers.BatchNormalization()(x)
13
+ x = layers.Activation('relu')(x)
14
+
15
+ x = layers.Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
16
+ x = layers.BatchNormalization()(x)
17
+ x = layers.Activation('relu')(x)
18
+
19
+ x = layers.Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
20
+ x = layers.BatchNormalization()(x)
21
+
22
+ x = layers.Add()([x, x_shortcut])
23
+ x = layers.Activation('relu')(x)
24
+
25
+ return x
26
+
27
+ def conv_block(x, filters, stride):
28
+ x_shortcut = x
29
+
30
+ x = layers.Conv2D(filters[0], (1, 1), strides=(stride, stride), padding='valid')(x)
31
+ x = layers.BatchNormalization()(x)
32
+ x = layers.Activation('relu')(x)
33
+
34
+ x = layers.Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
35
+ x = layers.BatchNormalization()(x)
36
+ x = layers.Activation('relu')(x)
37
+
38
+ x = layers.Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
39
+ x = layers.BatchNormalization()(x)
40
+
41
+ x_shortcut = layers.Conv2D(filters[2], (1, 1), strides=(stride, stride), padding='valid')(x_shortcut)
42
+ x_shortcut = layers.BatchNormalization()(x_shortcut)
43
+
44
+ x = layers.Add()([x, x_shortcut])
45
+ x = layers.Activation('relu')(x)
46
+
47
+ return x
48
+
49
+ def build_resnet15(input_shape=(224, 224, 3), num_classes=5):
50
+ input_tensor = layers.Input(shape=input_shape)
51
+
52
+ x = layers.Conv2D(32, (7, 7), strides=(2, 2), padding='valid')(input_tensor)
53
+ x = layers.BatchNormalization()(x)
54
+ x = layers.Activation('relu')(x)
55
+
56
+ x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(x)
57
+
58
+ x = conv_block(x, [32, 32, 64], stride=1)
59
+ x = identity_block(x, [64, 64, 64])
60
+
61
+ x = conv_block(x, [64, 64, 128], stride=2)
62
+ x = identity_block(x, [128, 128, 128])
63
+
64
+ x = conv_block(x, [128, 128, 256], stride=2)
65
+ x = identity_block(x, [256, 256, 256])
66
+
67
+ x = conv_block(x, [256, 256, 512], stride=2)
68
+ x = identity_block(x, [512, 512, 512])
69
+
70
+ x = layers.AveragePooling2D((4, 4), padding='valid')(x)
71
+ x = layers.Flatten()(x)
72
+
73
+ x = layers.Dense(512, activation='relu')(x)
74
+ x = layers.Dropout(0.2)(x)
75
+
76
+ output_tensor = layers.Dense(num_classes, activation='softmax')(x)
77
+
78
+ model = models.Model(inputs=input_tensor, outputs=output_tensor)
79
+
80
+ return model
81
+
82
+ # Instantiate the ResNet15 model
83
+ model = build_resnet15()
84
+
85
+ # Load the pre-trained weights
86
+ model.load_weights('model_weights.h5') # Replace with the actual path
87
+
88
+
89
+
90
+ # Define a function for making predictions
91
+ def predict(input_img):
92
+ # Preprocess the input image
93
+ input_img = input_img.resize((224, 224)) # Resize the image to match the model's input size
94
+ input_img_array = np.array(input_img) / 255.0 # Normalize pixel values
95
+ input_img_array = np.expand_dims(input_img_array, axis=0) # Add batch dimension
96
+
97
+ # Make predictions
98
+ predictions = model.predict(input_img_array)
99
+ predicted_class = np.argmax(predictions)
100
+
101
+ # Create a custom bar chart using gr.Table
102
+ prob_values = predictions[0].tolist()
103
+ bar_chart = gr.Table(data=[[labels_list[i], prob_values[i]] for i in range(len(labels_list))], columns=["Label", "Probability"])
104
+
105
+ return input_img, {
106
+ "Predicted Class": labels_list[predicted_class],
107
+ "Probabilities": bar_chart
108
+ }
109
+
110
+ # Create a Gradio interface
111
+ gr.Interface(
112
+ predict,
113
+ inputs=gr.Image(label="Select an image", type="pil"),
114
+ outputs=[gr.Image(label="Processed Image"), gr.Label(label="Predicted Class"), gr.Table(label="Probabilities")],
115
+ title="ResNet15 Image Classification"
116
+ ).launch()