Ejemplo n.º 1
0
def main():
    """
    """
    import random
    import numpy

    recogniter = ClaClassifier(net_structure, sensor_params, dest_resgion_data, class_encoder_params)

    print "training ..."
    for i, data in enumerate(train_data[:1000]):

        patch_data, movement = get_patch(data, height=patch_heigh, width=patch_width, step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x, y: x * y, patch.shape)
            input_data = {"pixel": patch.reshape((input_len)).tolist(), "label": train_label[i][0]}
            inferences = recogniter.run(input_data, learn=True, class_learn=True, learn_layer=None)
            # recogniter.layer_output(input_data)
            # recogniter.print_inferences(input_data, inferences)

        # print train_label[i][0] , inferences['classifier_region1']['best']

        recogniter.reset()

        # validate
        if i % 50 == 0 and not i == 0:
            valid = validate(recogniter, test_data, test_label, limit=30)
            print "%d : valid: %8.5f" % (i, valid)
Ejemplo n.º 2
0
def validate(model, test_data, test_label, limit=100):
    result = []
    tdata = test_data[:limit]
    for i, data in enumerate(tdata):
        if test_label[i][0] not in (0, 1):
            continue
        patch_result = Counter()
        patch_data, movement = get_patch(data, height=patch_heigh, width=patch_width, step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x,y: x * y, patch.shape)
            input_data = {
                    'pixel': patch.reshape((input_len)).tolist() ,
                    'label': 'no'
                    }
            model.disableLearning()
            modelresult = model.run(input_data)
            #print label[i][0], result.inferences['multiStepBestPredictions']


            best_result = modelresult.inferences['multiStepBestPredictions']
            #patch_result[best_result['value']] += best_result['prob']
            patch_result[best_result[0]] += 1

        # print test_label[i][0]
        # print patch_result

        if test_label[i][0] == max(patch_result.items(), key=lambda x:x[1])[0]:
            result.append(1)

        #model.resetSequenceStates()
        model._getTPRegion().getSelf().resetSequenceStates()

    return len(result)/len(tdata)
Ejemplo n.º 3
0
def validate(recogniter, test_data, test_label, limit=100):
    result = []
    tdata = test_data[:limit]
    for i, data in enumerate(tdata):
        patch_result = Counter()
        patch_data, movement = get_patch(data, height=patch_heigh, width=patch_width, step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x, y: x * y, patch.shape)
            input_data = {"pixel": patch.reshape((input_len)).tolist(), "label": "no"}
            inferences = recogniter.run(input_data, learn=True, class_learn=False, learn_layer=None)

            best_result = inferences["classifier_region1"]["best"]
            # patch_result[best_result['value']] += best_result['prob']
            patch_result[best_result["value"]] += 1

        # print test_label[i][0]
        # print patch_result

        if test_label[i][0] == max(patch_result.items(), key=lambda x: x[1])[0]:
            result.append(1)

        recogniter.reset()

    return float(len(result)) / len(tdata)
Ejemplo n.º 4
0
def validate(recogniter, test_data, test_label, limit=100):
    result = []
    tdata = test_data[:limit]
    for i, data in enumerate(tdata):
        patch_result = Counter()
        patch_data, movement = get_patch(data,
                                         height=patch_heigh,
                                         width=patch_width,
                                         step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x, y: x * y, patch.shape)
            input_data = {
                'pixel': patch.reshape((input_len)).tolist(),
                'label': 'no'
            }
            inferences = recogniter.run(input_data,
                                        learn=True,
                                        class_learn=False,
                                        learn_layer=None)

            best_result = inferences['classifier_region1']['best']
            #patch_result[best_result['value']] += best_result['prob']
            patch_result[best_result['value']] += 1

        # print test_label[i][0]
        # print patch_result

        if test_label[i][0] == max(patch_result.items(),
                                   key=lambda x: x[1])[0]:
            result.append(1)

        recogniter.reset()

    return float(len(result)) / len(tdata)
Ejemplo n.º 5
0
 def test_get_patch_step_2(self):
     # step = 2
     patch_data, movement = get_patch(self.tdata, 2, 2, 2)
     self.assertEqual(patch_data.shape, (1, 2, 2, 3))
     self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.])
     self.assertEqual(patch_data.tolist()[0][0][1], [8., 9., 10.])
     self.assertEqual(patch_data.tolist()[0][1][0], [4., 5., 6.])
     self.assertEqual(patch_data.tolist()[0][1][1], [5., 6., 7.])
 def test_get_patch_step_2(self):
     # step = 2
     patch_data, movement = get_patch(self.tdata, 2, 2, 2)
     self.assertEqual(patch_data.shape, (1,2,2,3))
     self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.] )
     self.assertEqual(patch_data.tolist()[0][0][1], [8., 9., 10.])
     self.assertEqual(patch_data.tolist()[0][1][0], [4., 5., 6.] )
     self.assertEqual(patch_data.tolist()[0][1][1], [5., 6., 7.] )
Ejemplo n.º 7
0
 def test_get_patch(self):
     # [1, 1]
     patch_data, movement = get_patch(self.tdata, 1, 1, 1)
     self.assertEqual(patch_data.shape, (9, 1, 1, 3))
     self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.])
     self.assertEqual(patch_data.tolist()[1][0][0], [8., 9., 10.])
     self.assertEqual(patch_data.tolist()[2][0][0], [0., 0., 0.])
     self.assertEqual(patch_data.tolist()[3][0][0], [0., 0., 0.])
     self.assertEqual(patch_data.tolist()[4][0][0], [5., 6., 7.])
 def test_get_patch(self):
     # [1, 1]
     patch_data, movement = get_patch(self.tdata, 1, 1, 1)
     self.assertEqual(patch_data.shape, (9,1,1,3))
     self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.] )
     self.assertEqual(patch_data.tolist()[1][0][0], [8., 9., 10.])
     self.assertEqual(patch_data.tolist()[2][0][0], [0., 0., 0.] )
     self.assertEqual(patch_data.tolist()[3][0][0], [0., 0., 0.] )
     self.assertEqual(patch_data.tolist()[4][0][0], [5., 6., 7.] )
Ejemplo n.º 9
0
    def test_get_patch_form_2(self):
        # [2, 2]
        patch_data, movement = get_patch(self.tdata, 2, 2, 1)
        self.assertEqual(patch_data.shape, (4, 2, 2, 3))
        self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.])
        self.assertEqual(patch_data.tolist()[0][0][1], [8., 9., 10.])
        self.assertEqual(patch_data.tolist()[0][1][0], [4., 5., 6.])
        self.assertEqual(patch_data.tolist()[0][1][1], [5., 6., 7.])

        self.assertEqual(patch_data.tolist()[1][0][0], [8., 9., 10.])
        self.assertEqual(patch_data.tolist()[1][0][1], [0., 0., 0.])
        self.assertEqual(patch_data.tolist()[1][1][0], [5., 6., 7.])
        self.assertEqual(patch_data.tolist()[1][1][1], [0., 0., 0.])
    def test_get_patch_form_2(self):
        # [2, 2]
        patch_data, movement = get_patch(self.tdata, 2, 2, 1)
        self.assertEqual(patch_data.shape, (4,2,2,3))
        self.assertEqual(patch_data.tolist()[0][0][0], [7., 8., 9.] )
        self.assertEqual(patch_data.tolist()[0][0][1], [8., 9., 10.])
        self.assertEqual(patch_data.tolist()[0][1][0], [4., 5., 6.] )
        self.assertEqual(patch_data.tolist()[0][1][1], [5., 6., 7.] )

        self.assertEqual(patch_data.tolist()[1][0][0], [8., 9., 10.] )
        self.assertEqual(patch_data.tolist()[1][0][1], [0., 0., 0.])
        self.assertEqual(patch_data.tolist()[1][1][0], [5., 6., 7.] )
        self.assertEqual(patch_data.tolist()[1][1][1], [0., 0., 0.] )
Ejemplo n.º 11
0
def main():
    """
    """
    import random
    import numpy

    model = createModel()
    shifter = InferenceShifter()

    tobological_data, label = load_dataset(
        './data/pylearn2_gcn_whitened/train.pkl')
    for i, data in enumerate(tobological_data[:1000]):
        if label[i][0] not in (0, 1):
            continue

        patch_data, movement = get_patch(data,
                                         height=patch_heigh,
                                         width=patch_width,
                                         step=patch_step)

        print '%d, label:%s, ' % (i, label[i][0]),
        for data in patch_data:
            input_len = reduce(lambda x, y: x * y, data.shape)
            input_data = {
                'pixel': data.reshape((input_len)).tolist(),
                'label': label[i][0]
            }

            model.enableLearning()
            result = model.run(input_data)

            #result = shifter.shift(result)
            print label[i][0], result.inferences['multiStepBestPredictions']

        #model.resetSequenceStates()
        #model._getTPRegion().executeCommand(['resetSequenceStates'])
        #model._getTPRegion().resetSequenceStates()
        model._getTPRegion().getSelf().resetSequenceStates()

        # validate
        if i % 3 == 0:
            valid = validate(model, test_data, test_label, limit=30)
            print '%d : valid: %8.5f' % (i, valid)
Ejemplo n.º 12
0
def main():
    """
    """
    import random
    import numpy

    model = createModel()
    shifter = InferenceShifter()


    tobological_data, label = load_dataset('./data/pylearn2_gcn_whitened/train.pkl')
    for i, data in enumerate(tobological_data[:1000]):
        if label[i][0] not in (0, 1):
            continue

        patch_data, movement = get_patch(data, height=patch_heigh, width=patch_width, step=patch_step)

        print '%d, label:%s, ' % (i, label[i][0]),
        for data in patch_data:
            input_len = reduce(lambda x,y: x * y, data.shape)
            input_data = {
                    'pixel': data.reshape((input_len)).tolist() ,
                    'label': label[i][0]
                    }

            model.enableLearning()
            result = model.run(input_data)

            #result = shifter.shift(result)
            print label[i][0], result.inferences['multiStepBestPredictions']

        #model.resetSequenceStates()
        #model._getTPRegion().executeCommand(['resetSequenceStates'])
        #model._getTPRegion().resetSequenceStates()
        model._getTPRegion().getSelf().resetSequenceStates()


        # validate
        if i % 3 == 0:
            valid = validate(model, test_data, test_label, limit=30)
            print '%d : valid: %8.5f' % (i, valid)
Ejemplo n.º 13
0
def main():
    """
    """
    import random
    import numpy

    recogniter = ClaClassifier(net_structure, sensor_params, dest_resgion_data,
                               class_encoder_params)

    print 'training ...'
    for i, data in enumerate(train_data[:1000]):

        patch_data, movement = get_patch(data,
                                         height=patch_heigh,
                                         width=patch_width,
                                         step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x, y: x * y, patch.shape)
            input_data = {
                'pixel': patch.reshape((input_len)).tolist(),
                'label': train_label[i][0]
            }
            inferences = recogniter.run(input_data,
                                        learn=True,
                                        class_learn=True,
                                        learn_layer=None)
            #recogniter.layer_output(input_data)
            #recogniter.print_inferences(input_data, inferences)

        #print train_label[i][0] , inferences['classifier_region1']['best']

        recogniter.reset()

        # validate
        if i % 50 == 0 and not i == 0:
            valid = validate(recogniter, test_data, test_label, limit=30)
            print '%d : valid: %8.5f' % (i, valid)
Ejemplo n.º 14
0
def validate(model, test_data, test_label, limit=100):
    result = []
    tdata = test_data[:limit]
    for i, data in enumerate(tdata):
        if test_label[i][0] not in (0, 1):
            continue
        patch_result = Counter()
        patch_data, movement = get_patch(data,
                                         height=patch_heigh,
                                         width=patch_width,
                                         step=patch_step)

        for patch in patch_data:
            input_len = reduce(lambda x, y: x * y, patch.shape)
            input_data = {
                'pixel': patch.reshape((input_len)).tolist(),
                'label': 'no'
            }
            model.disableLearning()
            modelresult = model.run(input_data)
            #print label[i][0], result.inferences['multiStepBestPredictions']

            best_result = modelresult.inferences['multiStepBestPredictions']
            #patch_result[best_result['value']] += best_result['prob']
            patch_result[best_result[0]] += 1

        # print test_label[i][0]
        # print patch_result

        if test_label[i][0] == max(patch_result.items(),
                                   key=lambda x: x[1])[0]:
            result.append(1)

        #model.resetSequenceStates()
        model._getTPRegion().getSelf().resetSequenceStates()

    return len(result) / len(tdata)