{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nimport shutil\nprint(os.listdir(\"/kaggle/input\"))\n\nfrom glob import glob \nfrom skimage.io import imread\nimport gc\n\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2024-07-12T20:40:14.00269Z","iopub.execute_input":"2024-07-12T20:40:14.003231Z","iopub.status.idle":"2024-07-12T20:40:14.018114Z","shell.execute_reply.started":"2024-07-12T20:40:14.003176Z","shell.execute_reply":"2024-07-12T20:40:14.017043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2024-07-12T20:40:14.020595Z","iopub.execute_input":"2024-07-12T20:40:14.021146Z","iopub.status.idle":"2024-07-12T20:40:14.031374Z","shell.execute_reply.started":"2024-07-12T20:40:14.021109Z","shell.execute_reply":"2024-07-12T20:40:14.030555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tf_keras","metadata":{"execution":{"iopub.status.busy":"2024-07-12T20:40:14.032527Z","iopub.execute_input":"2024-07-12T20:40:14.03286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import tf_keras as keras","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# version_fn = getattr(tf.keras, \"version\", None)\n# if version_fn and version_fn().startswith(\"3.\"):\n#     import tf_keras as keras\n# else:\n#     keras = tf.keras","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tf_keras import Sequential","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_probability as tfp","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"version_fn = getattr(tf.keras, \"version\", None)\nif version_fn and version_fn().startswith(\"3.\"):\n    import tf_keras as keras\nelse:\n    keras = tf.keras","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom keras.optimizers import Adam\n#from keras.optimizers import RMSprop\n#from keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import initializers","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.optimizers import Adam","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\ntfd = tfp.distributions\ntfpl = tfp.layers","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow as tf\n# import tensorflow_probability as tfp\n\n# # from tensorflow.keras.models import Sequential\n# # from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, InputLayer\n# # from tensorflow.keras.losses import SparseCategoricalCrossentropy, BinaryCrossentropy\n# # from tensorflow.keras.optimizers import RMSprop\n# #if this does *not* work delete the below and comment-in the above\n\n# import tensorflow as tf\n# import tensorflow_probability as tfp\n# from tensorflow.keras.models import Sequential\n# from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\n# from tensorflow.keras.optimizers import Adam\n# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n# import os\n# import numpy as np\n# import matplotlib.pyplot as plt\n\n\n# tfd = tfp.distributions\n# tfpl = tfp.layers","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#opt = Adam(lr=0.001)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objs as go\nimport copy\nimport os\nimport torch\nfrom PIL import Image\nfrom PIL import Image, ImageDraw","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#base_tile_dir = '/kaggle/working/base_dir/'\nbase_tile_dir = '/kaggle/input/base_dir/'\n\ndf = pd.DataFrame({'path': glob(os.path.join(base_tile_dir,'*.tif'))})\ndf['id'] = df.path.map(lambda x: x.split('/')[3].split(\".\")[0])\nlabels = pd.read_csv(\"../input/train_labels.csv\")\ndf_data = df.merge(labels, on = \"id\")\n\n# removing this image because it caused a training error previously\ndf_data = df_data[df_data['id'] != 'dd6dfed324f9fcb6f93f46f32fc800f2ec196be2']\n\n# removing this image because it's black\ndf_data = df_data[df_data['id'] != '9369c7278ec8bcc6c880d99194de09fc2bd4efbe']\ndf_data.head(3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"why did the previous *not* work?\n*try deleting the following directories as i may have made several copies of the same .tif files.*","metadata":{}},{"cell_type":"code","source":"#os.listdir('../input/histopathologic-cancer-detection')\nos.listdir('../input/')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(os.listdir('../input/histopathologic-cancer-detection/train')))\n# print(len(os.listdir('../input/histopathologic-cancer-detection/test')))\nprint(len(os.listdir('../input/train')))\nprint(len(os.listdir('../input/test')))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#df_data = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')\ndf_data = pd.read_csv('../input/train_labels.csv')\n\n\n# removing this image because it caused a training error previously\ndf_data[df_data['id'] != 'dd6dfed324f9fcb6f93f46f32fc800f2ec196be2']\n\n# removing this image because it's black\ndf_data[df_data['id'] != '9369c7278ec8bcc6c880d99194de09fc2bd4efbe']\n\n\nprint(df_data.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data['label'].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_category_images(col_name,figure_cols, df, IMAGE_PATH):\n    \n    \"\"\"\n    Give a column in a dataframe,\n    this function takes a sample of each class and displays that\n    sample on one row. The sample size is the same as figure_cols which\n    is the number of columns in the figure.\n    Because this function takes a random sample, each time the function is run it\n    displays different images.\n    \"\"\"\n    \n\n    categories = (df.groupby([col_name])[col_name].nunique()).index\n    f, ax = plt.subplots(nrows=len(categories),ncols=figure_cols, \n                         figsize=(4*figure_cols,4*len(categories))) # adjust size here\n    # draw a number of images for each location\n    for i, cat in enumerate(categories):\n        sample = df[df[col_name]==cat].sample(figure_cols) # figure_cols is also the sample size\n        for j in range(0,figure_cols):\n            file=IMAGE_PATH + sample.iloc[j]['id'] + '.tif'\n            im=cv2.imread(file)\n            ax[i, j].imshow(im, resample=True, cmap='gray')\n            ax[i, j].set_title(cat, fontsize=16)  \n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ls /kaggle/working/base_dir/\n#IMAGE_PATH = '../input/histopathologic-cancer-detection/train/' \nIMAGE_PATH = '../input/train/' \n\ndraw_category_images('label',4, df_data, IMAGE_PATH)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAMPLE_SIZE = 80000 # load 80k negative examples","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0=df_data[df_data['label']==0].sample(SAMPLE_SIZE,random_state=101)\ndf_1=df_data[df_data['label']==1].sample(SAMPLE_SIZE,random_state=101)\n\n# concat the dataframes\ndf_data = pd.concat([df_0, df_1], axis=0).reset_index(drop=True)\n# shuffle\ndf_data = shuffle(df_data)\n\ndf_data['label'].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df_data['label']\n\ndf_train, df_val = train_test_split(df_data, test_size=0.10, random_state=101, stratify=y)\n\nprint(df_train.shape)\nprint(df_val.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir='base_dir'\nos.mkdir(base_dir)\n\n# now we create 2 folders inside 'base_dir':\n\n# train_dir\n    # a_no_tumor_tissue\n    # b_has_tumor_tissue\n\n# val_dir\n    # a_no_tumor_tissue\n    # b_has_tumor_tissue\n\n\n\n# create a path to 'base_dir' to which we will join the names of the new folders\n# train_dir\ntrain_dir = os.path.join(base_dir, 'train_dir')\nos.mkdir(train_dir)\n\n# val_dir\nval_dir = os.path.join(base_dir, 'val_dir')\nos.mkdir(val_dir)\n\n\n\n# [CREATE FOLDERS INSIDE THE TRAIN AND VALIDATION FOLDERS]\n# Inside each folder we create seperate folders for each class\n\n# create new folders inside train_dir\nno_tumor_tissue = os.path.join(train_dir, 'a_no_tumor_tissue')\nos.mkdir(no_tumor_tissue)\nhas_tumor_tissue = os.path.join(train_dir, 'b_has_tumor_tissue')\nos.mkdir(has_tumor_tissue)\n\n\n# create new folders inside val_dir\nno_tumor_tissue = os.path.join(val_dir, 'a_no_tumor_tissue')\nos.mkdir(no_tumor_tissue)\nhas_tumor_tissue = os.path.join(val_dir, 'b_has_tumor_tissue')\nos.mkdir(has_tumor_tissue)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('base_dir/train_dir')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data.set_index('id', inplace=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a list of train and val images\ntrain_list = list(df_train['id'])\nval_list = list(df_val['id'])\n\n\n\n# Transfer the train images\n\nfor image in train_list:\n    \n    # the id in the csv file does not have the .tif extension therefore we add it here\n    fname = image + '.tif'\n    # get the label for a certain image\n    target = df_data.loc[image,'label']\n    \n    # these must match the folder names\n    if target == 0:\n        label = 'a_no_tumor_tissue'\n    if target == 1:\n        label = 'b_has_tumor_tissue'\n    \n    # source path to image\n#    src = os.path.join('../input/histopathologic-cancer-detection/train', fname)\n    src = os.path.join('../input/train', fname)\n\n    # destination path to image\n    dst = os.path.join(train_dir, label, fname)\n    # copy the image from the source to the destination\n    shutil.copyfile(src, dst)\n\n\n# Transfer the val images\n\nfor image in val_list:\n    \n    # the id in the csv file does not have the .tif extension therefore we add it here\n    fname = image + '.tif'\n    # get the label for a certain image\n    target = df_data.loc[image,'label']\n    \n    # these must match the folder names\n    if target == 0:\n        label = 'a_no_tumor_tissue'\n    if target == 1:\n        label = 'b_has_tumor_tissue'\n    \n\n    # source path to image\n    #src = os.path.join('../input/histopathologic-cancer-detection/train', fname)\n    src = os.path.join('../input/train', fname)\n\n    # destination path to image\n    dst = os.path.join(val_dir, label, fname)\n    # copy the image from the source to the destination\n    shutil.copyfile(src, dst)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(os.listdir('base_dir/train_dir/a_no_tumor_tissue')))\n# print(len(os.listdir('base_dir/train_dir/b_has_tumor_tissue')))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(os.listdir('base_dir/val_dir/a_no_tumor_tissue')))\n# print(len(os.listdir('base_dir/val_dir/b_has_tumor_tissue')))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = 'base_dir/train_dir'\nvalid_path = 'base_dir/val_dir'\n#test_path = '../input/histopathologic-cancer-detection/test'\ntest_path = '../input/test'\n\nIMAGE_SIZE = 96\nnum_train_samples = len(df_train)\nnum_val_samples = len(df_val)\ntrain_batch_size = 10\nval_batch_size = 10\n\n\ntrain_steps = np.ceil(num_train_samples / train_batch_size)\nval_steps = np.ceil(num_val_samples / val_batch_size)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0/255)\n\ntrain_gen = datagen.flow_from_directory(train_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=train_batch_size,\n                                        class_mode='categorical')\n\nval_gen = datagen.flow_from_directory(valid_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=val_batch_size,\n                                        class_mode='categorical')\n\n# Note: shuffle=False causes the test dataset to not be shuffled\ntest_gen = datagen.flow_from_directory(valid_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=1,\n                                        class_mode='categorical',\n                                        shuffle=False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install tf_keras","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install --upgrade tensorflow","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TF_USE_LEGACY_KERAS=1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"divergence_fn = lambda q, p, _: tfd.kl_divergence(q, p) / total_samples\n\ntfpl.Convolution2DReparameterization(\n           input_shape = (128,6), filters = 8, kernel_size = 16,\n           activation = 'relu',\n           kernel_prior_fn = tfpl.default_multivariate_normal_fn,\n           kernel_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n           kernel_divergence_fn = divergence_fn,\n           bias_prior_fn = tfpl.default_multivariate_normal_fn,\n           bias_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n           bias_divergence_fn = divergence_fn)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_normal_prior(dtype, shape, name, trainable, add_variable_fn):\n    distribution = tfd.Normal(loc = 0.1 * tf.ones(shape, dtype),\n                              scale = 1.5 * tf.ones(shape, dtype))\n    batch_ndims = tf.size(distribution.batch_shape_tensor())\n    \n    distribution = tfd.Independent(distribution,\n                                   reinterpreted_batch_ndims = batch_ndims)\n    return distribution\n    \ndef laplace_prior(dtype, shape, name, trainable, add_variable_fn):\n    distribution = tfd.Laplace(loc = tf.zeros(shape, dtype),\n                               scale = tf.ones(shape, dtype))\n    batch_ndims = tf.size(distribution.batch_shape_tensor())\n    \n    distribution = tfd.Independent(distribution,\n                                   reinterpreted_batch_ndims = batch_ndims)\n    return distribution","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shape = (4, )\ndtype = tf.float32\ndistribution = tfd.Normal(loc = tf.zeros(shape, dtype),\n                            scale = tf.ones(shape, dtype))\n\nfor i in range(len(shape) + 1):\n    print('reinterpreted_batch_ndims: %d:' %(i))\n    independent_dist = tfd.Independent(distribution,\n                                       reinterpreted_batch_ndims = i)\n    samples = independent_dist.sample()\n    print('batch_shape: {}' \n          ' event_shape: {}' \n          ' Sample shape: {}'.format(independent_dist._batch_shape(),\n                                    independent_dist._event_shape(),\n                                    samples.shape))\n    print('Samples:', samples.numpy(), '\\n')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def approximate_kl(q, p, q_tensor):\n    return tf.reduce_mean(q.log_prob(q_tensor) - p.log_prob(q_tensor))\n\ntotal_samples = 60000\ndivergence_fn = lambda q, p, q_tensor : approximate_kl(q, p, q_tensor) / total_samples","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def conv_reparameterization_layer(filters, kernel_size, activation):\n    # For simplicity, we use default prior and posterior.\n    # In the next parts, we will use custom mixture prior and posteriors.\n    return tfpl.Convolution2DReparameterization(\n            filters = filters,\n            kernel_size = kernel_size,\n            activation = activation, \n            padding = 'same',\n            kernel_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n            kernel_prior_fn = tfpl.default_multivariate_normal_fn,\n            \n            bias_prior_fn = tfpl.default_multivariate_normal_fn,\n            bias_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n            \n            kernel_divergence_fn = divergence_fn,\n            bias_divergence_fn = divergence_fn)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = tf.keras.Sequential()\nbayesian_cnn = tf.keras.Sequential([\n\n    tf.keras.layers.InputLayer((96, 96, 3)),\n    #tf.keras.layers.InputLayer((96, 96, 1)),\n\n    \n    #conv_reparameterization_layer(16, 3, 'swish'),\n    #tf.keras.layers.Conv2D(16, 3, strides=(1, 1)), # 'ReLu'),\n    tf.keras.layers.Conv2D(16, 3, strides=(1, 1), activation='sigmoid',kernel_initializer=initializers.glorot_uniform(seed=0)),\n    tf.keras.layers.MaxPooling2D(2),\n    \n    #conv_reparameterization_layer(32, 3, 'swish'),\n    #tf.keras.layers.Conv2D(32, 3, strides=(1, 1)), #, 'ReLu'),\n    tf.keras.layers.Conv2D(16, 3, strides=(1, 1), activation='sigmoid',kernel_initializer=initializers.glorot_uniform(seed=0)),\n    tf.keras.layers.MaxPooling2D(2),\n\n    #conv_reparameterization_layer(64, 3, 'swish'),\n    #tf.keras.layers.Conv2D(64, 3, strides=(1, 1)), #, 'ReLu'),\n    tf.keras.layers.Conv2D(16, 3, strides=(1, 1), activation='sigmoid',kernel_initializer=initializers.glorot_uniform(seed=0)),\n    tf.keras.layers.MaxPooling2D(2),\n\n    #conv_reparameterization_layer(128, 3), #, 'ReLu',\n    #tf.keras.layers.Conv2D(64, 3, strides=(1, 1)), #, 'ReLu'),\n    tf.keras.layers.Conv2D(16, 3, strides=(1, 1), activation='sigmoid',kernel_initializer=initializers.glorot_uniform(seed=0)),\n\n    tf.keras.layers.GlobalMaxPooling2D(),\n    \n    tfpl.DenseReparameterization(\n        units = tfpl.OneHotCategorical.params_size(2), activation = None,\n        kernel_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n        kernel_prior_fn = tfpl.default_multivariate_normal_fn,\n        \n        bias_prior_fn = tfpl.default_multivariate_normal_fn,\n        bias_posterior_fn = tfpl.default_mean_field_normal_fn(is_singular=False),\n        \n        kernel_divergence_fn = divergence_fn,\n        bias_divergence_fn = divergence_fn),\n    tfpl.OneHotCategorical(10)\n])\n\ndef nll(y_true, y_pred):\n    return -y_pred.log_prob(y_true)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bayesian_cnn.compile(loss=nll,\n              #optimizer=RMSprop(0.001),\n            optimizer='Adam',\n            metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#bayesian_cnn.fit(train_ds, epochs = 12, validation_data = test_ds)\nbayesian_cnn.fit(train_gen, epochs=12, validation_data = val_gen)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bayesian_cnn.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example_images = []\n# example_labels = []\n\n# for _, (x, y) in enumerate(train_gen):\n# #for _, (x, y) in iter(train_gen):\n# #for (x, y) in iteritems(train_gen)\n#     example_images.append(x)\n#     example_labels.append(y)\n \n#     print(example_images, example_labels)\n#how do i only take every 100th image fram the train_generator","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def analyse_model_prediction(image, label = None, forward_passes = 10):\n    if label is not None:\n        label = np.argmax(label, axis = -1)\n    \n    extracted_probabilities = np.empty(shape=(forward_passes, 10))\n    extracted_std = np.empty(shape=(forward_passes, 10))\n\n    for i in range(forward_passes):\n        model_output_distribution = bayesian_cnn((image.reshape(-1, 96, 96))) \n                                                  #tf.expand_dims(image\n                                                    #axis = 0)) #maybe try tf.newaxis\n                                                    #np.reshape(x, (-1, image, image, 3)) \n\n        extracted_probabilities[i] = model_output_distribution.mean().numpy().flatten()\n        extracted_std[i] = model_output_distribution.stddev().numpy().flatten()\n\n    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(16, 6),\n                                   gridspec_kw={'width_ratios': [2, 4]})\n    plt.xticks(fontsize = 16, rotation = 45)\n    plt.yticks(fontsize = 16)\n\n    # Show the image and the true label if provided.\n    ax1.imshow(image.squeeze(), cmap='gray')\n    ax1.axis('off')\n    if label is not None:\n        ax1.set_title('True Label: {}'.format(str(label)), fontsize = 20)\n    else:\n        ax1.set_title('True Label Not Given', fontsize = 20)\n    \n    # Obtain the 95% prediction interval.\n    # extracted_probabilities.shape = (forward_passes, 10)\n    # So if we sample from the model 100 times, there will be 100 different\n    # values for each of the 10 classes. \n    # We get the interval for each of the classes independently.\n    pct_2p5 = np.array([np.percentile(extracted_probabilities[:, i], \n                                      2.5) for i in range(10)])\n    pct_97p5 = np.array([np.percentile(extracted_probabilities[:, i], \n                                       97.5) for i in range(10)]) \n\n    # Std also contains 100 different values. We take median across the column\n    # to obtain a single value for each of the class label.\n    extracted_std = np.median(extracted_std, axis = 0)\n    highest_var_label = np.argmax(extracted_std, axis = -1)\n    if label is not None:\n        print('Label %d has the highest std in this'\n        ' prediction with the value %.3f' %(highest_var_label,\n                                            extracted_std[highest_var_label]))\n    else:\n        print('Std Array:', extracted_std)     \n    \n    bar = ax2.bar(np.arange(10), pct_97p5, color='red')\n    if label is not None:\n        bar[int(label)].set_color('green')\n    \n    ax2.bar(np.arange(10), pct_2p5-0.02, color='white', \n            linewidth=4, edgecolor='white')\n    ax2.set_xticks(np.arange(10))\n    \n    ax2.set_ylim([0, 1])\n    ax2.set_ylabel('Probability', fontsize = 18)\n    ax2.set_title(\"Model's Probabilities\", fontsize = 20)\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"analyse_model_prediction(example_images[0], example_labels[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_vector = np.random.uniform(size = (28, 28, 1), low = 0, high = 0.5)\nnoisy_image = np.clip(example_images[0] + noise_vector, 0, 1)\nanalyse_model_prediction(noisy_image, example_labels[4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"analyse_model_prediction(np.random.uniform(size = (96, 96, 3), low = 0, \n                                           high = 1))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Works Cited**","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/mbmlearner/baseline-keras-cnn-roc-fast-10min-0-925-lb/edit\nhttps://github.com/Frightera/Medium_Notebooks_English/tree/main\nhttps://www.kaggle.com/code/akarshu121/cancer-detection-with-cnn-for-beginners#b)","metadata":{}},{"cell_type":"markdown","source":"**Notes**","metadata":{}},{"cell_type":"markdown","source":"I was thinking the keras subsuming tensorflow resulted in a module mess.  I, literally yesterday, tried a C-VAE (a VERY similar model) in Pytorch, which felt much easier, today. Tensorflow seems easier.  I do not know if PyTorch has completely supplanted Keras/Tensorflow though I have it on good authority that it is much faster.  Is this error: \"Freezing at RunTime\"?\n","metadata":{}},{"cell_type":"markdown","source":"It seems to me that Python is at such a high level that neural neworks are written from considerable tinkering.  ","metadata":{}},{"cell_type":"markdown","source":"I should definitely try again with a LeakyReLu...no?","metadata":{}},{"cell_type":"markdown","source":"I think maybe my loss function should be... binary cross entropy...? no?","metadata":{}},{"cell_type":"markdown","source":"If the CPU is in red, then is it possible to switch to say GPUs?  If the CPU is in red, then is that what makes cells lock-up and not finish?\n","metadata":{}},{"cell_type":"markdown","source":"My model seems to consistently get stuck on certain cells.  It seems like it would be a memory-sucker... ...\n","metadata":{}},{"cell_type":"markdown","source":"ValueError: Only instances of `keras.Layer` is no longer solved by !pip install tf_keras","metadata":{}},{"cell_type":"code","source":"#Should I use a Softmax Activation function?","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Can I try adding Bayes hyperparameter optimization before hand and a AIC and BIC measure afterwards.  ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}