def _right_model(img_input_dim, merged_dim):
    c, h, w = img_input_dim

    valid = lambda x, y, kernel, stride : ((x-kernel)/stride + 1, (y-kernel)/stride + 1)
    full = lambda x, y, kernel, stride : ((x+kernel)/stride - 1, (y+kernel)/stride - 1)

    right_model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    right_model.add(Convolution2D(input_channels=3, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    h, w = full(h, w, 3, 1)
    right_model.add(RELU())
    right_model.add(Convolution2D(input_channels=8, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='valid'))
    h, w = valid(h, w, 3, 1)
    right_model.add(RELU())
    right_model.add(Pooling2D(poolsize=(2, 2), stride=(1,1), mode='max'))
    h, w = valid(h, w, 2, 1)
    right_model.add(Dropout(0.25))

    right_model.add(Convolution2D(input_channels=8, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    h, w = full(h, w, 3, 1)
    right_model.add(RELU())
    right_model.add(Convolution2D(input_channels=8, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='valid'))
    h, w = valid(h, w, 3, 1)
    right_model.add(RELU())
    right_model.add(Pooling2D(poolsize=(2, 2), stride=(1,1), mode='max'))
    h, w = valid(h, w, 2, 1)
    right_model.add(Dropout(0.25))

    right_model.add(Flatten())
    right_model.add(Linear(8*h*w, 512))
    right_model.add(Linear(512, 512))
    right_model.add(RELU())
    right_model.add(Dropout(0.5))

    right_model.add(Linear(512, merged_dim))
    return right_model
Exemple #2
0
    def __init__(self, input_shape, output_dim):
        '''
        FIELDS:
            self.params: any params from the layer that needs to be updated
                         by backpropagation can be put inside self.params
        PARAMS:
            input_shape: tuple
                         shape of the input image with format (channel, height, width)
            output_dim: int
                        the output dimension of the model
        '''
        assert len(input_shape) == 3, 'input_shape must be a tuple or list of dim (channel, height, width)'
        c, h, w = input_shape

        valid = lambda x, y, kernel, stride : ((x-kernel)/stride + 1, (y-kernel)/stride + 1)
        full = lambda x, y, kernel, stride : ((x+kernel)/stride - 1, (y+kernel)/stride - 1)

        self.layers = []
        self.layers.append(Convolution2D(input_channels=3, filters=96, kernel_size=(11,11),
                                         stride=(4,4), border_mode='valid'))
        nh, nw = valid(h, w, 11, 4)
        self.layers.append(RELU())
        self.layers.append(LRN())
        self.layers.append(Pooling2D(poolsize=(3,3), stride=(2,2), mode='max'))
        nh, nw = valid(nh, nw, 3, 2)
        self.layers.append(Convolution2D(input_channels=96, filters=256, kernel_size=(5,5),
                                         stride=(1,1), border_mode='full'))
        nh, nw = full(nh, nw, 5, 1)
        self.layers.append(RELU())
        self.layers.append(LRN())
        self.layers.append(Pooling2D(poolsize=(3,3), stride=(2,2), mode='max'))
        nh, nw = valid(nh, nw, 3, 2)
        self.layers.append(Convolution2D(input_channels=256, filters=384, kernel_size=(3,3),
                                         stride=(1,1), border_mode='full'))
        nh, nw = full(nh, nw, 3, 1)
        self.layers.append(RELU())
        self.layers.append(Convolution2D(input_channels=384, filters=384, kernel_size=(3,3),
                                         stride=(1,1), border_mode='full'))
        nh, nw = full(nh, nw, 3, 1)
        self.layers.append(RELU())
        self.layers.append(Convolution2D(input_channels=384, filters=256, kernel_size=(3,3),
                                         stride=(1,1), border_mode='full'))
        nh, nw = full(nh, nw, 3, 1)
        self.layers.append(RELU())
        self.layers.append(Pooling2D(poolsize=(3,3), stride=(2,2), mode='max'))
        nh, nw = valid(nh, nw, 3, 2)

        self.layers.append(Flatten())
        self.layers.append(Linear(256*nh*nw,4096))
        self.layers.append(RELU())
        self.layers.append(Dropout(0.5))
        self.layers.append(Linear(4096,4096))
        self.layers.append(RELU())
        self.layers.append(Dropout(0.5))
        self.layers.append(Linear(4096,output_dim))
        self.layers.append(Softmax())

        self.params = []
        for layer in self.layers:
            self.params += layer.params
Exemple #3
0
def train():

    data = Cifar10(batch_size=32, train_valid_test_ratio=[4,1,1])

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Convolution2D(input_channels=3, filters=32, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=32, filters=32, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Convolution2D(input_channels=32, filters=64, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=64, filters=64, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(64*8*8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemple #4
0
def train():
    batch_size = 256
    short_memory = 0.9
    learning_rate = 0.005
    data = Cifar10(batch_size=batch_size, train_valid_test_ratio=[4, 1, 1])
    _, c, h, w = data.train.X.shape

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(
        Convolution2D(input_channels=c,
                      filters=8,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='full'))
    h, w = full(h, w, kernel=3, stride=1)
    model.add(
        BatchNormalization(dim=8, layer_type='conv',
                           short_memory=short_memory))
    model.add(RELU())
    model.add(
        Convolution2D(input_channels=8,
                      filters=16,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='valid'))
    h, w = valid(h, w, kernel=3, stride=1)
    model.add(
        BatchNormalization(dim=16,
                           layer_type='conv',
                           short_memory=short_memory))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4, 4), mode='max'))
    h, w = valid(h, w, kernel=4, stride=4)
    model.add(Flatten())
    model.add(Linear(16 * h * w, 512))
    model.add(
        BatchNormalization(dim=512, layer_type='fc',
                           short_memory=short_memory))
    model.add(RELU())

    model.add(Linear(512, 10))
    model.add(Softmax())

    # learning_method = RMSprop(learning_rate=learning_rate)
    learning_method = Adam(learning_rate=learning_rate)
    # learning_method = SGD(learning_rate=0.001)

    # Build Logger
    log = Log(
        experiment_name='cifar10_cnn_tutorial',
        description='This is a tutorial',
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'hyperparam.sqlite3',
            'records': {
                'Batch_Size': batch_size,
                'Learning_Rate': learning_method.learning_rate
            }
        })  # end log

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 100,
                                   'epoch_look_back': 10,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemple #5
0
def train():

    data = Cifar10(batch_size=32, train_valid_test_ratio=[4, 1, 1])

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(
        Convolution2D(input_channels=3,
                      filters=8,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='full'))
    model.add(RELU())
    model.add(
        Convolution2D(input_channels=8,
                      filters=16,
                      kernel_size=(3, 3),
                      stride=(1, 1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4, 4), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(16 * 8 * 8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01,
                          momentum=0.9,
                          lr_decay_factor=0.9,
                          decay_batch=5000)

    # Build Logger
    log = Log(
        experiment_name='cifar10_cnn',
        description='This is a tutorial',
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'hyperparam.sqlite3',
            'records': {
                'Batch_Size': data.batch_size,
                'Learning_Rate': learning_method.learning_rate,
                'Momentum': learning_method.momentum
            }
        })  # end log

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 30,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy