def test_non5d_data_raises(): with pytest.raises(IteratorValidationError): _ = Flip(Undivided(default=np.random.randn(2, 3, 1, 2)), prob_dict={'default': 1}) with pytest.raises(IteratorValidationError): _ = Pad(Undivided(default=np.random.randn(2, 3, 1, 2)), size_dict={'default': 1}) with pytest.raises(IteratorValidationError): _ = RandomCrop(Undivided(default=np.random.randn(2, 3, 1, 2)), shape_dict={'default': (1, 1)})
def test_learn_xor_function(): # set up the network inp = Input(out_shapes={ 'default': ('T', 'B', 2), 'targets': ('T', 'B', 1) }) error_func = BinomialCrossEntropy() (inp >> FullyConnected(2, activation='sigmoid') >> FullyConnected( 1, activation='sigmoid', name='OutLayer') >> error_func >> Loss()) net = Network.from_layer(inp - 'targets' >> 'targets' - error_func) # net.set_handler(PyCudaHandler()) net.initialize(Gaussian(1.0), seed=42) # high weight-init needed # print(net.buffer.parameters) # set up the trainer tr = Trainer(SgdStepper(learning_rate=4.0), verbose=False) tr.add_hook(StopAfterEpoch(300)) # generate the data data = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]]).reshape( (1, 4, 2)) targets = np.array([0., 1., 1., 0.]).reshape((1, 4, 1)) tr.train(net, Undivided(default=data, targets=targets)) out = net.buffer.OutLayer.outputs.default success = np.all(np.round(out) == targets) if not success: print('Network output:', out.flatten()) print('Rounded output:', np.round(out.flatten())) print('Targets :', targets.flatten()) raise AssertionError("Network training did not succeed.") assert min(tr.logs['rolling_training']['total_loss']) < 0.5
def test_forward_pass_with_missing_data(): it = Undivided(default=np.random.randn(3, 2, 4))(simple_net.handler) with pytest.raises(KeyError): for _ in run_network(simple_net, it): pass for _ in run_network(simple_net, it, all_inputs=False): pass
def test_undivided_default(): input_data = np.zeros((2, 3, 5)) targets = np.ones((2, 3, 1)) iter = Undivided(my_data=input_data, my_targets=targets)(default_handler) x = next(iter) assert np.all(x['my_data'] == input_data) assert np.all(x['my_targets'] == targets) with pytest.raises(StopIteration): next(iter)
def test_undivided_named_targets(): input_data = np.zeros((2, 3, 5)) targets1 = np.ones((2, 3, 1)) targets2 = np.ones((2, 3, 1)) iter = Undivided(my_data=input_data, targets1=targets1, targets2=targets2)(default_handler) x = next(iter) assert np.all(x['my_data'] == input_data) assert np.all(x['targets1'] == targets1) assert np.all(x['targets2'] == targets2) with pytest.raises(StopIteration): next(iter)
def test_pad(): a = np.random.randn(2, 3, 5, 5, 4) b = np.random.randn(2, 3, 4, 4, 1) c = np.random.randn(2, 3, 1) iterator = Undivided(default=a, secondary=b, targets=c) pad = Pad(iterator, size_dict={'default': 1})(default_handler) x = next(pad) assert set(x.keys()) == set(iterator.data.keys()) assert x['default'].shape == (2, 3, 7, 7, 4) assert x['secondary'].shape == (2, 3, 4, 4, 1) assert x['targets'].shape == (2, 3, 1) assert np.allclose(x['default'][:, :, 1:-1, 1:-1, :], a) assert np.allclose(x['secondary'], b) assert np.allclose(x['targets'], c)
def test_random_crop(): a = np.random.randn(1, 3, 5, 5, 4) b = np.random.randn(1, 3, 4, 4, 1) c = np.random.randn(1, 3, 1) iterator = Undivided(default=a, secondary=b, targets=c) crop = RandomCrop(iterator, shape_dict={'default': (3, 3), 'secondary': (2, 2) })(default_handler) x = next(crop) assert set(x.keys()) == set(iterator.data.keys()) assert x['default'].shape == (1, 3, 3, 3, 4) assert x['secondary'].shape == (1, 3, 2, 2, 1) assert x['targets'].shape == (1, 3, 1) assert np.allclose(x['targets'], c)
def test_flip(): a = np.random.randn(2, 3, 5, 5, 4) a_copy = a.copy() b = np.random.randn(2, 3, 4, 4, 1) c = np.random.randn(2, 3, 1) iterator = Undivided(default=a, secondary=b, targets=c) flip = Flip(iterator, prob_dict={'default': 1.0})(default_handler) x = next(flip) assert set(x.keys()) == set(iterator.data.keys()) assert x['default'].shape == (2, 3, 5, 5, 4) assert x['secondary'].shape == (2, 3, 4, 4, 1) assert x['targets'].shape == (2, 3, 1) assert np.allclose(x['default'][:, :, :, ::-1, :], a_copy) assert np.allclose(x['secondary'], b) assert np.allclose(x['targets'], c)
# coding=utf-8 from __future__ import division, print_function, unicode_literals import numpy as np import pytest from brainstorm.data_iterators import (AddGaussianNoise, Flip, Minibatches, Pad, RandomCrop, Undivided) from brainstorm.handlers import default_handler from brainstorm.handlers._cpuop import _crop_images from brainstorm.utils import IteratorValidationError # ######################### Nested Iterators ################################## from brainstorm.data_iterators import _calculate_lengths_from_mask inner = Undivided(default=np.random.randn(2, 3, 1, 2, 2)) # TODO: Test AddGaussianNoise def test_flip_dict_mismatch_raises(): with pytest.raises(IteratorValidationError): _ = Flip(inner, prob_dict={'images': 1}) with pytest.raises(IteratorValidationError): _ = Flip(inner, prob_dict={'default': 10}) def test_flip(): a = np.random.randn(2, 3, 5, 5, 4) a_copy = a.copy() b = np.random.randn(2, 3, 4, 4, 1)