Ejemplo n.º 1
0
def test_build_model():
    m = Model("test model")
    l_in = m.add_layer(xnn.layers.InputLayer(shape=(10, 200)), name="l_in")
    l_h1 = m.add_layer(xnn.layers.DenseLayer(l_in, 100), name="l_h1")
    l_out = m.add_layer(xnn.layers.DenseLayer(l_h1, 200), name="l_out")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_h1, xnn.objectives.categorical_crossentropy, "emotions", "label", "mean")
    m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
Ejemplo n.º 2
0
def _build_lr_net(batch_size, in_size, out_size):
    np.random.seed(100)
    m = Model()
    l_in = m.add_layer(InputLayer(shape=(batch_size,in_size)))
    l_out = m.add_layer(DenseLayer(l_in, out_size, nonlinearity=softmax))
    m.bind_input(l_in, "inputs")
    m.bind_output(l_out, squared_error, "labels", "label", "mean")
    return m, l_in, l_out
Ejemplo n.º 3
0
def test_trained_model_serialization():
    batch_size = 2
    img_size = 10
    num_hid = 10
    m = _build_model(batch_size, img_size, num_hid)
    global_update_settings = ParamUpdateSettings(
        update=lasagne.updates.nesterov_momentum,
        learning_rate=0.1,
        momentum=0.2)
    trainer = Trainer(m, global_update_settings)

    pixels = np.random.rand(batch_size, img_size).astype(theano.config.floatX)
    emotions = np.random.rand(batch_size, num_hid).astype(theano.config.floatX)

    batch_dict = dict(
        # learning_rate_default=0.1,
        # momentum_default=0.5,
        pixels=pixels,
        emotions=emotions)
    outs = trainer.train_step(batch_dict)
    preds = m.predict(batch_dict)

    dirpath = tempfile.mkdtemp()
    filepath = os.path.join(dirpath, 'testtrainerout')
    m.save_model(filepath)
    m2 = Model('load')
    m2.load_model(filepath)
    shutil.rmtree(dirpath)

    global_update_settings = ParamUpdateSettings(
        update=lasagne.updates.nesterov_momentum,
        learning_rate=0.1,
        momentum=0.2)
    trainer2 = Trainer(m2, global_update_settings)
    preds2 = m.predict(batch_dict)
    outs = trainer.train_step(batch_dict)
    outs2 = trainer2.train_step(batch_dict)

    assert m.outputs.keys() == m2.outputs.keys()

    for p, p2 in zip(preds.values(), preds2.values()):
        assert np.allclose(p, p2)

    for o, o2 in zip(outs, outs2):
        assert np.allclose(o, o2)
Ejemplo n.º 4
0
def _build_lr_net(batch_size, in_size, out_size):
    np.random.seed(100)
    m = Model()
    l_in = m.add_layer(InputLayer(shape=(batch_size, in_size)))
    l_out = m.add_layer(DenseLayer(l_in, out_size, nonlinearity=softmax))
    m.bind_input(l_in, "inputs")
    m.bind_output(l_out, squared_error, "labels", "label", "mean")
    return m, l_in, l_out
Ejemplo n.º 5
0
def test_trained_model_serialization():
    batch_size = 2
    img_size = 10
    num_hid = 10
    m = _build_model(batch_size,img_size,num_hid)
    global_update_settings = ParamUpdateSettings(update=lasagne.updates.nesterov_momentum,learning_rate=0.1, momentum=0.2)
    trainer = Trainer(m,global_update_settings)

    pixels = np.random.rand(batch_size,img_size).astype(theano.config.floatX)
    emotions = np.random.rand(batch_size,num_hid).astype(theano.config.floatX)

    batch_dict = dict(
        # learning_rate_default=0.1,
        # momentum_default=0.5,
        pixels=pixels,
        emotions=emotions
    )
    outs = trainer.train_step(batch_dict)
    preds = m.predict(batch_dict)

    dirpath = tempfile.mkdtemp()
    filepath = os.path.join(dirpath, 'testtrainerout')
    m.save_model(filepath)
    m2 = Model('load')
    m2.load_model(filepath)
    shutil.rmtree(dirpath)

    global_update_settings = ParamUpdateSettings(update=lasagne.updates.nesterov_momentum,learning_rate=0.1, momentum=0.2)
    trainer2 = Trainer(m2,global_update_settings)
    preds2 = m.predict(batch_dict)
    outs  = trainer.train_step(batch_dict)
    outs2 = trainer2.train_step(batch_dict)
   
    assert m.outputs.keys()==m2.outputs.keys()

    for p,p2 in zip(preds.values(),preds2.values()):
        assert np.allclose(p,p2)

    for o,o2 in zip(outs,outs2):
        assert np.allclose(o,o2)
Ejemplo n.º 6
0
def _build_model():
    r = xnn.nonlinearities.rectify
    s = xnn.nonlinearities.scale
    m2 = Model("test convenience")
    l_in = m2.make_bound_input_layer((10, 1, 20, 10), "pixels")
    l_in2 = m2.make_bound_input_layer((10, 1, 20, 10), "pixels")
    l_conv = m2.add_layer(xnn.layers.Conv2DLayer(l_in2, 3, 4), name="l_conv")
    l_den = m2.make_dense_drop_stack(
        l_in, [60, 3, 2], [0.6, 0.4, 0.3], drop_type_list=["gauss", "gauss", "standard"], nonlin_list=[r, s(2.0), r]
    )
    l_reshp = m2.add_layer(xnn.layers.ReshapeLayer(l_conv, (10, -1)))
    l_mer = xnn.layers.ConcatLayer([l_reshp, l_den])
    m2.add_layer(l_mer, name="merger")
    l_bn = xnn.layers.BatchNormLayer(l_in2, nonlinearity=xnn.nonlinearities.sigmoid)
    m2.add_layer(l_bn)
    l_loc = xnn.layers.LocalLayer(l_bn, num_units=32, img_shape=(20, 10), local_filters=[(2, 1)], seed=121212)
    np.random.seed(int(time.time() * 10000 % 10000))
    m2.add_layer(l_loc)
    l_out = m2.add_layer(xnn.layers.DenseLayer(l_loc, num_units=2, nonlinearity=xnn.nonlinearities.linear), name="out")
    l_pr = m2.add_layer(xnn.layers.PReLULayer(l_out, xnn.init.Constant(0.25)))
    m2.bind_output(l_pr, xnn.objectives.squared_error, "age", "label", "mean", is_eval_output=True, scale=2)
    l_out2 = m2.add_layer(
        xnn.layers.DenseLayer(l_loc, num_units=2, nonlinearity=xnn.nonlinearities.softmax), name="out2"
    )
    m2.bind_output(l_out2, xnn.objectives.hinge_loss(threshold=2), "age", "label", "mean", is_eval_output=False)
    return m2
Ejemplo n.º 7
0
def test_serialization():
    m2 = _build_model()
    serialized_old = m2.to_dict()
    print "original model"
    pprint.pprint(serialized_old)

    m3 = Model("test serialize")
    m3.from_dict(serialized_old)
    print "model loaded from dict"
    pprint.pprint(m3.to_dict())
    data = dict(pixels=np.random.rand(10, 1, 20, 10).astype(theano.config.floatX))

    dirpath = tempfile.mkdtemp()
    filepath = os.path.join(dirpath, "testmodelout")

    m3.save_model(filepath)
    out3 = m3.predict(data, ["out", "out2"])

    m4 = Model("test convenience")
    m4.load_model(filepath)

    shutil.rmtree(dirpath)

    assert serialized_old == m4.to_dict()

    assert np.allclose(m4.layers["DenseLayer_2"].W.get_value(), m3.layers["DenseLayer_2"].W.get_value())
    assert ~np.allclose(m4.layers["DenseLayer_2"].W.get_value(), m2.layers["DenseLayer_2"].W.get_value())

    out4 = m4.predict(data, ["out", "out2"])

    print "out pre serialization"
    print out3["out"]
    print "out post serialization"
    print out4["out"]

    print "out2 pre serialization"
    print out3["out2"]
    print "out2 post serialization"
    print out4["out2"]
    assert np.allclose(out4["out"], out3["out"])
    assert np.allclose(out4["out2"], out3["out2"])
Ejemplo n.º 8
0
def _build_model(batch_size, img_size, num_hid):
    m = Model('test model cpu')
    l_in = m.add_layer(layers.InputLayer(shape=(batch_size, img_size)),
                       name="l_in")
    l_loc = m.add_layer(
        layers.LocalLayer(l_in,
                          num_units=3,
                          img_shape=(2, 5),
                          local_filters=[(2, 1)]))
    l_h1 = m.add_layer(layers.DenseLayer(l_loc, num_hid), name="l_h1")
    l_out = m.add_layer(layers.DenseLayer(l_h1, img_size), name="l_out")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_h1, xnn.objectives.kl_divergence, "emotions", "label",
                  "mean")
    m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
    return m
Ejemplo n.º 9
0
def test_aggregation():
    batch_size = 128
    img_size = 10
    num_out = 3

    m = Model('test model cpu')
    l_in = m.add_layer(layers.InputLayer(shape=(batch_size, img_size)),
                       name="l_in")
    l_out_mean = m.add_layer(layers.DenseLayer(l_in, num_out),
                             name="l_out_mean")
    l_out_sum = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_sum")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_out_mean, lasagne.objectives.squared_error, "emotions",
                  "label", "mean")
    m.bind_output(l_out_sum, lasagne.objectives.squared_error, "emotions",
                  "label", "sum")

    from pprint import pprint
    pprint(m.to_dict())
    global_update_settings = ParamUpdateSettings(
        update=lasagne.updates.nesterov_momentum,
        learning_rate=0.1,
        momentum=0.5)

    trainer = Trainer(m, global_update_settings)

    pixels = np.random.rand(batch_size, img_size).astype(theano.config.floatX)
    emotions = np.random.rand(batch_size, num_out).astype(theano.config.floatX)

    batch_dict = dict(
        # learning_rate_default=0.1,
        # momentum_default=0.5,
        pixels=pixels,
        emotions=emotions,
        exmotions=emotions.copy())
    outs = trainer.train_step(batch_dict)

    print "Aggregation test succeeded"
Ejemplo n.º 10
0
def _build_model():
    r = xnn.nonlinearities.rectify
    s = xnn.nonlinearities.scale
    m2 = Model('test convenience')
    l_in = m2.make_bound_input_layer((10, 1, 20, 10), 'pixels')
    l_in2 = m2.make_bound_input_layer((10, 1, 20, 10), 'pixels')
    l_conv = m2.add_layer(xnn.layers.Conv2DLayer(l_in2, 3, 4), name='l_conv')
    l_den = m2.make_dense_drop_stack(
        l_in, [60, 3, 2], [.6, .4, .3],
        drop_type_list=['gauss', 'gauss', 'standard'],
        nonlin_list=[r, s(2.), r])
    l_reshp = m2.add_layer(xnn.layers.ReshapeLayer(l_conv, (10, -1)))
    l_mer = xnn.layers.ConcatLayer([l_reshp, l_den])
    m2.add_layer(l_mer, name='merger')
    l_bn = xnn.layers.BatchNormLayer(l_in2,
                                     nonlinearity=xnn.nonlinearities.sigmoid)
    m2.add_layer(l_bn)
    l_loc = xnn.layers.LocalLayer(l_bn,
                                  num_units=32,
                                  img_shape=(20, 10),
                                  local_filters=[(2, 1)],
                                  seed=121212)
    np.random.seed(int(time.time() * 10000 % 10000))
    m2.add_layer(l_loc)
    l_out = m2.add_layer(xnn.layers.DenseLayer(
        l_loc, num_units=2, nonlinearity=xnn.nonlinearities.linear),
                         name='out')
    l_pr = m2.add_layer(xnn.layers.PReLULayer(l_out, xnn.init.Constant(.25)))
    m2.bind_output(l_pr,
                   xnn.objectives.squared_error,
                   'age',
                   'label',
                   'mean',
                   is_eval_output=True,
                   scale=2)
    l_out2 = m2.add_layer(xnn.layers.DenseLayer(
        l_loc, num_units=2, nonlinearity=xnn.nonlinearities.softmax),
                          name='out2')
    m2.bind_output(l_out2,
                   xnn.objectives.hinge_loss(threshold=2),
                   'age',
                   'label',
                   'mean',
                   is_eval_output=False)
    return m2
Ejemplo n.º 11
0
def test_serialization():
    m2 = _build_model()
    serialized_old = m2.to_dict()
    print "original model"
    pprint.pprint(serialized_old)

    m3 = Model('test serialize')
    m3.from_dict(serialized_old)
    print "model loaded from dict"
    pprint.pprint(m3.to_dict())
    data = dict(
        pixels=np.random.rand(10, 1, 20, 10).astype(theano.config.floatX))

    dirpath = tempfile.mkdtemp()
    filepath = os.path.join(dirpath, 'testmodelout')

    m3.save_model(filepath)
    out3 = m3.predict(data, ['out', 'out2'])

    m4 = Model('test convenience')
    m4.load_model(filepath)

    shutil.rmtree(dirpath)

    assert serialized_old == m4.to_dict()

    assert np.allclose(m4.layers['DenseLayer_2'].W.get_value(),
                       m3.layers['DenseLayer_2'].W.get_value())
    assert ~np.allclose(m4.layers['DenseLayer_2'].W.get_value(),
                        m2.layers['DenseLayer_2'].W.get_value())

    out4 = m4.predict(data, ['out', 'out2'])

    print 'out pre serialization'
    print out3['out']
    print 'out post serialization'
    print out4['out']

    print 'out2 pre serialization'
    print out3['out2']
    print 'out2 post serialization'
    print out4['out2']
    assert np.allclose(out4['out'], out3['out'])
    assert np.allclose(out4['out2'], out3['out2'])
Ejemplo n.º 12
0
def test_build_model():
    m = Model('test model')
    l_in = m.add_layer(xnn.layers.InputLayer(shape=(10, 200)), name="l_in")
    l_h1 = m.add_layer(xnn.layers.DenseLayer(l_in, 100), name="l_h1")
    l_out = m.add_layer(xnn.layers.DenseLayer(l_h1, 200), name="l_out")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_h1, xnn.objectives.categorical_crossentropy, "emotions",
                  "label", "mean")
    m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
Ejemplo n.º 13
0
def _build_model(batch_size,img_size,num_hid):
    m = Model('test model cpu')
    l_in = m.add_layer(layers.InputLayer(shape=(batch_size,img_size)), name="l_in")
    l_loc = m.add_layer(layers.LocalLayer(l_in,num_units=3,img_shape=(2,5),local_filters=[(2,1)]))
    l_h1 = m.add_layer(layers.DenseLayer(l_loc, num_hid), name="l_h1")
    l_out = m.add_layer(layers.DenseLayer(l_h1, img_size), name="l_out")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_h1, xnn.objectives.kl_divergence, "emotions", "label", "mean")
    m.bind_output(l_out, xnn.objectives.squared_error, "l_in", "recon", "mean")
    return m
Ejemplo n.º 14
0
def test_aggregation():
    batch_size = 128
    img_size = 10
    num_out = 3

    m = Model('test model cpu')
    l_in = m.add_layer(layers.InputLayer(shape=(batch_size,img_size)), name="l_in")
    l_out_mean = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_mean")
    l_out_sum = m.add_layer(layers.DenseLayer(l_in, num_out), name="l_out_sum")

    m.bind_input(l_in, "pixels")
    m.bind_output(l_out_mean, lasagne.objectives.squared_error, "emotions", "label", "mean")
    m.bind_output(l_out_sum, lasagne.objectives.squared_error, "emotions", "label", "sum")

    from pprint import pprint
    pprint(m.to_dict())
    global_update_settings = ParamUpdateSettings(update=lasagne.updates.nesterov_momentum,learning_rate=0.1, momentum=0.5)

    trainer = Trainer(m, global_update_settings)

    pixels = np.random.rand(batch_size,img_size).astype(theano.config.floatX)
    emotions = np.random.rand(batch_size,num_out).astype(theano.config.floatX)

    batch_dict = dict(
        # learning_rate_default=0.1,
        # momentum_default=0.5,
        pixels=pixels,
        emotions=emotions,
        exmotions=emotions.copy()
    )
    outs = trainer.train_step(batch_dict)

    print "Aggregation test succeeded"