예제 #1
0
def cnn_ensemble_leave_one_out():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    which_fold = 0
    while 1:
        print which_fold
        try:
            kwargs = get_default_configure_leave_one_out(which_fold=which_fold)
        except:
            break
        kwargs['num_rows'] = 40
        kwargs['num_cols'] = 40
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(kwargs)
        kwargs['save_path'] = str(which_fold) + '.pkl'
        t1 = time.time()
        ann = cnn_train(**kwargs)
        serial.save(kwargs['predict_path'] + 'f' + kwargs['save_path'],
                    ann,
                    on_overwrite='backup')
        print 'saved to: ' + kwargs['save_path']
        print 'Traing done. Take {}h'.format((time.time() - t1) / 3600)
        which_fold += 1
    utils.sms_notice('Training finished. Taking {}h in total.'.format(
        (time.time() - t0) / 3600))
    print 'Traing done. Take {}h'.format((time.time() - t0) / 3600)
    # sum of all predictions
    #predict_batch()
    evaluate_sets(kwargs['predict_path'])
예제 #2
0
def cnn_transform_ensemble():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    kwargs = get_default_configure()
    kwargs['num_rows'] = 41
    kwargs['num_cols'] = 41
    import pprint
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(kwargs)

    i = -1
    while under_sample_water.which_fold != i:
        if i == -1:
            i = under_sample_water.which_fold
        kwargs['save_path'] = str(under_sample_water.which_fold) + '.pkl'
        t1 = time.time()
        ann = cnn_train_tranformer(**kwargs)
        serial.save(kwargs['predict_path'] + 'f' + kwargs['save_path'],
                    ann,
                    on_overwrite='backup')
        print 'saved to: ' + kwargs['save_path']
        print 'Traing done. Take {}h'.format((time.time() - t1) / 3600)
        break
    utils.sms_notice('Training finished. Taking {}h in total.'.format(
        (time.time() - t0) / 3600))
    print 'Traing done. Take {}h'.format((time.time() - t0) / 3600)
    # sum of all predictions
    predict_batch(predict_path)
예제 #3
0
def cnn_ensemble_leave_one_out():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    which_fold = 0
    while 1:
        print which_fold
        try:
            kwargs = get_default_configure_leave_one_out(which_fold=which_fold)
        except:
            break
        kwargs['num_rows']=40
        kwargs['num_cols']=40
        pp=pprint.PrettyPrinter(indent=4)
        pp.pprint(kwargs)
        kwargs['save_path'] =  str(which_fold)+'.pkl'
        t1 = time.time()
        ann = cnn_train(**kwargs)
        serial.save(kwargs['predict_path']+'f'+kwargs['save_path'],ann,on_overwrite='backup')
        print 'saved to: '+kwargs['save_path']
        print 'Traing done. Take {}h'.format((time.time()-t1)/3600)
        which_fold += 1
    utils.sms_notice('Training finished. Taking {}h in total.'.format((time.time()-t0)/3600))
    print 'Traing done. Take {}h'.format((time.time()-t0)/3600)
    # sum of all predictions
    #predict_batch()
    evaluate_sets(kwargs['predict_path'])
예제 #4
0
def cnn_transform_ensemble():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    kwargs = get_default_configure()
    kwargs['num_rows']=41
    kwargs['num_cols']=41
    import pprint
    pp=pprint.PrettyPrinter(indent=4)
    pp.pprint(kwargs)

    i = -1
    while under_sample_water.which_fold != i:
        if i == -1:
            i = under_sample_water.which_fold
        kwargs['save_path'] =  str(under_sample_water.which_fold)+'.pkl'
        t1 = time.time()
        ann = cnn_train_tranformer(**kwargs)
        serial.save(kwargs['predict_path']+'f'+kwargs['save_path'],ann,on_overwrite='backup')
        print 'saved to: '+kwargs['save_path']
        print 'Traing done. Take {}h'.format((time.time()-t1)/3600)
        break
    utils.sms_notice('Training finished. Taking {}h in total.'.format((time.time()-t0)/3600))
    print 'Traing done. Take {}h'.format((time.time()-t0)/3600)
    # sum of all predictions
    predict_batch(predict_path)
예제 #5
0
def cnn_run():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    kwargs = get_default_configure_gsl2014()
    pp=pprint.PrettyPrinter(indent=4)
    pp.pprint(kwargs)
    t1 = time.time()
    ann = cnn_train(**kwargs)
    serial.save(kwargs['predict_path']+'f'+kwargs['save_path'],ann,on_overwrite='backup')
    print 'saved to: '+kwargs['save_path']
    print 'Traing done. Take {}h'.format((time.time()-t1)/3600)
    utils.sms_notice('Training finished. Taking {}h in total.'.format((time.time()-t0)/3600))
    print 'Traing done. Take {}h'.format((time.time()-t0)/3600)

    predict_batch(get_default_configure_gsl2014())
예제 #6
0
def cnn_run():
    import os
    from datetime import datetime
    print str(datetime.now())
    t0 = time.time()
    kwargs = get_default_configure_gsl2014()
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(kwargs)
    t1 = time.time()
    ann = cnn_train(**kwargs)
    serial.save(kwargs['predict_path'] + 'f' + kwargs['save_path'],
                ann,
                on_overwrite='backup')
    print 'saved to: ' + kwargs['save_path']
    print 'Traing done. Take {}h'.format((time.time() - t1) / 3600)
    utils.sms_notice('Training finished. Taking {}h in total.'.format(
        (time.time() - t0) / 3600))
    print 'Traing done. Take {}h'.format((time.time() - t0) / 3600)

    predict_batch(get_default_configure_gsl2014())
예제 #7
0
def cnn_train_tranformer(train_path,
                         test_path,
                         valid_path,
                         save_path,
                         predict_path,
                         num_rows=28,
                         num_cols=28,
                         num_channels=2,
                         batch_size=128,
                         output_channels=[64, 64],
                         kernel_shape=[[12, 12], [5, 5]],
                         pool_shape=[[4, 4], [2, 2]],
                         pool_stride=[[2, 2], [2, 2]],
                         irange=[0.05, 0.05, 0.05],
                         max_kernel_norm=[1.9365, 1.9365],
                         learning_rate=0.001,
                         init_momentum=0.9,
                         weight_decay=[0.0002, 0.0002, 0.0002],
                         n_epoch=1000,
                         image_path=''):

    ds = load_data_transformed(train_path, num_cols, batch_size)
    ds = (np.transpose(ds[0], axes=[0, 3, 1, 2]), ds[1])
    trn = SarDataset(np.array(ds[0]), ds[1])
    ds = load_data_transformed(valid_path, num_cols, batch_size)
    ds = (np.transpose(ds[0], axes=[0, 3, 1, 2]), ds[1])
    vld = SarDataset(np.array(ds[0]), ds[1])
    ds = load_data_transformed(test_path, num_cols, batch_size)
    ds = (np.transpose(ds[0], axes=[0, 3, 1, 2]), ds[1])
    tst = SarDataset(np.array(ds[0]), ds[1])
    #setup the network
    #X = np.random.random([400000,2,41,41])
    #y = np.random.random([400000,1])
    #trn = SarDataset(X,y)
    #X = np.random.random([60000,2,41,41])
    #y = np.random.random([60000,1])
    #tst = SarDataset(X,y)
    #X = np.random.random([60000,2,41,41])
    #y = np.random.random([60000,1])
    #vld = SarDataset(X,y)
    t = time.time()
    layers = []
    for i in range(len(output_channels)):
        layer_name = 'h{}'.format(i + 1)
        convlayer = mlp.ConvRectifiedLinear(layer_name=layer_name,
                                            output_channels=output_channels[i],
                                            irange=irange[i],
                                            kernel_shape=kernel_shape[i],
                                            pool_shape=pool_shape[i],
                                            pool_stride=pool_stride[i],
                                            max_kernel_norm=max_kernel_norm[i])
        layers.append(convlayer)

    output_mlp = mlp.Linear(dim=1, layer_name='output', irange=irange[-1])
    #output_mlp = mlp.linear_mlp_bayesian_cost(dim=1,layer_name='output',irange=irange[-1])
    layers.append(output_mlp)

    trainer = sgd.SGD(
        learning_rate=learning_rate,
        batch_size=batch_size,
        termination_criterion=EpochCounter(n_epoch),
        #termination_criterion = termination_criteria.And([termination_criteria.MonitorBased(channel_name = 'train_objective', prop_decrease=0.01,N=10),EpochCounter(n_epoch)]),
        #cost = dropout.Dropout(),
        cost=cost.SumOfCosts(
            [cost.MethodCost('cost_from_X'),
             WeightDecay(weight_decay)]),
        init_momentum=init_momentum,
        train_iteration_mode='even_shuffled_sequential',
        monitor_iteration_mode='even_shuffled_sequential',
        monitoring_dataset={
            'test': tst,
            'valid': vld,
            'train': trn
        })

    input_space = space.Conv2DSpace(shape=[num_rows, num_cols],
                                    num_channels=num_channels)
    #ann = mlp.MLP(layers,input_space=input_space,batch_size=batch_size)
    watcher = best_params.MonitorBasedSaveBest(channel_name='valid_objective',
                                               save_path=predict_path +
                                               save_path)
    #flip = window_flip.WindowAndFlip((num_rows,num_cols),randomize=[tst,vld,trn])
    experiment = Train(dataset=trn,
                       model=ann,
                       algorithm=trainer,
                       extensions=[watcher])
    print 'Take {}s to compile code'.format(time.time() - t)

    #train the network
    t = time.time()
    experiment.main_loop()
    print 'Training time: {}h'.format((time.time() - t) / 3600)
    utils.sms_notice('Training time:{}'.format((time.time() - t) / 3600))

    return ann
예제 #8
0
def cnn_train(
    train_path,
    test_path,
    valid_path,
    save_path,
    predict_path,
    image_path,
    num_rows=28,
    num_cols=28,
    num_channels=2,
    batch_size=128,
    output_channels=[64, 64],
    kernel_shape=[[12, 12], [5, 5]],
    pool_shape=[[4, 4], [2, 2]],
    pool_stride=[[2, 2], [2, 2]],
    irange=[0.05, 0.05, 0.05],
    max_kernel_norm=[1.9365, 1.9365],
    learning_rate=0.001,
    init_momentum=0.9,
    weight_decay=[0.0002, 0.0002, 0.0002],
    n_epoch=1000,
):
    #load data
    #t = time.time()
    ds = load_data(valid_path, num_rows, num_cols, num_channels)
    vld = SarDataset(np.array(ds[0]), ds[1])
    ds = load_data(train_path, num_rows, num_cols, num_channels)
    trn = SarDataset(np.array(ds[0]), ds[1])
    ds = load_data(test_path, num_rows, num_cols, num_channels)
    tst = SarDataset(np.array(ds[0]), ds[1])
    #load balanced data
    #ds = load_data_balance_under_sample(train_path, num_rows,num_cols, num_channels)
    #trn = SarDataset(np.array(ds[0]),ds[1])
    #ds = load_data_balance(valid_path, num_rows,num_cols, num_channels)
    #vld = SarDataset(np.array(ds[0]),ds[1])
    #ds = load_data_balance(test_path, num_rows,num_cols, num_channels)
    #tst = SarDataset(np.array(ds[0]),ds[1])
    #print 'Take {}s to read data'.format( time.time()-t)
    #use gaussian convlution on the origional image to see if it can concentrate in the center
    #trn,tst,vld = load_data_lidar()

    #mytransformer = transformer.TransformationPipeline(input_space=space.Conv2DSpace(shape=[num_rows,num_cols],num_channels=num_channels),transformations=[transformer.Rotation(),transformer.Flipping()])
    #trn = contestTransformerDataset.TransformerDataset(trn,mytransformer,space_preserving=True)
    #tst = contestTransformerDataset.TransformerDataset(tst,mytransformer,space_preserving=True)
    #vld = contestTransformerDataset.TransformerDataset(vld,mytransformer,space_preserving=True)

    #trn = transformer_dataset.TransformerDataset(trn,mytransformer,space_preserving=True)
    #tst = transformer_dataset.TransformerDataset(tst,mytransformer,space_preserving=True)
    #vld = transformer_dataset.TransformerDataset(vld,mytransformer,space_preserving=True)

    #setup the network
    t = time.time()
    layers = []
    for i in range(len(output_channels)):
        layer_name = 'h{}'.format(i + 1)
        convlayer = mlp.ConvRectifiedLinear(layer_name=layer_name,
                                            output_channels=output_channels[i],
                                            irange=irange[i],
                                            kernel_shape=kernel_shape[i],
                                            pool_shape=pool_shape[i],
                                            pool_stride=pool_stride[i],
                                            max_kernel_norm=max_kernel_norm[i])
        layers.append(convlayer)

    output_mlp = mlp.Linear(dim=1,
                            layer_name='output',
                            irange=irange[-1],
                            use_abs_loss=True)
    #output_mlp = mlp.linear_mlp_ace(dim=1,layer_name='output',irange=irange[-1])
    layers.append(output_mlp)

    #ann = cPickle.load(open('../output/train_with_2010_2l_40_64/original_500/f/f0.pkl'))
    #layers = []
    #for layer in ann.layers:
    #    layer.set_mlp_force(None)
    #    layers.append(layer)

    trainer = sgd.SGD(
        learning_rate=learning_rate,
        batch_size=batch_size,
        termination_criterion=EpochCounter(n_epoch),
        #termination_criterion = termination_criteria.And([termination_criteria.MonitorBased(channel_name = 'train_objective', prop_decrease=0.01,N=10),EpochCounter(n_epoch)]),
        #cost = dropout.Dropout(),
        cost=cost.SumOfCosts(
            [cost.MethodCost('cost_from_X'),
             WeightDecay(weight_decay)]),
        init_momentum=init_momentum,
        train_iteration_mode='even_shuffled_sequential',
        monitor_iteration_mode='even_shuffled_sequential',
        monitoring_dataset={
            'test': tst,
            'valid': vld,
            'train': trn
        })

    input_space = space.Conv2DSpace(shape=[num_rows, num_cols],
                                    num_channels=num_channels)
    #ann = mlp.MLP(layers,input_space=input_space,batch_size=batch_size)
    ann = serial.load(
        '../output/train_with_2010_2l_40_64/original_500/f/f0.pkl')
    ann = monitor.push_monitor(ann, 'stage_0')
    watcher = best_params.MonitorBasedSaveBest(channel_name='valid_objective',
                                               save_path=predict_path +
                                               save_path)
    flip = window_flip.WindowAndFlip((num_rows, num_cols),
                                     randomize=[tst, vld, trn])
    experiment = Train(dataset=trn,
                       model=ann,
                       algorithm=trainer,
                       extensions=[watcher, flip])
    print 'Take {}s to compile code'.format(time.time() - t)

    #train the network
    t = time.time()
    experiment.main_loop()
    print 'Training time: {}h'.format((time.time() - t) / 3600)
    utils.sms_notice('Training time:{}'.format((time.time() - t) / 3600))

    return ann
예제 #9
0
def cnn_train_tranformer(train_path, test_path, valid_path, save_path, predict_path,num_rows=28,
             num_cols =28,
             num_channels =2,
             batch_size =128,
             output_channels =[64,64],
             kernel_shape =[[12,12],[5,5]],
             pool_shape =[[4,4],[2,2]],
             pool_stride =[[2,2],[2,2]],
             irange =[0.05,0.05,0.05],
             max_kernel_norm =[1.9365,1.9365],
             learning_rate =0.001,
             init_momentum =0.9,
             weight_decay =[0.0002,0.0002,0.0002],
             n_epoch = 1000,
             image_path = ''
        ):


    ds = load_data_transformed(train_path, num_cols,batch_size)
    ds = (np.transpose(ds[0],axes=[0,3,1,2]),ds[1])
    trn = SarDataset(np.array(ds[0]),ds[1])
    ds = load_data_transformed(valid_path, num_cols,batch_size)
    ds = (np.transpose(ds[0],axes=[0,3,1,2]),ds[1])
    vld = SarDataset(np.array(ds[0]),ds[1])
    ds = load_data_transformed(test_path, num_cols,batch_size)
    ds = (np.transpose(ds[0],axes=[0,3,1,2]),ds[1])
    tst = SarDataset(np.array(ds[0]),ds[1])
    #setup the network
    #X = np.random.random([400000,2,41,41])
    #y = np.random.random([400000,1])
    #trn = SarDataset(X,y)
    #X = np.random.random([60000,2,41,41])
    #y = np.random.random([60000,1])
    #tst = SarDataset(X,y)
    #X = np.random.random([60000,2,41,41])
    #y = np.random.random([60000,1])
    #vld = SarDataset(X,y)
    t = time.time()
    layers = []
    for i in range(len(output_channels)):
        layer_name = 'h{}'.format(i+1)
        convlayer = mlp.ConvRectifiedLinear(layer_name=layer_name, output_channels=output_channels[i],irange=irange[i],kernel_shape=kernel_shape[i],pool_shape=pool_shape[i],pool_stride=pool_stride[i],max_kernel_norm=max_kernel_norm[i])
        layers.append(convlayer)


    output_mlp = mlp.Linear(dim=1,layer_name='output',irange=irange[-1])
    #output_mlp = mlp.linear_mlp_bayesian_cost(dim=1,layer_name='output',irange=irange[-1])
    layers.append(output_mlp)

    trainer = sgd.SGD(learning_rate=learning_rate,batch_size=batch_size,
                      termination_criterion=EpochCounter(n_epoch),
                      #termination_criterion = termination_criteria.And([termination_criteria.MonitorBased(channel_name = 'train_objective', prop_decrease=0.01,N=10),EpochCounter(n_epoch)]),
                      #cost = dropout.Dropout(),
                      cost = cost.SumOfCosts([cost.MethodCost('cost_from_X'), WeightDecay(weight_decay)]),
                      init_momentum=init_momentum,
                      train_iteration_mode='even_shuffled_sequential',
                      monitor_iteration_mode='even_shuffled_sequential',
                      monitoring_dataset={'test':  tst,
                                          'valid': vld,
                                          'train': trn})

    input_space = space.Conv2DSpace(shape=[num_rows,num_cols],num_channels=num_channels)
    #ann = mlp.MLP(layers,input_space=input_space,batch_size=batch_size)
    watcher = best_params.MonitorBasedSaveBest(
            channel_name='valid_objective',
            save_path = predict_path+save_path)
    #flip = window_flip.WindowAndFlip((num_rows,num_cols),randomize=[tst,vld,trn])
    experiment = Train(dataset=trn,
                       model=ann,
                       algorithm=trainer,
                       extensions=[watcher])
    print 'Take {}s to compile code'.format(time.time()-t)

    #train the network
    t = time.time()
    experiment.main_loop()
    print 'Training time: {}h'.format((time.time()-t)/3600)
    utils.sms_notice('Training time:{}'.format((time.time()-t)/3600))

    return ann
예제 #10
0
def cnn_train(train_path, test_path, valid_path, save_path, predict_path,image_path,num_rows=28,
             num_cols =28,
             num_channels =2,
             batch_size =128,
             output_channels =[64,64],
             kernel_shape =[[12,12],[5,5]],
             pool_shape =[[4,4],[2,2]],
             pool_stride =[[2,2],[2,2]],
             irange =[0.05,0.05,0.05],
             max_kernel_norm =[1.9365,1.9365],
             learning_rate =0.001,
             init_momentum =0.9,
             weight_decay =[0.0002,0.0002,0.0002],
             n_epoch = 1000,
             ):
    #load data
    #t = time.time()
    ds = load_data(valid_path, num_rows,num_cols, num_channels)
    vld = SarDataset(np.array(ds[0]),ds[1])
    ds = load_data(train_path, num_rows,num_cols, num_channels)
    trn = SarDataset(np.array(ds[0]),ds[1])
    ds = load_data(test_path, num_rows,num_cols, num_channels)
    tst = SarDataset(np.array(ds[0]),ds[1])
    #load balanced data
    #ds = load_data_balance_under_sample(train_path, num_rows,num_cols, num_channels)
    #trn = SarDataset(np.array(ds[0]),ds[1])
    #ds = load_data_balance(valid_path, num_rows,num_cols, num_channels)
    #vld = SarDataset(np.array(ds[0]),ds[1])
    #ds = load_data_balance(test_path, num_rows,num_cols, num_channels)
    #tst = SarDataset(np.array(ds[0]),ds[1])
    #print 'Take {}s to read data'.format( time.time()-t)
    #use gaussian convlution on the origional image to see if it can concentrate in the center
    #trn,tst,vld = load_data_lidar()

    #mytransformer = transformer.TransformationPipeline(input_space=space.Conv2DSpace(shape=[num_rows,num_cols],num_channels=num_channels),transformations=[transformer.Rotation(),transformer.Flipping()])
    #trn = contestTransformerDataset.TransformerDataset(trn,mytransformer,space_preserving=True)
    #tst = contestTransformerDataset.TransformerDataset(tst,mytransformer,space_preserving=True)
    #vld = contestTransformerDataset.TransformerDataset(vld,mytransformer,space_preserving=True)

    #trn = transformer_dataset.TransformerDataset(trn,mytransformer,space_preserving=True)
    #tst = transformer_dataset.TransformerDataset(tst,mytransformer,space_preserving=True)
    #vld = transformer_dataset.TransformerDataset(vld,mytransformer,space_preserving=True)

    #setup the network
    t = time.time()
    layers = []
    for i in range(len(output_channels)):
        layer_name = 'h{}'.format(i+1)
        convlayer = mlp.ConvRectifiedLinear(layer_name=layer_name, output_channels=output_channels[i],irange=irange[i],kernel_shape=kernel_shape[i],pool_shape=pool_shape[i],pool_stride=pool_stride[i],max_kernel_norm=max_kernel_norm[i])
        layers.append(convlayer)


    output_mlp = mlp.Linear(dim=1,layer_name='output',irange=irange[-1], use_abs_loss=True)
    #output_mlp = mlp.linear_mlp_ace(dim=1,layer_name='output',irange=irange[-1])
    layers.append(output_mlp)


    #ann = cPickle.load(open('../output/train_with_2010_2l_40_64/original_500/f/f0.pkl'))
    #layers = []
    #for layer in ann.layers:
    #    layer.set_mlp_force(None)
    #    layers.append(layer)

    trainer = sgd.SGD(learning_rate=learning_rate,batch_size=batch_size,
                      termination_criterion=EpochCounter(n_epoch),
                      #termination_criterion = termination_criteria.And([termination_criteria.MonitorBased(channel_name = 'train_objective', prop_decrease=0.01,N=10),EpochCounter(n_epoch)]),
                      #cost = dropout.Dropout(),
                      cost = cost.SumOfCosts([cost.MethodCost('cost_from_X'), WeightDecay(weight_decay)]),
                      init_momentum=init_momentum,
                      train_iteration_mode='even_shuffled_sequential',
                      monitor_iteration_mode='even_shuffled_sequential',
                      monitoring_dataset={'test':  tst,
                                          'valid': vld,
                                          'train': trn})

    input_space = space.Conv2DSpace(shape=[num_rows,num_cols],num_channels=num_channels)
    #ann = mlp.MLP(layers,input_space=input_space,batch_size=batch_size)
    ann = serial.load('../output/train_with_2010_2l_40_64/original_500/f/f0.pkl')
    ann = monitor.push_monitor(ann,'stage_0')
    watcher = best_params.MonitorBasedSaveBest(
            channel_name='valid_objective',
            save_path = predict_path+save_path)
    flip = window_flip.WindowAndFlip((num_rows,num_cols),randomize=[tst,vld,trn])
    experiment = Train(dataset=trn,
                       model=ann,
                       algorithm=trainer,
                       extensions=[watcher,flip])
    print 'Take {}s to compile code'.format(time.time()-t)

    #train the network
    t = time.time()
    experiment.main_loop()
    print 'Training time: {}h'.format((time.time()-t)/3600)
    utils.sms_notice('Training time:{}'.format((time.time()-t)/3600))

    return ann