def testExtractFeatures_multipleLayers(self): m = Model() img = toimage(np.random.random((100, 100)) * 255) s = BuildLayer(m, Layer.C1, m.MakeState(img)) fs = ExtractFeatures((Layer.S1, Layer.C1), [s]) self.assertEqual(fs.shape[0], 1) self.assertGreater(fs.shape[1], 0)
def testExtractFeatures_multipleImages(self): m = Model() img1 = toimage(np.random.random((100, 100)) * 255) img2 = toimage(np.random.random((100, 100)) * 255) s1 = BuildLayer(m, Layer.C1, m.MakeState(img1)) s2 = BuildLayer(m, Layer.C1, m.MakeState(img2)) fs = ExtractFeatures(Layer.S1, [s1, s2]) self.assertEqual(fs.shape[0], 2) self.assertGreater(fs.shape[1], 0)
def testComputeActivationMaps_noSaveAll(self): m = Model() img = toimage(np.random.random((100, 100)) * 255) exp = ExperimentData() exp.corpus.paths = [img] exp.extractor.model = Model() ComputeActivation(exp, Layer.C1, SinglecorePool(), save_all=False) states = exp.extractor.activation self.assertEqual(len(states), 1) self.assertEqual(len(states[0]), 1) self.assertIn(Layer.C1, states[0])
def testComputeActivationMaps_singleLayer(self): m = Model() img = toimage(np.random.random((100, 100)) * 255) exp = ExperimentData() exp.corpus.paths = [img] exp.extractor.model = Model() ComputeActivation(exp, Layer.C1, SinglecorePool(), save_all=True) states = exp.extractor.activation self.assertEqual(len(states), 1) self.assertEqual(len(states[0]), 4) for layer in (Layer.IMAGE, Layer.RETINA, Layer.S1, Layer.C1): self.assertIn(layer, states[0])
def SetModel(exp, model=None, params=None, **kw): """Add a model to an existing experiment. If `model` is passed, it is stored in the experiment's `extractor.model` attribute. Otherwise, the parameters are created/updated, and used to create a new :class:`Model` object. :type model: :class:`Model` :param model: Existing model object to use. :type params: :class:`Params` :param params: Existing model parameters to use. All remaining keyword arguments are treated as model parameters and overwrite values in the (created or passed) `params` argument. This allows the set of parameters to be specified in short-hand as >>> SetModel(param1=1, param2=2) without creating a :class:`Params` object. """ from glimpse.models.ml import Model, Params if model is None: if params is None: params = Params() for k, v in kw.items(): setattr(params, k, v) model = Model(params) exp.extractor.model = model
def testShuffledAlg_default(self): def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp model = Model() # These patches don't match C1 structure, but that's fine. We just want to # test that shuffling the imprinted array data works. patches_per_shape = [ np.random.random((3, 4, w, w)) for w in model.params.s2_kernel_widths ] f = RecordedFunctionCall((patches_per_shape, None)) with MonkeyPatch(prototype_algorithms, 'SamplePatchesFromImages', f): alg = ShuffledAlg() patches_per_shape_ = alg( 11, # anything numeric model, make_training_exp, None, None) self.assertEqual(len(patches_per_shape_), len(model.params.s2_kernel_widths)) for ps1, ps2 in zip(patches_per_shape, patches_per_shape_): for p1, p2 in zip(ps1, ps2): self.assertSequenceEqual(sorted(p1.flat), sorted(p2.flat))
def testImprintAlg_withNorm(self): locs_per_shape = 'LOCS' # some value to test for def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp model = Model(Params(s2_kernel_widths=(7, 9), s2_operation='NormRbf')) # These patches don't match C1 structure, but that's fine. We just want to # test that normalizing multi-dimensional arrays works when imprinting. patches_per_shape = [np.random.random((3, 4, w, w)) for w in (7, 9)] f = RecordedFunctionCall((patches_per_shape, locs_per_shape)) with MonkeyPatch(prototype_algorithms, 'SamplePatchesFromImages', f): alg = ImprintAlg() patches_per_shape_ = alg( 11, # anything numeric model, make_training_exp, None, None) self.assertEqual(len(patches_per_shape_), 2) self.assertTrue( all((ps1 == ps2).all() for ps1, ps2 in zip(patches_per_shape, patches_per_shape_))) self.assertEqual(locs_per_shape, alg.locations)
def testImprintAlg_recordLocations(self): locs_per_shape = 'LOCS' # some value to test for def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp patches_per_shape = ['PATCHES%d' % i for i in range(2)] # two kernel widths model = Model(Params(s2_kernel_widths=(7, 9))) f = RecordedFunctionCall((patches_per_shape, locs_per_shape)) with MonkeyPatch(prototype_algorithms, 'SamplePatchesFromImages', f): alg = ImprintAlg(True) patches_per_shape_ = alg( 11, # anything numeric model, make_training_exp, None, None) self.assertEqual(len(patches_per_shape_), 2) self.assertSequenceEqual(patches_per_shape, patches_per_shape_) #~ self.assertTrue(all((ps1 == ps2).all() for ps1,ps2 in zip(patches_per_shape, #~ patches_per_shape_))) self.assertEqual(locs_per_shape, alg.locations)
def testResolveLayers(self): exp = ExperimentData() exp.extractor.model = Model() ResolveLayers(exp, 'C2') == [Layer.C2] ResolveLayers(exp, Layer.C2) == [Layer.C2] ResolveLayers(exp, ('C1', 'C2')) == [Layer.C1, Layer.C2] ResolveLayers(exp, ('C1', Layer.C2)) == [Layer.C1, Layer.C2] ResolveLayers(exp, (Layer.C1, Layer.C2)) == [Layer.C1, Layer.C2]
def _buildHelper(self, exp, layers, **kw): exp.extractor.model = Model() # Provide 12 features per instance extractor = lambda *args, **kw: np.random.random( (self.NUM_INSTANCES, 12)) exp.corpus.paths = ["img%d.jpg" % x for x in range(self.NUM_INSTANCES)] exp.corpus.labels = np.array([1] * (self.NUM_INSTANCES / 2) + [2] * (self.NUM_INSTANCES / 2)) with MonkeyPatch(experiment, 'ExtractFeatures', extractor): CrossValidateClassifier(exp, layers, **kw) self.assertEqual(len(exp.evaluation), 1)
def testUniformAlg_default(self): num_prototypes = 11 model = Model() alg = UniformAlg() patches_per_shape = alg(num_prototypes, model, None, None, None) self.assertEqual(len(patches_per_shape), len(model.params.s2_kernel_widths)) for ps, kshape in zip(patches_per_shape, model.params.s2_kernel_shapes): self.assertEqual(ps.dtype, ACTIVATION_DTYPE) self.assertSequenceEqual(ps.shape, (num_prototypes, ) + kshape)
def testUniformAlg_withNorm(self): num_prototypes = 11 model = Model(Params(s2_kernel_widths=(7, 9), s2_operation='NormRbf')) alg = UniformAlg() patches_per_shape = alg(num_prototypes, model, None, None, None) self.assertEqual(len(patches_per_shape), len(model.params.s2_kernel_widths)) for ps, kshape in zip(patches_per_shape, model.params.s2_kernel_shapes): self.assertEqual(ps.dtype, ACTIVATION_DTYPE) self.assertSequenceEqual(ps.shape, (num_prototypes, ) + kshape)
def testGetActivity_strImage(self): image_name = 'fake-image-name' layer = 'image' layer_data = 'IMAGE-DATA' exp = ExperimentData() exp.extractor.model = Model() f = RecordedFunctionCall(State({layer:layer_data})) with MonkeyPatch(misc, 'BuildLayer', f): data = misc.GetActivity(exp, image_name, layer) self.assertEqual(data, layer_data) self.assertTrue(f.called) self.assertEqual(f.args[1], layer) self.assertEqual(f.args[2][Layer.SOURCE].image_path, image_name)
def testUniformAlg_customLimits(self): num_prototypes = 11 low = -1 high = 10 model = Model(Params(s2_kernel_widths=(7, ))) f = RecordedFunctionCall('PATCHES') with MonkeyPatch('glimpse.prototypes', 'UniformRandom', f): alg = UniformAlg(low=low, high=high) patches_per_shape = alg(num_prototypes, model, None, None, None) self.assertEqual(patches_per_shape, ['PATCHES'] * len(model.params.s2_kernel_widths)) self.assertTrue(f.called) self.assertSequenceEqual(f.args[0:1] + f.args[2:4], (num_prototypes, low, high))
def testGetActivity_intImage_withActivity_noLayer(self): image_name = 0 image_path = 'fake-image-path' layer = 'image' layer_data = 'IMAGE-DATA' exp = ExperimentData() exp.extractor.model = Model() exp.corpus.paths = [image_path] exp.extractor.activation = [State()] f = RecordedFunctionCall(State({layer:layer_data})) with MonkeyPatch(misc, 'BuildLayer', f): data = misc.GetActivity(exp, image_name, layer) self.assertEqual(data, layer_data) self.assertTrue(f.called) self.assertEqual(f.args[1], layer)
def test_noTrainingExp(self): # no call to make_training_exp(), so corpus information is never pulled num_prototypes = 11 exp = ExperimentData() m = exp.extractor.model = Model() ks = [ np.random.random((num_prototypes, ) + kshape).astype(ACTIVATION_DTYPE) for kshape in m.params.s2_kernel_shapes ] algorithm = lambda *args, **kw: ks MakePrototypes(exp, num_prototypes, algorithm, pool=SinglecorePool()) for k1, k2 in zip(ks, exp.extractor.model.s2_kernels): self.assertTrue((k1 == k2).all()) self.assertIsNone(exp.extractor.training_set) self.assertEquals(algorithm, exp.extractor.prototype_algorithm)
def _buildHelper(self, exp, layers, **kw): exp.extractor.model = Model() # Provide 12 features per instance extractor = lambda *args, **kw: np.random.random( (self.NUM_INSTANCES, 12)) exp.corpus.paths = ["img%d.jpg" % x for x in range(self.NUM_INSTANCES)] exp.corpus.labels = np.array([1] * (self.NUM_INSTANCES / 2) + [2] * (self.NUM_INSTANCES / 2)) exp.extractor.activation = list() # fake activation list with MonkeyPatch(experiment, 'ExtractFeatures', extractor): TrainAndTestClassifier(exp, layers, **kw) self.assertEqual(len(exp.evaluation), 1) eval_ = exp.evaluation[0] self.assertIn('classifier', eval_.results) self.assertIn('score', eval_.results) self.assertIn('score_func', eval_.results) self.assertIn('training_score', eval_.results)
def testImprintAlg_default(self): def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp model = Model() f = RecordedFunctionCall(('PATCHES', None)) with MonkeyPatch(prototype_algorithms, 'SamplePatchesFromImages', f): alg = ImprintAlg() patches_per_shape_ = alg( 11, # anything numeric model, make_training_exp, None, None) self.assertEqual(patches_per_shape_, 'PATCHES')
def test_SimpleLearningAlg_default(self): num_prototypes = 11 patches_per_shape = 'PATCHES' learner = 'LEARNER' model = Model() def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp f = RecordedFunctionCall(patches_per_shape) with MonkeyPatch('glimpse.prototypes', 'SampleAndLearnPatches', f): alg = _SimpleLearningAlg(learner) patches_per_shape_ = alg(num_prototypes, model, make_training_exp, None, None) self.assertSequenceEqual(f.args[2:4], (learner, num_prototypes))
def testLoadPrototypes(self): m = Model() ks = [ np.random.random((10, ) + kshape).astype(ACTIVATION_DTYPE) for kshape in m.params.s2_kernel_shapes ] exp = ExperimentData() exp.extractor.model = m opts = MakeOpts() opts.extractor.no_activity = True with TempFile() as path: with open(path, 'w') as fh: pickle.dump(ks, fh) opts.extractor.prototypes.path = path CliWithActivity(opts, exp, SinglecorePool()) self.assertEqual(len(exp.extractor.model.s2_kernels), len(ks)) for s2_k, k in zip(exp.extractor.model.s2_kernels, ks): self.assertTrue(np.all(s2_k == k))
def testSetModel(self): # default exp = ExperimentData() SetModel(exp) self.assertIsNotNone(exp.extractor.model) # from model model = Model() exp = ExperimentData() SetModel(exp, model=model) self.assertEqual(exp.extractor.model, model) # from params params = Params() exp = ExperimentData() SetModel(exp, params=params) self.assertEqual(exp.extractor.model.params, params) # from keyword params exp = ExperimentData() SetModel(exp, image_resize_length=10) self.assertEqual(exp.extractor.model.params.image_resize_length, 10)
def test_algorithmLocations(self): num_prototypes = 11 exp = ExperimentData() m = exp.extractor.model = Model() ks = [ np.random.random((11, ) + kshape) for kshape in m.params.s2_kernel_shapes ] algorithm = lambda *xs: ks # Use image indices 1 and 3 for algorithm, relative to training set. This is # indices 5 and 7, relative to full image set. algorithm.locations = [np.array([[1,0,0,0],[3,0,0,0]])] \ * len(m.params.s2_kernel_shapes) # no call to make_training_exp(), so need to set it manually exp.corpus.training_set = np.array([False] * 4 + [True] * 4, dtype=bool) MakePrototypes(exp, num_prototypes, algorithm, pool=SinglecorePool()) self.assertEqual(len(algorithm.locations), len(ks)) for locs in algorithm.locations: # algorithm used image indices 4-7 (relative to full image set) self.assertTrue((locs[:, 0] == (5, 7)).all())
def test_SimpleLearningAlg_withNorm(self): num_prototypes = 3 # These patches don't match C1 structure, but that's fine. We just want to # test normalization of patch data. patches_per_shape = [np.random.random((3, 4, w, w)) for w in (7, 9)] learner = 'LEARNER' model = Model() def make_training_exp(): exp = ExperimentData() exp.corpus.paths = list() return exp f = RecordedFunctionCall(patches_per_shape) with MonkeyPatch('glimpse.prototypes', 'SampleAndLearnPatches', f): alg = _SimpleLearningAlg(learner) patches_per_shape_ = alg(num_prototypes, model, make_training_exp, None, None) self.assertEqual(len(patches_per_shape_), len(patches_per_shape)) for ps, kshape in zip(patches_per_shape_, model.params.s2_kernel_shapes): self.assertSequenceEqual(ps.shape, (num_prototypes, ) + kshape)
def testEndToEnd(self): NUM_PROTOS = 10 exp = ExperimentData() exp.extractor.model = Model() with TempDir() as root: MakeCorpusWithImages(root, a=('a1.png', 'a2.png'), b=('b1.png', 'b2.png')) SetCorpus(exp, root) MakePrototypes(exp, NUM_PROTOS, 'imprint') ComputeActivation(exp, Layer.C2, SinglecorePool()) TrainAndTestClassifier(exp, Layer.C2) self.assertEqual(len(exp.corpus.paths), 4) self.assertTrue((exp.corpus.labels == (0, 0, 1, 1)).all()) self.assertEqual(exp.extractor.training_set.sum(), 2) self.assertEqual(len(exp.extractor.model.s2_kernels[0]), NUM_PROTOS) st = exp.extractor.activation[0] self.assertEqual(len(exp.extractor.activation), 4) self.assertIn(Layer.C2, st) self.assertEqual(len(st[Layer.C2][0]), NUM_PROTOS) self.assertEqual(len(exp.evaluation), 1) self.assertIsNotNone(exp.evaluation[0].results.score)