Sfoglia il codice sorgente

Adds new python_rpnifs tests

Yann Weber 3 anni fa
parent
commit
2348d6abaf
5 ha cambiato i file con 472 aggiunte e 11 eliminazioni
  1. 18
    7
      python_rpnifs/__main__.py
  2. 17
    4
      python_rpnifs/ifs1.py
  3. 147
    0
      python_rpnifs/ifs3.py
  4. 143
    0
      python_rpnifs/ifs4.py
  5. 147
    0
      python_rpnifs/ifs5.py

+ 18
- 7
python_rpnifs/__main__.py Vedi File

@@ -14,20 +14,31 @@ from tqdm import tqdm
14 14
 
15 15
 from .ifs1 import IFS1
16 16
 from .ifs2 import IFS2
17
+from .ifs3 import IFS3
18
+from .ifs4 import IFS4
19
+from .ifs5 import IFS5
17 20
 
18 21
 
19 22
 #steps = 1024**2
20
-steps=512**2
23
+#steps=512**2
24
+steps=1024**2
21 25
 #steps*=4
22 26
 #steps*=10
23
-pool_sz = 32
24
-best_sz = 4
25
-n_mutation = 5
27
+pool_sz = 30
28
+best_sz = 6
29
+n_mutation = 10
26 30
 outfile='/tmp/rpnifs2_%02d.png'
27
-world = IFS1.get_world()
28
-pool = [IFS1(init_sz=3, world=world) for _ in range(pool_sz)]
31
+#world = IFS1.get_world()
32
+#pool = [IFS1(init_sz=3, world=world) for _ in range(pool_sz)]
29 33
 #world = IFS2.get_world()
30 34
 #pool = [IFS2(init_sz=3, world=world) for _ in range(pool_sz)]
35
+#world = IFS3.get_world()
36
+#pool = [IFS3(init_sz=3, world=world) for _ in range(pool_sz)]
37
+#world = IFS4.get_world()
38
+#pool = [IFS4(init_sz=3, world=world) for _ in range(pool_sz)]
39
+world = IFS5.get_world()
40
+#pool = [IFS5(init_sz=3, world=world) for _ in range(pool_sz)]
41
+pool = [IFS5(init_sz=6, world=world) for _ in range(pool_sz)]
31 42
 print('POOL ready')
32 43
 
33 44
 generation=0
@@ -49,7 +60,7 @@ while True:
49 60
 
50 61
         score = ifs.score()
51 62
         scores.append((ifs, score))
52
-        #print('%02d) %5.3f %s' % (i, score, ifs))
63
+        print('%02d) %5.3f %s' % (i, score, ifs))
53 64
         im = Image.fromarray(ifs.get_image())
54 65
         im.save(outfile % i)
55 66
 

+ 17
- 4
python_rpnifs/ifs1.py Vedi File

@@ -1,4 +1,5 @@
1 1
 import random
2
+import time
2 3
 import copy
3 4
 
4 5
 import numpy as np
@@ -93,6 +94,11 @@ class IFS1(object):
93 94
         self._world[self._position[0]][self._position[1]] = [r,g,b,a]
94 95
 
95 96
     def score(self):
97
+        start = time.time()
98
+        
99
+        colcount = len(np.unique(np.reshape(self._world[:,:,:3], (-1,3)),
100
+                       axis=0))
101
+
96 102
         #gray = rgb2gray(self._world)
97 103
         #sigma = estimate_sigma(self._world, multichannel=True, average_sigmas=True)
98 104
         sigmas = estimate_sigma(self._world, multichannel=True, average_sigmas=False)
@@ -111,9 +117,16 @@ class IFS1(object):
111 117
 
112 118
 
113 119
         score += mod 
114
-        if sigma and sigma > 5:
115
-            score /= sigma/5
116
-        print("SIGMAS : %r SIGMA  : %f " %  (sigmas, sigma))
117
-        print("SCORES : %r SCORE : %r" % (scores, score))
120
+        if sigma and sigma > 50:
121
+            score -= (sigma-50)/100
122
+        colscore = abs(colcount-1024) / 1e5
123
+        score -= colscore
124
+
125
+        printscore = lambda arr: '['+(', '.join(['%1.3f' % e for e in arr]))+']'
126
+        print("colscore %3.3f (%4d colors) scores time %5.2fs" % (colscore,
127
+                                                                 colcount,
128
+                                                                 time.time() - start))
129
+        print("SIGMAS : %s SIGMA  : %f " %  (printscore(sigmas), sigma))
130
+        print("SCORES : %s SCORE : %r" % (printscore(scores), score))
118 131
         return score
119 132
 

+ 147
- 0
python_rpnifs/ifs3.py Vedi File

@@ -0,0 +1,147 @@
1
+import random
2
+import time
3
+import copy
4
+
5
+import numpy as np
6
+from skimage.restoration import estimate_sigma
7
+
8
+from .expr import *
9
+from .fractdim import *
10
+
11
+
12
+class IFS3(object):
13
+    """ @brief 1 Variable IFS with R,G,B,A X,Y decomposition """
14
+
15
+    #height=width=1024
16
+    height=width=768
17
+    #height=width=512
18
+
19
+    def __init__(self, nexpr=4, init_sz=1, world=None):
20
+        self._nexpr = nexpr
21
+        self._expr = [RpnExpr(sz=init_sz, nvar=1)
22
+                      for _ in range(nexpr)]
23
+        self._world = world
24
+        if self._world is None:
25
+            self._world = self.get_world()
26
+        self._position = [random.randint(0, self.height-1),
27
+                          random.randint(0, self.width-1)]
28
+
29
+    @classmethod
30
+    def get_world(cls):
31
+        return np.zeros((cls.height,cls.width,4), dtype=np.uint8)
32
+
33
+    def raz_world(self):
34
+        for i in range(self.height):
35
+            for j in range(self.width):
36
+                self._world[i][j] = [0,0,0,0]
37
+    
38
+    def __str__(self):
39
+        return ";".join([str(e) for e in self._expr])
40
+
41
+    def __copy__(self):
42
+        ret = IFS3(nexpr=self._nexpr, world=self._world)
43
+        ret._expr = [copy.copy(e) for e in self._expr]
44
+        #ret._world = copy.deepcopy(self._world)
45
+        return ret
46
+
47
+    def get_image(self):
48
+    	return self._world
49
+ 
50
+    def mutation(self, n=1, rand=True):
51
+        n = 1 if n <= 1 else random.randint(1,n)
52
+        for _ in range(n):
53
+            random.choice(self._expr).mutation()
54
+
55
+    def step(self):
56
+        r,g,b,a = [int(e)
57
+                   for e in self._world[self._position[0]][self._position[1]]]
58
+        arg = r
59
+        arg <<= 8
60
+        arg += g
61
+        arg <<= 8
62
+        arg += b
63
+        arg <<= 8
64
+        arg += a
65
+        arg <<=16
66
+        arg += self._position[0]
67
+        arg <<=16
68
+        arg += self._position[1]
69
+
70
+        ret = int(random.choice(self._expr).eval(arg))
71
+        dr, dg, db, da = r,g,b,a
72
+
73
+        #self._position[1] = (ret & 0xFFFF)%self.width
74
+        self._position[1] = ((ret & 0xFFFF)*self.width)//0x10000
75
+        ret >>= 16
76
+        self._position[0] = ((ret & 0xFFFF)*self.height)//0x10000
77
+        ret >>= 16
78
+        a = ret & 0xFF
79
+        ret >>= 8
80
+        b = ret & 0xFF
81
+        ret >>= 8
82
+        g = ret & 0xFF
83
+        ret >>= 8
84
+        r = ret & 0xFF
85
+
86
+        sa = a/255
87
+        outa = (a + da*(1-sa))/255
88
+        if outa == 0:
89
+            ro,go,bo = 0,0,0
90
+        else:
91
+            ro, go, bo = [(c*(a/255)+dc*da*(1-sa))/outa
92
+                          for c, dc in ((r,dr), (g, dg), (b, db))]
93
+        r,g,b,a = [int(e) for e in (ro,go,bo,outa*255)]
94
+
95
+
96
+        self._world[self._position[0]][self._position[1]] = [r,g,b,a]
97
+
98
+    def score(self):
99
+        start = time.time()
100
+        
101
+        colcount = len(np.unique(np.reshape(self._world[:,:,:3], (-1,3)),
102
+                       axis=0))
103
+
104
+        #sigma = estimate_sigma(self._world, multichannel=True, average_sigmas=True)
105
+        sigmas = estimate_sigma(self._world[:,:,:3], multichannel=True,
106
+                                average_sigmas=False)
107
+
108
+        scores = [fractal_dimension(self._world[:,:,i]*self._world[:,:,3]/255)
109
+                  for i in range(3)]
110
+        #alpha score
111
+        #scores += [fractal_dimension(self._world[:,:,3])]
112
+
113
+        gray = rgb2gray(self._world)
114
+        graysigma = estimate_sigma(gray)
115
+        grayscore = fractal_dimension(gray)
116
+        del(gray)
117
+
118
+        sigmas += [graysigma]*3
119
+        sigmas = [0 if np.isnan(sigma) else sigma for sigma in sigmas]
120
+
121
+        scores += [grayscore]*3
122
+
123
+        sigma = sum(sigmas)/len(sigmas)
124
+
125
+        mod = abs(scores[0]-scores[1])
126
+        mod += abs(scores[0]-scores[2])
127
+        mod += abs(scores[0]-scores[3])
128
+        mod += abs(scores[1]-scores[2])
129
+        mod += abs(scores[1]-scores[3])
130
+        mod /= 5
131
+        score = sum(scores)/len(scores)
132
+
133
+
134
+        score += mod 
135
+        if sigma and sigma > 0:
136
+            score -= sigma/100
137
+        colscore = abs(colcount-1024) / 1e5
138
+        score -= colscore
139
+
140
+        printscore = lambda arr: '['+(', '.join(['%1.3f' % e for e in arr]))+']'
141
+        print("colscore %3.3f (%4d colors) scores time %5.2fs" % (colscore,
142
+                                                                 colcount,
143
+                                                                 time.time() - start))
144
+        print("SIGMAS : %s SIGMA  : %f " %  (printscore(sigmas), sigma))
145
+        print("SCORES : %s SCORE : %r" % (printscore(scores), score))
146
+        return score
147
+

+ 143
- 0
python_rpnifs/ifs4.py Vedi File

@@ -0,0 +1,143 @@
1
+import random
2
+import time
3
+import copy
4
+
5
+import numpy as np
6
+from skimage.restoration import estimate_sigma
7
+
8
+from .expr import *
9
+from .fractdim import *
10
+
11
+
12
+class IFS4(object):
13
+    """ @brief 1 Variable IFS with R,G,B,A X,Y decomposition """
14
+
15
+    #height=width=1024
16
+    height=width=768
17
+    #height=width=512
18
+
19
+    def __init__(self, nexpr=4, init_sz=1, world=None):
20
+        self._nexpr = nexpr
21
+        self._expr_pos = [[RpnExpr(sz=init_sz, nvar=6)
22
+                           for _ in range(nexpr)]
23
+			  for _ in range(2)]
24
+        self._expr_col = [[RpnExpr(sz=init_sz, nvar=6)
25
+                           for _ in range(nexpr)]
26
+			  for _ in range(4)]
27
+        self._world = world
28
+        if self._world is None:
29
+            self._world = self.get_world()
30
+        self._position = [random.randint(0, self.height-1),
31
+                          random.randint(0, self.width-1)]
32
+        self._col = [random.randint(0, 255) for _ in range(4)]
33
+
34
+    @classmethod
35
+    def get_world(cls):
36
+        return np.zeros((cls.height,cls.width,4), dtype=np.uint8)
37
+
38
+    def raz_world(self):
39
+        for i in range(self.height):
40
+            for j in range(self.width):
41
+                self._world[i][j] = [0,0,0,0]
42
+    
43
+    def __str__(self):
44
+        ret = 'X<%s>' % (';'.join([str(e) for e in self._expr_pos[0]]))
45
+        ret += 'Y<%s>' % (';'.join([str(e) for e in self._expr_pos[1]]))
46
+        ret += 'R<%s>' % (';'.join([str(e) for e in self._expr_col[0]]))
47
+        ret += 'G<%s>' % (';'.join([str(e) for e in self._expr_col[1]]))
48
+        ret += 'B<%s>' % (';'.join([str(e) for e in self._expr_col[2]]))
49
+        ret += 'A<%s>' % (';'.join([str(e) for e in self._expr_col[3]]))
50
+        return ret
51
+
52
+    def __copy__(self):
53
+        ret = IFS4(nexpr=self._nexpr, world=self._world)
54
+        ret._expr_pos = [[copy.copy(e) for e in el]
55
+                         for el in self._expr_pos]
56
+        ret._expr_col = [[copy.copy(e) for e in el]
57
+                         for el in self._expr_col]
58
+        #ret._world = copy.deepcopy(self._world)
59
+        return ret
60
+
61
+    def get_image(self):
62
+    	return self._world
63
+ 
64
+    def mutation(self, n=1, rand=True):
65
+        n = 1 if n <= 1 else random.randint(1,n)
66
+        for _ in range(n):
67
+            random.choice(self._expr_pos + self._expr_col).mutation()
68
+
69
+    def step(self):
70
+        dr,dg,db,da = [int(e)
71
+                   for e in self._world[self._position[0]][self._position[1]]]
72
+        cx = int((self._position[0] / self.width) * ((1<<64)-1))
73
+        cy = int((self._position[0] / self.height) * ((1<<64)-1))
74
+        args = (cx,cy,dr,dg,db,da)
75
+        rx,ry = [int(random.choice(expr).eval(*args))
76
+                 for expr in self._expr_pos]
77
+        rx = (rx * self.width) // ((1<<64)-1)
78
+        ry = (ry * self.height) // ((1<<64)-1)
79
+        r,g,b,a = [int(random.choice(expr).eval(*args) % 256)
80
+                   for expr in self._expr_col]
81
+
82
+        sa = a/255
83
+        outa = (a + da*(1-sa))/255
84
+        if outa == 0:
85
+            ro,go,bo = 0,0,0
86
+        else:
87
+            ro, go, bo = [(c*(a/255)+dc*da*(1-sa))/outa
88
+                          for c, dc in ((r,dr), (g, dg), (b, db))]
89
+        r,g,b,a = [int(e) for e in (ro,go,bo,outa*255)]
90
+
91
+
92
+        self._world[self._position[0]][self._position[1]] = [r,g,b,a]
93
+
94
+    def score(self):
95
+        start = time.time()
96
+        
97
+        colcount = len(np.unique(np.reshape(self._world[:,:,:3], (-1,3)),
98
+                       axis=0))
99
+
100
+        #sigma = estimate_sigma(self._world, multichannel=True, average_sigmas=True)
101
+        sigmas = estimate_sigma(self._world[:,:,:3], multichannel=True,
102
+                                average_sigmas=False)
103
+
104
+        scores = [fractal_dimension(self._world[:,:,i]*self._world[:,:,3]/255)
105
+                  for i in range(3)]
106
+        #alpha score
107
+        #scores += [fractal_dimension(self._world[:,:,3])]
108
+
109
+        gray = rgb2gray(self._world)
110
+        graysigma = estimate_sigma(gray)
111
+        grayscore = fractal_dimension(gray)
112
+        del(gray)
113
+
114
+        sigmas += [graysigma]*3
115
+        sigmas = [0 if np.isnan(sigma) else sigma for sigma in sigmas]
116
+
117
+        scores += [grayscore]*3
118
+
119
+        sigma = sum(sigmas)/len(sigmas)
120
+
121
+        mod = abs(scores[0]-scores[1])
122
+        mod += abs(scores[0]-scores[2])
123
+        mod += abs(scores[0]-scores[3])
124
+        mod += abs(scores[1]-scores[2])
125
+        mod += abs(scores[1]-scores[3])
126
+        mod /= 5
127
+        score = sum(scores)/len(scores)
128
+
129
+
130
+        score += mod 
131
+        if sigma and sigma > 0:
132
+            score -= sigma/100
133
+        colscore = abs(colcount-1024) / 1e5
134
+        score -= colscore
135
+
136
+        printscore = lambda arr: '['+(', '.join(['%1.3f' % e for e in arr]))+']'
137
+        print("colscore %3.3f (%4d colors) scores time %5.2fs" % (colscore,
138
+                                                                 colcount,
139
+                                                                 time.time() - start))
140
+        print("SIGMAS : %s SIGMA  : %f " %  (printscore(sigmas), sigma))
141
+        print("SCORES : %s SCORE : %r" % (printscore(scores), score))
142
+        return score
143
+

+ 147
- 0
python_rpnifs/ifs5.py Vedi File

@@ -0,0 +1,147 @@
1
+import random
2
+import time
3
+import copy
4
+
5
+import numpy as np
6
+from skimage.restoration import estimate_sigma
7
+
8
+from .expr import *
9
+from .fractdim import *
10
+
11
+
12
+class IFS5(object):
13
+    """ @brief 1 Variable IFS with R,G,B,A X,Y decomposition """
14
+
15
+    #height=width=1024
16
+    height=width=768
17
+    #height=width=512
18
+
19
+    def __init__(self, nexpr=4, init_sz=1, world=None):
20
+        self._nexpr = nexpr
21
+        self._expr = [[RpnExpr(sz=init_sz, nvar=6) for _ in range(6)]
22
+                      for _ in range(nexpr)]
23
+        self._world = world
24
+        if self._world is None:
25
+            self._world = self.get_world()
26
+        self._position = [random.randint(0, self.height-1),
27
+                          random.randint(0, self.width-1)]
28
+        self._col = [random.randint(0, 255) for _ in range(4)]
29
+
30
+    @classmethod
31
+    def get_world(cls):
32
+        return np.zeros((cls.height,cls.width,4), dtype=np.uint8)
33
+
34
+    def raz_world(self):
35
+        for i in range(self.height):
36
+            for j in range(self.width):
37
+                self._world[i][j] = [0,0,0,0]
38
+    
39
+    def __str__(self):
40
+        el_lbl = 'XYrgba'
41
+        fel = lambda el: ';'.join(['%s=(%s)' % (el_lbl[i], str(el[i]))
42
+                                   for i in range(len(el))])
43
+        ret = ';'.join(['<%s>' % fel(el) for el in self._expr])
44
+        return ret
45
+
46
+    def __copy__(self):
47
+        ret = IFS5(nexpr=self._nexpr, world=self._world)
48
+        ret._expr = [[copy.copy(e) for e in el]
49
+                      for el in self._expr]
50
+        #ret._world = copy.deepcopy(self._world)
51
+        return ret
52
+
53
+    def get_image(self):
54
+    	return self._world
55
+ 
56
+    def mutation(self, n=1, rand=True):
57
+        n = 1 if n <= 1 else random.randint(1,n)
58
+        for _ in range(n):
59
+            random.choice(random.choice(self._expr)).mutation()
60
+
61
+    def step(self):
62
+        dr,dg,db,da = [int(e)
63
+                       for e in self._world[self._position[0]][self._position[1]]]
64
+
65
+        cx = int((self._position[0] / self.width) * ((1<<64)-1))
66
+        cy = int((self._position[0] / self.height) * ((1<<64)-1))
67
+
68
+        args = (cx,cy,dr,dg,db,da)
69
+        rx,ry,r,g,b,a = (int(expr.eval(*args))
70
+                         for expr in random.choice(self._expr))
71
+        rx = (rx * (self.width-1)) // ((1<<64)-1)
72
+        ry = (ry * (self.height-1)) // ((1<<64)-1)
73
+        self._position[0], self._position[1] = rx, ry
74
+        r,g,b,a = (c%256 for c in (r,g,b,a))
75
+
76
+        sa = a/255
77
+        outa = (a + da*(1-sa))/255
78
+        if outa == 0:
79
+            ro,go,bo = 0,0,0
80
+        else:
81
+            ro, go, bo = [(c*(a/255)+dc*da*(1-sa))/outa
82
+                          for c, dc in ((r,dr), (g, dg), (b, db))]
83
+        r,g,b,a = [int(e) for e in (ro,go,bo,outa*255)]
84
+
85
+
86
+        self._world[self._position[0]][self._position[1]] = [r,g,b,a]
87
+
88
+    def score(self):
89
+        start = time.time()
90
+        
91
+        colcount = len(np.unique(np.reshape(self._world[:,:,:3], (-1,3)),
92
+                       axis=0))
93
+
94
+        #sigma = estimate_sigma(self._world, multichannel=True, average_sigmas=True)
95
+        sigmas = estimate_sigma(self._world[:,:,:3], multichannel=True,
96
+                                average_sigmas=False)
97
+
98
+        scores = [fractal_dimension(self._world[:,:,i]*self._world[:,:,3]/255)
99
+                  for i in range(3)]
100
+        #alpha score
101
+        #scores += [fractal_dimension(self._world[:,:,3])]
102
+
103
+        gray = rgb2gray(self._world)
104
+        graysigma = estimate_sigma(gray)
105
+        grayscore = fractal_dimension(gray)
106
+        del(gray)
107
+
108
+        sigmas += [graysigma]*3
109
+        sigmas = [0 if np.isnan(sigma) else sigma for sigma in sigmas]
110
+
111
+        scores += [grayscore]*3
112
+
113
+        sigma = sum(sigmas)/len(sigmas)
114
+
115
+        mod = abs(scores[0]-scores[1])
116
+        mod += abs(scores[0]-scores[2])
117
+        mod += abs(scores[0]-scores[3])
118
+        mod += abs(scores[1]-scores[2])
119
+        mod += abs(scores[1]-scores[3])
120
+        mod /= 5
121
+
122
+        null_comp = 0
123
+        for i in range(3):
124
+            null_comp += 1 if scores[i] == 0 else 0
125
+
126
+        if null_comp >= 2:
127
+            score = 0
128
+            mod *= 0.8
129
+        else:
130
+            score = sum(scores)/len(scores)
131
+
132
+        score += mod 
133
+
134
+        if sigma and sigma > 0:
135
+            score -= sigma/100
136
+        #colscore = abs(colcount-1024) / 1e5
137
+        colscore = abs(colcount-2048) / 1e5
138
+        score -= colscore
139
+
140
+        printscore = lambda arr: '['+(', '.join(['%1.3f' % e for e in arr]))+']'
141
+        print("colscore %3.3f (%4d colors) scores time %5.2fs" % (colscore,
142
+                                                                 colcount,
143
+                                                                 time.time() - start))
144
+        print("SIGMAS : %s SIGMA  : %f " %  (printscore(sigmas), sigma))
145
+        print("SCORES : %s SCORE : %r" % (printscore(scores), score))
146
+        return score
147
+

Loading…
Annulla
Salva