-
Notifications
You must be signed in to change notification settings - Fork 0
/
variety.go
245 lines (218 loc) · 6.74 KB
/
variety.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2018 Fabian Wenzelmann
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gomosaic
import (
"image"
"math/rand"
"time"
log "github.com/sirupsen/logrus"
)
func computeSingleHeap(storage ImageStorage, metric ImageMetric, i, j int, target *ImageHeap) error {
numImages := storage.NumImages()
var imageID ImageID
for ; imageID < numImages; imageID++ {
dist, distErr := metric.Compare(storage, imageID, i, j)
if distErr != nil {
log.WithFields(log.Fields{
log.ErrorKey: distErr,
"image": imageID,
"tileY": i,
"tileX": j,
}).Error("Can't compute metric value, ignoring it")
continue
}
target.Add(imageID, dist)
}
return nil
}
// ComputeHeaps computes the image heap for each tile given k (the number of
// images to store in each heap).
//
// Metric will not be initialized, that must happen before.
func ComputeHeaps(storage ImageStorage, metric ImageMetric, query image.Image, dist TileDivision,
k, numRoutines int, progress ProgressFunc) ([][]*ImageHeap, error) {
// concurrently compute heaps
// first, create all heapss
heaps := make([][]*ImageHeap, len(dist))
for i, col := range dist {
size := len(col)
heapsCol := make([]*ImageHeap, size)
// initialize heap
for j := 0; j < size; j++ {
heapsCol[j] = NewImageHeap(k)
}
heaps[i] = heapsCol
}
type job struct {
i, j int
}
jobs := make(chan job, BufferSize)
errors := make(chan error, BufferSize)
// set later
var err error
for w := 0; w < numRoutines; w++ {
go func() {
for next := range jobs {
i, j := next.i, next.j
target := heaps[i][j]
errors <- computeSingleHeap(storage, metric, i, j, target)
}
}()
}
// start jobs
go func() {
for i, col := range dist {
for j := range col {
jobs <- job{i, j}
}
}
close(jobs)
}()
// wait until done
numDone := 0
for _, col := range dist {
for j := 0; j < len(col); j++ {
nextErr := <-errors
if nextErr != nil && err == nil {
err = nextErr
}
numDone++
if progress != nil {
progress(numDone)
}
}
}
if err != nil {
return nil, err
}
return heaps, nil
}
// HeapSelector is used to select the actual images after creating the image
// heaps.
type HeapSelector interface {
Select(storage ImageStorage, query image.Image, dist TileDivision, heaps [][]*ImageHeap) ([][]ImageID, error)
}
// GenHeapViews can be used to transform the image heaps into the actual list
// of images in that heap.
// It's only a shortcut calling GetView on each heap.
func GenHeapViews(heaps [][]*ImageHeap) [][][]ImageHeapEntry {
res := make([][][]ImageHeapEntry, len(heaps))
for i, col := range heaps {
size := len(col)
colRes := make([][]ImageHeapEntry, size)
for j := 0; j < size; j++ {
heap := heaps[i][j]
colRes[j] = heap.GetView()
}
res[i] = colRes
}
return res
}
// HeapImageSelector implements ImageSelector. It first computes the image
// heaps given the metric and then uses the provided HeapSelector to select
// the actual images from the heaps.
type HeapImageSelector struct {
Metric ImageMetric
Selector HeapSelector
K int
NumRoutines int
}
// NewHeapImageSelector returns a new selector.
// Metric is the image metric that is used for the image heaps, selector is
// used to select the actual images from the heaps. k is the number of images
// stored in each image heap (that is the k best images are stored in the
// heaps).
// NumRoutines is the number of things that happen concurrently (not exactly,
// but guidance level),
func NewHeapImageSelector(metric ImageMetric, selector HeapSelector, k, numRoutines int) *HeapImageSelector {
if numRoutines <= 0 {
numRoutines = 1
}
return &HeapImageSelector{
Metric: metric,
Selector: selector,
K: k,
NumRoutines: numRoutines,
}
}
// Init just calls InitStorage on the provided image metric.
func (sel *HeapImageSelector) Init(storage ImageStorage) error {
return sel.Metric.InitStorage(storage)
}
// SelectImages first calls InitTiles on the provided metric, then computes
// the heaps and applies the selector on the heaps.
func (sel *HeapImageSelector) SelectImages(storage ImageStorage,
query image.Image, dist TileDivision, progress ProgressFunc) ([][]ImageID, error) {
if initErr := sel.Metric.InitTiles(storage, query, dist); initErr != nil {
return nil, initErr
}
// compute heaps
heaps, heapsErr := ComputeHeaps(storage, sel.Metric, query, dist, sel.K,
sel.NumRoutines, progress)
if heapsErr != nil {
return nil, heapsErr
}
// apply selector
return sel.Selector.Select(storage, query, dist, heaps)
}
// RandomHeapSelector implements HeapSelector by using just a random element
// from each heap.
//
// Note that instances of this selector are not safe for concurrent use.
type RandomHeapSelector struct {
randGen *rand.Rand
}
// NewRandomHeapSelector returns a new random selector.
// The provided random generator is used to generate random numbers. You can
// use nil and a random generator will be created.
//
// Note that rand.Rand instances are not safe for concurrent use.
// Thus using the same generator on two instances that run concurrently is
// not allowed.
func NewRandomHeapSelector(randGen *rand.Rand) *RandomHeapSelector {
if randGen == nil {
seed := time.Now().UnixNano()
randGen = rand.New(rand.NewSource(seed))
}
return &RandomHeapSelector{randGen}
}
// Select implements the HeapSelector interface, it selects the random images.
func (sel *RandomHeapSelector) Select(storage ImageStorage, query image.Image, dist TileDivision, heaps [][]*ImageHeap) ([][]ImageID, error) {
res := make([][]ImageID, len(dist))
views := GenHeapViews(heaps)
for i, col := range dist {
size := len(col)
colDist := make([]ImageID, size)
for j := 0; j < size; j++ {
view := views[i][j]
// select a random one
n := len(view)
if n == 0 {
res[i][j] = NoImageID
} else {
// there are elements
index := sel.randGen.Intn(n)
colDist[j] = view[index].Image
}
}
res[i] = colDist
}
return res, nil
}
// RandomHeapImageSelector returns a HeapImageSelector using a random selection.
// Thus it can be used as an ImageSelector.
func RandomHeapImageSelector(metric ImageMetric, k, numRoutines int) *HeapImageSelector {
heapSel := NewRandomHeapSelector(nil)
return NewHeapImageSelector(metric, heapSel, k, numRoutines)
}