Spaces:
Runtime error
Runtime error
Download app.py from Sjleeiv/WeatherClassifier: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/Sjleeiv/WeatherClassifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sjleeiv/WeatherClassifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sjleeiv/WeatherClassifier/resolve/main/app.py
3.84 kB
| 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() | |