text_plain_output_1.png
from sklearn.model_selection import train_test_split

from tensorflow.keras.applications import VGG16

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import glob

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import pickle

import tensorflow as tf

import tensorflow.keras.backend as K

breast_img = glob.glob('/kaggle/input/breast-histopathology-images/IDC_regular_ps50_idx5/**/*.png', recursive=True)
data = pd.read_csv('/kaggle/input/selected-images/selected_images.csv')

train_data, val_data = train_test_split(data, test_size=0.3, random_state=42)
val_data, test_data = train_test_split(val_data, test_size=0.5, random_state=42)
datagen = ImageDataGenerator(rescale=1.0 / 255)
target_size = (50, 50)
batch_size = 32
train_generator = datagen.flow_from_dataframe(dataframe=train_data, x_col='path', y_col='label', target_size=target_size, batch_size=batch_size, class_mode='raw')
val_generator = datagen.flow_from_dataframe(dataframe=val_data, x_col='path', y_col='label', target_size=target_size, batch_size=batch_size, class_mode='raw')
test_generator = datagen.flow_from_dataframe(dataframe=test_data, x_col='path', y_col='label', target_size=target_size, batch_size=batch_size, class_mode='raw')

A, B = np.unique(train_generator.labels, return_counts=True)
n = len(train_generator.labels)
cls_weights = {i: (n - j) / n for i, j in zip(A, B)}

def f1_score(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    recall = true_positives / (possible_positives + K.epsilon())
    f1_val = 2 * (precision * recall) / (precision + recall + K.epsilon())
    return f1_val

def train_best_model(learning_rate, units):
    pre = VGG16(input_shape=(50, 50, 3), include_top=False, pooling='max')
    for layer in pre.layers:
        layer.trainable = False
    x = tf.keras.layers.Dense(units, activation='relu')(pre.output)
    x = tf.keras.layers.Dense(units / 2, activation='relu')(x)
    out = tf.keras.layers.Dense(1, activation='sigmoid')(x)
    transfer_learning_model = tf.keras.models.Model(pre.input, out)
    transfer_learning_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), loss=tf.keras.losses.BinaryCrossentropy(), metrics=[tf.keras.metrics.Recall(), tf.keras.metrics.Precision(), f1_score])
    history = transfer_learning_model.fit(train_generator, epochs=300, validation_data=val_generator, callbacks=tf.keras.callbacks.EarlyStopping(patience=5, min_delta=0.001))
    test_results = transfer_learning_model.evaluate(test_generator)
    return (transfer_learning_model, history, test_results)

model.save('transfer_learning_modelVGG16.h5')
with open('transfer_learning_historyVGG16.pkl', 'wb') as file:
    pickle.dump(history.history, file)
with open('transfer_learning_test_resultsVGG16.pkl', 'wb') as file:
    pickle.dump(test_results, file)

def plot_the_results(history):
    plt.style.use('seaborn')
    plt.figure(figsize=(10, 5))
    plt.plot(history.epoch, history.history['val_f1_score'], label='Val F1 Score', linewidth=2)
    plt.plot(history.epoch, history.history['f1_score'], label='f1_score', linewidth=2)
    plt.legend()
    plt.title('F1 Score')
    plt.show()
    plt.figure(figsize=(10, 5))
    plt.plot(history.epoch, history.history['val_precision'], label='Val Precision', linewidth=2)
    plt.plot(history.epoch, history.history['precision'], label='Precision', linewidth=2)
    plt.legend()
    plt.title('Precision')
    plt.show()
    plt.figure(figsize=(10, 5))
    plt.plot(history.epoch, history.history['val_recall'], label='Val Recall', linewidth=2)
    plt.plot(history.epoch, history.history['recall'], label='Recall', linewidth=2)
    plt.legend()
    plt.title('Recall')
    plt.show()
plot_the_results(history)