Mercurial > ift6266
diff transformations/local_elastic_distortions.py @ 52:c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
author | fsavard |
---|---|
date | Thu, 04 Feb 2010 16:53:50 -0500 |
parents | a8ac3402eb45 |
children | b3d76ebf2fac |
line wrap: on
line diff
--- a/transformations/local_elastic_distortions.py Thu Feb 04 15:38:10 2010 -0500 +++ b/transformations/local_elastic_distortions.py Thu Feb 04 16:53:50 2010 -0500 @@ -23,7 +23,7 @@ import numpy.random import scipy.signal # convolve2d -_TEST_DIR = "/home/francois/Desktop/dist_tests/" +_TEST_DIR = "/u/savardf/ift6266/debug_images/" def _raw_zeros(size): return [[0 for i in range(size[1])] for j in range(size[0])] @@ -62,18 +62,19 @@ def __init__(self, image_size=(32,32)): self.image_size = image_size - self.current_complexity = 0.0 + self.current_complexity_10 = 0 + self.current_complexity = 0 # number of precomputed fields # (principle: as complexity doesn't change often, we can # precompute a certain number of fields for a given complexity, # each with its own parameters. That way, we have good # randomization, but we're much faster). - self.to_precompute = 50 + self.to_precompute_per_complexity = 50 # Both use ElasticDistortionParams self.current_params = None - self.precomputed_params = [] + self.precomputed_params = [[] for i in range(10)] # self.kernel_size = None @@ -85,32 +86,38 @@ def get_settings_names(self): return ['alpha', 'sigma'] - def regenerate_parameters(self, complexity): - if abs(complexity - self.current_complexity) > 1e-4: - self.current_complexity = complexity + def _floor_complexity(self, complexity): + return self._to_complexity_10(complexity) / 10.0 + + def _to_complexity_10(self, complexity): + return min(9, max(0, int(complexity * 10))) - # complexity changed, fields must be regenerated - self.precomputed_params = [] + def regenerate_parameters(self, complexity): + complexity_10 = self._to_complexity_10(complexity) - if len(self.precomputed_params) <= self.to_precompute: + if complexity_10 != self.current_complexity_10: + self.current_complexity_10 = complexity_10 + self.current_complexity = self._floor_complexity(complexity) + + if len(self.precomputed_params[complexity_10]) <= self.to_precompute_per_complexity: # not yet enough params generated, produce one more # and append to list new_params = self._initialize_new_params() new_params = self._generate_fields(new_params) self.current_params = new_params - self.precomputed_params.append(new_params) + self.precomputed_params[complexity_10].append(new_params) else: # if we have enough precomputed fields, just select one # at random and set parameters to match what they were # when the field was generated - idx = numpy.random.randint(0, len(self.precomputed_params)) - self.current_params = self.precomputed_params[idx] + idx = numpy.random.randint(0, len(self.precomputed_params[complexity_10])) + self.current_params = self.precomputed_params[complexity_10][idx] # don't return anything, to avoid storing deterministic parameters return [] # self.current_params.alpha_sigma() def get_parameters_determined_by_complexity(self, complexity): - tmp_params = self._initialize_new_params(complexity) + tmp_params = self._initialize_new_params(_floor_complexity(complexity)) return tmp_params.alpha_sigma() # adapted from http://blenderartists.org/forum/showthread.php?t=163361 @@ -347,37 +354,47 @@ dist = LocalElasticDistorter((32,32)) orig_img = _load_image(imgpath) - # time the first 10 - t1 = time.time() - for i in range(10): - dist.regenerate_parameters(0.2) - img = dist.transform_image(orig_img) - t2 = time.time() + for cpx in (0.21, 0.35): + # time the first 10 + t1 = time.time() + for i in range(10): + dist.regenerate_parameters(cpx) + img = dist.transform_image(orig_img) + t2 = time.time() - print "first 10, total = ", t2-t1, ", avg=", (t2-t1)/10 + print "first 10, total = ", t2-t1, ", avg=", (t2-t1)/10 + + # time the next 40 + t1 = time.time() + for i in range(40): + dist.regenerate_parameters(cpx) + img = dist.transform_image(orig_img) + t2 = time.time() + + print "next 40, total = ", t2-t1, ", avg=", (t2-t1)/40 - # time the next 40 - t1 = time.time() - for i in range(40): - dist.regenerate_parameters(0.2) - img = dist.transform_image(orig_img) - t2 = time.time() - - print "next 40, total = ", t2-t1, ", avg=", (t2-t1)/40 + # time the next 50 + t1 = time.time() + for i in range(50): + dist.regenerate_parameters(cpx) + img = dist.transform_image(orig_img) + t2 = time.time() + + print "next 50, total = ", t2-t1, ", avg=", (t2-t1)/50 - # time the next 50 - t1 = time.time() - for i in range(50): - dist.regenerate_parameters(0.2) - img = dist.transform_image(orig_img) - t2 = time.time() - - print "next 50, total = ", t2-t1, ", avg=", (t2-t1)/50 + # time the next 1000 + t1 = time.time() + for i in range(1000): + dist.regenerate_parameters(cpx) + img = dist.transform_image(orig_img) + t2 = time.time() + + print "next 1000, total = ", t2-t1, ", avg=", (t2-t1)/1000 - # time the next 1000 + # time the next 1000 with old complexity t1 = time.time() for i in range(1000): - dist.regenerate_parameters(0.2) + dist.regenerate_parameters(0.21) img = dist.transform_image(orig_img) t2 = time.time() @@ -385,6 +402,7 @@ + def _save_image(img, path): img2 = Image.fromarray((img * 255).astype('uint8'), "L") img2.save(path) @@ -427,9 +445,9 @@ import os.path #_distorter_tests() #_benchmark() - _specific_test() + #_specific_test() #_complexity_tests() - #_complexity_benchmark() + _complexity_benchmark()