Mercurial > ift6266
annotate transformations/local_elastic_distortions.py @ 72:af2f9252dd14
forgot to import sys in testmod
author | Arnaud Bergeron <abergeron@gmail.com> |
---|---|
date | Wed, 10 Feb 2010 11:34:32 -0500 |
parents | c89defea1e65 |
children | b3d76ebf2fac |
rev | line source |
---|---|
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
1 #!/usr/bin/python |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
2 # coding: utf-8 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
3 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
4 ''' |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
5 Implementation of elastic distortions as described in |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
6 Simard, Steinkraus, Platt, "Best Practices for Convolutional |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
7 Neural Networks Applied to Visual Document Analysis", 2003 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
8 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
9 Author: François Savard |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
10 Date: Fall 2009, revised Winter 2010 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
11 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
12 Usage: create the Distorter with proper alpha, sigma etc. |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
13 Then each time you want to change the distortion field applied, |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
14 call regenerate_field(). |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
15 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
16 (The point behind this is that regeneration takes some time, |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
17 so we better reuse the fields a few times) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
18 ''' |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
19 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
20 import sys |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
21 import math |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
22 import numpy |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
23 import numpy.random |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
24 import scipy.signal # convolve2d |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
25 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
26 _TEST_DIR = "/u/savardf/ift6266/debug_images/" |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
27 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
28 def _raw_zeros(size): |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
29 return [[0 for i in range(size[1])] for j in range(size[0])] |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
30 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
31 class ElasticDistortionParams(): |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
32 def __init__(self, image_size=(32,32), alpha=0.0, sigma=0.0): |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
33 self.image_size = image_size |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
34 self.alpha = alpha |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
35 self.sigma = sigma |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
36 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
37 h,w = self.image_size |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
38 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
39 self.matrix_tl_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
40 self.matrix_tl_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
41 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
42 self.matrix_tr_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
43 self.matrix_tr_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
44 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
45 self.matrix_bl_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
46 self.matrix_bl_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
47 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
48 self.matrix_br_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
49 self.matrix_br_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
50 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
51 # those will hold the precomputed ratios for |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
52 # bilinear interpolation |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
53 self.matrix_tl_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
54 self.matrix_tr_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
55 self.matrix_bl_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
56 self.matrix_br_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
57 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
58 def alpha_sigma(self): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
59 return [self.alpha, self.sigma] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
60 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
61 class LocalElasticDistorter(): |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
62 def __init__(self, image_size=(32,32)): |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
63 self.image_size = image_size |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
64 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
65 self.current_complexity_10 = 0 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
66 self.current_complexity = 0 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
67 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
68 # number of precomputed fields |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
69 # (principle: as complexity doesn't change often, we can |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
70 # precompute a certain number of fields for a given complexity, |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
71 # each with its own parameters. That way, we have good |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
72 # randomization, but we're much faster). |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
73 self.to_precompute_per_complexity = 50 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
74 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
75 # Both use ElasticDistortionParams |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
76 self.current_params = None |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
77 self.precomputed_params = [[] for i in range(10)] |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
78 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
79 # |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
80 self.kernel_size = None |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
81 self.kernel = None |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
82 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
83 # set some defaults |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
84 self.regenerate_parameters(0.0) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
85 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
86 def get_settings_names(self): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
87 return ['alpha', 'sigma'] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
88 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
89 def _floor_complexity(self, complexity): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
90 return self._to_complexity_10(complexity) / 10.0 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
91 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
92 def _to_complexity_10(self, complexity): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
93 return min(9, max(0, int(complexity * 10))) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
94 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
95 def regenerate_parameters(self, complexity): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
96 complexity_10 = self._to_complexity_10(complexity) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
97 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
98 if complexity_10 != self.current_complexity_10: |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
99 self.current_complexity_10 = complexity_10 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
100 self.current_complexity = self._floor_complexity(complexity) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
101 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
102 if len(self.precomputed_params[complexity_10]) <= self.to_precompute_per_complexity: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
103 # not yet enough params generated, produce one more |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
104 # and append to list |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
105 new_params = self._initialize_new_params() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
106 new_params = self._generate_fields(new_params) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
107 self.current_params = new_params |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
108 self.precomputed_params[complexity_10].append(new_params) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
109 else: |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
110 # if we have enough precomputed fields, just select one |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
111 # at random and set parameters to match what they were |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
112 # when the field was generated |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
113 idx = numpy.random.randint(0, len(self.precomputed_params[complexity_10])) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
114 self.current_params = self.precomputed_params[complexity_10][idx] |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
115 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
116 # don't return anything, to avoid storing deterministic parameters |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
117 return [] # self.current_params.alpha_sigma() |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
118 |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
119 def get_parameters_determined_by_complexity(self, complexity): |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
120 tmp_params = self._initialize_new_params(_floor_complexity(complexity)) |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
121 return tmp_params.alpha_sigma() |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
122 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
123 # adapted from http://blenderartists.org/forum/showthread.php?t=163361 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
124 def _gen_gaussian_kernel(self, sigma): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
125 # the kernel size can change DRAMATICALLY the time |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
126 # for the blur operation... so even though results are better |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
127 # with a bigger kernel, we need to compromise here |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
128 # 1*s is very different from 2*s, but there's not much difference |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
129 # between 2*s and 4*s |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
130 ks = self.kernel_size |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
131 s = sigma |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
132 target_ks = (1.5*s, 1.5*s) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
133 if not ks is None and ks[0] == target_ks[0] and ks[1] == target_ks[1]: |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
134 # kernel size is good, ok, no need to regenerate |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
135 return |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
136 self.kernel_size = target_ks |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
137 h,w = self.kernel_size |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
138 a,b = h/2.0, w/2.0 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
139 y,x = numpy.ogrid[0:w, 0:h] |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
140 gauss = numpy.exp(-numpy.square((x-a)/s))*numpy.exp(-numpy.square((y-b)/s)) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
141 # Normalize so we don't reduce image intensity |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
142 self.kernel = gauss/gauss.sum() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
143 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
144 def _gen_distortion_field(self, params): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
145 self._gen_gaussian_kernel(params.sigma) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
146 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
147 # we add kernel_size on all four sides so blurring |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
148 # with the kernel produces a smoother result on borders |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
149 ks0 = self.kernel_size[0] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
150 ks1 = self.kernel_size[1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
151 sz0 = self.image_size[1] + ks0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
152 sz1 = self.image_size[0] + ks1 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
153 field = numpy.random.uniform(-1.0, 1.0, (sz0, sz1)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
154 field = scipy.signal.convolve2d(field, self.kernel, mode='same') |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
155 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
156 # crop only image_size in the middle |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
157 field = field[ks0:ks0+self.image_size[0], ks1:ks1+self.image_size[1]] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
158 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
159 return params.alpha * field |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
160 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
161 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
162 def _initialize_new_params(self, complexity=None): |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
163 if not complexity: |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
164 complexity = self.current_complexity |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
165 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
166 params = ElasticDistortionParams(self.image_size) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
167 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
168 # pour faire progresser la complexité un peu plus vite |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
169 # tout en gardant les extrêmes de 0.0 et 1.0 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
170 complexity = complexity ** (1./3.) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
171 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
172 # the smaller the alpha, the closest the pixels are fetched |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
173 # a max of 10 is reasonable |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
174 params.alpha = complexity * 10.0 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
175 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
176 # the bigger the sigma, the smoother is the distortion |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
177 # max of 1 is "reasonable", but produces VERY noisy results |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
178 # And the bigger the sigma, the bigger the blur kernel, and the |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
179 # slower the field generation, btw. |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
180 params.sigma = 10.0 - (7.0 * complexity) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
181 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
182 return params |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
183 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
184 def _generate_fields(self, params): |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
185 ''' |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
186 Here's how the code works: |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
187 - We first generate "distortion fields" for x and y with these steps: |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
188 - Uniform noise over [-1, 1] in a matrix of size (h,w) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
189 - Blur with a Gaussian kernel of spread sigma |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
190 - Multiply by alpha |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
191 - Then (conceptually) to compose the distorted image, we loop over each pixel |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
192 of the new image and use the corresponding x and y distortions |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
193 (from the matrices generated above) to identify pixels |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
194 of the old image from which we fetch color data. As the |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
195 coordinates are not integer, we interpolate between the |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
196 4 nearby pixels (top left, top right etc.). |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
197 - That's just conceptually. Here I'm using matrix operations |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
198 to speed up the computation. I first identify the 4 nearby |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
199 pixels in the old image for each pixel in the distorted image. |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
200 I can then use them as "fancy indices" to extract the proper |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
201 pixels for each new pixel. |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
202 - Then I multiply those extracted nearby points by precomputed |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
203 ratios for the bilinear interpolation. |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
204 ''' |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
205 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
206 p = params |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
207 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
208 dist_fields = [None, None] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
209 dist_fields[0] = self._gen_distortion_field(params) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
210 dist_fields[1] = self._gen_distortion_field(params) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
211 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
212 #pylab.imshow(dist_fields[0]) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
213 #pylab.show() |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
214 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
215 # regenerate distortion index matrices |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
216 # "_rows" are row indices |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
217 # "_cols" are column indices |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
218 # (separated due to the way fancy indexing works in numpy) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
219 h,w = p.image_size |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
220 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
221 for y in range(h): |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
222 for x in range(w): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
223 distort_x = dist_fields[0][y,x] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
224 distort_y = dist_fields[1][y,x] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
225 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
226 # the "target" is the coordinate we fetch color data from |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
227 # (in the original image) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
228 # target_left and _top are the rounded coordinate on the |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
229 # left/top of this target (float) coordinate |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
230 target_pixel = (y+distort_y, x+distort_x) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
231 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
232 target_left = int(math.floor(x + distort_x)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
233 target_top = int(math.floor(y + distort_y)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
234 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
235 index_tl = [target_top, target_left] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
236 index_tr = [target_top, target_left+1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
237 index_bl = [target_top+1, target_left] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
238 index_br = [target_top+1, target_left+1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
239 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
240 # x_ratio is the ratio of importance of left pixels |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
241 # y_ratio is the """" of top pixels |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
242 # (in bilinear combination) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
243 y_ratio = 1.0 - (target_pixel[0] - target_top) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
244 x_ratio = 1.0 - (target_pixel[1] - target_left) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
245 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
246 # We use a default background color of 0 for displacements |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
247 # outside of boundaries of the image. |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
248 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
249 # if top left outside bounds |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
250 if index_tl[0] < 0 or index_tl[0] >= h or index_tl[1] < 0 or index_tl[1] >= w: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
251 p.matrix_tl_corners_rows[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
252 p.matrix_tl_corners_cols[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
253 p.matrix_tl_multiply[y,x] = 0 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
254 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
255 p.matrix_tl_corners_rows[y][x] = index_tl[0] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
256 p.matrix_tl_corners_cols[y][x] = index_tl[1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
257 p.matrix_tl_multiply[y,x] = x_ratio*y_ratio |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
258 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
259 # if top right outside bounds |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
260 if index_tr[0] < 0 or index_tr[0] >= h or index_tr[1] < 0 or index_tr[1] >= w: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
261 p.matrix_tr_corners_rows[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
262 p.matrix_tr_corners_cols[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
263 p.matrix_tr_multiply[y,x] = 0 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
264 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
265 p.matrix_tr_corners_rows[y][x] = index_tr[0] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
266 p.matrix_tr_corners_cols[y][x] = index_tr[1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
267 p.matrix_tr_multiply[y,x] = (1.0-x_ratio)*y_ratio |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
268 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
269 # if bottom left outside bounds |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
270 if index_bl[0] < 0 or index_bl[0] >= h or index_bl[1] < 0 or index_bl[1] >= w: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
271 p.matrix_bl_corners_rows[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
272 p.matrix_bl_corners_cols[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
273 p.matrix_bl_multiply[y,x] = 0 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
274 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
275 p.matrix_bl_corners_rows[y][x] = index_bl[0] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
276 p.matrix_bl_corners_cols[y][x] = index_bl[1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
277 p.matrix_bl_multiply[y,x] = x_ratio*(1.0-y_ratio) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
278 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
279 # if bottom right outside bounds |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
280 if index_br[0] < 0 or index_br[0] >= h or index_br[1] < 0 or index_br[1] >= w: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
281 p.matrix_br_corners_rows[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
282 p.matrix_br_corners_cols[y][x] = 0 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
283 p.matrix_br_multiply[y,x] = 0 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
284 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
285 p.matrix_br_corners_rows[y][x] = index_br[0] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
286 p.matrix_br_corners_cols[y][x] = index_br[1] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
287 p.matrix_br_multiply[y,x] = (1.0-x_ratio)*(1.0-y_ratio) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
288 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
289 # not really necessary, but anyway |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
290 return p |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
291 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
292 def transform_image(self, image): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
293 p = self.current_params |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
294 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
295 # index pixels to get the 4 corners for bilinear combination |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
296 tl_pixels = image[p.matrix_tl_corners_rows, p.matrix_tl_corners_cols] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
297 tr_pixels = image[p.matrix_tr_corners_rows, p.matrix_tr_corners_cols] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
298 bl_pixels = image[p.matrix_bl_corners_rows, p.matrix_bl_corners_cols] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
299 br_pixels = image[p.matrix_br_corners_rows, p.matrix_br_corners_cols] |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
300 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
301 # bilinear ratios, elemwise multiply |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
302 tl_pixels = numpy.multiply(tl_pixels, p.matrix_tl_multiply) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
303 tr_pixels = numpy.multiply(tr_pixels, p.matrix_tr_multiply) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
304 bl_pixels = numpy.multiply(bl_pixels, p.matrix_bl_multiply) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
305 br_pixels = numpy.multiply(br_pixels, p.matrix_br_multiply) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
306 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
307 # sum to finish bilinear combination |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
308 return numpy.sum([tl_pixels,tr_pixels,bl_pixels,br_pixels], axis=0).astype(numpy.float32) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
309 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
310 # TESTS ---------------------------------------------------------------------- |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
311 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
312 def _load_image(filepath): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
313 _RGB_TO_GRAYSCALE = [0.3, 0.59, 0.11, 0.0] |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
314 img = Image.open(filepath) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
315 img = numpy.asarray(img) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
316 if len(img.shape) > 2: |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
317 img = (img * _RGB_TO_GRAYSCALE).sum(axis=2) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
318 return (img / 255.0).astype('float') |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
319 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
320 def _specific_test(): |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
321 imgpath = os.path.join(_TEST_DIR, "d.png") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
322 img = _load_image(imgpath) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
323 dist = LocalElasticDistorter((32,32)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
324 print dist.regenerate_parameters(0.5) |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
325 img = dist.transform_image(img) |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
326 print dist.get_parameters_determined_by_complexity(0.4) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
327 pylab.imshow(img) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
328 pylab.show() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
329 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
330 def _complexity_tests(): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
331 imgpath = os.path.join(_TEST_DIR, "d.png") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
332 dist = LocalElasticDistorter((32,32)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
333 orig_img = _load_image(imgpath) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
334 html_content = '''<html><body>Original:<br/><img src='d.png'>''' |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
335 for complexity in numpy.arange(0.0, 1.1, 0.1): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
336 html_content += '<br/>Complexity: ' + str(complexity) + '<br/>' |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
337 for i in range(10): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
338 t1 = time.time() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
339 dist.regenerate_parameters(complexity) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
340 t2 = time.time() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
341 print "diff", t2-t1 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
342 img = dist.transform_image(orig_img) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
343 filename = "complexity_" + str(complexity) + "_" + str(i) + ".png" |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
344 new_path = os.path.join(_TEST_DIR, filename) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
345 _save_image(img, new_path) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
346 html_content += '<img src="' + filename + '">' |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
347 html_content += "</body></html>" |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
348 html_file = open(os.path.join(_TEST_DIR, "complexity.html"), "w") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
349 html_file.write(html_content) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
350 html_file.close() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
351 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
352 def _complexity_benchmark(): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
353 imgpath = os.path.join(_TEST_DIR, "d.png") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
354 dist = LocalElasticDistorter((32,32)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
355 orig_img = _load_image(imgpath) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
356 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
357 for cpx in (0.21, 0.35): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
358 # time the first 10 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
359 t1 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
360 for i in range(10): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
361 dist.regenerate_parameters(cpx) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
362 img = dist.transform_image(orig_img) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
363 t2 = time.time() |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
364 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
365 print "first 10, total = ", t2-t1, ", avg=", (t2-t1)/10 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
366 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
367 # time the next 40 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
368 t1 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
369 for i in range(40): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
370 dist.regenerate_parameters(cpx) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
371 img = dist.transform_image(orig_img) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
372 t2 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
373 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
374 print "next 40, total = ", t2-t1, ", avg=", (t2-t1)/40 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
375 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
376 # time the next 50 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
377 t1 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
378 for i in range(50): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
379 dist.regenerate_parameters(cpx) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
380 img = dist.transform_image(orig_img) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
381 t2 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
382 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
383 print "next 50, total = ", t2-t1, ", avg=", (t2-t1)/50 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
384 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
385 # time the next 1000 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
386 t1 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
387 for i in range(1000): |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
388 dist.regenerate_parameters(cpx) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
389 img = dist.transform_image(orig_img) |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
390 t2 = time.time() |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
391 |
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
392 print "next 1000, total = ", t2-t1, ", avg=", (t2-t1)/1000 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
393 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
394 # time the next 1000 with old complexity |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
395 t1 = time.time() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
396 for i in range(1000): |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
397 dist.regenerate_parameters(0.21) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
398 img = dist.transform_image(orig_img) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
399 t2 = time.time() |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
400 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
401 print "next 1000, total = ", t2-t1, ", avg=", (t2-t1)/1000 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
402 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
403 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
404 |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
405 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
406 def _save_image(img, path): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
407 img2 = Image.fromarray((img * 255).astype('uint8'), "L") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
408 img2.save(path) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
409 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
410 # TODO: reformat to follow new class... it function of complexity now |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
411 ''' |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
412 def _distorter_tests(): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
413 #import pylab |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
414 #pylab.imshow(img) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
415 #pylab.show() |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
416 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
417 for letter in ("d", "a", "n", "o"): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
418 img = _load_image("tests/" + letter + ".png") |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
419 for alpha in (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
420 for sigma in (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0): |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
421 id = LocalElasticDistorter((32,32)) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
422 img2 = id.distort_image(img) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
423 img2 = Image.fromarray((img2 * 255).astype('uint8'), "L") |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
424 img2.save("tests/"+letter+"_alpha"+str(alpha)+"_sigma"+str(sigma)+".png") |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
425 ''' |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
426 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
427 def _benchmark(): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
428 img = _load_image("tests/d.png") |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
429 dist = LocalElasticDistorter((32,32)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
430 dist.regenerate_parameters(0.0) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
431 import time |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
432 t1 = time.time() |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
433 for i in range(10000): |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
434 if i % 1000 == 0: |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
435 print "-" |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
436 dist.distort_image(img) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
437 t2 = time.time() |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
438 print "t2-t1", t2-t1 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
439 print "avg", 10000/(t2-t1) |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
440 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
441 if __name__ == '__main__': |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
442 import time |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
443 import pylab |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
444 import Image |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
445 import os.path |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
446 #_distorter_tests() |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
447 #_benchmark() |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
448 #_specific_test() |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
449 #_complexity_tests() |
52
c89defea1e65
Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents:
30
diff
changeset
|
450 _complexity_benchmark() |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
451 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
452 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
453 |