示例#1
0
def get_cnn_layer_label_mismatch_penalties(non_assignment_penalty, max_conv_size=7,
                                           conv_scale=None):
  """ Gets the label mismatch matrix for a CNN. """
  conv_scale = np.sqrt(2)/10.0 if conv_scale is None else conv_scale
  cnn_layer_labels = neural_network.get_cnn_layer_labels(max_conv_size)
  num_labels = len(cnn_layer_labels)
  label_penalties = np.zeros((num_labels, num_labels))
  for i in range(num_labels):
    for j in range(i, num_labels):
      labi = cnn_layer_labels[i]
      labj = cnn_layer_labels[j]
      if labi == labj:
        cost = 0.0
      elif (labi.startswith('conv') and labj.startswith('conv')) or \
           (labi.startswith('res') and labj.startswith('res')):
        cost = _get_conv_filter_size_cost(labi, labj, conv_scale)
      elif (labi.startswith('conv') and labj.startswith('res')) or \
           (labi.startswith('res') and labj.startswith('conv')):
        raw_cost = _get_conv_filter_size_cost(labi, labj, conv_scale)
        cost = raw_cost if raw_cost > non_assignment_penalty else \
               (CONV_RES_RAW_COST_FRAC * raw_cost +
                (1-CONV_RES_RAW_COST_FRAC) * non_assignment_penalty)
        # When mapping the a convolutional block to a residual block, set the cost
        # to be in-between the cost for a conv-conv layer and the non_assignment_penalty.
      elif labi.endswith('pool') and labj.endswith('pool'):
        cost = 0.5
      else:
        cost = np.inf
      label_penalties[i, j] = cost * non_assignment_penalty
      label_penalties[j, i] = cost * non_assignment_penalty
  return cnn_layer_labels, label_penalties
示例#2
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
示例#3
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
示例#4
0
 def test_get_labels(self):
     """ Tests labels generated for a CNN and MLP. """
     self.report('Testing the labels for CNN and MLP.')
     cnn_true_labels_7 = sorted([
         'ip', 'op', 'fc', 'max-pool', 'avg-pool', 'softmax', 'res3',
         'res5', 'res7', 'conv3', 'conv5', 'conv7'
     ])
     cnn_true_labels_3 = sorted([
         'ip', 'op', 'fc', 'max-pool', 'avg-pool', 'softmax', 'res3',
         'conv3'
     ])
     mlp_true_labels = sorted(['ip', 'op', 'relu', 'linear'])
     cnn_ret_labels_7 = sorted(neural_network.get_cnn_layer_labels(7))
     cnn_ret_labels_3 = sorted(neural_network.get_cnn_layer_labels(3))
     mlp_ret_labels = sorted(
         neural_network.get_mlp_layer_labels('reg', ['relu']))
     assert cnn_true_labels_7 == cnn_ret_labels_7
     assert cnn_true_labels_3 == cnn_ret_labels_3
     assert mlp_true_labels == mlp_ret_labels
示例#5
0
def _get_multidepth_cnn_eg12_common():
    """ A network with 2 softmax layers mostly for debugging common operations. """
    cnn_layer_labels = get_cnn_layer_labels()
    layer_labels = [
        'ip', 'op', 'softmax', 'fc', 'softmax', 'fc', 'conv5', 'avg-pool',
        'max-pool', 'conv3', 'conv3', 'max-pool', 'max-pool', 'conv3', 'conv7'
    ]
    num_filters_each_layer = [
        None, None, None, 64, None, 64, 128, None, None, 64, 64, None, None,
        128, 64
    ]
    edges = [(0, 14), (14, 6), (14, 9), (14, 10), (6, 7), (7, 3), (3, 2),
             (2, 1), (9, 8), (8, 5), (5, 4), (4, 1), (10, 11), (11, 13),
             (13, 12), (12, 5)]
    strides = [(1 if is_a_conv_layer_label(ll) else None)
               for ll in layer_labels]
    return layer_labels, edges, num_filters_each_layer, cnn_layer_labels, strides
示例#6
0
def _get_blocked_cnn_params(num_blocks,
                            num_layers_per_block,
                            block_layer_type,
                            num_fc_layers,
                            num_conv_filters_in_layers=None,
                            num_fc_nodes_in_layers=None,
                            cnn_layer_labels=None):
    """ Returns parameters for a blocked CNN.
    num_blocks: # blocks, i.e. the number of repeated convolutional layers.
    num_layers_per_block: # 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 = ['ip', 'conv7', 'max-pool']
    num_filters_each_layer = [None, 64, None]
    strides = [None, 1, None]
    num_conv_filters_in_layers = num_conv_filters_in_layers if num_conv_filters_in_layers \
      is not None else _dflt_unit_sizes(num_blocks)
    num_fc_nodes_in_layers = num_fc_nodes_in_layers if num_fc_nodes_in_layers is not None \
      else [2 * num_conv_filters_in_layers[-1]] * num_fc_layers
    cnn_layer_labels = cnn_layer_labels if cnn_layer_labels is not None else \
                       get_cnn_layer_labels()
    # Construct blocks
    for block_idx in range(num_blocks):
        layer_labels.extend(
            [block_layer_type for _ in range(num_layers_per_block)])
        num_filters_each_layer.extend([num_conv_filters_in_layers[block_idx]] *
                                      num_layers_per_block)
        strides.extend([2] + [1] * (num_layers_per_block - 1))
    # Pooling layer after the blocks
    layer_labels.append('avg-pool')
    num_filters_each_layer.append(None)
    strides.append(None)
    # Add FC layers
    layer_labels.extend(['fc'
                         for _ in range(num_fc_layers)] + ['softmax', 'op'])
    num_filters_each_layer.extend(num_fc_nodes_in_layers + [None, None])
    strides.extend([None] * (num_fc_layers + 2))
    # Construct the connectivity matrix
    num_layers = len(layer_labels)
    conn_mat = get_feedforward_adj_mat(num_layers)
    return layer_labels, conn_mat, num_filters_each_layer, cnn_layer_labels, strides
示例#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
示例#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(),
    ]