Apex For Bres 1
Apex For Bres 1
import warnings
warnings.filterwarnings('ignore')
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os, time, tqdm
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import *
import tensorflow as tf
from functools import partial
# import keras
from tensorflow.keras import layers
import albumentations as A
import tensorflow_hub as hub
from tensorflow.keras.layers import Input, Dense, Flatten, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.callbacks import CSVLogger
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.metrics import Precision, Recall
from sklearn.metrics import classification_report, f1_score
from sklearn.model_selection import KFold
image_dir = './BreaKHis_v1/'
data_path = './Folds.csv'
# experimental API for making data pipelines
tf.data.experimental.AUTOTUNE
# defining the class names
class_names = ['malignant', 'benign']
print(class_names)
# loading the data
data = pd.read_csv(data_path)
data.head(5)
data['fold'].value_counts()
# renaming and structuring the columns for better data understanding
data = data.rename(columns={'filename': 'path'})
data['label'] = data.path.apply(lambda x: x.split('/')[3])
data['label_int'] = data.label.apply(lambda x: class_names.index(x))
data['filename'] = data.path.apply(lambda x: x.split('/')[-1])
# view first n rows of strucrured data
data.head(6)
data.shape
# making a plot to see data distribution
# sns.figure()
sns.set_theme()
sns.displot(x='label', data=data)
sns.countplot(x=data['label'], data=data)
# sorting out training, validation and testing images
test_images = data.groupby(by='label').sample(3000)
train_images = data.drop(test_images.index).reset_index(drop=True)
test_images = test_images.reset_index(drop=True)
# making splits of training & validation datasets
validation_images = train_images.sample(frac = 0.3)
train_images = train_images.drop(validation_images.index).reset_index(drop=True)
validation_images = validation_images.reset_index(drop=True)
print('Total training images: % s' % str(train_images.shape[0]))
print('Total validation images: % s' % str(validation_images.shape[0]))
print('Total testing images: % s' % str(test_images.shape[0]))
train_images['set'] = 'train'
validation_images['set'] = 'validation'
test_images['set'] = 'test'
new_data = pd.concat([train_images, validation_images, test_images])
new_data.head(5)
sns.set(rc={'figure.figsize':(10.4, 5.4)})
sns.countplot(x=new_data['label'], hue=new_data['set'])
max_count = np.max(train_images.label.value_counts())
min_count = np.min(train_images.label.value_counts())
train_images = train_images.groupby('label').sample(n=max_count, replace=True)
train_images = train_images.reset_index(drop=True)
train_images.head(5)
model_handle_map = {"efficientnetv2-b0":
"https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b0/feature_vector/2",
"inception_v3": "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/4",
"inception_resnet_v2": "https://tfhub.dev/google/imagenet/inception_resnet_v2/feature-
vector/4"}
model_image_size = {"efficientnetv2-b0": 224,
"inception_v3": 299,
"inception_resnet_v2": 299}
# function to decode a PNG image into a tf tensor
def load_image(path, label):
image = tf.io.read_file(path)
image = tf.io.decode_png(image, channels=3)
return image, label
def view_model_predictions():
plt.figure(figsize = (30, 8))
plt.rcParams.update({'font.size': 10})
plt.subplots_adjust(wspace = 0.05, hspace = 0.15)
for i in range(30):
ax = plt.subplot(3, 10, i + 1)
shape = str(test_image[i].numpy().shape)
plt.imshow(test_image[i].numpy())
plt.title(predicted_label[i][0])
plt.axis("off")
plt.tight_layout
return None
train_dataset
# checking the path of images
train_images.path[5]
start = time.time()
view_image(train_dataset)
end = time.time()
print('Time Taken: %.3f seconds' % (end-start))
start = time.time()
view_image(val_dataset)
end = time.time()
print('Time Taken: %.3f seconds' % (end-start))
def get_labels_from_tfdataset(tfdataset, batched=False):
if not batched:
return tf.concat(labels, axis=0) # concat the list of batched labels
return labels
get_labels_from_tfdataset(train_dataset)
print('Size of Image being used: %d' % (IMG_SIZE))
# starting a new sesion for TF
image_size = 224
tf.keras.backend.clear_session()
model_nn = make_nn_model(IMG_SIZE)
# model logs
csv_logger = CSVLogger('cnn_model_logs.csv', append=True)