Ejemplo n.º 1
0
 def __init__(self, params_file, data_path):
     self.model = Model(params_file, batch_size=1)
     self.data_path = data_path
     self.batch_size = 1000
     self.data = []
     self.loadData()
Ejemplo n.º 2
0
 def __init__(self, params_file, data_path):
     self.model = Model(params_file, batch_size=1)
     self.data_path = data_path
     self.batch_size = 1000
     self.data = []
     self.loadData()
Ejemplo n.º 3
0
class DataBase:
    def __init__(self, params_file, data_path):
        self.model = Model(params_file, batch_size=1)
        self.data_path = data_path
        self.batch_size = 1000
        self.data = []
        self.loadData()

    def loadData(self):
        file_names = get_files(self.data_path)
        data = []
        for fn in file_names:
            with open(fn, 'r') as f:
                data += pickle.load(f)
        self.data = data

    def __save2database(self, imgIDs, deepIDfeas):
        """
        save data to database(pickle files)
        save style: [(id_1, fea_1), .... , (id_batch_size, fea_batch_size)]
        """
        data_folder = self.data_path
        batch_size = self.batch_size

        def save(count, imgIDs, deepIDfeas):
            fn = data_folder + count.__str__() + '.pkl'
            if os.path.isfile(fn):
                with open(fn, 'r') as f:
                    orgnl_data = pickle.load(f)
                    left_space = batch_size - len(orgnl_data)
                with open(fn, 'w') as f:
                    if left_space >= len(imgIDs):
                        pickle.dump(orgnl_data + zip(imgIDs, deepIDfeas), f)
                    else:
                        pickle.dump(
                            orgnl_data + zip(imgIDs[0:left_space],
                                             deepIDfeas[0:left_space]), f)
                        save(count + 1, imgIDs[left_space::],
                             deepIDfeas[left_space::])
            else:
                with open(fn, 'w') as f:
                    if batch_size >= len(imgIDs):
                        pickle.dump(zip(imgIDs, deepIDfeas), f)
                    else:
                        pickle.dump(
                            zip(imgIDs[0:batch_size],
                                deepIDfeas[0:batch_size]), f)
                        save(count + 1, imgIDs[batch_size::],
                             deepIDfeas[batch_size::])

        file_names = get_files(data_folder)
        n_files = len(file_names)
        count = n_files if n_files != 0 else 1
        save(count, imgIDs, deepIDfeas)

    def add_faces(self, imgIDs, img_paths):
        """
        model: retrieval_face.Model
        add faces to database,
        return invalid image ids
        """
        inval_ids = []
        imgs = []
        for path, id in zip(img_paths, imgIDs):
            aligned_path = send2align(path)
            aligned_arr = path2arr(aligned_path)
            if aligned_arr is None:
                inval_ids.append(id)
                imgIDs.remove(id)
            else:
                imgs.append(aligned_arr)
        if len(imgs) != 0:
            deepIDfeas = self.model.getID(imgs)
            self.__save2database(imgIDs, deepIDfeas)
        return inval_ids

    def search(self, fn, top_n=10, sim_thresh=None):
        """
        retrieval face from database,
        return top_n similar faces' imgIDs, return None if failed
        """
        if top_n > len(self.data):
            top_n = len(self.data)
        aligned_fn = send2align(fn)
        aligned_arr = path2arr(aligned_fn)
        if aligned_arr is None:
            print "align none."
            return None
        deepIDfea = self.model.getID([aligned_arr])[0]
        sims = [
            cosine_similarity(deepIDfea, item[1])[0][0] for item in self.data
        ]
        #print len(self.data), len(sims)
        for i in range(len(sims)):
            print sims[i], self.data[i][0]
        sort_index = np.argsort(-np.array(sims))
        result = []
        if sim_thresh is None:
            for index in np.nditer(sort_index):
                cur_id = self.data[index][0].split('-')[0]
                if cur_id not in result and len(result) < top_n:
                    result.append(cur_id)
            return result
        else:
            for index in np.nditer(sort_index):
                if sims[index] < sim_thresh:
                    break
                cur_id = self.data[index][0].split('-')[0]
                if cur_id not in result:
                    result.append(cur_id)
            return result
Ejemplo n.º 4
0
class DataBase:
    def __init__(self, params_file, data_path):
        self.model = Model(params_file, batch_size=1)
        self.data_path = data_path
        self.batch_size = 1000
        self.data = []
        self.loadData()

    def loadData(self):
        file_names = get_files(self.data_path)
        data = []
        for fn in file_names:
            with open(fn, "r") as f:
                data += pickle.load(f)
        self.data = data

    def __save2database(self, imgIDs, deepIDfeas):
        """
        save data to database(pickle files)
        save style: [(id_1, fea_1), .... , (id_batch_size, fea_batch_size)]
        """
        data_folder = self.data_path
        batch_size = self.batch_size

        def save(count, imgIDs, deepIDfeas):
            fn = data_folder + count.__str__() + ".pkl"
            if os.path.isfile(fn):
                with open(fn, "r") as f:
                    orgnl_data = pickle.load(f)
                    left_space = batch_size - len(orgnl_data)
                with open(fn, "w") as f:
                    if left_space >= len(imgIDs):
                        pickle.dump(orgnl_data + zip(imgIDs, deepIDfeas), f)
                    else:
                        pickle.dump(orgnl_data + zip(imgIDs[0:left_space], deepIDfeas[0:left_space]), f)
                        save(count + 1, imgIDs[left_space::], deepIDfeas[left_space::])
            else:
                with open(fn, "w") as f:
                    if batch_size >= len(imgIDs):
                        pickle.dump(zip(imgIDs, deepIDfeas), f)
                    else:
                        pickle.dump(zip(imgIDs[0:batch_size], deepIDfeas[0:batch_size]), f)
                        save(count + 1, imgIDs[batch_size::], deepIDfeas[batch_size::])

        file_names = get_files(data_folder)
        n_files = len(file_names)
        count = n_files if n_files != 0 else 1
        save(count, imgIDs, deepIDfeas)

    def add_faces(self, imgIDs, img_paths):
        """
        model: retrieval_face.Model
        add faces to database,
        return invalid image ids
        """
        inval_ids = []
        imgs = []
        for path, id in zip(img_paths, imgIDs):
            aligned_path = send2align(path)
            aligned_arr = path2arr(aligned_path)
            if aligned_arr is None:
                inval_ids.append(id)
                imgIDs.remove(id)
            else:
                imgs.append(aligned_arr)
        if len(imgs) != 0:
            deepIDfeas = self.model.getID(imgs)
            self.__save2database(imgIDs, deepIDfeas)
        return inval_ids

    def search(self, fn, top_n=10, sim_thresh=None):
        """
        retrieval face from database,
        return top_n similar faces' imgIDs, return None if failed
        """
        if top_n > len(self.data):
            top_n = len(self.data)
        aligned_fn = send2align(fn)
        aligned_arr = path2arr(aligned_fn)
        if aligned_arr is None:
            print "align none."
            return None
        deepIDfea = self.model.getID([aligned_arr])[0]
        sims = [cosine_similarity(deepIDfea, item[1])[0][0] for item in self.data]
        # print len(self.data), len(sims)
        for i in range(len(sims)):
            print sims[i], self.data[i][0]
        sort_index = np.argsort(-np.array(sims))
        result = []
        if sim_thresh is None:
            for index in np.nditer(sort_index):
                cur_id = self.data[index][0].split("-")[0]
                if cur_id not in result and len(result) < top_n:
                    result.append(cur_id)
            return result
        else:
            for index in np.nditer(sort_index):
                if sims[index] < sim_thresh:
                    break
                cur_id = self.data[index][0].split("-")[0]
                if cur_id not in result:
                    result.append(cur_id)
            return result