Update app.py
Browse files
app.py
CHANGED
|
@@ -1,395 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
# coding: utf-8
|
| 3 |
-
|
| 4 |
-
# In[2]:
|
| 5 |
-
|
| 6 |
-
import gradio as ga
|
| 7 |
-
from transformers import TFAutoModelForImageClassification
|
| 8 |
-
|
| 9 |
-
model = TFAutoModelForImageClassification.from_pretrained("sjleeiv/Weather",from_tf=True, token="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")
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
import random
|
| 13 |
-
import shutil
|
| 14 |
-
import numpy as np
|
| 15 |
-
import seaborn as sns
|
| 16 |
-
import matplotlib.pyplot as plt
|
| 17 |
import tensorflow as tf
|
| 18 |
-
from tensorflow.keras.models import Model
|
| 19 |
-
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, MaxPooling2D, Activation, Add, AveragePooling2D, Dense,Flatten,Dropout
|
| 20 |
-
from tensorflow.keras.optimizers import SGD
|
| 21 |
-
from tensorflow.keras.losses import categorical_crossentropy
|
| 22 |
-
from tensorflow.keras.utils import to_categorical
|
| 23 |
-
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 24 |
-
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 25 |
-
from tensorflow.keras.callbacks import EarlyStopping
|
| 26 |
-
from tensorflow.keras.metrics import Recall, Precision
|
| 27 |
-
from tensorflow.keras.callbacks import LearningRateScheduler
|
| 28 |
-
from sklearn.metrics import classification_report, confusion_matrix
|
| 29 |
-
from sklearn.model_selection import train_test_split
|
| 30 |
from PIL import Image
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
# Model weights URL
|
| 34 |
-
weights_url = f"https://huggingface.co/sjleeiv/Weather/resolve/main/tf_model.h5?token={token}"
|
| 35 |
-
|
| 36 |
-
# Load the model weights without specifying the config
|
| 37 |
-
model = TFAutoModelForImageClassification.from_pretrained(weights_url)
|
| 38 |
-
|
| 39 |
-
# In[3]:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# Specify the directory
|
| 43 |
-
root_directory = 'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset'
|
| 44 |
-
|
| 45 |
-
# List of desired labels in the specified order
|
| 46 |
-
labels_list = ['rime', 'fogsmog', 'dew', 'sandstorm', 'glaze']
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# In[4]:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# Initialize empty lists to store file paths and labels for the entire dataset
|
| 53 |
-
X, y = [], []
|
| 54 |
-
|
| 55 |
-
# Gather files and labels for the entire dataset
|
| 56 |
-
for label_idx, label in enumerate(labels_list):
|
| 57 |
-
# Construct the directory path for the current label
|
| 58 |
-
label_directory = os.path.join(root_directory, label)
|
| 59 |
-
# Iterate through files in the label directory
|
| 60 |
-
for file_name in os.listdir(label_directory):
|
| 61 |
-
# Construct the full file path
|
| 62 |
-
file_path = os.path.join(label_directory, file_name)
|
| 63 |
-
# Append file path and corresponding label index to lists
|
| 64 |
-
X.append(file_path)
|
| 65 |
-
y.append(label_idx)
|
| 66 |
-
|
| 67 |
-
# Split the dataset into training and testing sets
|
| 68 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
|
| 69 |
-
|
| 70 |
-
# Split the training set into training and validation sets
|
| 71 |
-
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42,stratify=y_train)
|
| 72 |
-
|
| 73 |
-
# Convert y_train, y_val and y_test to categorical
|
| 74 |
-
num_classes = len(labels_list)
|
| 75 |
-
y_train = to_categorical(y_train, num_classes=num_classes)
|
| 76 |
-
y_val = to_categorical(y_val, num_classes=num_classes)
|
| 77 |
-
y_test = to_categorical(y_test, num_classes=num_classes)
|
| 78 |
-
|
| 79 |
-
# Create folders for training and testing sets
|
| 80 |
-
for dataset_type, X_data, y_data in [('train', X_train, y_train), ('val', X_val, y_val),('test', X_test, y_test)]:
|
| 81 |
-
# Construct the directory path for the current dataset type
|
| 82 |
-
dataset_directory = os.path.join(root_directory, dataset_type)
|
| 83 |
-
os.makedirs(dataset_directory, exist_ok=True)
|
| 84 |
-
# Iterate through data in the current dataset
|
| 85 |
-
for file_path, label in zip(X_data, y_data):
|
| 86 |
-
# Get the label folder name based on one-hot encoded label
|
| 87 |
-
label_folder = labels_list[label.argmax()]
|
| 88 |
-
# Construct the destination folder path
|
| 89 |
-
destination_folder = os.path.join(dataset_directory, label_folder)
|
| 90 |
-
os.makedirs(destination_folder, exist_ok=True)
|
| 91 |
-
|
| 92 |
-
# Extract the file name from the file path
|
| 93 |
-
file_name = os.path.basename(file_path)
|
| 94 |
-
|
| 95 |
-
# Construct the destination file path
|
| 96 |
-
destination_path = os.path.join(destination_folder, file_name)
|
| 97 |
-
# Copy the file from the source to the destination
|
| 98 |
-
shutil.copy(file_path, destination_path)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# In[5]:
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def print_dataset_info(dataset_name, path):
|
| 105 |
-
print(f"{dataset_name} DATASET")
|
| 106 |
-
print("==================================================================")
|
| 107 |
-
# Initialize a counter for total files in the dataset
|
| 108 |
-
total_files = 0
|
| 109 |
-
# Iterate through the directory structure using os.walk
|
| 110 |
-
for root, dirs, files in os.walk(path):
|
| 111 |
-
# Check if there are files in the current directory
|
| 112 |
-
if files != []:
|
| 113 |
-
# Print the current directory and the number of files in it
|
| 114 |
-
print(f"{root} ---> {len(files)}")
|
| 115 |
-
# Update the total file count
|
| 116 |
-
total_files += len(files)
|
| 117 |
-
# Print a separator line and the total file count for the entire dataset
|
| 118 |
-
print("==================================================================")
|
| 119 |
-
print(f"TOTAL = {total_files}\n\n")
|
| 120 |
-
|
| 121 |
-
# Printing information for each dataset
|
| 122 |
-
print_dataset_info("TRAIN", 'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\train\\')
|
| 123 |
-
print_dataset_info("VAL", 'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\val\\')
|
| 124 |
-
print_dataset_info("TEST", 'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\test\\')
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
# In[6]:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Define data generators for training, validation, and testing sets
|
| 131 |
-
train_datagen = ImageDataGenerator(rescale=1./255)
|
| 132 |
-
val_datagen = ImageDataGenerator(rescale=1./255)
|
| 133 |
-
test_datagen = ImageDataGenerator(rescale=1./255)
|
| 134 |
-
|
| 135 |
-
# Training Data Generator
|
| 136 |
-
train_generator = train_datagen.flow_from_directory(
|
| 137 |
-
'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\train\\',
|
| 138 |
-
target_size=(224, 224), # Resize images to (224, 224) during loading
|
| 139 |
-
batch_size=32, # Number of samples per batch
|
| 140 |
-
class_mode='categorical', # Categorical labels for multi-class classification
|
| 141 |
-
shuffle=True # Shuffle the data for better training
|
| 142 |
-
)
|
| 143 |
-
# Validation Data Generator
|
| 144 |
-
val_generator = val_datagen.flow_from_directory(
|
| 145 |
-
'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\val\\',
|
| 146 |
-
target_size=(224, 224),
|
| 147 |
-
batch_size=32,
|
| 148 |
-
class_mode='categorical',
|
| 149 |
-
shuffle=False # No need to shuffle the validation data
|
| 150 |
-
)
|
| 151 |
-
# Test Data Generator
|
| 152 |
-
test_generator = test_datagen.flow_from_directory(
|
| 153 |
-
'C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\test\\',
|
| 154 |
-
target_size=(224, 224),
|
| 155 |
-
batch_size=32,
|
| 156 |
-
class_mode='categorical',
|
| 157 |
-
shuffle=False # No need to shuffle the test data
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
# In[7]:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
# Check GPU availability
|
| 165 |
-
# Get a list of available physical devices (e.g., GPUs)
|
| 166 |
-
physical_devices = tf.config.list_physical_devices('GPU')
|
| 167 |
-
# Check if there are any GPUs available
|
| 168 |
-
if physical_devices:
|
| 169 |
-
# Enable dynamic GPU memory growth to allocate memory as needed
|
| 170 |
-
tf.config.experimental.set_memory_growth(physical_devices[0], True)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# In[8]:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def identity_block(x, filters):
|
| 177 |
-
# Identity block with three convolutional layers
|
| 178 |
-
x_shortcut = x
|
| 179 |
-
|
| 180 |
-
x = Conv2D(filters[0], (1, 1), strides=(1, 1), padding='valid')(x)
|
| 181 |
-
x = BatchNormalization()(x)
|
| 182 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 183 |
-
|
| 184 |
-
x = Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
|
| 185 |
-
x = BatchNormalization()(x)
|
| 186 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 187 |
-
|
| 188 |
-
x = Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
|
| 189 |
-
x = BatchNormalization()(x)
|
| 190 |
-
|
| 191 |
-
x = Add()([x, x_shortcut])
|
| 192 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 193 |
-
|
| 194 |
-
return x
|
| 195 |
-
|
| 196 |
-
def conv_block(x, filters, stride):
|
| 197 |
-
# Convolutional block with shortcut connection
|
| 198 |
-
x_shortcut = x
|
| 199 |
-
|
| 200 |
-
x = Conv2D(filters[0], (1, 1), strides=(stride, stride), padding='valid')(x)
|
| 201 |
-
x = BatchNormalization()(x)
|
| 202 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 203 |
-
|
| 204 |
-
x = Conv2D(filters[1], (3, 3), strides=(1, 1), padding='same')(x)
|
| 205 |
-
x = BatchNormalization()(x)
|
| 206 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 207 |
-
|
| 208 |
-
x = Conv2D(filters[2], (1, 1), strides=(1, 1), padding='valid')(x)
|
| 209 |
-
x = BatchNormalization()(x)
|
| 210 |
-
|
| 211 |
-
x_shortcut = Conv2D(filters[2], (1, 1), strides=(stride, stride), padding='valid')(x_shortcut)
|
| 212 |
-
x_shortcut = BatchNormalization()(x_shortcut)
|
| 213 |
-
|
| 214 |
-
x = Add()([x, x_shortcut])
|
| 215 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 216 |
-
|
| 217 |
-
return x
|
| 218 |
-
|
| 219 |
-
def build_resnet15(input_shape=(224, 224, 3), num_classes=5):
|
| 220 |
-
# Building ResNet15 model
|
| 221 |
-
input_tensor = Input(shape=input_shape)
|
| 222 |
-
|
| 223 |
-
x = Conv2D(32, (7, 7), strides=(2, 2), padding='valid')(input_tensor)
|
| 224 |
-
x = BatchNormalization()(x)
|
| 225 |
-
x = tf.keras.layers.Activation('relu')(x)
|
| 226 |
-
|
| 227 |
-
x = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(x)
|
| 228 |
-
|
| 229 |
-
# Convolutional blocks
|
| 230 |
-
x = conv_block(x, [32, 32, 64], stride=1)
|
| 231 |
-
x = identity_block(x, [64, 64, 64])
|
| 232 |
-
|
| 233 |
-
x = conv_block(x, [64, 64, 128], stride=2)
|
| 234 |
-
x = identity_block(x, [128, 128, 128])
|
| 235 |
-
|
| 236 |
-
x = conv_block(x, [128, 128, 256], stride=2)
|
| 237 |
-
x = identity_block(x, [256, 256, 256])
|
| 238 |
-
|
| 239 |
-
x = conv_block(x, [256, 256, 512], stride=2)
|
| 240 |
-
x = identity_block(x, [512, 512, 512])
|
| 241 |
-
|
| 242 |
-
x = AveragePooling2D((4, 4), padding='valid')(x)
|
| 243 |
-
x = Flatten()(x)
|
| 244 |
-
|
| 245 |
-
# Fully connected layers
|
| 246 |
-
x = Dense(512, activation='relu')(x)
|
| 247 |
-
x = Dropout(0.2)(x)
|
| 248 |
-
|
| 249 |
-
output_tensor = Dense(num_classes, activation='softmax')(x)
|
| 250 |
-
|
| 251 |
-
model = Model(inputs=input_tensor, outputs=output_tensor)
|
| 252 |
-
|
| 253 |
-
return model
|
| 254 |
-
|
| 255 |
-
# Instantiate the ResNet15 model
|
| 256 |
model = build_resnet15()
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
#
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
accuracies = [] # List to store accuracy values during training
|
| 283 |
-
|
| 284 |
-
# Create a learning rate scheduler callback
|
| 285 |
-
lr_scheduler = LearningRateScheduler(lr_schedule)
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
# Custom callback to log learning rates and accuracy
|
| 289 |
-
class LearningRateLogger(tf.keras.callbacks.Callback):
|
| 290 |
-
def on_epoch_begin(self, epoch, logs=None):
|
| 291 |
-
lr = float(tf.keras.backend.get_value(self.model.optimizer.lr))
|
| 292 |
-
learning_rates.append(lr)
|
| 293 |
-
|
| 294 |
-
def on_epoch_end(self, epoch, logs=None):
|
| 295 |
-
accuracies.append(logs.get('accuracy'))
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
# In[10]:
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
# Create an early stopping callback
|
| 302 |
-
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
# In[11]:
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
# Fit the model with training and validation data
|
| 309 |
-
history = model.fit(train_generator, epochs=50, validation_data=val_generator, callbacks=[early_stopping, lr_scheduler, LearningRateLogger()])
|
| 310 |
-
|
| 311 |
-
# Plotting accuracy against learning rates
|
| 312 |
-
plt.figure(figsize=(10, 6))
|
| 313 |
-
plt.plot(learning_rates, accuracies, marker='o')
|
| 314 |
-
plt.title('Accuracy vs Learning Rates')
|
| 315 |
-
plt.xlabel('Learning Rate')
|
| 316 |
-
plt.ylabel('Accuracy')
|
| 317 |
-
plt.xscale('log') # Set x-axis to logarithmic scale for better visualization if needed
|
| 318 |
-
plt.grid(True)
|
| 319 |
-
plt.show()
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
# In[12]:
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
# Evaluate the model on the test generator
|
| 326 |
-
test_loss, test_accuracy, test_recall, test_precision = model.evaluate(test_generator)
|
| 327 |
-
# Print evaluation metrics
|
| 328 |
-
print(f'Test Loss: {test_loss:.4f}')
|
| 329 |
-
print(f'Test Accuracy: {test_accuracy:.4f}')
|
| 330 |
-
print(f'Test Precision: {test_precision:.4f}')
|
| 331 |
-
print(f'Test Recall: {test_recall:.4f}')
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
# In[13]:
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
# Generate predictions
|
| 338 |
-
predictions = model.predict(test_generator)
|
| 339 |
-
predicted_labels = np.argmax(predictions, axis=1)
|
| 340 |
-
true_labels = test_generator.classes
|
| 341 |
-
|
| 342 |
-
# Create a confusion matrix
|
| 343 |
-
conf_matrix = confusion_matrix(true_labels, predicted_labels)
|
| 344 |
-
|
| 345 |
-
# Display confusion matrix
|
| 346 |
-
plt.figure(figsize=(8, 6))
|
| 347 |
-
sns.heatmap(conf_matrix, annot=True, cmap='Blues', fmt='g', xticklabels=labels_list, yticklabels=labels_list)
|
| 348 |
-
plt.xlabel('Predicted labels')
|
| 349 |
-
plt.ylabel('True labels')
|
| 350 |
-
plt.title('Confusion Matrix')
|
| 351 |
-
plt.show()
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
# In[20]:
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
# Display a couple of test images with true and predicted labels
|
| 358 |
-
num_images_to_display = 10 # Specify the number of images to display
|
| 359 |
-
num_cols = 5 # Number of columns in the grid
|
| 360 |
-
num_rows = (num_images_to_display + num_cols - 1) // num_cols # Calculate number of rows needed
|
| 361 |
-
indices = np.random.choice(len(test_generator.filenames), num_images_to_display)
|
| 362 |
-
|
| 363 |
-
# Create subplots in a grid
|
| 364 |
-
fig, axes = plt.subplots(num_rows, num_cols, figsize=(10, 10)) # Adjust figsize as needed
|
| 365 |
-
|
| 366 |
-
for i, idx in enumerate(indices):
|
| 367 |
-
row = i // num_cols
|
| 368 |
-
col = i % num_cols
|
| 369 |
-
|
| 370 |
-
image_path = os.path.join('C:\\Users\\sjimm\\Downloads\\Telegram Desktop\\dataset\\test\\', test_generator.filenames[idx])
|
| 371 |
-
|
| 372 |
-
# Load and resize the image
|
| 373 |
-
img = Image.open(image_path)
|
| 374 |
-
img = img.resize((224, 224))
|
| 375 |
-
img_array = np.array(img) / 255.0 # Normalize pixel values
|
| 376 |
-
|
| 377 |
-
# Reshape and predict
|
| 378 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 379 |
-
prediction = model.predict(img_array)
|
| 380 |
-
predicted_class = np.argmax(prediction)
|
| 381 |
-
true_class = test_generator.classes[idx]
|
| 382 |
-
|
| 383 |
-
# Display image and labels in subplots
|
| 384 |
-
axes[row, col].imshow(img)
|
| 385 |
-
axes[row, col].axis('off')
|
| 386 |
-
axes[row, col].set_title(f"True label: {labels_list[true_class]}\nPredicted label: {labels_list[predicted_class]}")
|
| 387 |
-
|
| 388 |
-
# Hide empty subplots if num_images_to_display is less than num_cols * num_rows
|
| 389 |
-
for i in range(num_images_to_display, num_rows * num_cols):
|
| 390 |
-
row = i // num_cols
|
| 391 |
-
col = i % num_cols
|
| 392 |
-
axes[row, col].axis('off')
|
| 393 |
-
|
| 394 |
-
plt.tight_layout() # Adjust spacing between subplots
|
| 395 |
-
plt.show()
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Load your custom ResNet15 model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model = build_resnet15()
|
| 8 |
+
model.load_weights('"C:\Users\sjimm\Downloads\ResNet15.ipynb"') # Replace with the actual path
|
| 9 |
+
|
| 10 |
+
# Define a function for making predictions
|
| 11 |
+
def predict(input_img):
|
| 12 |
+
# Preprocess the input image
|
| 13 |
+
input_img = input_img.resize((224, 224)) # Resize the image to match the model's input size
|
| 14 |
+
input_img_array = np.array(input_img) / 255.0 # Normalize pixel values
|
| 15 |
+
input_img_array = np.expand_dims(input_img_array, axis=0) # Add batch dimension
|
| 16 |
+
|
| 17 |
+
# Make predictions
|
| 18 |
+
predictions = model.predict(input_img_array)
|
| 19 |
+
predicted_class = np.argmax(predictions)
|
| 20 |
+
|
| 21 |
+
return input_img, {
|
| 22 |
+
"Predicted Class": labels_list[predicted_class],
|
| 23 |
+
"Probabilities": {labels_list[i]: float(predictions[0][i]) for i in range(len(labels_list))}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Create a Gradio interface
|
| 27 |
+
gr.Interface(
|
| 28 |
+
predict,
|
| 29 |
+
inputs=gr.Image(label="Select an image", type="pil"),
|
| 30 |
+
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Predicted Class"), gr.Bar(label="Probabilities")],
|
| 31 |
+
title="ResNet15 Image Classification"
|
| 32 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|