Spaces:
Runtime error
Runtime error
File size: 3,840 Bytes
0dab622 0df4a24 0dab622 19cb71a 0dab622 dce54b6 0dab622 8fd4573 0dab622 cfc7c47 8fd4573 30a8f8d 5daecdf 357dbcc 8fd4573 0dab622 8fd4573 0dab622 8fd4573 0dab622 30f261a 9a78e3c 30f261a | 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 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 = ['rime', 'fogsmog', 'dew', 'sandstorm', 'glaze']
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
# Define a function for making predictions
def predict(input_img):
# Preprocess the input image
input_img = input_img.resize((224, 224)) # Resize the image to match the model's input size
input_img_array = np.array(input_img) / 255.0 # Normalize pixel values
input_img_array = np.expand_dims(input_img_array, axis=0) # Add batch dimension
# Make predictions
predictions = model.predict(input_img_array)
predicted_class = np.argmax(predictions)
# Create a custom bar chart using gr.Tabs
prob_values = predictions[0].tolist()
bar_chart = gr.Bar(values=prob_values, labels=labels_list, title="Probabilities")
return input_img, {
"Predicted Class": labels_list[predicted_class],
"Probabilities": bar_chart
}
# Create a Gradio interface
gr.Interface(
predict,
inputs=gr.Image(label="Select an image", type="pil"),
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Predicted Class"), gr.Tabs("Probabilities")],
title="ResNet15 Image Classification"
).launch()
|