-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathactive_learning.py
69 lines (61 loc) · 2.07 KB
/
active_learning.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
import metaomr
from metaomr import preprocessing, forest, bitimage, opencl
import gc
from forest_config import COLOR_LABELS
import sys
import os
import numpy
IMSLP = '../IMSLP'
all_imslp = list(numpy.random.choice(os.listdir(IMSLP), 10))
CORPUS = [os.path.join(IMSLP,p) for p in all_imslp]
images = []
class_num = []
for score in CORPUS:
try:
score = metaomr.open(score)
except Exception:
continue
while len(score):
page = score[0]
preprocessing.process(page)
if type(page.staff_dist) is not tuple and page.staff_dist is not None and page.staff_dist >= 8:
image, scale = forest.scale_img(page)
pred = forest.predict(forest.classifier, image, get_classes=False)
images.append(bitimage.as_hostimage(image))
class_num.append(pred.get())
del image
del page
del score[0]
opencl.q.finish()
gc.collect()
if len(images) > 25:
break
import numpy as np
nums=np.concatenate([c.ravel() for c in class_num])
NUM_PATCHES = 100
CUTOFF = np.percentile(nums, 100.0*NUM_PATCHES/len(nums))
print 'cutoff', CUTOFF, 'num patches', sum([np.sum(c <= CUTOFF) for c in class_num])
i=0
def get_images():
global i
for img, cls in zip(images, class_num):
y, x = np.where(cls <= CUTOFF)
img = np.pad(img, 35/2, 'constant', constant_values=0)
y = y + 35/2
x = x + 35/2
for yval, xval in zip(y, x):
c=35/2
patch = np.empty((35, 35, 3), np.uint8)
img_patch = img[yval-c:yval+c+1, xval-c:xval+c+1]
patch[:] = np.where(img_patch, 0, 255)[:,:,None]
patch[c-5:c+6,c,0] ^= 0xFF
patch[c,c-5:c+6,0] ^= 0xFF
#patch[[c-1,c-1,c+1,c+1],[c-1,c+1]*2,0] ^= 0xFF
patch[c-1:c+2, c-1:c+2, 1] ^= 0xFF
i += 1
yield img_patch, patch
if i == NUM_PATCHES: return
outfile = open('unlabeled_patches', 'a')
for img, p in get_images():
outfile.write(''.join(map(str, (img.ravel() != 0).astype(int))) + '\n')
outfile.close()