@@ -0,0 +1,3102 @@
+# -*- coding: gbk -*-%0A%0A'''%0AWeihao Kong, Wu-Jun Li, 2012. Double-Bit Quantization for Hashing.%0A'''%0A%0Aimport numpy as np%0A%0Aclass DBQ(object):%0A    def __init__(self):%0A        self.thresholds_list = %5B%5D%0A%0A    def gen_thresholds(self, proj_mat_list):%0A        '''%0A        Make sure input proj_mat_list(n_table, n_bit, n_point) is subjected to Gaussian distribution.%0A        '''%0A        cnt = 0%0A        for proj_mat in proj_mat_list:%0A            for col in proj_mat:%0A                s1 = %5B%5D%0A                s2 = %5B%5D%0A                s3 = %5B%5D%0A                for e in col:%0A                    if e %3E 0:%0A                        s3.append(e)%0A                    else:%0A                        s1.append(e)%0A                s1.sort(reverse=True)%0A                s3.sort()%0A                f_max = 0%0A                i = 0%0A                j = 0%0A                sum1 = sum(s1)%0A                n_s1 = len(s1)%0A                sum2 = 0%0A                sum3 = sum(s3)%0A                n_s3 = len(s3)%0A                left = 0%0A                right = 0%0A                s1_max = -100%0A                s2_max = -100%0A                while(i %3C len(s1) or j %3C len(s3)):%0A                    if sum2 %3C= 0 and j %3C len(s3):%0A                        s2.append(s3%5Bj%5D)%0A                        sum2 += s3%5Bj%5D%0A                        sum3 -= s3%5Bj%5D%0A                        s2_max = s3%5Bj%5D%0A                        j += 1%0A                        n_s3 -= 1%0A                    elif sum2 %3E 0 and i %3C len(s1):%0A                        s2.append(s1%5Bi%5D)%0A                        sum2 += s1%5Bi%5D%0A                        sum1 -= s1%5Bi%5D%0A                        i += 1%0A                        n_s1 -= 1%0A                        if i %3C len(s1):%0A                            s1_max = s1%5Bi%5D%0A                    else:%0A                        pass%0A                    part1 = part2 = 0%0A                    if n_s1 %3E 0:%0A                        part1 = sum1 ** 2 / n_s1%0A                    if n_s3 %3E 0:%0A                        part2 = sum3 ** 2 / n_s3%0A                    f = part1 + part2%0A                    if f %3E f_max:%0A                        left = s1_max%0A                        right = s2_max%0A                        f_max = f%0A                        # print 'left: %25f, right: %25f, f_max: %25f' %25 (left, right, f_max)%0A                # print 'final =%3E left: %25f, right: %25f, f_max: %25f' %25 (left, right, f_max)%0A                self.thresholds_list.extend( (left, right) )%0A            cnt += 1%0A            print 'tabel %25d complete.' %25 cnt%0A        self.thresholds_list = np.array(self.thresholds_list).reshape(proj_mat_list.shape%5B0%5D, 2, proj_mat_list.shape%5B1%5D)%0A%0A    def quantization(self, proj_arr, thresholds):%0A        '''%0A        proj_arr: (n_bit, )%0A        thresholds: (n_bit, 2)%0A        '''%0A        assert proj_arr.shape%5B0%5D == thresholds.shape%5B0%5D%0A        bit_str = ''%0A        for val, left_right in zip(proj_arr, thresholds):%0A            left, right = left_right%0A            if val %3C= left:%0A                bit_str += '01'%0A            elif val %3E left and val %3C= right:%0A                bit_str += '00'%0A            else:%0A                bit_str += '10'%0A        return bit_str%0A

add double bit quantization