<jupyter_start><jupyter_text>Preparing Working Env<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

from importlib.util import find_spec
if find_spec("core") is None:
    import sys
    sys.path.append('..')

import tensorflow as tf
import tensorflow_datasets as tfds
from core.datasets import RetinaDataset
#This code snippet helps if your computer has RTX 2070 GPU. If not then comment this cell.
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
from functools import partial
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.metrics import classification_report, plot_confusion_matrix, confusion_matrix, silhouette_samples
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.mixture import GaussianMixture<jupyter_output><empty_output><jupyter_text>Preparing the Data<jupyter_code>ds_val, ds_val_info     = tfds.load('RetinaDataset', split='train[-2%:]', shuffle_files=True, as_supervised=True,with_info=True)
ds_test, ds_test_info   = tfds.load('RetinaDataset', split='test', shuffle_files=False, as_supervised=True,with_info=True)
len(ds_val),len(ds_test)
NCLASS   = 4
IMG_SIZE = 224

def resize_image(img, lb):
  return tf.image.resize(img, (224,224)), tf.one_hot(lb, NCLASS)

ds_val = ds_val.map(resize_image)<jupyter_output><empty_output><jupyter_text>Creating Classifier with ResNet Base<jupyter_code>def create_basemodel(input_shape=(224,224,3),output_units=4):
    preprocess = tf.keras.applications.resnet_v2.preprocess_input
    base_model = tf.keras.applications.ResNet50V2(include_top=False)

    base_model.trainable = False

    inputs = tf.keras.layers.Input(input_shape)
    pool    = tf.keras.layers.GlobalAveragePooling2D()
    flatten = tf.keras.layers.Flatten()

    softmax   = tf.keras.layers.Dense(output_units, activation='softmax')

    x = inputs
    x = preprocess(x)
    x = base_model(x)
    x = tf.keras.layers.Conv2D(64, 3, activation="relu")(x)
    x = pool(x)
    x = flatten(x)
    out = softmax(x)

    return tf.keras.Model(inputs=inputs, outputs=out)<jupyter_output><empty_output><jupyter_text>Selecting the Best Model<jupyter_code>refine_resnet_base = create_basemodel()
# Extract the base model
base_model = refine_resnet_base.layers[3]

# unfreeze it
base_model.trainable = True

# select only the last resnet block for retraining
# keeping the batchnorm layer unchanged
for l in base_model.layers:
  name = l.name
  if name.startswith('conv5_block3') and not isinstance(l, tf.keras.layers.BatchNormalization):
    l.trainable = True
  else:
    l.trainable = False
optimizer = tf.keras.optimizers.Adam(lr=1e-4)
metrics = ['accuracy']
refine_resnet_base.compile(optimizer=optimizer, 
                           loss='categorical_crossentropy', 
                           metrics=metrics)
refine_resnet_base.load_weights('checkpoints/finetune_resnet_model.10-0.47.h5')
refine_resnet_base.evaluate(ds_test.map(resize_image).batch(32))
refine_resnet_base.evaluate(ds_val.batch(32))<jupyter_output>53/53 [==============================] - 3s 62ms/step - loss: 0.4728 - accuracy: 0.8401<jupyter_text>Active Learning<jupyter_code># refine_resnet_base.layers[5]
idx = 6  # index of desired layer

inputs = tf.keras.layers.Input((224,224,3))
x = refine_resnet_base.layers[0](inputs)
for layer in refine_resnet_base.layers[1:idx+1]:
    x = layer(x)

new_model = tf.keras.Model(inputs, x)
proj_testset = new_model.predict(ds_test.map(resize_image).batch(32))<jupyter_output><empty_output><jupyter_text>PCA followed by KMeans<jupyter_code>test_targets = [t.numpy() for t in ds_test.map(lambda img, lb: lb).batch(128)]
test_targets = np.hstack(test_targets)
def get_pca_kmeans_res(n_components=2,n_clusters=4):
    l2 = np.square(proj_testset).mean(axis=-1, keepdims=True)**0.5
    proj_normed = proj_testset / l2
    proj_normed_pcas = PCA(n_components=n_components).fit_transform(proj_normed)
    cols = []
    for i in range(n_components):
        cols.append('PCA{}'.format(i+1))
    df = pd.DataFrame(proj_normed_pcas, columns=cols)
    df['targets'] = test_targets
    kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(proj_normed_pcas)
    df['cluster'] = kmeans.labels_
    cluster_reverse_map = df.loc[:,['targets','cluster']].groupby('cluster').agg(lambda x:x.value_counts().index[0]).to_dict('index')
    df['inferred_label'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['targets'])
    print(f"Accuracy:  {sum(df.targets == df.inferred_label)/ len(df.targets)}")
    print("\n")
    print("Classification Report:")
    print(classification_report(df.targets, df.inferred_label))
get_pca_kmeans_res(n_components=2,n_clusters=4)
get_pca_kmeans_res(n_components=3,n_clusters=4)
get_pca_kmeans_res(n_components=4,n_clusters=4)<jupyter_output>Accuracy:  0.862603305785124


Classification Report:
              precision    recall  f1-score   support

           0       0.76      1.00      0.86       242
           1       0.87      0.74      0.80       242
           2       0.93      0.88      0.91       242
           3       0.93      0.83      0.88       242

    accuracy                           0.86       968
   macro avg       0.87      0.86      0.86       968
weighted avg       0.87      0.86      0.86       968<jupyter_text>TSNE followed by KMeans<jupyter_code>def get_tsne_kmeans_res(n_components=2,n_clusters=4):
    l2 = np.square(proj_testset).mean(axis=-1, keepdims=True)**0.5
    proj_normed = proj_testset / l2
    proj_normed_pcas = TSNE(random_state=0,n_components=n_components).fit_transform(proj_normed)
    cols = []
    for i in range(n_components):
        cols.append('PCA{}'.format(i+1))
    df = pd.DataFrame(proj_normed_pcas, columns=cols)
    df['targets'] = test_targets
    kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(proj_normed_pcas)
    df['cluster'] = kmeans.labels_
    cluster_reverse_map = df.loc[:,['targets','cluster']].groupby('cluster').agg(lambda x:x.value_counts().index[0]).to_dict('index')
    df['inferred_label'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['targets'])
    print(f"Accuracy:  {sum(df.targets == df.inferred_label)/ len(df.targets)}")
    print("\n")
    print("Classification Report:")
    print(classification_report(df.targets, df.inferred_label))
get_tsne_kmeans_res(n_components=2,n_clusters=4)
get_tsne_kmeans_res(n_components=3,n_clusters=4)<jupyter_output>Accuracy:  0.9214876033057852


Classification Report:
              precision    recall  f1-score   support

           0       0.90      1.00      0.95       242
           1       0.90      0.92      0.91       242
           2       0.96      0.90      0.93       242
           3       0.92      0.86      0.89       242

    accuracy                           0.92       968
   macro avg       0.92      0.92      0.92       968
weighted avg       0.92      0.92      0.92       968<jupyter_text>PCA followed by GMM<jupyter_code>def get_pca_gmm_res(n_components=2,n_clusters=4):
    l2 = np.square(proj_testset).mean(axis=-1, keepdims=True)**0.5
    proj_normed = proj_testset / l2
    proj_normed_pcas = PCA(n_components=n_components).fit_transform(proj_normed)
    cols = []
    for i in range(n_components):
        cols.append('PCA{}'.format(i+1))
    df = pd.DataFrame(proj_normed_pcas, columns=cols)
    df['targets'] = test_targets
    gmm = GaussianMixture(n_components=n_clusters, random_state=0).fit(proj_normed_pcas)
    df['cluster'] = gmm.predict(proj_normed_pcas)
    cluster_reverse_map = df.loc[:,['targets','cluster']].groupby('cluster').agg(lambda x:x.value_counts().index[0]).to_dict('index')
    df['inferred_label'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['targets'])
    print(f"Accuracy:  {sum(df.targets == df.inferred_label)/ len(df.targets)}")
    print("\n")
    print("Classification Report:")
    print(classification_report(df.targets, df.inferred_label))
get_pca_gmm_res(n_components=2,n_clusters=4)
get_pca_gmm_res(n_components=3,n_clusters=4)<jupyter_output>Accuracy:  0.9070247933884298


Classification Report:
              precision    recall  f1-score   support

           0       0.98      0.86      0.91       242
           1       0.79      0.94      0.86       242
           2       0.97      0.92      0.94       242
           3       0.92      0.91      0.92       242

    accuracy                           0.91       968
   macro avg       0.92      0.91      0.91       968
weighted avg       0.92      0.91      0.91       968<jupyter_text>TSNE followed by GMM<jupyter_code>def get_tsne_gmm_res(n_components=2,n_clusters=4):
    l2 = np.square(proj_testset).mean(axis=-1, keepdims=True)**0.5
    proj_normed = proj_testset / l2
    proj_normed_pcas = TSNE(random_state=0,n_components=n_components).fit_transform(proj_normed)
    cols = []
    for i in range(n_components):
        cols.append('PCA{}'.format(i+1))
    df = pd.DataFrame(proj_normed_pcas, columns=cols)
    df['targets'] = test_targets
    gmm = GaussianMixture(n_components=n_clusters, random_state=0).fit(proj_normed_pcas)
    df['cluster'] = gmm.predict(proj_normed_pcas)
    cluster_reverse_map = df.loc[:,['targets','cluster']].groupby('cluster').agg(lambda x:x.value_counts().index[0]).to_dict('index')
    df['inferred_label'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['targets'])
    print(f"Accuracy:  {sum(df.targets == df.inferred_label)/ len(df.targets)}")
    print("\n")
    print("Classification Report:")
    print(classification_report(df.targets, df.inferred_label))
get_tsne_gmm_res(n_components=2,n_clusters=4)
get_tsne_gmm_res(n_components=3,n_clusters=4)<jupyter_output>Accuracy:  0.8150826446280992


Classification Report:
              precision    recall  f1-score   support

           0       0.99      0.75      0.85       242
           1       0.68      0.62      0.65       242
           2       0.97      0.92      0.94       242
           3       0.70      0.97      0.82       242

    accuracy                           0.82       968
   macro avg       0.83      0.82      0.82       968
weighted avg       0.83      0.82      0.82       968<jupyter_text>Identifying And Visualizing Decision Boundaries using Silhouette Scores<jupyter_code>n_components = 2
n_clusters = 4
l2 = np.square(proj_testset).mean(axis=-1, keepdims=True)**0.5
proj_normed = proj_testset / l2
proj_normed_pcas = TSNE(random_state=0,n_components=n_components).fit_transform(proj_normed)
cols = []
for i in range(n_components):
    cols.append('PCA{}'.format(i+1))
df = pd.DataFrame(proj_normed_pcas, columns=cols)
df['targets'] = test_targets
kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(proj_normed_pcas)
df['cluster'] = kmeans.labels_
cluster_reverse_map = df.loc[:,['targets','cluster']].groupby('cluster').agg(lambda x:x.value_counts().index[0]).to_dict('index')
for k_idx in range(len(cluster_reverse_map.keys())):
    cluster_reverse_map[k_idx]['centroid'] = kmeans.cluster_centers_[k_idx]
df['inferred_label'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['targets'])
df['centroid'] = df.cluster.apply(lambda x: cluster_reverse_map[x]['centroid'])
df['silhouette_score'] = silhouette_samples(proj_normed_pcas, df.cluster)
df.head()
df.groupby('cluster').silhouette_score.hist(bins=50, alpha=0.5, legend=True, figsize=(15,5))
plt.show()
df.loc[df.silhouette_score<0.5].shape[0],df.shape[0]
df['low_silhouette_flag'] = df.silhouette_score<0.3
df.low_silhouette_flag = df.low_silhouette_flag.astype(int)
f, axes = plt.subplots(1, 3, figsize=(15,5))
sns.kdeplot(x ='PCA1', y='PCA2', data= df, hue='cluster', palette= sns.color_palette()[0:4], ax=axes[0])
sns.kdeplot(x ='PCA1', y='PCA2', data= df, hue='targets', palette= sns.color_palette()[0:4], ax=axes[1])
sns.kdeplot(x ='PCA1', y='PCA2', data= df, hue='low_silhouette_flag', palette= sns.color_palette()[0:2], ax=axes[2])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12696443341604632, 'fraction_numerical': 0.04846980976013234, 'mean_word_length': 3.6737533822960957, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '195128', 'license': 'MIT', 'n_tokens_mistral': 4757, 'n_tokens_neox': 4380, 'n_words': 715, 'path': 'notebooks/09-Sub_Sampling_For_Active_Learning.ipynb', 'repo_name': 'swipswaps/retinal_oct', 'size': '12090'}