-
Notifications
You must be signed in to change notification settings - Fork 10
/
DE_Euclidean.py
127 lines (105 loc) · 3.26 KB
/
DE_Euclidean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import random
import cv2
import math
import imageio
MAX_NUMBER_OF_FRAMES = 100
TOTAL_KEY_FRAMES = 10
STOPPING_ITERATION = 40
NUMBER_OF_NP_CANDIDATES = 10
# Location to read images from.
# NOTE: "_" is used to follow a naming convention
# eg. _20.jpg, _21.jpg etc...
READ_LOCATION = "/path/to/location/_"
# Location to write GIF images to.
GIF_WRITE_LOCATION = "/path/to/location/sample.GIF"
# Population matrix.
NP = []
# Mutation vector.
MV = []
# Trail vector.
TV = []
# Scale factor.
F = 0.9
# Cr probability value.
Cr = 0.6
# Calculate AED for a chromosome.
def getAED( KF ):
ED_sum = 0
for i in range(1, TOTAL_KEY_FRAMES - 1):
while True:
try:
im1 = cv2.imread(READ_LOCATION + str(KF[i]) + ".jpg",0)
im2 = cv2.imread(READ_LOCATION + str(KF[i+1]) + ".jpg",0)
ED_sum += cv2.norm(im1, im2, cv2.NORM_L2)
except:
print i, KF, KF[i], KF[i+1]
continue
break
return ED_sum/(TOTAL_KEY_FRAMES - 1)
# INITIALISATION : Generates population NP of 10 parent vectors (and AEDs).
def initialize_NP():
for i in range(NUMBER_OF_NP_CANDIDATES):
NP.append(sorted(random.sample(range(1, MAX_NUMBER_OF_FRAMES+1), TOTAL_KEY_FRAMES)))
NP[-1].append(getAED(NP[-1]))
print NP[-1]
# MUTATION
def mutation(num):
R = random.sample(NP,3)
global MV
MV[:] = []
MV_value = 0
for i in range(TOTAL_KEY_FRAMES):
MV_value = int(NP[num][i] + F*(R[1][i] - R[2][i]))
if(MV_value < 1):
MV.append(1)
elif(MV_value > MAX_NUMBER_OF_FRAMES):
MV.append(MAX_NUMBER_OF_FRAMES)
else:
MV.append(MV_value)
MV.sort()
MV.append(getAED(MV))
# CROSSOVER (uniform crossover with Cr = 0.6).
def crossover(parent, mutant):
print "mutant: ", mutant
print "parent: ", parent
for j in range(TOTAL_KEY_FRAMES) :
if(random.uniform(0,1) < Cr) :
TV.append(mutant[j])
else:
TV.append(parent[j])
TV.sort()
TV.append(getAED(TV))
print "TV : ", TV
# SELECTION : Selects offspring / parent based on higher ED value.
def selection(parent, trail_vector):
if(trail_vector[-1] > parent[-1]):
parent[:] = trail_vector
print "yes", parent
else:
print "no"
# bestParent returns the parent with then maximum ED value.
def bestParent(population):
Max_AED_value = population[0][-1]
Best_Parent_Index = population[0]
for parent in population:
if (parent[-1] > Max_AED_value):
Max_AED_value = parent[-1]
Best_Parent_Index = parent
return Best_Parent_Index
initialize_NP()
for GENERATION in range(STOPPING_ITERATION):
for i in range(NUMBER_OF_NP_CANDIDATES):
print "---------------------", "PARENT:", i+1 , "GENERATION:", GENERATION+1, "---------------------"
mutation(i)
crossover(NP[i], MV)
selection(NP[i], TV)
print NP[i]
TV[:] = []
print ""
print ""
best_parent = bestParent(NP)
print "best solution is: ", best_parent
images_for_gif = []
for frame_number in best_parent[:-1]:
images_for_gif.append(imageio.imread(READ_LOCATION + str(frame_number) + '.jpg'))
imageio.mimsave(GIF_WRITE_LOCATION, images_for_gif)