Ejemplo n.º 1
0
def inference2(input_tensor,
               n_classes,
               train=True,
               regularizer=None,
               evaluate=False):
    input_tensor = layers.full_connect('fc1',
                                       input_tensor,
                                       out_nodes=512,
                                       regularizer=regularizer)
    #    input_tensor = layers.full_connect('fc2', input_tensor, out_nodes=256, regularizer=regularizer)
    #    input_tensor = layers.full_connect('fc3', input_tensor, out_nodes=128, regularizer=regularizer)
    output_tensor = layers.full_connect_not_relu('fc4',
                                                 input_tensor,
                                                 out_nodes=n_classes,
                                                 regularizer=regularizer)
    return output_tensor
Ejemplo n.º 2
0
def inference(input_tensor,
              n_classes,
              train=True,
              regularizer=None,
              evaluate=False):
    with tf.name_scope('cnn'):
        input_tensor = layers.conv('conv1',
                                   input_tensor,
                                   out_channels=32,
                                   kernel_size=[2, 2],
                                   strides=[1, 2, 2, 1],
                                   train=train)
        input_tensor = layers.pool('pool1',
                                   input_tensor,
                                   kernel_size=[1, 2, 2, 1],
                                   strides=[1, 2, 2, 1],
                                   by_max=True)

        input_tensor = layers.conv('conv2',
                                   input_tensor,
                                   out_channels=64,
                                   kernel_size=[2, 2],
                                   strides=[1, 1, 1, 1],
                                   train=train)
        input_tensor = layers.batch_norm(input_tensor)
        input_tensor = layers.pool('pool2',
                                   input_tensor,
                                   kernel_size=[1, 2, 2, 1],
                                   strides=[1, 1, 1, 1],
                                   by_max=True)

        input_tensor = layers.full_connect('fc1',
                                           input_tensor,
                                           out_nodes=512,
                                           regularizer=regularizer)
        output_tensor = layers.full_connect_not_relu('fc2',
                                                     input_tensor,
                                                     out_nodes=n_classes,
                                                     regularizer=regularizer)

        return output_tensor
Ejemplo n.º 3
0
    def create_architecture(self, mode, hdf5_data):
        """Returns the architecture (i.e., caffe prototxt) of the model.

        Jer: One day this should probably be written to be more general.
        """

        arch = self.arch
        pars = self.pars
        n = caffe.NetSpec()

        if mode == 'deploy':
            n.data = L.DummyData(shape=[dict(dim=pars['deploy_dims'])])
        elif mode == 'train':
            n.data, n.label = L.HDF5Data(batch_size=pars['train_batch_size'],
                                         source=hdf5_data,
                                         ntop=pars['ntop'])
        else:  # Test.
            n.data, n.label = L.HDF5Data(batch_size=pars['test_batch_size'],
                                         source=hdf5_data,
                                         ntop=pars['ntop'])

        # print(n.to_proto())
        in_layer = n.data1

        for layer in arch:
            layer_type, vals = layer

            if layer_type == 'e2e':
                in_layer = n.e2e = e2e_conv(in_layer, vals['n_filters'],
                                            vals['kernel_h'], vals['kernel_w'])
            elif layer_type == 'e2n':
                in_layer = n.e2n = e2n_conv(in_layer, vals['n_filters'],
                                            vals['kernel_h'], vals['kernel_w'])
            elif layer_type == 'fc':
                in_layer = n.fc = full_connect(in_layer, vals['n_filters'])
            elif layer_type == 'out':
                n.out = full_connect(in_layer, vals['n_filters'])
                # Rename to user specified unique layer name.
                # n.__setattr__('out', n.new_layer)

            elif layer_type == 'dropout':
                in_layer = n.dropout = L.Dropout(
                    in_layer,
                    in_place=True,
                    dropout_param=dict(dropout_ratio=vals['dropout_ratio']))
            elif layer_type == 'relu':
                in_layer = n.relu = L.ReLU(
                    in_layer,
                    in_place=True,
                    relu_param=dict(negative_slope=vals['negative_slope']))
            else:
                raise ValueError('Unknown layer type: ' + str(layer_type))

        # ~ end for.

        if mode != 'deploy':
            if self.pars['loss'] == 'EuclideanLoss':
                n.loss = L.EuclideanLoss(n.out, n.label)
            else:
                ValueError(
                    "Only 'EuclideanLoss' currently implemented for pars['loss']!"
                )
        return n