python.dgbpy.keras_classes

Module Contents

Classes

TrainingSequence

DataPredType

Generic enumeration.

OutputType

Generic enumeration.

DimType

Generic enumeration.

UserModel

Abstract base class for user defined Keras machine learning models

Attributes

mlmodels

class python.dgbpy.keras_classes.TrainingSequence(trainbatch, forvalidation, model, exfilenm=None, batch_size=1, with_augmentation=True, tempnm=None)

Bases: tensorflow.keras.utils.Sequence

__len__(self)
set_chunk(self, ichunk)
on_epoch_end(self)
__getitem__(self, index)
__data_generation(self, data_IDs_temp)
class python.dgbpy.keras_classes.DataPredType

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

Continuous = Continuous Data
Classification = Classification Data
Segmentation = Segmentation
Any = Any
class python.dgbpy.keras_classes.OutputType

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

Pixel = 1
Image = 2
Any = 3
class python.dgbpy.keras_classes.DimType

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

D1 = 1
D2 = 2
D3 = 3
Any = 4
class python.dgbpy.keras_classes.UserModel

Bases: abc.ABC

Abstract base class for user defined Keras machine learning models

This module provides support for users to add their own machine learning models to OpendTect.

It defines an abstract base class. Users derive there own model classes from this base class and implement the _make_model static method to define the structure of the keras model. The users model definition should be saved in a file name with “mlmodel_keras” as a prefix and be at the top level of the module search path so it can be discovered.

The “mlmodel_keras” class should also define some class variables describing the class: uiname : str - this is the name that will appear in the user interface uidescription : str - this is a short description which may be displayed to help the user predtype : DataPredType enum - type of prediction (must be member of DataPredType enum) outtype: OutputType enum - output shape type (OutputType.Pixel or OutputType.Image) dimtype : DimType enum - the input dimensions supported by model (must be member of DimType enum)

from dgbpy.keras_classes import UserModel, DataPredType, OutputType, DimType

class myModel(UserModel):

uiname = ‘mymodel’ uidescription = ‘short description of model’ predtype = DataPredType.Classification outtype = OutputType.Pixel dimtype = DimType.D3

def _make_model(self, input_shape, nroutputs, learnrate, data_format):

inputs = Input(input_shape) conv1 = Conv3D(2, (3,3,3), activation=’relu’, padding=’same’)(inputs) conv1 = Conv3D(2, (3,3,3), activation=’relu’, padding=’same’)(conv1) pool1 = MaxPooling3D(pool,size=(2,2,2))(conv1) … conv8 = Conv3D(1, (1,1,1,), activation=’sigmoid’)(conv7)

model = Model(inputs=[inputs], outputs=[conv8]) model.compile(optimizer = Adam(lr = 1e-4), loss = cross_entropy_balanced, metrics = [‘accuracy’]) return model

mlmodels = []
static findModels()

Static method that searches the PYTHONPATH for modules containing user defined Keras machine learning models (UserModels).

The module name must be prefixed by “mlmodel_keras”. All subclasses of the UserModel base class is each found module will be added to the mlmodels class variable.

static findName(modname)

Static method that searches the found UserModel’s for a match with the uiname class variable

modname : str Name (i.e. uiname) of the UserModel to search for.

an instance of the class with the first matching name in the mlmodels list or None if no match is found

static getModelsByType(pred_type, out_type, dim_type)

Static method that returns a list of the UserModels filtered by the given prediction, output and dimension types

pred_type: DataPredType enum The prediction type of the model to filter by out_type: OutputType enum The output shape type of the model to filter by dim_type: DimType enum The dimensions that the model must support

a list of matching model or None if no match found

static getNamesByType(pred_type, out_type, dim_type)
static isPredType(modelnm, pred_type)
static isOutType(modelnm, out_type)
static isClassifier(modelnm)
static isRegressor(modelnm)
static isImg2Img(modelnm)
abstract _make_model(self, input_shape, nroutputs, learnrate, data_format)

Abstract static method that defines a machine learning model.

Must be implemented in the user’s derived class

input_shape : tuple Defines input data shape in the Keras default data_format for the current backend. For the TensorFlow backend the default data_format is ‘channels_last’ nroutputs : int (number of discrete classes for a classification) Number of outputs learnrate : float The step size applied at each iteration to move toward a minimum of the loss function

a compiled keras model

model(self, input_shape, nroutputs, learnrate, data_format='channels_first')

Creates/returns a compiled keras model instance

input_shape : tuple Defines input data shape arranged as per the data_format setting. nroutputs : int (number of discrete classes for a classification) Number of outputs learnrate : float The step size applied at each iteration to move toward a minimum of the loss function data_format: str The data format used. The machine learning plugin uses ‘channels_first’ data_format.

a compiled keras model

python.dgbpy.keras_classes.mlmodels