def test_tuning_ipex(self): from lpot import Quantization model = torchvision.models.resnet18() model = MODELS['pytorch_ipex'](model) quantizer = Quantization('ipex_yaml.yaml') dataset = quantizer.dataset('dummy', (100, 3, 256, 256), label=True) quantizer.model = common.Model(model) quantizer.calib_dataloader = common.DataLoader(dataset) quantizer.eval_dataloader = common.DataLoader(dataset) lpot_model = quantizer() lpot_model.save("./saved") new_model = MODELS['pytorch_ipex'](model.model, { "workspace_path": "./saved" }) new_model.model.to(ipex.DEVICE) try: script_model = torch.jit.script(new_model.model) except: script_model = torch.jit.trace( new_model.model, torch.randn(10, 3, 224, 224).to(ipex.DEVICE)) from lpot import Benchmark evaluator = Benchmark('ipex_yaml.yaml') evaluator.model = common.Model(script_model) evaluator.b_dataloader = common.DataLoader(dataset) results = evaluator()
def test_quantization_saved(self): from lpot.utils.pytorch import load model = copy.deepcopy(self.model) for fake_yaml in ['qat_yaml.yaml', 'ptq_yaml.yaml']: if fake_yaml == 'ptq_yaml.yaml': model.eval().fuse_model() quantizer = Quantization(fake_yaml) dataset = quantizer.dataset('dummy', (100, 3, 256, 256), label=True) quantizer.model = common.Model(model) quantizer.calib_dataloader = common.DataLoader(dataset) quantizer.eval_dataloader = common.DataLoader(dataset) if fake_yaml == 'qat_yaml.yaml': quantizer.q_func = q_func q_model = quantizer() q_model.save('./saved') # Load configure and weights by lpot.utils saved_model = load("./saved", model) eval_func(saved_model) from lpot import Benchmark evaluator = Benchmark('ptq_yaml.yaml') # Load configure and weights by lpot.model evaluator.model = common.Model(model) evaluator.b_dataloader = common.DataLoader(dataset) results = evaluator() evaluator.model = common.Model(model) fp32_results = evaluator() self.assertTrue( (fp32_results['accuracy'][0] - results['accuracy'][0]) < 0.01)
def test_register_metric_postprocess(self): import PIL.Image image = np.array(PIL.Image.open(self.image_path)) resize_image = np.resize(image, (224, 224, 3)) mean = [123.68, 116.78, 103.94] resize_image = resize_image - mean images = np.expand_dims(resize_image, axis=0) labels = [768] from lpot import Benchmark, Quantization, common from lpot.data.transforms.imagenet_transform import LabelShift from lpot.metric.metric import TensorflowTopK evaluator = Benchmark('fake_yaml.yaml') evaluator.postprocess = common.Postprocess(LabelShift, 'label_benchmark', label_shift=1) evaluator.metric = common.Metric(TensorflowTopK, 'topk_benchmark') evaluator.b_dataloader = common.DataLoader( dataset=list(zip(images, labels))) evaluator.model = self.pb_path result = evaluator() acc, batch_size, result_list = result['accuracy'] self.assertEqual(acc, 0.0) quantizer = Quantization('fake_yaml.yaml') quantizer.postprocess = common.Postprocess(LabelShift, 'label_quantize', label_shift=1) quantizer.metric = common.Metric(TensorflowTopK, 'topk_quantize') evaluator = Benchmark('fake_yaml.yaml') evaluator.metric = common.Metric(TensorflowTopK, 'topk_second') evaluator.b_dataloader = common.DataLoader( dataset=list(zip(images, labels))) evaluator.model = self.pb_path result = evaluator() acc, batch_size, result_list = result['accuracy'] self.assertEqual(acc, 0.0)
input_shapes = [shape.split('x') for shape in input_shapes] shapes = [tuple([args.benchmark_nums] + [int(dim) for dim in shape]) for shape in input_shapes] from lpot.data.datasets.dummy_dataset import DummyDataset from lpot.data.dataloaders.onnxrt_dataloader import ONNXRTDataLoader dummy_dataset = DummyDataset(shapes, low=lows, high=highs, dtype=dtypes, label=True) dummy_dataloader = ONNXRTDataLoader(dummy_dataset, batch_size=args.eval_batch_size) def eval_func(model): return evaluate_onnxrt(model, dummy_dataloader, reference) if args.benchmark: from lpot import Benchmark, common evaluator = Benchmark(args.config) evaluator.model = common.Model(model) evaluator.b_dataloader = dummy_dataloader results = evaluator() for mode, result in results.items(): acc, batch_size, result_list = result latency = np.array(result_list).mean() / batch_size print('\n quantized model {} mode benchmark result:'.format(mode)) print('Accuracy is {:.3f}'.format(acc)) print('Batch size = {}'.format(batch_size)) print('Latency: {:.3f} ms'.format(latency * 1000)) print('Throughput: {:.3f} images/sec'.format(batch_size * 1./ latency)) if args.tune: from lpot import Quantization, common quantize = Quantization(args.config)