Sjleeiv commited on
Commit
0dab622
·
1 Parent(s): 7a1c567

Create app.py

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