예제 #1
0
파일: server.py 프로젝트: tech-team/convnet
class Application(tornado.web.Application):
    def __init__(self):
        self.net = ConvNet()
        self.net.setup_layers([
            InputLayer(InputLayerSettings(in_shape=(28, 28, 1))),

            ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=16, filter_size=5, stride=1, zero_padding=0)),
            ReluLayer(ReluLayerSettings(activation='max')),
            PoolingLayer(PoolingLayerSettings(filter_size=2, stride=2)),

            ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=8, filter_size=5, stride=1, zero_padding=0)),
            ReluLayer(ReluLayerSettings(activation='max')),
            PoolingLayer(PoolingLayerSettings(filter_size=3, stride=3)),

            FullConnectedLayer(FullConnectedLayerSettings(neurons_count=10, activation='sigmoid')),
        ])

        s = {
            'debug': convsettings.DEBUG,
            'template_path': convsettings.TEMPLATES_DIR,
            'static_path': convsettings.STATIC_DIR,
            'static_url_prefix': convsettings.STATIC_PREFIX
        }

        handlers = [
            (r"/", convnet_web.handlers.VisualHandler),
            (r"/config", convnet_web.handlers.ConfigHandler),

            (r"/api/config", convnet_web.handlers.ApiConfig, dict(net=self.net)),
            (r"/api/config_mnist", convnet_web.handlers.ApiConfigMnist, dict(net=self.net)),
            (r"/api/predict", convnet_web.handlers.ApiPredict, dict(net=self.net)),
        ]
        super(Application, self).__init__(handlers, **s)
예제 #2
0
    def _test_basic(self):
        X = [np.zeros((3, 3, 2)) for _ in xrange(2)]
        y = [np.zeros((1, 1, 2)) for _ in xrange(2)]

        net = ConvNet()
        net.setup_layers([
            InputLayer(InputLayerSettings(in_shape=X[0].shape)),
            ConvolutionalLayer(ConvolutionalLayerSettings(filter_size=2, filters_count=2, stride=1)),
            PoolingLayer(PoolingLayerSettings(filter_size=2, stride=1)),
            ReluLayer(ReluLayerSettings(activation='max')),
            FullConnectedLayer(FullConnectedLayerSettings(neurons_count=y[0].shape[-1])),
            ReluLayer(ReluLayerSettings(activation='sigmoid')),
        ])

        net.fit(X, y)
예제 #3
0
    def test_backward(self):
        arr1 = np.empty((3, 3, 1))
        arr1[:, :, 0] = np.array([
            [1, 0, 0],
            [0, 1, 0],
            [0, 0, 1]])

        arr2 = np.empty((3, 3, 1))
        arr2[:, :, 0] = np.array([
            [0, 0, 1],
            [0, 1, 0],
            [1, 0, 0]])

        samples = [arr1, arr2]

        expected_res1 = np.empty((1, 1, 2))
        expected_res1[0, 0, 0] = 1
        expected_res1[0, 0, 1] = -1
        expected_res2 = np.empty((1, 1, 2))
        expected_res2[0, 0, 0] = -1
        expected_res2[0, 0, 1] = 1

        expected_res = [expected_res1, expected_res2]

        net = ConvNet()
        net.setup_layers([
            InputLayer(InputLayerSettings(in_shape=arr1.shape)),
            ConvolutionalLayer(ConvolutionalLayerSettings(filter_size=3,
                                                          stride=1,
                                                          filters_count=2,
                                                          zero_padding=0))
        ])

        # before learn
        res_before = []
        for i in xrange(len(samples)):
            res = net.predict(samples[i])
            res_before.append(res)

        # calc distance before
        dist_before = get_dist(res_before, expected_res)

        # learning
        net.fit(samples, expected_res)

        # after learn
        res_after = []
        for i in xrange(len(samples)):
            res = net.predict(samples[i])
            res_after.append(res)

        # calc distance after
        dist_after = get_dist(res_after, expected_res)

        print 'Before: %s' % dist_before
        print 'After: %s' % dist_after

        self.assertLess(dist_after, dist_before)
예제 #4
0
    def _test_crossings(self):
        X = to_3d([
            np.asarray([
                [1, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
            ]),
            np.asarray([
                [0, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
            ]),
            np.asarray([
                [0, 0, 1],
                [0, 1, 0],
                [1, 0, 0],
            ]),
            np.asarray([
                [0, 0, 1],
                [0, 1, 0],
                [0, 0, 0],
            ]),
        ])
        Y = [
            np.asarray([1, 0]),
            np.asarray([1, 0]),
            np.asarray([0, 1]),
            np.asarray([0, 1]),
        ]

        net = ConvNet(iterations_count=1000, learning_rate=0.01)
        net.setup_layers([
            InputLayer(InputLayerSettings(in_shape=X[0].shape)),
            ConvolutionalLayer(ConvolutionalLayerSettings(filter_size=3, filters_count=2, stride=1)),
            ReluLayer(ReluLayerSettings(activation='max')),
            # PoolingLayer(PoolingLayerSettings(filter_size=1, stride=1)),
            # FullConnectedLayer(FullConnectedLayerSettings(neurons_count=Y[0].shape[-1])),
            # ReluLayer(ReluLayerSettings(activation='sigmoid')),
        ])

        net.fit(X, y_to_3d(Y))

        # net = ConvNet.load_net('/home/igor/Desktop/net.pkl')

        for x, y in zip(X, Y):
            h = net.predict(x)
            print("predicted = {}; \nreal = {}\n\n".format(h, y))
        pass
예제 #5
0
    def test_mnist(self):
        script_path = os.path.dirname(os.path.abspath(__file__))
        f = gzip.open(os.path.join(script_path, '../mnist/mnist.pkl.gz'), 'rb')
        train_set, valid_set, test_set = cPickle.load(f)
        f.close()

        X_train, Y_train = train_set

        X_train, Y_train = self.get_examples(X_train, Y_train, labels=np.arange(0, 4), count=10)
        X_train, Y_train = self.shuffle_in_unison_inplace(X_train, Y_train)
        X_train = self.transform_X(X_train)
        Y_train = self.transform_Y(Y_train)

        net = ConvNet(iterations_count=10, batch_size=1, learning_rate=0.001, momentum=0.8, weight_decay=0.001)
        net.setup_layers([
            InputLayer(InputLayerSettings(in_shape=X_train[0].shape)),

            ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=8, filter_size=5, stride=1, zero_padding=0)),
            ReluLayer(ReluLayerSettings(activation='max')),
            PoolingLayer(PoolingLayerSettings(filter_size=2, stride=2)),

            ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=16, filter_size=5, stride=1, zero_padding=0)),
            ReluLayer(ReluLayerSettings(activation='max')),
            PoolingLayer(PoolingLayerSettings(filter_size=3, stride=3)),

            FullConnectedLayer(FullConnectedLayerSettings(neurons_count=Y_train[0].shape[-1], activation='sigmoid')),
            # ReluLayer(ReluLayerSettings(activation='sigmoid')),
        ])

        examples_count = 100000
        net.fit(X_train[:examples_count], Y_train[:examples_count])

        matched = 0
        for x, y in zip(X_train[:examples_count], Y_train[:examples_count]):
            h = net.predict(x)
            h_res = h.argmax()
            y_res = y.argmax()
            print("predicted = {}; max = {}".format(h, h.argmax()))
            print("real =      {}; max = {}".format(y, y.argmax()))
            print("\n")
            matched += int(h_res == y_res)

        print("Accuracy {}/{}".format(matched, len(X_train[:examples_count])))
예제 #6
0
파일: mnist.py 프로젝트: tech-team/convnet
def mnist():
    script_path = os.path.dirname(os.path.abspath(__file__))
    f = gzip.open(os.path.join(script_path, './mnist.pkl.gz'), 'rb')
    train_set, valid_set, test_set = cPickle.load(f)
    f.close()

    X_train, Y_train = train_set
    #X_train, Y_train = get_examples(X_train, Y_train, labels=np.arange(0, 10), count=200)
    #X_train, Y_train = shuffle_in_unison_inplace(X_train, Y_train)
    X_train = transform_X(X_train)
    Y_train = transform_Y(Y_train)

    print("Train set size: {}".format(len(X_train)))

    X_cv, Y_cv = valid_set
    #X_cv, Y_cv = get_examples(X_cv, Y_cv, labels=np.arange(0, 10), count=15)
    #X_cv, Y_cv = shuffle_in_unison_inplace(X_cv, Y_cv)
    X_cv = transform_X(X_cv)
    Y_cv = transform_Y(Y_cv)

    print("Cross validation set size: {}".format(len(X_cv)))

    X_test, Y_test = test_set
    #X_test, Y_test = get_examples(X_test, Y_test, labels=np.arange(0, 10), count=100)
    #X_test, Y_test = shuffle_in_unison_inplace(X_test, Y_test)
    X_test = transform_X(X_test)
    Y_test = transform_Y(Y_test)

    print("Test set size: {}".format(len(X_test)))

    net = ConvNet(iterations_count=1000, batch_size=200, learning_rate=0.001, momentum=0.9, weight_decay=0.001)
    net.setup_layers([
        InputLayer(InputLayerSettings(in_shape=X_train[0].shape)),

        ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=8, filter_size=5, stride=1, zero_padding=0)),
        ReluLayer(ReluLayerSettings(activation='max')),
        PoolingLayer(PoolingLayerSettings(filter_size=2, stride=2)),

        ConvolutionalLayer(ConvolutionalLayerSettings(filters_count=16, filter_size=5, stride=1, zero_padding=0)),
        ReluLayer(ReluLayerSettings(activation='max')),
        PoolingLayer(PoolingLayerSettings(filter_size=3, stride=3)),

        FullConnectedLayer(FullConnectedLayerSettings(neurons_count=Y_train[0].shape[-1], activation='sigmoid')),
    ])

    # net = ConvNet.load_net(os.path.join(script_path, './convnet1.pkl'))
    try:
        net.fit(X_train, Y_train)
    except KeyboardInterrupt:
        print("Training stopped")

    train_matched = 0
    for x, y in zip(X_train, Y_train):
        h = net.predict(x)
        h_res = h.argmax()
        y_res = y.argmax()
        # print("predicted = {}; max = {}".format(h, h.argmax()))
        # print("real =      {}; max = {}".format(y, y.argmax()))
        # print("\n")
        train_matched += int(h_res == y_res)

    test_matched = 0
    for x, y in zip(X_test, Y_test):
        h = net.predict(x)
        h_res = h.argmax()
        y_res = y.argmax()
        # print("predicted = {}; max = {}".format(h, h.argmax()))
        # print("real =      {}; max = {}".format(y, y.argmax()))
        # print("\n")
        test_matched += int(h_res == y_res)

    print("Accuracy train {}/{}".format(train_matched, len(X_train)))
    print("Accuracy test {}/{}".format(test_matched, len(X_test)))

    path = os.path.join(script_path, "./convnet1.pkl")
    net.dump_net(path)
    print("Dumped to {}".format(path))