<jupyter_start><jupyter_text>Efficient-CapsNet Model TrainIn this notebook we provide a simple interface to train Efficient-CapsNet on the three dataset discussed in "Efficient-CapsNet: Capsule Network with Self-Attention Routing":- MNIST (AD dataset with MHD files)The hyperparameters have been only slightly investigated. So, there's a lot of room for improvements. Good luck!**NB**: remember to modify the "config.json" file with the appropriate parameters.<jupyter_code>%load_ext autoreload
%autoreload 2
import tensorflow as tf
from utils import Dataset, plotImages, plotWrongImages, plotHistory
from models import EfficientCapsNet
import matplotlib.pyplot as plt
import numpy as np
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'
# model_name = 'MHD'<jupyter_output><empty_output><jupyter_text>1.0 Import the Dataset<jupyter_code>#load mhd data
dataset = Dataset(model_name, config_path='config.json')
dataset.X_test.shape<jupyter_output><empty_output><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>model_train = EfficientCapsNet(model_name, mode='train', verbose=True)<jupyter_output>[128, 128, 1]
Model: "Efficient_CapsNet"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_39 (InputLayer)        [(None, 128, 128, 1)]     0         
_________________________________________________________________
conv2d_28 (Conv2D)           (None, 124, 124, 32)      832       
_________________________________________________________________
batch_normalization_28 (Batc (None, 124, 124, 32)      128       
_________________________________________________________________
conv2d_29 (Conv2D)           (None, 122, 122, 64)      18496     
_________________________________________________________________
batch_normalization_29 (Batc (None, 122, 122, 64)      256       
_________________________________________________________________
conv2d_30 (Conv2D)           (None, 120, 120, 64)      36928     
___________________________________[...]<jupyter_text>3.0 Train the Model<jupyter_code>dataset_train, dataset_val = dataset.get_tf_data()
history = model_train.train(dataset, initial_epoch=0)
def plot_history(history):
    plt.plot(history.history['val_Efficient_CapsNet_accuracy'])
    plt.plot(history.history['Efficient_CapsNet_accuracy'])

    plt.legend(['val_Efficient_CapsNet_accuracy','Efficient_CapsNet_accuracy'])
    plt.grid(True)
    plt.show()


    plt.plot(history.history['val_Efficient_CapsNet_loss'])
    plt.plot(history.history['Efficient_CapsNet_loss'])

    plt.legend(['val_Efficient_CapsNet_loss','Efficient_CapsNet_loss'])
    plt.grid(True)
    plt.show()
plot_history(history)
history.history<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11010605115408609, 'fraction_numerical': 0.03337492202121023, 'mean_word_length': 6.110864745011087, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '641247', 'license': 'Apache-2.0', 'n_tokens_mistral': 1023, 'n_tokens_neox': 912, 'n_words': 198, 'path': 'efficient_capsnet_train.ipynb', 'repo_name': 'Kur1su0/Efficient-CapsNet', 'size': '3206'}