<jupyter_start><jupyter_text>Original CapsNet Model TrainIn this notebook we provide a simple interface to train the original CapsNet model described in "Dynamic routinig between capsules". The model is copycat of the original Sara's repository (https://github.com/Sarasra/models/tree/master/research/capsules). However, if you really reach 99.75, you've got to buy me a drink :)<jupyter_code>%load_ext autoreload
%autoreload 2
import tensorflow as tf
from utils import Dataset, plotImages, plotWrongImages
from models import CapsNet
import matplotlib.pyplot as plt
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
# some parameters
model_name = 'MNIST' #- MNIST (AD dataset with MHD files)
n_routing = 3<jupyter_output><empty_output><jupyter_text>1.0 Import the Dataset<jupyter_code>dataset = Dataset(model_name, config_path='config.json') # MHD<jupyter_output>(378, 128, 128)
(378,)
(50, 128, 128)
(50,)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.
 1. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 1. 0. 0. 1. 0. 1. 1. 0. 0. 0. 1. 0.
 0. 1.]
[INFO] Dataset loaded!<jupyter_text>1.1 Visualize imported dataset<jupyter_code>n_images = 20 # number of images to be plotted
plotImages(dataset.X_test[:n_images,...,0], dataset.y_test[:n_images], n_images, dataset.class_names)<jupyter_output><empty_output><jupyter_text>2.0 Load the Model<jupyter_code>dataset_train, dataset_val = dataset.get_tf_data()
model_train = CapsNet(model_name, mode='train', verbose=True, n_routing=n_routing)<jupyter_output>WARNING:tensorflow:From C:\Users\1997w_l8qqcuy\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\array_ops.py:5049: calling gather (from tensorflow.python.ops.array_ops) with validate_indices is deprecated and will be removed in a future version.
Instructions for updating:
The `validate_indices` argument has no effect. Indices are always validated on CPU and never validated on GPU.
Model: "Original_CapsNet"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            [(None, 128, 128, 1) 0                                            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 120, 120, 256 20992       input_4[0][[...]<jupyter_text>3.0 Train the Model<jupyter_code>history = model_train.train(dataset, initial_epoch=0)
def plot_history(history):
    plt.plot(history.history['val_Original_CapsNet_accuracy'])
    plt.plot(history.history['Original_CapsNet_accuracy'])

    plt.legend(['val_Original_CapsNet_accuracy','Original_CapsNet_accuracy'])
    plt.grid(True)
    plt.show()


    plt.plot(history.history['val_Original_CapsNet_loss'])
    plt.plot(history.history['Original_CapsNet_loss'])

    plt.legend(['val_Original_CapsNet_loss','loss'])
    plt.grid(True)
    plt.show()
plot_history(history)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13799448022079117, 'fraction_numerical': 0.03986507206378412, 'mean_word_length': 4.742957746478873, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '411870', 'license': 'Apache-2.0', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1010, 'n_words': 255, 'path': 'original_capsnet_train.ipynb', 'repo_name': 'Kur1su0/Efficient-CapsNet', 'size': '3261'}