-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
167 lines (115 loc) · 4.34 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import matplotlib.pyplot as plt
import sys
from tqdm import tqdm
import random
import itertools
import os
import time
import math
from photo import Photo, transition_score
from slide import Slide
from slideshow import Slideshow
input_file = sys.argv[1]
def generate_similarity_matrix(photos):
"""
Convenience function to generate an adjacency matrix between all photos
"""
similarity = np.zeros((len(photos),len(photos)), dtype='uint8')
n = len(photos)
for i, photo_i in tqdm(enumerate(photos)):
for j, photo_j in enumerate(photos[i:]):
similarity[i, j] = transition_score(photo_i.tags, photo_j.tags)
return similarity
def plot_similarity_matrix(similarity):
plt.imshow(similarity)
plt.colorbar()
plt.show()
def parse_input(input_filename):
with open(input_filename, 'r') as f:
data = f.readlines()
photos = []
for i, meta in enumerate(data[1:]):
meta = meta.strip().split(" ")
orientation = meta[0]
tags = meta[2:]
photos.append(Photo(i, tags, orientation))
return photos
def find_greedy_match_photo(slide, photos):
"""
Find the best photo
"""
if len(photos) == 0:
return 0, -1
best_score = 0
best_match = random.randint(0, len(photos)-1)
comparison_slide = Slide()
for i, p in enumerate(photos):
comparison_slide.photos = [p]
comparison_slide.tags = p.tags
score = slide.score(comparison_slide)
if score > best_score:
best_score = score
best_match = i
return best_match, best_score
def find_greedy_match_vertical_photo(slide, photo, photos):
"""
Find the best vertical photo
"""
if len(photos) == 0:
return 0, -1
best_possible = len(slide.tags)
best_score = 0
best_match = random.randint(0, len(photos)-1)
for i, p in enumerate(photos):
score = slide.score(Slide(photo, p))
if score > best_score:
best_score = score
best_match = i
if score == best_possible:
break
return best_match, best_score
def greedy_slideshow(photos, rand_seed=time.time()):
random.seed(rand_seed)
random.shuffle(photos)
# Set up slideshow
vertical_photos = [p for p in photos if p.orientation == "V"]
horizontal_photos = [p for p in photos if p.orientation == "H"]
if len(horizontal_photos) == 0:
show = Slideshow([ Slide(vertical_photos.pop(), vertical_photos.pop()) ])
else:
show = Slideshow([ Slide(horizontal_photos.pop()) ])
n_slides = len(horizontal_photos) + len(vertical_photos)//2
# This runs for N^2 iterations...
pbar = tqdm(range(n_slides))
for i, _ in enumerate(pbar):
best_idx_h, score_h = find_greedy_match_photo(show.slides[-1], horizontal_photos)
best_idx_v, score_v = find_greedy_match_photo(show.slides[-1], vertical_photos)
if score_h > score_v:
# Next slide should be horizontal
show.add_slide(Slide(horizontal_photos.pop(best_idx_h)))
else:
# Next slide has vertical images
best_photo = vertical_photos.pop(best_idx_v)
# Find the next best vertical image to include
best_idx, _ = find_greedy_match_vertical_photo(show.slides[-1], best_photo, vertical_photos)
show.add_slide(Slide(best_photo, vertical_photos.pop(best_idx)))
avg_score = show.score/len(show.slides)
pbar.set_postfix(score=show.score, avg_score=avg_score)
return show
if __name__ == "__main__":
photos = parse_input(input_file)
base_name = os.path.splitext(os.path.basename(input_file))[0]
if len(sys.argv) > 2:
if sys.argv[2] == "similarity":
similarity = generate_similarity_matrix(photos)
np.save("./results/similarity_{}".format(base_name), similarity)
# Get slides and generate a slideshow
# Fast, scores about 1M
#slides = get_vertical_slides_simple(photos) + get_horizontal_slides(photos)
#show = greedy_slideshow_slides(slides)
# A bit slower, scores 1.185M
show = greedy_slideshow(photos)
print("Slideshow score: ", show.score)
os.makedirs("./results", exist_ok=True)
show.save("./results/{}_{}_{}.txt".format(base_name, show.score, time.time()))