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