Example #1
0
def get_vgg_net(num_conv_layers_per_block=4, cnn_layer_labels=None):
    """ Returns a VGG net. """
    cnn_layer_labels = cnn_layer_labels if cnn_layer_labels is not None else \
                       get_cnn_layer_labels()
    layer_labels = [
        'ip', 'conv3', 'conv3', 'max-pool', 'conv3', 'conv3', 'max-pool'
    ]
    num_filters_each_layer = [None, 64, 64, None, 128, 128, None]
    # Now create the blocks
    block_filter_sizes = [128, 256, 512]
    for bfs in block_filter_sizes:
        layer_labels.extend(
            ['conv3' for _ in range(num_conv_layers_per_block)] + ['max-pool'])
        num_filters_each_layer.extend([bfs] * num_conv_layers_per_block +
                                      [None])
    layer_labels.extend(['fc', 'fc', 'fc', 'softmax', 'op'])
    num_filters_each_layer.extend([128, 256, 512, None, None])
    num_layers = len(layer_labels)
    # Construct the connectivity matrix
    conn_mat = get_feedforward_adj_mat(num_layers)
    strides = [(1 if is_a_conv_layer_label(ll) else None)
               for ll in layer_labels]
    vgg = ConvNeuralNetwork(layer_labels, conn_mat, num_filters_each_layer,
                            strides, cnn_layer_labels)
    return vgg
Example #2
0
def get_vgg_net(num_conv_layers_per_block=2, cnn_layer_labels=None):
    """ Returns a VGG net. """
    cnn_layer_labels = cnn_layer_labels if cnn_layer_labels is not None else \
                       get_cnn_layer_labels()
    #print("cnn_layer_labels:",cnn_layer_labels)
    layer_labels = [
        'ip', 'conv3', 'conv3', 'avg-pool', 'conv3', 'conv3', 'avg-pool'
    ]
    num_filters_each_layer = [None, 64, 64, None, 128, 128, None]
    # Now create the blocks
    block_filter_sizes = [256, 512]
    for bfs in block_filter_sizes:
        layer_labels.extend(
            ['conv3' for _ in range(num_conv_layers_per_block)] + ['avg-pool'])
        num_filters_each_layer.extend([bfs] * num_conv_layers_per_block +
                                      [None])
    layer_labels.extend(['fc', 'softmax', 'op'])
    num_filters_each_layer.extend([512, None, None])
    #print("layer_labels",layer_labels)
    #print("number_of_lyers",len(layer_labels))
    #print("num_filters_each_layer",num_filters_each_layer)
    num_layers = len(layer_labels)
    # Construct the connectivity matrix
    conn_mat = get_feedforward_adj_mat(num_layers)
    strides = [(1 if is_a_conv_layer_label(ll) else None)
               for ll in layer_labels]
    vgg = ConvNeuralNetwork(layer_labels, conn_mat, num_filters_each_layer,
                            strides, cnn_layer_labels)
    #print("strides:",strides)
    #print("layer_parents:",vgg.conn_mat.viewkeys())
    return vgg
Example #3
0
def get_multidepth_cnn_eg1():
    """ A network with 2 softmax layers mostly for debugging. """
    layer_labels, edges, num_filters_each_layer, cnn_layer_labels, strides = \
      _get_multidepth_cnn_eg12_common()
    edges.append((3, 4))
    conn_mat = get_dok_mat_with_set_coords(len(layer_labels), edges)
    strides[9] = 2
    return ConvNeuralNetwork(layer_labels, conn_mat, num_filters_each_layer,
                             strides, cnn_layer_labels)
Example #4
0
def get_resnet_cnn(num_res_blocks,
                   num_conv_layers_per_block,
                   num_fc_layers,
                   num_conv_filters_in_layers=None,
                   num_fc_nodes_in_layers=None,
                   cnn_layer_labels=None):
    """ Returns a Resnet CNN.
      num_layers_to_skip: The number of layers to skip when adding skip connections.
      see get_blocked_cnn for other arguments.
  """
    layer_labels, conn_mat, num_filters_each_layer, cnn_layer_labels, strides = \
      _get_blocked_cnn_params(num_res_blocks, num_conv_layers_per_block, 'res3',
        num_fc_layers, num_conv_filters_in_layers, num_fc_nodes_in_layers, cnn_layer_labels)
    return ConvNeuralNetwork(layer_labels, conn_mat, num_filters_each_layer,
                             strides, cnn_layer_labels)
Example #5
0
def get_blocked_cnn(num_blocks,
                    num_conv_layers_per_block,
                    num_fc_layers,
                    num_conv_filters_in_layers=None,
                    num_fc_nodes_in_layers=None,
                    cnn_layer_labels=None):
    """ Returns a blocked CNN.
    num_blocks: # blocks, i.e. the number of repeated convolutional layers.
    num_conv_layers_per_block: # convolutaional layers per block.
    num_fc_layers: # fully connected layers.
    num_conv_filters_in_layers: # filters in the layers for each block.
    num_fc_nodes_in_layers: sizes of the fc layers.
    cnn_layer_labels: Labels for all layer types in a CNN.
  """
    layer_labels, conn_mat, num_filters_each_layer, cnn_layer_labels, strides = \
      _get_blocked_cnn_params(num_blocks, num_conv_layers_per_block, 'conv3', num_fc_layers,
        num_conv_filters_in_layers, num_fc_nodes_in_layers, cnn_layer_labels)
    return ConvNeuralNetwork(layer_labels, conn_mat, num_filters_each_layer,
                             strides, cnn_layer_labels)
Example #6
0
def get_new_nn(old_nn, layer_labels, num_units_in_each_layer, conn_mat,
               mandatory_child_attributes):
  """ Returns a new neural network of the same type as old_nn. """
  known_nn_class = True
  try:
    if old_nn.nn_class == 'cnn':
      new_cnn = ConvNeuralNetwork(layer_labels, conn_mat, num_units_in_each_layer,
                                  mandatory_child_attributes.strides,
                                  old_nn.all_layer_label_classes,
                                  old_nn.layer_label_similarities)
      return new_cnn
    elif old_nn.nn_class.startswith('mlp'):
      return MultiLayerPerceptron(old_nn.nn_class[4:], layer_labels, conn_mat,
                                  num_units_in_each_layer, old_nn.all_layer_label_classes,
                                  old_nn.layer_label_similarities)
    else:
      known_nn_class = False
  except (CNNImageSizeMismatchException, CNNNoConvAfterIPException, AssertionError):
    return None
  if not known_nn_class:
    raise ValueError('Unidentified nn_class %s.'%(old_nn.nn_class))
Example #7
0
def get_vgg_net_chen(num_conv_layers_per_block=3, cnn_layer_labels=None):
    cnn_layer_labels = cnn_layer_labels if cnn_layer_labels is not None else \
                      get_cnn_layer_labels()
    layer_labels = [
        'ip', 'conv3', 'conv3', 'max-pool', 'conv3', 'conv3', 'max-pool'
    ]
    num_filters_each_layer = [None, 64, 64, None, 128, 128, None]

    #now create the blocks
    block_filter_sizes = [256, 512, 512]
    for bfs in block_filter_sizes:
        layer_labels.extend(
            ['conv3' for _ in range(num_conv_layers_per_block)] + ['max-pool'])
        num_filters_each_layer.extend([bfs] * num_conv_layers_per_block +
                                      [None])
    layer_labels.extend(['fc', 'fc', 'fc', 'softmax', 'op'])
    num_filters_each_layer.extend([4096, 4096, 1000, None, None])
    num_layers = len(layer_labels)
    conn_mat = get_feedforward_adj_mat(num_layers)
    strides = []
    for ll in layer_labels:
        if is_a_conv_layer_label(ll):
            strides.extend([1])
        elif is_a_pooling_layer_label(ll):
            strides.extend([2])
        else:
            strides.extend([None])
    #print("layer_labels:",layer_labels)
    #print("strides:",strides)
    #print("num_filters_each_layer:",num_filters_each_layer)

    vgg_16_chen = ConvNeuralNetwork(layer_labels,conn_mat,num_filters_each_layer,strides,\
                                    cnn_layer_labels)
    #print("layer_parents:",vgg_16_chen.conn_mat.viewkeys())

    return vgg_16_chen
Example #8
0
def generate_cnn_architectures():
    # pylint: disable=bad-whitespace
    """ Generates 4 neural networks. """
    all_layer_label_classes = get_cnn_layer_labels()
    # Network 1
    layer_labels = [
        'ip', 'op', 'conv3', 'fc', 'conv3', 'conv3', 'conv3', 'softmax',
        'max-pool'
    ]
    num_filters_each_layer = [None, None, 32, 16, 16, 8, 8, None, None]
    A = get_dok_mat_with_set_coords(9, [(0, 4), (3, 7), (4, 5), (4, 6), (5, 2),
                                        (6, 2), (7, 1), (2, 8), (8, 3)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_1 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 2
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv3', 'fc', 'softmax', 'op', 'max-pool'
    ]
    num_filters_each_layer = [None, 16, 16, 32, 16, None, None, None]
    A = get_dok_mat_with_set_coords(8, [(0, 1), (1, 2), (2, 3), (4, 5), (5, 6),
                                        (3, 7), (7, 4)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_2 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 3
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv5', 'conv3', 'max-pool', 'fc', 'softmax',
        'op'
    ]
    num_filters_each_layer = [None, 16, 16, 16, 32, None, 32, None, None]
    A = get_dok_mat_with_set_coords(9, [(0, 1), (1, 2), (1, 3), (1, 4), (2, 5),
                                        (3, 5), (4, 6), (5, 6), (6, 7),
                                        (7, 8)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    strides[4] = 2
    cnn_3 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 4
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv5', 'conv3', 'avg-pool', 'conv5', 'fc',
        'softmax', 'op'
    ]
    num_filters_each_layer = [None, 16, 16, 16, 32, None, 32, 32, None, None]
    A = get_dok_mat_with_set_coords(10,
                                    [(0, 1), (1, 2), (1, 3), (1, 4), (2, 5),
                                     (3, 5), (4, 6), (5, 7), (6, 7), (7, 8),
                                     (8, 9)])
    strides = [
        None
        if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool', 'avg-pool'] else 1
        for ll in layer_labels
    ]
    strides[4] = 2
    cnn_4 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 5
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv5', 'conv5', 'avg-pool', 'fc', 'softmax',
        'op', 'conv3'
    ]
    num_filters_each_layer = [None, 16, 16, 16, 32, None, 32, None, None, 16]
    A = get_dok_mat_with_set_coords(10,
                                    [(0, 1), (1, 2), (1, 3), (2, 5), (3, 5),
                                     (4, 6), (5, 6), (6, 7), (7, 8), (0, 9),
                                     (9, 4)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'avg-pool'] else 1
        for ll in layer_labels
    ]
    strides[4] = 2
    cnn_5 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 6
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv3', 'fc', 'fc', 'op', 'max-pool', 'fc',
        'softmax'
    ]
    num_filters_each_layer = [None, 16, 16, 32, 32, 32, None, None, 32, None]
    A = get_dok_mat_with_set_coords(10,
                                    [(0, 1), (1, 2), (2, 3), (4, 5), (5, 9),
                                     (3, 7), (7, 4), (4, 8), (8, 9), (9, 6)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_6 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 7 - Two softmax layers both pointing to output
    layer_labels = [
        'ip', 'conv3', 'conv3', 'conv3', 'fc', 'fc', 'op', 'max-pool', 'fc',
        'softmax', 'softmax'
    ]
    num_filters_each_layer = [
        None, 16, 16, 32, 32, 32, None, None, 32, None, None
    ]
    A = get_dok_mat_with_set_coords(11,
                                    [(0, 1), (1, 2), (2, 3), (4, 5), (5, 9),
                                     (3, 7), (7, 4), (4, 8), (8, 9), (9, 6),
                                     (8, 10), (10, 6)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_7 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 8 - Similar to previous, except with residual layers
    layer_labels = [
        'ip', 'conv3', 'res3', 'res5', 'fc', 'fc', 'op', 'max-pool', 'fc',
        'softmax', 'softmax'
    ]
    num_filters_each_layer = [
        None, 16, 16, 32, 32, 32, None, None, 32, None, None
    ]
    A = get_dok_mat_with_set_coords(11,
                                    [(0, 1), (1, 2), (2, 3), (4, 5), (5, 9),
                                     (3, 7), (7, 4), (4, 8), (8, 9), (9, 6),
                                     (8, 10), (10, 6)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_8 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)
    # Network 9 - Similar to previous, except decreasing units for residual layers
    layer_labels = [
        'ip', 'conv3', 'res3', 'res5', 'res7', 'fc', 'op', 'max-pool', 'fc',
        'softmax', 'softmax'
    ]
    num_filters_each_layer = [
        None, 16, 32, 8, 32, 128, None, None, 256, None, None
    ]
    A = get_dok_mat_with_set_coords(11,
                                    [(0, 1), (1, 2), (2, 3), (4, 5), (5, 9),
                                     (3, 7), (7, 4), (4, 8), (8, 9), (9, 6),
                                     (8, 10), (10, 6)])
    strides = [
        None if ll in ['ip', 'op', 'fc', 'softmax', 'max-pool'] else 1
        for ll in layer_labels
    ]
    cnn_9 = ConvNeuralNetwork(layer_labels, A, num_filters_each_layer, strides,
                              all_layer_label_classes)

    return [
        cnn_1,
        cnn_2,
        cnn_3,
        cnn_4,
        cnn_5,
        cnn_6,
        cnn_7,
        cnn_8,
        cnn_9,
        get_vgg_net(2),
        get_blocked_cnn(3, 4, 1),
        get_resnet_cnn(3, 2, 1),
        get_multidepth_cnn_eg1(),
        get_multidepth_cnn_eg2(),
    ]