Example #1
0
File: DSQ.py Project: stoneyang/DRQ
    def GetFeature(self, dataset):
        if os.path.exists(self.SESSION_SAVE_PATH + '.meta'):
            with tf.Session(config=self._config) as sess:
                self.InitVariables()
                self._saver = tf.train.Saver()
                self._saver.restore(sess, self.SESSION_SAVE_PATH)
                PrintWithTime("Restored model from " + self.SESSION_SAVE_PATH)
                database = Object()

                dim = self.NetPQ.X.get_shape().as_list()[1]

                Nb = dataset.DataNum
                database_feature = np.zeros([Nb, dim], dtype=np.float32)
                database.label = np.zeros([Nb, self._classNum], dtype=np.int16)

                codes = np.zeros([Nb, self._stackLevel], np.int32)

                total_db = (Nb // self._batchSize) + 1
                with trange(total_db, ncols=50) as t:
                    for i in t:
                        idx = np.arange(start=i * self._batchSize,
                                        stop=np.minimum(
                                            Nb, (i + 1) * self._batchSize),
                                        step=1)
                        inp, label = dataset.Get(idx)
                        num = inp.shape[0]
                        database.label[i *
                                       self._batchSize:(i * self._batchSize +
                                                        num)] = label
                        if inp.shape[0] != self._batchSize:
                            placeholder = np.zeros([
                                self._batchSize - inp.shape[0], inp.shape[1],
                                inp.shape[2], inp.shape[3]
                            ])
                            inp = np.concatenate((inp, placeholder))

                        out, hardCode = sess.run(
                            [self.NetPQ.X, self.NetPQ.HardCode],
                            {self.Input: inp})
                        hardCode = sess.run(self.NetPQ.HardCode,
                                            {self.Input: inp})
                        codes[i * self._batchSize:(i * self._batchSize) +
                              num] = np.array(hardCode, np.int32).T[:num]
                        database_feature[i *
                                         self._batchSize:(i *
                                                          self._batchSize) +
                                         num] = out[:num]
                database.output = database_feature

                # [N, M]
                database.codes = codes
                codebook = sess.run(self.NetPQ.Codebook)
                scale = sess.run(self.NetPQ.CodebookScale)
            return database, codebook, scale
Example #2
0
File: DPQ.py Project: stoneyang/DPQ
    def GetRetrievalMat(self, queryX, queryY, dataset):
        self.R = self._recallatR if self._recallatR > 0 else dataset.DataNum
        if os.path.exists(SESSION_SAVE_PATH + '.meta'):
            with tf.Session(config=self._config) as sess:
                self.InitVariables()
                self._saver = tf.train.Saver()
                self._saver.restore(sess, SESSION_SAVE_PATH)
                PrintWithTime("Restored model from " + SESSION_SAVE_PATH)

                query = Object()
                database = Object()
                query.label = queryY

                Nq = queryX.shape[0]

                dim = self.NetPQ.X.get_shape().as_list()[1]

                query_feature = np.zeros([Nq, dim], np.float16)
                for i in range((Nq // self._batchSize) + 1):
                    inp = queryX[i * self._batchSize:(i + 1) * self._batchSize]
                    num = inp.shape[0]
                    if inp.shape[0] != self._batchSize:
                        placeholder = np.zeros([
                            self._batchSize - inp.shape[0], inp.shape[1],
                            inp.shape[2], inp.shape[3]
                        ])
                        inp = np.concatenate((inp, placeholder))

                    out = sess.run(self.NetPQ.X, {self.Input: inp})
                    query_feature[i * self._batchSize:(i * self._batchSize) +
                                  num] = out[:num]
                query.output = query_feature

                Nb = dataset.DataNum
                database.label = np.zeros([Nb, self._classNum], dtype=np.int16)

                codes = np.zeros([Nb, self._stackLevel], np.int32)

                total_db = (Nb // self._batchSize) + 1
                for i in range(total_db):
                    idx = np.arange(start=i * self._batchSize,
                                    stop=np.minimum(Nb,
                                                    (i + 1) * self._batchSize),
                                    step=1)
                    inp, label = dataset.Get(idx)
                    print(inp.shape, label.shape)
                    num = inp.shape[0]
                    database.label[i * self._batchSize:(i * self._batchSize +
                                                        num)] = label
                    if inp.shape[0] != self._batchSize:
                        placeholder = np.zeros([
                            self._batchSize - inp.shape[0], inp.shape[1],
                            inp.shape[2], inp.shape[3]
                        ])
                        inp = np.concatenate((inp, placeholder))

                    hardCode = sess.run(self.NetPQ.HardCode, {self.Input: inp})
                    codes[i * self._batchSize:(i * self._batchSize) +
                          num] = np.array(hardCode, np.int32).T[:num]
                    ProgressBar((i + 1) / total_db)

                # [N, M]
                database.codes = codes
                codebook = sess.run(self.NetPQ.Codebook)
                # np.save('database_codes_DSQ', codes)
            db = mAP.Quantize_AQ(database.codes, codebook, 4).T

            del dataset
            id_all = np.zeros([query.output.shape[0], self.R], np.int)
            retrieval_mat = np.zeros([query.output.shape[0], self.R], np.bool)
            for j in range(query.output.shape[0] // 50 + 1):
                q = query.output[j * 50:(j + 1) * 50]
                d = -np.dot(q, db)
                ids = np.argsort(d, 1)
                for i in range(d.shape[0]):
                    label = query.label[j * 50 + i, :]
                    label[label == 0] = -1
                    idx = ids[i, :]
                    imatch = np.sum(database.label[idx[0:self.R], :] == label,
                                    1) > 0
                    id_all[j * 50 + i] = idx[:self.R]
                    retrieval_mat[j * 50 + i] = imatch[:self.R]
            np.save('retrievalMat_' + self.DatasetName, retrieval_mat)
            np.save('ids', id_all)
            return retrieval_mat, id_all
Example #3
0
File: DPQ.py Project: stoneyang/DPQ
    def Evaluate(self, queryX, queryY, dataset):
        print(self._recallatR if self._recallatR > 0 else 'all')
        if os.path.exists(SESSION_SAVE_PATH + '.meta'):
            with tf.Session(config=self._config) as sess:
                self.InitVariables()
                self._saver = tf.train.Saver()
                self._saver.restore(sess, SESSION_SAVE_PATH)
                PrintWithTime("Restored model from " + SESSION_SAVE_PATH)

                query = Object()
                database = Object()
                query.label = queryY

                Nq = queryX.shape[0]

                dim = self.NetPQ.X.get_shape().as_list()[1]

                query.output = np.zeros([Nq, dim], np.float16)
                for i in range((Nq // self._batchSize) + 1):
                    inp = queryX[i * self._batchSize:(i + 1) * self._batchSize]
                    num = inp.shape[0]
                    if inp.shape[0] != self._batchSize:
                        placeholder = np.zeros([
                            self._batchSize - inp.shape[0], inp.shape[1],
                            inp.shape[2], inp.shape[3]
                        ])
                        inp = np.concatenate((inp, placeholder))

                    out = sess.run(self.NetPQ.X, {self.Input: inp})
                    query.output[i * self._batchSize:(i * self._batchSize) +
                                 num] = out[:num]

                Nb = dataset.DataNum
                database_feature = np.zeros([Nb, dim], dtype=np.float16)
                database.label = np.zeros([Nb, self._classNum], dtype=np.int16)

                database.codes = np.zeros([Nb, self._stackLevel], np.int32)

                start = time.time()
                print('Encoding database')
                total_db = (Nb // self._batchSize) + 1
                for i in range(total_db):
                    idx = np.arange(start=i * self._batchSize,
                                    stop=np.minimum(Nb,
                                                    (i + 1) * self._batchSize),
                                    step=1)
                    inp, label = dataset.Get(idx)
                    print(inp.shape, label.shape)
                    num = inp.shape[0]
                    database.label[i * self._batchSize:(i * self._batchSize +
                                                        num)] = label
                    if inp.shape[0] != self._batchSize:
                        placeholder = np.zeros([
                            self._batchSize - inp.shape[0], inp.shape[1],
                            inp.shape[2], inp.shape[3]
                        ])
                        inp = np.concatenate((inp, placeholder))

                    hardCode = sess.run(self.NetPQ.HardCode, {self.Input: inp})
                    database.codes[i * self._batchSize:(i * self._batchSize) +
                                   num] = np.array(hardCode, np.int32).T[:num]
                    database_feature[i *
                                     self._batchSize:(i * self._batchSize) +
                                     num] = out[:num]
                    ProgressBar((i + 1) / total_db)

                end = time.time()
                print('Encoding Complete')
                print('Time:', end - start)
                print('Average time for single sample:')
                print((end - start) / Nb)
                database.output = database_feature

                del dataset

                codebook = sess.run(self.NetPQ.Codebook)

                res = mAP(
                    codebook, self._recallatR if self._recallatR > 0 else
                    database.codes.shape[0], database)

                return res.AQD_mAP(query)
Example #4
0
File: DSQ.py Project: stoneyang/DRQ
    def GetFeature(self, queryX, queryY, dataset):
        if os.path.exists(self.SESSION_SAVE_PATH + '.meta'):
            with tf.Session(config=self._config) as sess:
                self.InitVariables()
                self._saver = tf.train.Saver()
                self._saver.restore(sess, self.SESSION_SAVE_PATH)
                PrintWithTime("Restored model from " + self.SESSION_SAVE_PATH)

                query = Object()
                database = Object()
                query.label = queryY

                Nq = queryX.shape[0]

                dim = self.NetPQ.X.get_shape().as_list()[1]

                query_feature = np.zeros([Nq, dim], np.float32)
                for i in range((Nq // self._batchSize) + 1):
                    inp = queryX[i * self._batchSize:(i + 1) * self._batchSize]
                    num = inp.shape[0]
                    if inp.shape[0] != self._batchSize:
                        placeholder = np.zeros([
                            self._batchSize - inp.shape[0], inp.shape[1],
                            inp.shape[2], inp.shape[3]
                        ])
                        inp = np.concatenate((inp, placeholder))

                    out = sess.run(self.NetPQ.X, {self.Input: inp})
                    query_feature[i * self._batchSize:(i * self._batchSize) +
                                  num] = out[:num]
                query.output = query_feature

                Nb = dataset.DataNum
                database_feature = np.zeros([Nb, dim], dtype=np.float32)
                database.label = np.zeros([Nb, self._classNum], dtype=np.int16)

                codes = np.zeros([Nb, self._stackLevel], np.int32)

                start = time.time()
                print('Encoding database')
                total_db = (Nb // self._batchSize) + 1
                with trange(total_db, ncols=50) as t:
                    for i in t:
                        idx = np.arange(start=i * self._batchSize,
                                        stop=np.minimum(
                                            Nb, (i + 1) * self._batchSize),
                                        step=1)
                        inp, label = dataset.Get(idx)
                        num = inp.shape[0]
                        database.label[i *
                                       self._batchSize:(i * self._batchSize +
                                                        num)] = label
                        if inp.shape[0] != self._batchSize:
                            placeholder = np.zeros([
                                self._batchSize - inp.shape[0], inp.shape[1],
                                inp.shape[2], inp.shape[3]
                            ])
                            inp = np.concatenate((inp, placeholder))

                        out, hardCode = sess.run(
                            [self.NetPQ.X, self.NetPQ.HardCode],
                            {self.Input: inp})
                        hardCode = sess.run(self.NetPQ.HardCode,
                                            {self.Input: inp})
                        codes[i * self._batchSize:(i * self._batchSize) +
                              num] = np.array(hardCode, np.int32).T[:num]
                        database_feature[i *
                                         self._batchSize:(i *
                                                          self._batchSize) +
                                         num] = out[:num]

                end = time.time()
                print('Encoding Complete')
                print('Time:', end - start)
                print('Average time for single sample:')
                print((end - start) / Nb)
                database.output = database_feature
                scale = sess.run(self.NetPQ.CodebookScale)
                # [N, M]
                database.codes = codes
                return query, database, scale