File size: 3,141 Bytes
628ac72
 
 
 
548d068
628ac72
 
 
a56d669
9cb2f69
628ac72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51466d5
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

from tensorflow.keras import layers, models
from tensorflow.keras.applications.resnet import preprocess_input

labels_list = ['dew', 'fogsmog', 'glaze', 'rime', 'sandstorm']

def identity_block(x, filters):
    x_shortcut = x

    x = layers.Conv2D(filters[0], (1, 1), strides=(1, 1), padding='valid')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)

    x = layers.Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)

    x = layers.Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
    x = layers.BatchNormalization()(x)

    x = layers.Add()([x, x_shortcut])
    x = layers.Activation('relu')(x)

    return x

def conv_block(x, filters, stride):
    x_shortcut = x

    x = layers.Conv2D(filters[0], (1, 1), strides=(stride, stride), padding='valid')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)

    x = layers.Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)

    x = layers.Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
    x = layers.BatchNormalization()(x)

    x_shortcut = layers.Conv2D(filters[2], (1, 1), strides=(stride, stride), padding='valid')(x_shortcut)
    x_shortcut = layers.BatchNormalization()(x_shortcut)

    x = layers.Add()([x, x_shortcut])
    x = layers.Activation('relu')(x)

    return x

def build_resnet15(input_shape=(224, 224, 3), num_classes=5):
    input_tensor = layers.Input(shape=input_shape)
    
    x = layers.Conv2D(32, (7, 7), strides=(2, 2), padding='valid')(input_tensor)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    
    x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(x)

    x = conv_block(x, [32, 32, 64], stride=1)
    x = identity_block(x, [64, 64, 64])

    x = conv_block(x, [64, 64, 128], stride=2)
    x = identity_block(x, [128, 128, 128])

    x = conv_block(x, [128, 128, 256], stride=2)
    x = identity_block(x, [256, 256, 256])

    x = conv_block(x, [256, 256, 512], stride=2)
    x = identity_block(x, [512, 512, 512])

    x = layers.AveragePooling2D((4, 4), padding='valid')(x)
    x = layers.Flatten()(x)

    x = layers.Dense(512, activation='relu')(x)
    x = layers.Dropout(0.2)(x)

    output_tensor = layers.Dense(num_classes, activation='softmax')(x)

    model = models.Model(inputs=input_tensor, outputs=output_tensor)

    return model

# Instantiate the ResNet15 model
model = build_resnet15()

# Load the pre-trained weights
model.load_weights('model_weights.h5')  # Replace with the actual path



def predict_image(img):
  img_4d=img.reshape(-1,224,224,3)
  prediction=model.predict(img_4d)[0]
  return {class_names[i]: float(prediction[i]) for i in range(5)}
image = gr.inputs.Image(shape=(224,224))
label = gr.outputs.Label(num_top_classes=5)

gr.Interface(fn=predict_image, inputs=image, outputs=label,interpretation='default').launch(debug='True')