Package: eznet-keras
ezNet-KerasKeras (TensorFlow) implementation ofezNet("easy net"), a package containing "easy" implementation of a collection of basic and widely-used deep learning models.This implementation is for Keras (Tensorflow). SeeHerefor an identical PyTorch implementation.Author: Pouya P. Niaz (pniaz20@ku.edu.tr,pouya.p.niaz@gmail.com)Version: 1.1.2Last Update: January 02, 2024Install with:pipinstalleznet-keras1- IntroYou can build, train and evaluate all manner of Keras models using the utilities in this package.
Furthermore, there is a collection of basic and widely-used deep learning models ready to be used immediately.
This package also offersKerasSmartModel, a sublass oftf.keras.Modelthat has built-in functions for manipulating its hyperparameters,
training, evaluation, and testing.Note that for all the functions and classes described briefly here, the docstrings provide much more detailed information.1-1- Implementation notesUnlike, e.g., TensorFlow, you may not be able to just import everything together as inimport eznet_kerasand then use dot indexing to access everything underneath
(I would love to do that, I haven't yet quite figured out how to. I am a newbie.)Instead, import the specific module, class or function, e.g.,fromeznet_keras.modelsimportANN# orfromeznet_keras.utilsimportcalc_image_size2- Applications2-1- Smart Model for Convenient DL Training and DeploymentTheKerasSmartModelclass enables you to write any kind of TensorFlow model, and it has built-in functions for training, testing, evaluaiton, plotting training history, exporting to C++ using Keras2Cpp, and so forth.fromeznet_keras.modelsimportKerasSmartModelclassMyModel(KerasSmartModel):def__init__(self,hparams:dict=None):super(MyModel,self).__init__(hparams)## Some code defining final_model#self.net=final_modeldefcall(self,x,*args,**kwargs):returnself.net(x,*args,**kwargs)sample_hparams={'model_name':'KerasSmartModel',# Name of the model'l2_reg':0.0001,# L2 regularization parameter'batch_size':16,# Mini-batch size'epochs':40,# Maximum training epochs'validation_data':[0.05,'trainset'],# Portion of train set used for validation (if necessary)'early_stopping_patience_epochs':10,# Validation patience for early stopping'learning_rate':0.0001,# (Initial) Learning rate'exponential decay rate':0.99,# Learning rate exponential decay factor'loss_function':'categorical_crossentropy',# Loss function string'metrics':['accuracy'],# List of metrics'optimizer':'Adam',# Optimizer string'optimizer_params':None,# Dictionary of parameters to pass to the optimizer constructor'checkpoint_path':None,# Path to store checkpoints in every epoch'early_stopping_monitor':'loss',# Early stopping criteria (stop when alue is reached and performance is good enough)'early_stopping_mode':'min',# Early stopping variable mode (minimization or maximization)'early_stopping_value':1.0e-6# Threshold for early stopping}model=MyModel(sample_hparams)x_train=...y_train=...x_test=...y_test=...model.train_model(x_train,y_train,x_test,y_test,verbose=1,saveto="my_model",export="my_model.model")model.plot_history(metrics=['loss','val_loss'],fig_title='model loss',saveto="training_history.png")A few implementation notes:Whatever hyperparameters you have for the model itself, can be included in the hparams dictionary.The model must always have aself.netatttribute which contains the network itself. It can be a Sequential or Functional model.The__init__method must always calculate the listsself.batch_input_shapeandbatch_output_shape.The__init__method of theKerasSmartModelclass will create attributes with the same name as the hyperparameters shown above.If possible, do not touch thecallmethod, leave it be.Tha__init__method of the class may alkter some of its attributes if the input hyperparameters don't exist or don't make sense.Theget_config()method returns the usual configuration, plus a key calledhparamsholding the (perhaps modified) input hyperparameters.Thefrom_config()method gets a config that must have thehparamskey, whose value is the same as thehparamsinput of the constructor.model.summary()will returnmodel.net.summary(). Other training and evaluation functions will also use theself.netattribute.Theself.historyattribute isNoneat the beginning, but is set to the return value of thefit()method.Thesave_toargument of themodel.train()function can be used to save the trained model as.h5file or as a folder in the TensorFlowSavedModelformat.Theexportargument in themodel.train()function (attempts to) use Gosha20777'sKeras2Cpppackage to export the model
to a.modelfile which can be imported in aC++ 2017compiler to implement the same model in C++.
Comes in handy if you work with robotic/mechatronic hardware that use limited versions and builds of C++ like I do :).2-2- Utility Functions for Manipulating Keras ModelsIneznet_keras.utils, there are some functions for manipulating Keras models, that come in handy if you frequently work with custom DL models in TensorFlow.make_path(path)creates a path for a folder or file, if some folders in the path don't exist.
Anywhere you want to save something, instead ofpath/to/foo.baryou can just usemake_path("path/to/foo.bar")so ifpathortodirectories
don't exist they will be created.plot_keras_model_history(history, **kwargs)function gets ahistoryobject returned by anytf.keras.Model.fit()function, and plots its training history.compile_keras_model(model, **kwargs)gets some strings as inputs like learning rate decay, batch size, etc., and compiles a keras model.fit_keras_model(model, **kwargs)runs thefit()function for a Keras model, using appropriate callbacks and early stopping criteria.save_keras_model(model, **kwargs)attempts to serialize and save a Keras model in either.h5format or a diretory with TensorFlow SavedModel format.export_keras_model(model, path)attempts to export a Keras model as a.modelfile using the Keras2Cpp package.autoname(name)gets a string as a name, and appends the current time stamp to it. Comes in handy when trying to time stamp the multiple training runs you'll do.calc_image_size(size_in, kernel_size, padding, stride, dilation)gets the input image dimension (1D, 2D or 3D), along with the
parameters of a convolution or pooling operation, and returns the output image dimensions. Comes in handy when you want to check to see if your
CNN layers are not shrinking the image too much.generate_geometric_array()gets an initial count and returns an array where the count doubles or halves along the array.
Comes in handy when you want to automatically assign number of filters/channels or hidden sizes in ANNs and CNNs.generate_array_for_hparam()gets the value of a hyperparameter specified by the user (e.g. ANN width), computes whether the hyperparameter needs to be an array
(e.g. the user input an integer but ANN width should be an array with length equal to ANN depth), and then returns an array that properly holds the hyperparameter values.generate_sample_batch(model)getsmodel.batch_input_shapeandmodel.batch_output_shapeand returns random input and output batches.2-3- Functions and Classes for Adding and Manipulating Modular Dense Blocks and Conv BlocksThis package also has classes and functions that can be used to create entire Dense blocks and Conv blocks.classeznet_keras.models.DenseBlock(tf.keras.layers.Layer)Gets some arguments to the constructor and returns a layer instance that holds
aDenselayer, followed optionally by anormlizationlayer, anactivationlayer, and adropoutlayer. The arguments to the constructor are
sufficient to build any kind of modularDenseBlocklayer, stacking it on top of other layers in your model.
I created this class becasue Dense blocks almost always have a widely-used format: dense, norm, activation, dropout.Functioneznet_keras.models.dense_block.add_dense_block()does the same, except rather than returning atf.keras.Layerinstance,
adds the layers to atf.keras.models.Sequential()model that is also the input of the function.classeznet_keras.models.ConvBlock(tf.keras.layers.Layer)Gets some arguments to the constructor and returns a layer instance that contains aconvolutionlayer, followed optionally
by anormalizationlayer, anactivationlayer, apoolinglayer, and adropoutlayer.
Again, the inputs are fully sufficient to make any kind ofConvBlockand stack it on top of other layers in your CNN.
Similar to the previous case, I created this class becasue it has easy-to-use modular capabilities to build widely-used CNN blocks
that have these kinds of layers in them.Functioneznet_keras.models.conv_block.add_conv_block()does the same, except it gets aSequential()model as input,
and adds the layers described above to it.2-4- Easy to Use Famous Deep Learning Models for ConvenienceThis package also holds some widely-used and basic deep learning models as in MLp, CNN, RNN, etc. that can get a dictionary of
well-defined hyperparameters, and return aKerasSmartModelinstance that can be easily trained, evaluated, stored and deployed.All of the following models reside in theeznet_keras.modelssubmodule.NOTETo see a list of all hyperparameters that each of the following classes use, simply invoke theclass.sample_hparamsclass attribute.
Also, you can simply call thehelp(class)function to read the docstrings.
ForANN, for instance,fromeznet_keras.modelsimportANNprint(ANN.sample_hparams)help(ANN)ANNis a multi-layer perceptron containing manyDenseBlockblocks, stacked together. For all hyperparameters such as width,
you can specify an integer to be used for all hidden layers (i.e., all blocks), or an array of different values for each hidden layer (i.e., block).
For every hyperparameter such as normalization layer type, useNonein its place in the array of hyperparameters to indicate that the corresponding
Dense block in that place does not have any normalization layers at all. The same goes for many other hyperparameters.Conv_Networkis a CNN where you not only choose the dimensionality (1D, 2D or 3D convolutions) but also all the other hyperparameters of all
Convolution blocks and Dense blocks residing in the network. This network is some Convulution blocks, followed by some Dense blocks.
You get to choose which Conv block has what kind of Conv, Norm (if any), Activation, Pooling (if any), or Dropout (if any) layer.
You also get to choose custom parametrers (**kwargs) for Conv, Pooling, Norm, Activation and Dense layer constructors, so that you can add additional parameters,
or overwrite the ones used by the class itself. You have full freedom.Recurrent_Networkis an RNN containing some RNN layers (SimpleRNN, GRU, LSTM, etc.) followed by some Dense blocks. Again, the whole thing is fully modular and you have full freedom.Keras_VarAEis a standard Variational Autoencoder. It is not aKerasSmartModelinstance (yet, keep tuned in), but it is a fully developed
autoencoder model which subclasses thetf.keras.Modelclass and also has KL-divergence loss integrated into it.More model varieties with modular and easy-to-use functionality will be added in time.3- LicenseThis package itself has MIT license, but Keras and TensorFlow have different licenses, which need to be accounted for when using this package.4- CreditsKerashttps://github.com/keras-team/kerashttps://keras.io/TensorFlowhttps://github.com/tensorflowhttps://www.tensorflow.org/Keras2CppGosha20777https://github.com/gosha20777/keras2cpp