예제 #1
0
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   gabor = GaborRegion((144, 192), rotations=3, 
                       initial_wavelength=3, 
                       num_wavelengths=2)
   
   # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork((144,192), num_regions=1, 
                               input_region = gabor)
   and_region = self.network.regions[1]
   classifier = self.network.get_classifier()
   
   i = 0
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     gabor.do_inference(numpy.array(image))
     active_nodes = gabor.get_active_nodes()
     pos = and_region.create_node((0,0), cxns = active_nodes)
     classifier.create_node(category, pos)
     i += 1
     if i % self.PRINT_INCR == 0: print "Iter:", i
     
   and_region.prepare_for_inference()
   classifier.prepare_for_inference()
   
   num_cxns = and_region.get_num_cxns() + classifier.get_num_cxns()
   print "Number of connections:", num_cxns
   elapsed = (time.time() - start)
   print "Training time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Training complete", "green")
예제 #2
0
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   self.image_shape = (144, 192)
   
   gabor = GaborRegion(self.image_shape, rotations=3, 
                       initial_wavelength=3, 
                       num_wavelengths=2)
   
   # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork((144,192), num_regions=2, 
                               input_region = gabor)
   f1 = self.network.regions[1]
   f2 = self.network.regions[2]
   classifier = self.network.get_classifier()
   
   self.gabor_acts = gabor.precompute_image_activations(self.image_iter)
   windows = self.get_windows()
   
   for window in windows:
     
   
   
   self.network.prepare_for_inference(1)
   elapsed = (time.time() - start)
   
   total_cxns = 0
   for i, r in enumerate(self.network.regions[1:]):
     num_cxns = r.get_num_cxns()
     print "Region %s cxns: %s" % (i, num_cxns)
     total_cxns += num_cxns
   
   print "Total connections:", total_cxns
   print "Training time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Training complete", "green")
     
 def test(self):
   """ Test that every image is correctly recognized. """
   Experiment.test(self)
   start = time.time()
   
   classifier = self.network.get_classifier()
   i = 0
   while self.image_iterator.has_next():
     image, category = self.image_iterator.next()
     recognized = self.network.do_inference(numpy.array(image), category)
     if not recognized:
       active_cats = classifier.get_active_categories()
       print colored("Failed: " + category + " recognized as "+repr(active_cats), 'red')
     i += 1
     if i % self.PRINT_INCR == 0: print "Iter:", i
   
   elapsed = (time.time() - start)
   print "Testing time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Testing complete", "green")
예제 #3
0
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   num_images = len(self.image_iterator)
   print "Num images:", num_images
   assert num_images > 0
   
   gabor = self.gabor_region = GaborRegion((144, 192), rotations=3, 
                                           initial_wavelength=3, 
                                           num_wavelengths=2)
   
   kmeans = KmeansRegion(max(1,int(float(num_images) * self.compression)),
                         self.window_sampler)
   and_region = AndOrRegion(kmeans.image_shape, num_images)
   
   
   # Regions = [ GaborRegion, Kmeans, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork([gabor, kmeans, and_region])
   classifier = self.network.get_classifier()
   
   self.categories = []
   print "Extracting windows..."
   i = 0
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     self.categories.append(category)
     gabor.do_inference(numpy.array(image))
     kmeans.do_learning()
     i += 1
     
   print "Training K-means..."
   kmeans.prepare_for_inference()
   
   # Send all of the images through the feature learner to save the image
   # activations.
   print "Storing classifier features..."
   self.image_iterator.reset()
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     gabor.do_inference(numpy.array(image))
     kmeans.do_inference()
     cxns = kmeans.get_active_nodes()
     pos = and_region.create_node((0,0), cxns = cxns)
     classifier.create_node(category, pos)
   
   print "Preparing for inference..."
   self.network.prepare_for_inference(2)
예제 #4
0
class WindowGridExperiment(Experiment):
  """ Features are learned within predetermined windows. Each image attempts to
  learn a single feature within the window, and this is done greedily. Windows
  learn features independently from each other. """
  
  def get_windows(self):
    """ Returns a list of windows that fill the image. """
    windows = []
    return windows
  
  def get_window_gabor_acts(self):
    """ Returns """

  def train(self):
    """ Store a copy of every image in the iterator. """
    Experiment.train(self)
    start = time.time()
    
    self.image_shape = (144, 192)
    
    gabor = GaborRegion(self.image_shape, rotations=3, 
                        initial_wavelength=3, 
                        num_wavelengths=2)
    
    # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
    self.network = AndOrNetwork((144,192), num_regions=2, 
                                input_region = gabor)
    f1 = self.network.regions[1]
    f2 = self.network.regions[2]
    classifier = self.network.get_classifier()
    
    self.gabor_acts = gabor.precompute_image_activations(self.image_iter)
    windows = self.get_windows()
    
    for window in windows:
      
    
    
    self.network.prepare_for_inference(1)
    elapsed = (time.time() - start)
    
    total_cxns = 0
    for i, r in enumerate(self.network.regions[1:]):
      num_cxns = r.get_num_cxns()
      print "Region %s cxns: %s" % (i, num_cxns)
      total_cxns += num_cxns
    
    print "Total connections:", total_cxns
    print "Training time:", elapsed
    print "Time per category:", (elapsed / i)
    print colored("Training complete", "green")
      
  def test(self):
    """ Test that every image is correctly recognized. """
    Experiment.test(self)
    start = time.time()
    
    classifier = self.network.get_classifier()
    i = 0
    while self.image_iterator.has_next():
      image, category = self.image_iterator.next()
      recognized = self.network.do_inference(numpy.array(image), category)
      if not recognized:
        active_cats = classifier.get_active_categories()
        print colored("Failed: " + category + " recognized as "+repr(active_cats), 'red')
      i += 1
      if i % self.PRINT_INCR == 0: print "Iter:", i
    
    elapsed = (time.time() - start)
    print "Testing time:", elapsed
    print "Time per category:", (elapsed / i)
    print colored("Testing complete", "green")
      


def run_experiment():
  from and_or_images.tools.image_iterators.aloi_iterator import AloiIterator
  
  image_iter = AloiIterator( #selected_categories = "145 15 2 3 4".split(),
                             num_categories = 1000,
                             images_per_category = 72 )
  experiment = BruteForceExperiment(image_iter)
  experiment.run()
  
  
if __name__ == '__main__':
  run_experiment()
예제 #5
0
class BruteForceExperiment(Experiment):
  """ This experiment does bruce-force recognition, storing exact copies of
  every image and attempting to match them one by one during inference. """
  PRINT_INCR = 500
  
  def train(self):
    """ Store a copy of every image in the iterator. """
    Experiment.train(self)
    start = time.time()
    
    gabor = GaborRegion((144, 192), rotations=3, 
                        initial_wavelength=3, 
                        num_wavelengths=2)
    
    # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
    self.network = AndOrNetwork((144,192), num_regions=1, 
                                input_region = gabor)
    and_region = self.network.regions[1]
    classifier = self.network.get_classifier()
    
    i = 0
    while self.image_iterator.has_next():
      image, category, img_idx = self.image_iterator.next()
      gabor.do_inference(numpy.array(image))
      active_nodes = gabor.get_active_nodes()
      pos = and_region.create_node((0,0), cxns = active_nodes)
      classifier.create_node(category, pos)
      i += 1
      if i % self.PRINT_INCR == 0: print "Iter:", i
      
    and_region.prepare_for_inference()
    classifier.prepare_for_inference()
    
    num_cxns = and_region.get_num_cxns() + classifier.get_num_cxns()
    print "Number of connections:", num_cxns
    elapsed = (time.time() - start)
    print "Training time:", elapsed
    print "Time per category:", (elapsed / i)
    print colored("Training complete", "green")
      
  def test(self):
    """ Test that every image is correctly recognized. """
    Experiment.test(self)
    start = time.time()
    
    classifier = self.network.get_classifier()
    i = 0
    while self.image_iterator.has_next():
      image, category, img_idx = self.image_iterator.next()
      recognized = self.network.do_inference(numpy.array(image), category)
      if not recognized:
        active_cats = classifier.get_winning_category()
        print colored("Failed: " + category + " recognized as "+active_cats, 'red')
        print classifier.node_values[:10]
      i += 1
      if i % self.PRINT_INCR == 0: print "Iter:", i
    
    elapsed = (time.time() - start)
    print "Testing time:", elapsed
    print "Time per category:", (elapsed / i)
    print colored("Testing complete", "green")
예제 #6
0
class KmeansCompressionExperiment(Experiment):
  """ This experiment uses K-means to learn features. 
  The output is a plot of recognition accuracy versus compression.
  """
  PRINT_INCR = 100
  
  def __init__(self, image_iter, window_sampler):
    Experiment.__init__(self, image_iter)
    self.window_sampler = window_sampler
  
  #@profile
  def train(self):
    """ Store a copy of every image in the iterator. """
    Experiment.train(self)
    start = time.time()
    
    num_images = len(self.image_iterator)
    print "Num images:", num_images
    assert num_images > 0
    
    gabor = self.gabor_region = GaborRegion((144, 192), rotations=3, 
                                            initial_wavelength=3, 
                                            num_wavelengths=2)
    
    kmeans = KmeansRegion(max(1,int(float(num_images) * self.compression)),
                          self.window_sampler)
    and_region = AndOrRegion(kmeans.image_shape, num_images)
    
    
    # Regions = [ GaborRegion, Kmeans, AndRegion, OrRegion (classifier) ]
    self.network = AndOrNetwork([gabor, kmeans, and_region])
    classifier = self.network.get_classifier()
    
    self.categories = []
    print "Extracting windows..."
    i = 0
    while self.image_iterator.has_next():
      image, category, img_idx = self.image_iterator.next()
      self.categories.append(category)
      gabor.do_inference(numpy.array(image))
      kmeans.do_learning()
      i += 1
      
    print "Training K-means..."
    kmeans.prepare_for_inference()
    
    # Send all of the images through the feature learner to save the image
    # activations.
    print "Storing classifier features..."
    self.image_iterator.reset()
    while self.image_iterator.has_next():
      image, category, img_idx = self.image_iterator.next()
      gabor.do_inference(numpy.array(image))
      kmeans.do_inference()
      cxns = kmeans.get_active_nodes()
      pos = and_region.create_node((0,0), cxns = cxns)
      classifier.create_node(category, pos)
    
    print "Preparing for inference..."
    self.network.prepare_for_inference(2)
  
  def test(self):
    """ Test that every image is correctly recognized. """
    Experiment.test(self)
    start = time.time()
    
    num_confused = 0.0
    
    classifier = self.network.get_classifier()
    i = 0
    while self.image_iterator.has_next():
      image, category, img_idx = self.image_iterator.next()
      recognized = self.network.do_inference(numpy.array(image), category)
      if not recognized:
        active_cats = classifier.get_winning_category()
        #print colored("Failed: " + category + " recognized as "+active_cats, 'red')
        num_confused += 1
      i += 1
      if i % self.PRINT_INCR == 0: print "Iter:", i
      
    confusion_rate = num_confused / float(i)
    
    elapsed = (time.time() - start)
    print "Testing time:", elapsed
    print "Time per category:", (elapsed / i)
    print colored("Testing complete", "green")
    print colored("\nConfusion rate:  " + str(confusion_rate), 'cyan')