| |
| """ResNet50.ipynb |
| |
| Automatically generated by Colaboratory. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1Ztagc2mpxc2YEeFMut7EwFhL8SFY2gAm |
| """ |
|
|
| import numpy as np |
| import pandas as pd |
| from PIL import Image |
| import pickle |
| from sklearn.utils import shuffle |
| from sklearn.model_selection import train_test_split |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator |
| from tensorflow.keras.models import Sequential |
| from tensorflow.keras.layers import Dense |
| import os |
| from tensorflow.keras.applications import ResNet50 |
| from tensorflow.keras.applications.resnet50 import preprocess_input |
|
|
| datagen = ImageDataGenerator(rescale=1.0/255.0, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') |
|
|
| batch_size = 20 |
| train_data_dir = '/content/drive/MyDrive/BoneFractureDataset/training' |
| validation_data_dir = '/content/drive/MyDrive/BoneFractureDataset/testing' |
| train_generator = datagen.flow_from_directory( train_data_dir, |
| target_size=(224, 224), |
| batch_size=batch_size, |
| class_mode='binary', |
| shuffle=True ) |
| validation_generator = datagen.flow_from_directory( |
| validation_data_dir, |
| target_size=(224, 224), |
| batch_size=batch_size, |
| class_mode='binary', |
| shuffle=False |
| ) |
|
|
| !ls /kaggle/input/resnet50-weights/ |
|
|
| !stat /kaggle/input/resnet50-weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 |
|
|
| !wget https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 -P /kaggle/input/resnet50-weights/ |
|
|
| !cat /kaggle/input/resnet50-weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 |
|
|
| !ls -l /kaggle/input/resnet50-weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 |
|
|
| !wget https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 -P /kaggle/input/resnet50-weights |
|
|
| resModel = Sequential() |
| resModel.add(ResNet50( |
| include_top=False, |
| pooling='avg', |
| weights=None, |
| )) |
| resModel.add(Dense(1, activation='sigmoid')) |
| for layer in resModel.layers[0].layers[-50:]: |
| layer.trainable = True |
|
|
| from tensorflow.keras.optimizers import Adam |
| from tensorflow.keras.callbacks import ReduceLROnPlateau |
| optimizer = Adam(learning_rate=0.001) |
| reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.0001) |
| resModel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy']) |
| epochs = 10 |
| history = resModel.fit(train_generator, epochs=epochs, validation_data=validation_generator, callbacks=[reduce_lr]) |
| evaluation = resModel.evaluate(train_generator) |
| print(f"Test Accuracy: {evaluation[1] * 100:.2f}%") |
|
|
| initial_epoch = 0 |
| saved_history = { |
| 'loss': history.history['loss'], |
| 'accuracy': history.history['accuracy'], |
| 'val_loss': history.history['val_loss'], |
| 'val_accuracy': history.history['val_accuracy'], |
| } |
|
|
| import matplotlib.pyplot as plt |
| from matplotlib.lines import Line2D |
| from matplotlib.legend_handler import HandlerLine2D |
| import numpy as np |
|
|
| initial_epoch = 10 |
| saved_history = { |
| 'loss': history.history['loss'], |
| 'accuracy': history.history['accuracy'], |
| 'val_loss': history.history['val_loss'], |
| 'val_accuracy': history.history['val_accuracy'], |
| } |
|
|
| !ls /kaggle/working |
|
|
| !ls -l /kaggle/working/saved_D201history.npy |
|
|
| !find / -name saved_D201history.npy |
|
|
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, log_loss, jaccard_score |
| true_classes = [1, 0, 1, 1, 0] |
| predicted_classes = [1, 1, 0, 1, 0] |
| print(f"Accuracy: {accuracy_score(true_classes, predicted_classes)}") |
| print(f"Precision: {precision_score(true_classes, predicted_classes)}") |
| print(f"Recall: {recall_score(true_classes, predicted_classes)}") |
| print(f"F1 Score: {f1_score(true_classes, predicted_classes)}") |
| print(f"Log Loss: {log_loss(true_classes, predicted_classes)}") |
| print(f"Jaccard Score: {jaccard_score(true_classes, predicted_classes)}") |
|
|
| from sklearn.metrics import classification_report |
|
|
| print("\nClassification Report:") |
| print(classification_report(true_classes, predicted_classes,digits=4)) |
|
|
| from sklearn.metrics import roc_curve, roc_auc_score |
| import matplotlib.pyplot as plt |
| from matplotlib.patches import Patch |
|
|
| def save_and_display_gradcam(img_path, heatmap, alpha=0.7): |
| img = cv2.imread(img_path) |
| img = cv2.resize(img, (299, 299)) |
| heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) |
| heatmap = np.uint8(255 * heatmap) |
| heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_PLASMA) |
| superimposed_img = cv2.addWeighted(heatmap, alpha, img, 1 - alpha, 0) |
| plt.figure(figsize=(4, 4)) |
| plt.imshow(cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB)) |
| plt.title('GradCAM', fontdict={'family': 'Serif', 'weight': 'bold', 'size': 12}) |
| plt.axis('off') |
| plt.tight_layout() |
| plt.show() |
|
|
| def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None): |
| model.layers[-1].activation = None |
| grad_model = tf.keras.models.Model( |
| [model.inputs], [model.get_layer(last_conv_layer_name).output, model.output] |
| ) |
| with tf.GradientTape() as tape: |
| last_conv_layer_output, preds = grad_model(img_array) |
| if pred_index is None: |
| pred_index = tf.argmax(preds[0]) |
| class_channel = preds[:, pred_index] |
| grads = tape.gradient(class_channel, last_conv_layer_output) |
| pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2)) |
| last_conv_layer_output = last_conv_layer_output[0] |
| heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis] |
| heatmap = tf.squeeze(heatmap) |
| heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap) |
| return heatmap.numpy() |
|
|
| import cv2 |
|
|
| abcd = cv2.imread('/content/drive/MyDrive/BoneFractureDataset/testing/fractured/3.jpg') |
|
|
| plt.imshow(abcd) |