def selectVerificationModel(self, threshold):
        best_log_path = '{}/{}/{}'.format(ConfigPath.best_model_path,
                                          self.current_date,
                                          self.test_set_type)
        log_file_list = [
            elem for elem in os.listdir(best_log_path) if elem.endswith('.log')
        ]

        #read informations in the log file
        best_model_acc_pair = {}
        for log_file in log_file_list:
            f = open('{}/{}'.format(best_log_path, log_file), 'r')
            data = f.read().splitlines()
            f.close()
            for line in data:
                parts = line.split(' ')
                if parts[0] not in best_model_acc_pair.keys():
                    best_model_acc_pair[parts[0]] = parts[1]

        best_verification_path = '{}/{}/{}'.format(
            ConfigPath.best_verification_model_path, self.current_date,
            self.test_set_type)
        utility.make_dirs(best_verification_path)
        self.selectBestModel(best_model_acc_pair.items(), \
                                    best_verification_path, \
                                    threshold)
    def findBestModel(self, model_num=1):
        out_model_path = '{}/{}/{}'.format(ConfigPath.best_model_path,
                                           self.current_date,
                                           self.test_set_type)
        best_model_acc_pair = []
        test_result_path = '{}/{}/{}'.format(ConfigPath.out_root_path,
                                             self.current_date,
                                             self.test_set_type)
        print(test_result_path)
        for root_path, folder_path, filename_path in os.walk(test_result_path):
            for filename in filename_path:
                if filename.find('roc_statistic_result'
                                 ) >= 0 and filename.endswith('log'):
                    log_path = '{}/{}'.format(root_path, filename)
                    best_pair = self.parseLogFile(log_path, model_num)
                    best_model_acc_pair += best_pair

        #rank accord to model name
        best_model_acc_pair.sort(key=lambda k: k[0])
        #save into file
        if len(best_model_acc_pair) > 0:
            utility.make_dirs(out_model_path)
            date_minu = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
            best_model_list = '{}/statistic_info_{}.log'.format(
                out_model_path, date_minu)
            f = open(best_model_list, 'w')
            for elem in best_model_acc_pair:
                #print (elem)
                f.write('{} {}\n'.format(elem[0], elem[1]))
            f.close()

        #select models
        self.selectBestModel(best_model_acc_pair, out_model_path)
Exemplo n.º 3
0
    def findBestGenderAccuracyModel(self, model_num=1):
        out_model_path = '{}/{}/{}'.format(ConfigPath.best_model_path,
                                           self.current_date,
                                           self.test_set_type)
        best_model_acc_pair = []
        test_result_path = '{}/{}/{}'.format(ConfigPath.out_root_path,
                                             self.current_date,
                                             self.test_set_type)
        print(test_result_path)
        for folder_path in os.listdir(test_result_path):
            root_folder_path = '{}/{}/distance_result'.format(
                test_result_path, folder_path)
            best_pair = self.getbestGenderAccuracy_pairs(
                root_folder_path, model_num)
            best_model_acc_pair += best_pair

        #rank accord to model name
        best_model_acc_pair.sort(key=lambda k: k[0])
        #save into file
        if len(best_model_acc_pair) > 0:
            utility.make_dirs(out_model_path)
            date_minu = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
            best_model_list = '{}/statistic_info_{}.log'.format(
                out_model_path, date_minu)
            f = open(best_model_list, 'w')
            for elem in best_model_acc_pair:
                #print (elem)
                f.write('{} {}\n'.format(elem[0], elem[1]))
            f.close()

        #select models
        self.selectBestModel(best_model_acc_pair, out_model_path)
Exemplo n.º 4
0
    def selectBestModel(self,
                        best_model_acc_pair,
                        out_model_path,
                        threshold=0.0):

        #sort according to model name
        best_model_acc_pair.sort(key=lambda k: k[0])
        for elem in best_model_acc_pair:
            model_name = elem[0]
            model_acc = float(elem[1])

            if model_acc >= threshold:
                print("{}\t{}".format(model_name, model_acc))
                train_date = model_name.split('_')[0]
                trainer = model_name.split('_')[-3]
                end_sign = model_name.find(trainer) - 1

                model_prefix = model_name[0:end_sign].split(train_date +
                                                            '_')[1]
                #print(model_prefix)
                loss_type = model_prefix.split('_')[0]
                index = loss_type.find('-')
                if index >= 0:
                    loss_type = loss_type[0:index]

                #set caffe model path and test result path deploy path
                model_path = '{}/{}/{}'.format(ConfigPath.model_root_path,
                                               loss_type, model_prefix)
                test_result_path = '{}/{}/distance_result'.format(
                    self.test_result_root_path, model_prefix)
                test_deploy_path = '{}/{}/train_file'.format(
                    self.test_result_root_path, model_prefix)

                #copy model
                copy_model_path = '{}/{}/models'.format(
                    out_model_path, model_prefix)
                utility.make_dirs(copy_model_path)
                for elem_model_name in os.listdir(model_path):
                    if elem_model_name.find(
                            model_name) >= 0 and elem_model_name.endswith(
                                '.caffemodel'):
                        copy_model_name = elem_model_name.replace(
                            train_date, self.current_date)
                        shutil.copy('{}/{}'.format(model_path, elem_model_name) , \
                        '{}/{}'.format(copy_model_path, copy_model_name))

                #copy roc curve
                # for roc_name in os.listdir(test_result_path):
                #     if roc_name.endswith('.png') and roc_name.find(model_name) >=0 :
                #         copy_roc_name = roc_name.replace(train_date, self.current_date)
                #         shutil.copy('{}/{}'.format(test_result_path, roc_name) ,\
                #                     '{}/{}'.format(copy_model_path, copy_roc_name))

                #copy deploy file
                copy_deploy_path = '{}/{}/train_file'.format(
                    out_model_path, model_prefix)
                utility.make_dirs(copy_deploy_path)
                deploy_file = os.listdir(test_deploy_path)[0]
                shutil.copy('{}/{}'.format(test_deploy_path, deploy_file), \
                            '{}/deploy.prototxt'.format(copy_deploy_path))
Exemplo n.º 5
0
    def testRun(self):
        model_info_pair = self.selectModelDeployList()
        if len(model_info_pair) == 0:
            print("no caffemodel to test..")
            return
        else:
            #set path
            out_path = '{}/Result_{}'.format(self.model_path, self.test_set)
            script_path = '{}/scripts/face_feature_scripts'.format(
                ConfigPath.local_caffe_path)
            utility.make_dirs(out_path)
            utility.make_dirs(script_path)

            #create param file
            for index, batch_model_info in enumerate(model_info_pair):
                param = self.createParamFile(batch_model_info, out_path)
                #save into files .prototxt
                param_path = '{}/test_config_file_E{}.prototxt'.format(
                    script_path, index)
                f = open(param_path, 'w')
                print(param, file=f)
                f.close()

                #create execute file .sh  --where test engine is
                execute_path = '{}/verification_test_E{}.sh'.format(
                    script_path, index)
                f = open(execute_path, 'w')
                f.write('cd {}\n'.format(ConfigPath.local_caffe_path))
                f.write('./build/tools/face_feature_extractor.bin {}'.format(
                    param_path))
                f.close()

                #execute file
                os.chmod(execute_path, stat.S_IRWXU)
                subprocess.call(execute_path, shell=True)

            #copy scripts back to out path
        copy_path = '{}/face_feature_scripts'.format(self.model_path)
        if not os.path.exists(copy_path):
            shutil.copytree(script_path,
                            '{}/face_feature_scripts'.format(self.model_path))

        self.curvePrecious(out_path)
    def runTest(self, model_path):

        self.model_path = model_path
        #find whether exists model to test  split models into batches
        select_model_list = self.selectModelList()

        if len(select_model_list[0]) == 0:
            #print("{} date's model is empty".format(self.select_date))
            return
        else:

            model_prefix = self.model_path.split('/')[-1]
            #get patch info
            begin = model_prefix.find(
                '_'
            )  #AMImageMeanCdata-b0.3s30_fc_0.35_112x96_b+FaceAdd_MobileFaceNet
            sign_pos = model_prefix.find('x')
            split_part2 = model_prefix[sign_pos:]  #x96_b+FaceAdd_MobileFaceNet
            end = split_part2.find('_')
            patch_info = '{}{}'.format(model_prefix[begin + 1:sign_pos],
                                       split_part2[0:end])  #fc_0.35_112 x96

            #set dst save path
            output_path = '{}/{}/{}/{}'.format(ConfigPath.out_root_path,
                                               self.current_date,
                                               self.test_set_type,
                                               model_prefix)
            output_deploy_path = '{}/train_file'.format(output_path)
            output_result_path = '{}/distance_result'.format(output_path)
            output_script_path = '{}/scripts'.format(output_path)
            utility.make_dirs(output_deploy_path)
            utility.make_dirs(output_result_path)
            utility.make_dirs(output_script_path)

            #train net
            net_name = model_prefix.split('_')[-1]
            deploy_file = '{}_deploy.prototxt'.format(net_name)
            src_deploy_path = '{}/{}'.format(ConfigPath.deploy_root_path,
                                             deploy_file)
            dst_deploy_path = '{}/{}'.format(output_deploy_path, deploy_file)

            # change deploy's input and save into dst path
            patch_info_dict = getPatchInfoFunc.splitPatchInfo(patch_info)
            utility.change_input(src_deploy_path, dst_deploy_path,
                                 patch_info_dict['height'],
                                 patch_info_dict['width'])

            #ceeate the config file
            print(model_prefix)
            for index, elem in enumerate(select_model_list):

                #no means and scale
                param = self.create_param(elem, patch_info, dst_deploy_path,
                                          output_result_path)
                #save into files .prototxt
                param_path = '{}/test_config_file_E{}.prototxt'.format(
                    output_script_path, index)
                f = open(param_path, 'w')
                print(param, file=f)
                f.close()

                #create execute file .sh  --where test engine is
                execute_path = '{}/verification_test_E{}.sh'.format(
                    output_script_path, index)
                f = open(execute_path, 'w')
                f.write('cd {}\n'.format(ConfigPath.local_caffe_path))
                f.write('./build/tools/face_feature_extractor.bin {}'.format(
                    param_path))
                f.close()

                #execute file
                os.chmod(execute_path, stat.S_IRWXU)
                subprocess.call(execute_path, shell=True)

        #save roc curve result into log
        self.curvePrecious(output_result_path)