def testCreateImage(self):
   with TempFile(suffix = '.jpg') as path:
     toimage(np.random.random((256, 128)) * 255).save(path)
     source = InputSource(path)
     img = source.CreateImage()
     self.assertIsNotNone(img)
     self.assertEqual(img.size, (128, 256))
     self.assertEqual(img.fp.name, path)
Beispiel #2
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)
Beispiel #3
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_multipleLayers(self):
   m = Model()
   img1 = toimage(np.random.random((100,100)) * 255)
   img2 = toimage(np.random.random((100,100)) * 255)
   exp = ExperimentData()
   exp.corpus.paths = [img1, img2]
   exp.extractor.model = Model()
   ComputeActivation(exp, Layer.C1, SinglecorePool(), save_all=True)
   states = exp.extractor.activation
   self.assertEqual(len(states), 2)
   for state in states:
     self.assertEqual(len(state), 4)
     for layer in (Layer.IMAGE, Layer.RETINA, Layer.S1, Layer.C1):
       self.assertIn(layer, state)
 def testComputeActivationMaps_multipleLayers(self):
     m = Model()
     img1 = toimage(np.random.random((100, 100)) * 255)
     img2 = toimage(np.random.random((100, 100)) * 255)
     exp = ExperimentData()
     exp.corpus.paths = [img1, img2]
     exp.extractor.model = Model()
     ComputeActivation(exp, Layer.C1, SinglecorePool(), save_all=True)
     states = exp.extractor.activation
     self.assertEqual(len(states), 2)
     for state in states:
         self.assertEqual(len(state), 4)
         for layer in (Layer.IMAGE, Layer.RETINA, Layer.S1, Layer.C1):
             self.assertIn(layer, state)
Beispiel #6
0
 def test_noResize(self):
   data = np.random.random((100,101)) * 255
   data = data.astype(np.uint8)
   img = toimage(data)
   params = Params(image_resize_method = ResizeMethod.NONE)
   img2 = PrepareImage(img, params)
   self.assertEqual(img, img2)
Beispiel #7
0
 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)
Beispiel #8
0
 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)
Beispiel #9
0
 def testBuild_C1FromImage(self):
   model = Model(Params(retina_enabled=True))
   img = np.random.random((100,100)).astype(ACTIVATION_DTYPE)
   state = BuildLayer(model, Layer.C1, model.MakeState(toimage(img)))
   for layer in (Layer.IMAGE, Layer.RETINA, Layer.S1, Layer.C1):
     self.assertIn(layer, state)
     self.assertIsNotNone(state[layer])
Beispiel #10
0
 def testBuild_C1FromImage(self):
     model = Model(Params(retina_enabled=True))
     img = np.random.random((100, 100)).astype(ACTIVATION_DTYPE)
     state = BuildLayer(model, Layer.C1, model.MakeState(toimage(img)))
     for layer in (Layer.IMAGE, Layer.RETINA, Layer.S1, Layer.C1):
         self.assertIn(layer, state)
         self.assertIsNotNone(state[layer])
 def test_noResize(self):
     data = np.random.random((100, 101)) * 255
     data = data.astype(np.uint8)
     img = toimage(data)
     params = Params(image_resize_method=ResizeMethod.NONE)
     img2 = PrepareImage(img, params)
     self.assertEqual(img, img2)
Beispiel #12
0
 def _test(self, num_kernels, num_images, normalize):
     p = Params(num_scales=3)
     model = ReshapedModel(p)
     kernel_sizes = (5, 7, 11)
     states = [
         model.MakeState(toimage(np.random.random((100, 100))))
         for _ in range(num_images)
     ]
     kernels, locs = SamplePatchesFromImages(model,
                                             ReshapedLayer.RESHAPED_IMAGE,
                                             kernel_sizes,
                                             num_kernels,
                                             states,
                                             SinglecorePool(),
                                             normalize=normalize)
     self.assertEqual(len(kernels), len(kernel_sizes))
     self.assertEqual(len(locs), len(kernel_sizes))
     for idx, ksize in enumerate(kernel_sizes):
         self.assertEqual(len(kernels[idx]), num_kernels)
         self.assertEqual(len(locs[idx]), num_kernels)
         for k, l in zip(kernels[idx], locs[idx]):
             self.assertEqual(k.ndim, 2)
             self.assertEqual(len(l), 4)
             if normalize:
                 self.assertAlmostEqual(
                     np.linalg.norm(k), 1, places=5
                 )  # don't be too strict with floating-point comparison
 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_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])
Beispiel #15
0
def ToImage(data):
    """Create an image from a 2D array of model activity.

  :param data: Single scale of one layer of model activity, with elements in the
     range [0,1].
  :type data: 2D ndarray of floats
  :rtype: Image
  :returns: Greyscale image of layer activity.

  """
    if not (isinstance(data, np.ndarray) and data.ndim == 2 and \
        data.dtype == ACTIVATION_DTYPE):
        raise ValueError("Invalid image layer data")
    data = data.copy()
    data *= 255
    img = toimage(data.astype(np.uint8))
    return img
Beispiel #16
0
def ToImage(data):
  """Create an image from a 2D array of model activity.

  :param data: Single scale of one layer of model activity, with elements in the
     range [0,1].
  :type data: 2D ndarray of floats
  :rtype: Image
  :returns: Greyscale image of layer activity.

  """
  if not (isinstance(data, np.ndarray) and data.ndim == 2 and \
      data.dtype == ACTIVATION_DTYPE):
    raise ValueError("Invalid image layer data")
  data = data.copy()
  data *= 255
  img = toimage(data.astype(np.uint8))
  return img
Beispiel #17
0
 def _test(self, num_kernels, num_images, normalize):
   p = Params(num_scales = 3)
   model = ReshapedModel(p)
   kernel_sizes = (5, 7, 11)
   states = [ model.MakeState(toimage(np.random.random((100,100))))
       for _ in range(num_images) ]
   kernels, locs = SamplePatchesFromImages(model, ReshapedLayer.RESHAPED_IMAGE,
       kernel_sizes, num_kernels, states, SinglecorePool(),
       normalize = normalize)
   self.assertEqual(len(kernels), len(kernel_sizes))
   self.assertEqual(len(locs), len(kernel_sizes))
   for idx, ksize in enumerate(kernel_sizes):
     self.assertEqual(len(kernels[idx]), num_kernels)
     self.assertEqual(len(locs[idx]), num_kernels)
     for k, l in zip(kernels[idx], locs[idx]):
       self.assertEqual(k.ndim, 2)
       self.assertEqual(len(l), 4)
       if normalize:
         self.assertAlmostEqual(np.linalg.norm(k), 1,
             places = 5)  # don't be too strict with floating-point comparison