Module tripleblind.model_factory
The ModelFactory makes it easier to define common architectures than by using the NetworkBuilder and defining each layer explicitly. For example, a classic VGG network can be defined with:
builder = tb.ModelFactory.vgg(vgg_type="vgg11", num_classes=10)
Rather than:
builder = tb.NetworkBuilder()
builder.add_conv2d_layer(3, 64, kernel_size=3, padding=1)
builder.add_relu()
builder.add_max_pool2d_layer(kernel_size=2, stride=2)
builder.add_conv2d_layer(64, 128, kernel_size=3, padding=1)
builder.add_relu()
builder.add_max_pool2d_layer(kernel_size=2, stride=2)
builder.add_conv2d_layer(128, 256, kernel_size=3, padding=1)
builder.add_relu()
builder.add_conv2d_layer(256, 256, kernel_size=3, padding=1)
builder.add_relu()
builder.add_max_pool2d_layer(kernel_size=2, stride=2)
builder.add_conv2d_layer(256, 512, kernel_size=3, padding=1)
builder.add_relu()
builder.add_conv2d_layer(512, 512, kernel_size=3, padding=1)
builder.add_relu()
builder.add_max_pool2d_layer(kernel_size=2, stride=2)
builder.add_conv2d_layer(512, 512, kernel_size=3, padding=1)
builder.add_relu()
builder.add_conv2d_layer(512, 512, kernel_size=3, padding=1)
builder.add_relu()
builder.add_adaptive_avg_pool2d_layer((7, 7))
builder.add_flatten_layer(1)
builder.add_split()
builder.add_dense_layer(512 * 7 * 7, 4096)
builder.add_dropout(0.0)
builder.add_relu()
builder.add_dense_layer(4096, 4096)
builder.add_dropout(0.0)
builder.add_relu()
builder.add_dense_layer(4096, num_classes)
Both methods produce an identical architecture, but the latter is much more difficult to create and understand, and more error-prone.
Classes
class ModelFactory
-
Static methods
def vgg(vgg_type: str, num_classes: int, batch_norm: bool = False, dropout: float = 0.0, split_layer: bool = True) -> NetworkBuilder
-
Construct a VGG-style Convolutional Neural Network architecture
The Visual Geometry Group (VGG) created this architecture for image recognition purposes. It can bin images into the given number of object classes. There are four subtypes of VGG with slightly different internal architectures.
Args
vgg_type
:str
- Specific type of model to create, one of: "vgg11", "vgg13", "vgg16", "vgg19"
num_classes
:int
- The number of classes for the output layer
batch_norm
:bool
, optional- Whether to use batch normalization or not. Defaults to False
dropout
:float
, optional- The dropout rate to use. Defaults to 0.0
split_layer
:bool
, optional- When False, the builder is for training a Federated Learning model. Otherwise it defaults to a builder for Blind Learning.
Returns
NetworkBuilder
- A builder for the requested network architecture