-
Notifications
You must be signed in to change notification settings - Fork 3
/
analyze_hash.go
291 lines (252 loc) · 6.57 KB
/
analyze_hash.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main // import "pixur.org/pixur/tools/analyze_hash"
import (
"container/heap"
"context"
"encoding/binary"
"encoding/csv"
"flag"
"log"
"os"
"sort"
"strconv"
"pixur.org/pixur/be/imaging"
"pixur.org/pixur/be/schema"
sdb "pixur.org/pixur/be/schema/db"
tab "pixur.org/pixur/be/schema/tables"
"pixur.org/pixur/be/server/config"
)
var allMedians = []float32{
-1.726552, 2.964345, -0.13845398, -3.3160644, 0.51064676, -0.11798997, 4.7331654e-30, 1.4199496e-29,
-3.9443045e-30, 4.9267707, -0.24924812, -6.854629, 3.9443045e-30, 7.099748e-30, 3.1554436e-29, -0.5742262,
0.9638992, -0.4710116, -0.065479964, 4.7331654e-30, -7.099748e-30, 0.30259687, -0.33178842, -0.42226526,
-7.888609e-31, -1.4325413, -0.014203334, -1.0259316, -6.661338e-16, 1.5777218e-30, 0.034086976, -0.18520246,
0.5318168, -0.527036, -2.2088105e-29, -0.4211659, 0.5060927, 0.09501513, -0.35342145, 0.046108875,
-0.08382997, -0.77674377, -0.034913637, 1.0007774, -0.10231467, 7.888609e-31, -0.072848566, 0.60153186,
-0.03843284, -1.2586465, -2.2088105e-29, 0.41432515, -0.24322185, 0.011488877, 0.008951891, 7.888609e-30,
0.3603853, -0.14286107, -0.44190237, 0.09469462, -7.099748e-30, 8.67747e-30, 0.44547608, 0.24279852}
type imgDiff struct {
leftBits, rightBits uint64
diff int
ids pair
}
type pair struct {
leftId, rightId int64
}
type imgDiffs []imgDiff
func (id imgDiffs) Len() int {
return len(id)
}
func (id imgDiffs) Less(i, j int) bool {
return id[i].diff < id[j].diff
}
func (id imgDiffs) Swap(i, j int) {
id[i], id[j] = id[j], id[i]
}
func (id *imgDiffs) Push(x interface{}) {
*id = append(*id, x.(imgDiff))
}
func (id *imgDiffs) Pop() interface{} {
old := *id
n := len(old)
x := old[n-1]
*id = old[0 : n-1]
return x
}
func run(ctx context.Context) error {
db, err := sdb.Open(ctx, config.Conf.DbName, config.Conf.DbConfig)
if err != nil {
return err
}
defer db.Close()
pis, err := getIdents(db)
if err != nil {
return err
}
log.Println("meds", allMedians)
hist, bitCounts := hashBitHistogram(pis)
log.Println("Found", hist, bitCounts)
comp1 := findSimilar(pis)
firstPairs := make(map[pair]*imgDiff, comp1.Len())
for comp1.Len() > 0 {
d := heap.Pop(comp1).(imgDiff)
firstPairs[d.ids] = &d
}
allMedians = findMedians(pis)
updateHashes(pis)
comp2 := findSimilar(pis)
secondPairs := make(map[pair]*imgDiff, comp2.Len())
for comp2.Len() > 0 {
d := heap.Pop(comp2).(imgDiff)
secondPairs[d.ids] = &d
}
allDiffs, firstOnly, secondOnly := compareHashes(firstPairs, secondPairs)
log.Println(len(allDiffs), len(firstOnly), len(secondOnly))
var comp = make(imgDiffs, 0)
heap.Init(&comp)
for _, d := range secondOnly {
heap.Push(&comp, d)
}
if err := writeDiffs(&comp); err != nil {
return err
}
return nil
}
type float32Slice []float32
func (f float32Slice) Len() int { return len(f) }
func (f float32Slice) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f float32Slice) Less(i, j int) bool { return f[i] < f[j] }
func findMedians(pis []*schema.PicIdent) []float32 {
vals := make([][]float32, 64)
meds := make([]float32, 0, 64)
for _, pi := range pis {
for i := 0; i < 64; i++ {
vals[i] = append(vals[i], pi.Dct0Values[i])
}
}
for _, val := range vals {
sort.Sort(float32Slice(val))
// handles even and odd lists
left := val[(len(val)-1)/2]
right := val[(len(val))/2]
meds = append(meds, (left+right)/2)
}
return meds
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func compareHashes(first, second map[pair]*imgDiff) (allDiffs, firstOnly, secondOnly imgDiffs) {
allKeys := make(map[pair]struct{}, len(first))
for k, _ := range first {
allKeys[k] = struct{}{}
}
for k, _ := range second {
allKeys[k] = struct{}{}
}
for k, _ := range allKeys {
left, leftPresent := first[k]
right, rightPresent := second[k]
if leftPresent && rightPresent {
// we don't care if one of them was not a match anyways
if (left.diff > 10) == (right.diff > 10) {
continue
}
allDiffs = append(allDiffs, imgDiff{
diff: left.diff - right.diff,
ids: k,
})
} else if leftPresent && !rightPresent {
if left.diff > 10 {
continue
}
firstOnly = append(firstOnly, *left)
} else if !leftPresent && rightPresent {
if right.diff > 10 {
continue
}
secondOnly = append(secondOnly, *right)
} else {
panic(k)
}
}
return
}
func updateHashes(pis []*schema.PicIdent) {
for _, pi := range pis {
var hash uint64
for i := uint(0); i < 64; i++ {
if pi.Dct0Values[i] > allMedians[i] {
hash |= 1 << i
}
}
binary.BigEndian.PutUint64(pi.Value, hash)
}
}
func writeDiffs(h heap.Interface) error {
f, err := os.Create("diffs.csv")
if err != nil {
return err
}
defer f.Close()
w := csv.NewWriter(f)
i := 0
for h.Len() > 0 {
d := heap.Pop(h).(imgDiff)
if i++; i < 2000 {
log.Println(d.diff, schema.Varint(d.ids.leftId), schema.Varint(d.ids.rightId))
}
dd, dl, dr := strconv.Itoa(d.diff), strconv.FormatInt(d.ids.leftId, 10), strconv.FormatInt(d.ids.rightId, 10)
if err := w.Write([]string{dd, dl, dr}); err != nil {
return err
}
}
w.Flush()
if err := w.Error(); err != nil {
return err
}
return nil
}
func findSimilar(pis []*schema.PicIdent) heap.Interface {
var comp = make(imgDiffs, 0)
heap.Init(&comp)
for i := 0; i < len(pis); i++ {
leftBits := binary.BigEndian.Uint64(pis[i].Value)
if imaging.CountBits(leftBits) < 20 {
continue // skip, probably not worth our time
}
for k := i + 1; k < len(pis)-1; k++ {
rightBits := binary.BigEndian.Uint64(pis[k].Value)
if imaging.CountBits(rightBits) < 20 {
continue // skip, probably not worth our time
}
if count := imaging.CountBits(leftBits ^ rightBits); count <= 20 {
heap.Push(&comp, imgDiff{
//leftBits: leftBits,
//rightBits: rightBits,
diff: count,
ids: pair{
pis[i].PicId,
pis[k].PicId},
})
}
}
}
return &comp
}
func hashBitHistogram(pis []*schema.PicIdent) ([]int, map[int]int) {
hist := make([]int, 64)
histCount := make(map[int]int)
for _, pi := range pis {
bits := binary.BigEndian.Uint64(pi.Value)
bitCount := 0
for i := uint(0); i < 64; i++ {
if (bits & (1 << i)) > 0 {
hist[i]++
bitCount++
}
}
histCount[bitCount]++
}
return hist, histCount
}
func getIdents(db sdb.DB) ([]*schema.PicIdent, error) {
j, err := tab.NewJob(context.Background(), db)
if err != nil {
return nil, err
}
defer j.Rollback()
pis, err := j.FindPicIdents(sdb.Opts{})
if err != nil {
return nil, err
}
return pis, nil
}
func main() {
flag.Parse()
if err := run(context.Background()); err != nil {
log.Println(err)
}
}