def test_external_delegate_options_memory_import(delegate_dir, test_data_folder): # create armnn delegate with memory-import option armnn_delegate = tflite.load_delegate(delegate_dir, options={ 'backends': 'CpuAcc,CpuRef', 'memory-import': '1' }) model_file_name = 'fallback_model.tflite' tensor_shape = [1, 2, 2, 1] input0 = np.array([1, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape) input1 = np.array([2, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape) inputs = [input0, input0, input1] expected_output = np.array([1, 2, 2, 2], dtype=np.uint8).reshape(tensor_shape) # run the inference armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate]) # check results compare_outputs(armnn_outputs, [expected_output])
def get(self): args = parser.parse_args() img_url = args.img_url if args.pred_num: top_predictions = args.pred_num bytes_str = parse_base64(img_url) if not bytes_str: res = requests.get(img_url) if res.status_code != 200: try: res.raise_for_status() except Exception as e: return jsonify({"status": res.status_code,\ "msg": str(e)}) else: bytes_str = res.content im = Image.open(BytesIO(bytes_str)) im.save("sample.jpg") im = preprocess_image(im) prediction = run_inference(model, im, top_predictions) prediction = one_prediction(prediction, imagenet_classes) response = {"status": 200, "msg": prediction} return jsonify(response)
def test_external_delegate_options_fp32_to_bf16(capfd, delegate_dir, test_data_folder): # create armnn delegate with reduce-fp32-to-bf16 option armnn_delegate = tflite.load_delegate(delegate_dir, options={ 'backends': 'CpuRef', 'debug-data': '1', 'reduce-fp32-to-bf16': '1' }) model_file_name = 'conv2d.tflite' inputShape = [1, 5, 5, 1] outputShape = [1, 3, 3, 1] inputValues = [ 1, 5, 2, 3, 5, 8, 7, 3, 6, 3, 3, 3, 9, 1, 9, 4, 1, 8, 1, 3, 6, 8, 1, 9, 2 ] expectedResult = [28, 38, 29, 96, 104, 53, 31, 55, 24] input = np.array(inputValues, dtype=np.float32).reshape(inputShape) expected_output = np.array(expectedResult, dtype=np.float32).reshape(outputShape) # run the inference armnn_outputs = run_inference(test_data_folder, model_file_name, [input], [armnn_delegate]) # check results compare_outputs(armnn_outputs, [expected_output]) captured = capfd.readouterr() assert 'convert_fp32_to_bf16' in captured.out
def test_external_delegate_options_fp32_to_fp16(capfd, delegate_dir, test_data_folder): # create armnn delegate with reduce-fp32-to-fp16 option armnn_delegate = tflite.load_delegate(delegate_dir, options={ 'backends': 'CpuRef', 'debug-data': '1', 'reduce-fp32-to-fp16': '1' }) model_file_name = 'fp32_model.tflite' tensor_shape = [1, 2, 2, 1] input0 = np.array([1, 2, 3, 4], dtype=np.float32).reshape(tensor_shape) input1 = np.array([2, 2, 3, 4], dtype=np.float32).reshape(tensor_shape) inputs = [input0, input0, input1] expected_output = np.array([1, 2, 2, 2], dtype=np.float32).reshape(tensor_shape) # run the inference armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate]) # check results compare_outputs(armnn_outputs, [expected_output]) captured = capfd.readouterr() assert 'convert_fp32_to_fp16' in captured.out assert 'convert_fp16_to_fp32' in captured.out
def test_external_delegate_gpu_fastmath(delegate_dir, test_data_folder): # create armnn delegate with enable-fast-math # fast-math is only enabled on Conv2d layer, so use conv2d model. armnn_delegate = tflite.load_delegate(delegate_dir, options={ 'backends': 'GpuAcc', 'enable-fast-math': '1', "logging-severity": "info" }) model_file_name = 'conv2d.tflite' inputShape = [1, 5, 5, 1] outputShape = [1, 3, 3, 1] inputValues = [ 1, 5, 2, 3, 5, 8, 7, 3, 6, 3, 3, 3, 9, 1, 9, 4, 1, 8, 1, 3, 6, 8, 1, 9, 2 ] expectedResult = [28, 38, 29, 96, 104, 53, 31, 55, 24] input = np.array(inputValues, dtype=np.float32).reshape(inputShape) expected_output = np.array(expectedResult, dtype=np.float32).reshape(outputShape) # run the inference armnn_outputs = run_inference(test_data_folder, model_file_name, [input], [armnn_delegate]) # check results compare_outputs(armnn_outputs, [expected_output])
def test_external_delegate_cpu_options(capfd, delegate_dir, test_data_folder): # create armnn delegate with enable-fast-math and number-of-threads options # fast-math is only enabled on Conv2d layer, so use conv2d model. armnn_delegate = tflite.load_delegate(delegate_dir, options={ 'backends': 'CpuAcc', 'enable-fast-math': '1', 'number-of-threads': '4', "logging-severity": "info" }) model_file_name = 'conv2d.tflite' inputShape = [1, 5, 5, 1] outputShape = [1, 3, 3, 1] inputValues = [ 1, 5, 2, 3, 5, 8, 7, 3, 6, 3, 3, 3, 9, 1, 9, 4, 1, 8, 1, 3, 6, 8, 1, 9, 2 ] expectedResult = [28, 38, 29, 96, 104, 53, 31, 55, 24] input = np.array(inputValues, dtype=np.float32).reshape(inputShape) expected_output = np.array(expectedResult, dtype=np.float32).reshape(outputShape) # run the inference armnn_outputs = run_inference(test_data_folder, model_file_name, [input], [armnn_delegate]) # check results compare_outputs(armnn_outputs, [expected_output]) captured = capfd.readouterr() assert 'Set CPPScheduler to Linear mode, with 4 threads to use' in captured.out
# # # # # # # # ## # # # ## # # OPTIMIZER, LOSS # # # # # # # # ## # # # ## # # optimizer = tagging_utils.build_optimizer( model, int((num_train_examples * ARGS.epochs) / ARGS.train_batch_size), ARGS.learning_rate) loss_fn = tagging_utils.build_loss_fn() # # # # # # # # ## # # # ## # # TRAIN # # # # # # # # ## # # # ## # # writer = SummaryWriter(ARGS.working_dir) print('INITIAL EVAL...') model.eval() results = tagging_utils.run_inference(model, eval_dataloader, loss_fn, tokenizer) writer.add_scalar('eval/tok_loss', np.mean(results['tok_loss']), 0) writer.add_scalar('eval/tok_acc', np.mean(results['labeling_hits']), 0) attention_results = [] def output_attention_results(results, epoch): global attention_results ''' Write out the results of the attention layer for interpretable AI. Results is a dictionary that contains the following important entries: - results['input_toks'] - results['post_toks'] - results['tok_labels']
from utils import read_imagenet_classnames, display_results, run_inference, parse_base64 from torchvision import models parser = argparse.ArgumentParser(description='Inference Trained Model') parser.add_argument('--data', metavar='DIR', default='./data', help='default data path') parser.add_argument('-bs', '--batch-size', metavar='BS', default=2, help='maximum batchsize') parser.add_argument('-tp', '--top-predictions', metavar='NUMPRED',\ default=5, help='number of top predictions per sample') parser.add_argument('-exp', '--export', action="store_true",help='export model to onnx') def export_model(model): model_path = 'checkpoints/model.pt' sample_input = torch.randn((1, 3, 256, 256)) model = model.cpu() model.eval() model = torch.jit.trace(model, sample_input) torch.jit.save(model, model_path) if __name__ == "__main__": args = parser.parse_args() model = models.resnet18(pretrained=True) if args.export: export_model(model) else: imagenet_classes = read_imagenet_classnames("cache/imagenet_classnames.txt") data = preprocess_data("cache") predictions = run_inference(model, data[0], args.top_predictions) display_results(data, predictions, imagenet_classes)