Best Python code snippet using hypothesis
validate_kabi.py
Source:validate_kabi.py
1import torch2import argparse3import torch.nn as nn4from networks.rps_net_mlp import RPS_net_mlp5import torch.nn.functional as F6import os7from utils.logger import mkdir_if_missing, Logger8from networks.resnet18_for_cifer100 import resnet189from utils.Utils import accuracy, get_val_loader10from utils.AverageMeter import AverageMeter11import sys12from torchvision import models13def get_parser():14 parser = argparse.ArgumentParser()15 parser.add_argument("--data_root", type=str, default='../datasets_folder')16 parser.add_argument("--dataset_name", type=str, default='cifar100')17 parser.add_argument("--batch_size", type=int, default=16)18 parser.add_argument("--model", type=str, default='resnet18')19 parser.add_argument("--model_root", type=str, default='../model')20 parser.add_argument("--gpu_id", type=str, default='0')21 parser.add_argument("--num_of_sample_group", type=int, default=2)22 parser.add_argument("--phases", type=int, default=10)23 return parser24_model_dict = { # model dict25 'rps_net_mlp':RPS_net_mlp,26 'resnet18': resnet18,27 'standard_resnet18': models.resnet1828}29# compute probabilities the data contunnum belong to each batch30# for discriminating 'batch' in mini-batch gradient descent and 'batch' in batch identifier we change the name of batch to task31def predict_task(distinguish_tasks_model, images):32 outputs = distinguish_tasks_model(images)33 outputs = outputs.sum(0)34 return F.softmax(outputs)35def predict_class(distinguish_classes_model, images, task_probability, labels): # 计ç®å±äºæ¯ä¸ªç±»å«çæ¦ç36 opts = get_parser().parse_args()37 num_batch = len(images)//opts.batch_size + 1 # ä¸ç»å¾çåå¤å°ä¸ªbatchè®ç»å®38 outputs=None39 for i in range(num_batch):40 outputs_part = distinguish_classes_model(images[opts.batch_size * i:opts.batch_size*(i+1)]) # å表æªåæ¶ï¼å³ä¾§ä¸æ 大äºå表é¿åº¦ä¹å¯41 class_num_for_single_task = outputs_part.shape[1]//task_probability.shape[0] # æ»ç±»å«ä¸ªæ°/ä»»å¡æ°=æ¯ä¸ªä»»å¡å
å«çç±»å«ä¸ªæ°42 outputs_part = F.softmax(outputs_part) # outputç»è¿softmaxæ£åååçç»æ43 for task_id, p in enumerate(task_probability):44 outputs_part[:, task_id * class_num_for_single_task:(task_id + 1) * class_num_for_single_task] = outputs_part[: ,task_id * class_num_for_single_task:(task_id + 1) * class_num_for_single_task] * p45 if i == 0:46 outputs=outputs_part47 else:48 outputs=torch.cat((outputs, outputs_part), dim=0) # æè¡æ¼æ¥49 return outputs50def predict(val_dataset_path_list, distinguish_tasks_model, distinguish_classes_model, num_of_sample_group, dataset_name): # é¢æµå¾ççç±»å«51 opts = get_parser().parse_args()52 top5 = AverageMeter()53 top1 = AverageMeter()54 for val_dataset_path in val_dataset_path_list: # åæ¹æ¬¡éªè¯éªè¯é55 val_loader = get_val_loader(opts.dataset_name, val_dataset_path,batch_size=opts.num_of_sample_group)56 for cur_step, (images, labels) in enumerate(val_loader):57 images = images.to(0, dtype=torch.float32)58 labels = labels.to(0, dtype=torch.long)59 task_probability = predict_task(distinguish_tasks_model, images)60 outputs = predict_class(distinguish_classes_model, images, task_probability, labels)61 # ilsvrc2012 ââtop5 mnist,cifar100ââtop162 if opts.dataset_name == 'ilsvrc2012':63 _, prec5 = accuracy(outputs, labels, topk=(1,5))64 top5.update(prec5.item(), images.size(0))65 elif opts.dataset_name in ['mnist','cifar100']:66 prec1 = accuracy(outputs,labels)[0]67 top1.update(prec1.item(), images.size(0))68 return top5.avg if opts.dataset_name == 'ilsvrc2012' else top1.avg69def main(): # éªè¯æµç¨70 # 读ååæ°71 opts = get_parser().parse_args()72 os.environ['CUDA_VISIBLE_DEVICES'] = opts.gpu_id73 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')74 # dir and log75 mkdir_if_missing('../logs')76 sys.stdout = Logger(os.path.join('../logs', opts.dataset_name,str(opts.phases)+'phases', 'validate_kabi_result.txt'))77 distinguish_task_model_dir = os.path.join(opts.model_root, opts.dataset_name,str(opts.phases)+'phases','batch_identifiers')78 distinguish_task_model_filename_list = os.listdir(distinguish_task_model_dir)79 distinguish_task_model_path_list = [] # ææä¸å®¶æ¨¡åè·¯å¾80 for distinguish_task_model_filename in distinguish_task_model_filename_list:81 distinguish_task_model_path = os.path.join(distinguish_task_model_dir, distinguish_task_model_filename)82 distinguish_task_model_path_list.append(distinguish_task_model_path)83 distinguish_class_model_dir = os.path.join(opts.model_root, opts.dataset_name, str(opts.phases)+'phases','amalgamation_models')84 distinguish_class_model_filename_list = os.listdir(distinguish_class_model_dir)85 distinguish_class_model_path_list = [] # ææèå模åè·¯å¾86 for distinguish_class_model_filename in distinguish_class_model_filename_list:87 distinguish_class_model_path = os.path.join(distinguish_class_model_dir, distinguish_class_model_filename)88 distinguish_class_model_path_list.append(distinguish_class_model_path)89 val_dataset_dir = os.path.join(opts.data_root, opts.dataset_name, str(opts.phases)+'phases','separated/test/batch_test')90 val_dataset_filename_list = os.listdir(val_dataset_dir)91 val_dataset_path_list = [] # ææéªè¯æ°æ®éè·¯å¾92 for val_dataset_filename in val_dataset_filename_list:93 val_dataset_path = os.path.join(val_dataset_dir, val_dataset_filename)94 val_dataset_path_list.append(val_dataset_path)95 val_dataset_path_list_for_all_tasks = [] # ç»è£
éªè¯é96 for i in range(len(distinguish_class_model_path_list)):97 val_dataset_path_list_for_all_tasks.append(val_dataset_path_list[:i+2])98 task_and_class_model_and_val_dataset_path_list = zip(99 distinguish_task_model_path_list,100 distinguish_class_model_path_list,101 val_dataset_path_list_for_all_tasks)102 for task_and_class_model_path in task_and_class_model_and_val_dataset_path_list:103 model_for_task = _model_dict[opts.model]()104 print('batch identifier pathï¼', task_and_class_model_path[0])105 task_model_state = torch.load(task_and_class_model_path[0], map_location=lambda storage, loc: storage)106 num_batches = task_model_state['num_batches']107 model_for_task.fc = nn.Linear(model_for_task.fc.in_features, num_batches)108 model_for_task.load_state_dict(task_model_state['state_dict'])109 model_for_task.to(device)110 model_for_task.eval()111 model_for_class = _model_dict[opts.model]()112 class_model_state = torch.load(task_and_class_model_path[1], map_location=lambda storage, loc: storage)113 num_classes = class_model_state['num_classes']114 model_for_class.fc = nn.Linear(model_for_class.fc.in_features, num_classes)115 model_for_class.load_state_dict(class_model_state['state_dict'])116 model_for_class.to(device)117 model_for_class.eval()118 print('batch identifier path', task_and_class_model_path[0])119 print('amalgamation model path', task_and_class_model_path[1])120 print('datasets listï¼', task_and_class_model_path[2])121 print('accurcyï¼', predict(task_and_class_model_path[2], model_for_task, model_for_class, opts.num_of_sample_group, opts.dataset_name))122if __name__ == '__main__':...
test_distinguish_between_pn_samples_001.py
Source:test_distinguish_between_pn_samples_001.py
1# Copyright 2019 Huawei Technologies Co., Ltd2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""15################################################16Testcase_PrepareCondition:17Testcase_TestSteps:18Testcase_ExpectedResult:19"""20import os21import pytest22from tests.common.base import TestBase23from tests.common.test_run.ascend.distinguish_between_pn_samples_run import distinguish_between_pn_samples_run24#from print_debug import print_debug25############################################################26# TestCase= class: put to tests/*/27############################################################28class TestCase(TestBase):29 def setup(self):30 case_name = "test_akg_distinguish_between_pn_samples_001"31 case_path = os.getcwd()32 self.params_init(case_name, case_path)33 self.caseresult = True34 self._log.info("============= {0} Setup case============".format(self.casename))35 self.testarg = [36 # testflag,opfuncname,testRunArgs, dimArgs37 ("01_distinguish_between_pn_samples_01", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.618, "float16")),38 ("01_distinguish_between_pn_samples_02", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.618, "float32")),39 ("01_distinguish_between_pn_samples_03", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.222, "float16")),40 ("01_distinguish_between_pn_samples_04", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.222, "float32")),41 ("01_distinguish_between_pn_samples_05", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.999, "float16")),42 ("01_distinguish_between_pn_samples_06", distinguish_between_pn_samples_run, ((8, 4718, 16), 0.999, "float32")),43 ]44 self.testarg_2 = [45 # testflag,opfuncname,testRunArgs, dimArgs46 ("01_distinguish_between_pn_samples_01", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.618, "float16")),47 ("01_distinguish_between_pn_samples_02", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.618, "float32")),48 ("01_distinguish_between_pn_samples_03", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.222, "float16")),49 ("01_distinguish_between_pn_samples_04", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.222, "float32")),50 ("01_distinguish_between_pn_samples_05", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.999, "float16")),51 ("01_distinguish_between_pn_samples_06", distinguish_between_pn_samples_run, ((8, 8732, 16), 0.999, "float32")),52 ]53 self.testarg_aic_cloud = [54 # testflag,opfuncname,testRunArgs, dimArgs55 ("01_distinguish_between_pn_samples_01", distinguish_between_pn_samples_run, ((8, 18, 16), 0.618, "float16")),56 ("01_distinguish_between_pn_samples_02", distinguish_between_pn_samples_run, ((8, 18, 16), 0.618, "float32")),57 ]58 return59 @pytest.mark.level060 @pytest.mark.platform_arm_ascend_training61 @pytest.mark.platform_x86_ascend_training62 @pytest.mark.env_onecard63 def test_run(self):64 self.common_run(self.testarg)65 def test_run_2(self):66 self.common_run(self.testarg)67 def test_run_ci(self):68 self.common_run(self.testarg_aic_cloud)69 def teardown(self):70 self._log.info("============= {0} Teardown============".format(self.casename))71 return72if __name__ == "__main__":73 t = TestCase()74 t.setup()75 t.test_run()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!