|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#import "ORTSuperResolutionPerformer.h" |
| 5 | +#import <Foundation/Foundation.h> |
| 6 | +#import <UIKit/UIKit.h> |
| 7 | + |
| 8 | +#include <array> |
| 9 | +#include <cstdint> |
| 10 | +#include <stdexcept> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include <onnxruntime_cxx_api.h> |
| 15 | +#include <onnxruntime_extensions.h> |
| 16 | + |
| 17 | + |
| 18 | +@implementation ORTSuperResolutionPerformer |
| 19 | + |
| 20 | ++ (nullable UIImage*)performSuperResolutionWithError:(NSError **)error { |
| 21 | + |
| 22 | + UIImage* output_image = nil; |
| 23 | + |
| 24 | + try { |
| 25 | + |
| 26 | + // Register custom ops |
| 27 | + |
| 28 | + const auto ort_log_level = ORT_LOGGING_LEVEL_INFO; |
| 29 | + auto ort_env = Ort::Env(ort_log_level, "ORTSuperResolution"); |
| 30 | + auto session_options = Ort::SessionOptions(); |
| 31 | + |
| 32 | + if (RegisterCustomOps(session_options, OrtGetApiBase()) != nullptr) { |
| 33 | + throw std::runtime_error("RegisterCustomOps failed"); |
| 34 | + } |
| 35 | + |
| 36 | + // Step 1: Load model |
| 37 | + |
| 38 | + NSString *model_path = [NSBundle.mainBundle pathForResource:@"pt_super_resolution_with_pre_post_processing_opset16" |
| 39 | + ofType:@"onnx"]; |
| 40 | + if (model_path == nullptr) { |
| 41 | + throw std::runtime_error("Failed to get model path"); |
| 42 | + } |
| 43 | + |
| 44 | + // Step 2: Create Ort Inference Session |
| 45 | + |
| 46 | + auto sess = Ort::Session(ort_env, [model_path UTF8String], session_options); |
| 47 | + |
| 48 | + // Read input image |
| 49 | + |
| 50 | + // note: need to set Xcode settings to prevent it from messing with PNG files: |
| 51 | + // in "Build Settings": |
| 52 | + // - set "Compress PNG Files" to "No" |
| 53 | + // - set "Remove Text Metadata From PNG Files" to "No" |
| 54 | + NSString *input_image_path = |
| 55 | + [NSBundle.mainBundle pathForResource:@"cat_224x224" ofType:@"png"]; |
| 56 | + if (input_image_path == nullptr) { |
| 57 | + throw std::runtime_error("Failed to get image path"); |
| 58 | + } |
| 59 | + |
| 60 | + // Step 3: Prepare input tensors and input/output names |
| 61 | + |
| 62 | + NSMutableData *input_data = |
| 63 | + [NSMutableData dataWithContentsOfFile:input_image_path]; |
| 64 | + const int64_t input_data_length = input_data.length; |
| 65 | + const auto memoryInfo = |
| 66 | + Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); |
| 67 | + |
| 68 | + const auto input_tensor = Ort::Value::CreateTensor(memoryInfo, [input_data mutableBytes], input_data_length, |
| 69 | + &input_data_length, 1, ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8); |
| 70 | + |
| 71 | + constexpr auto input_names = std::array{"image"}; |
| 72 | + constexpr auto output_names = std::array{"image_out"}; |
| 73 | + |
| 74 | + // Step 4: Call inference session run |
| 75 | + |
| 76 | + const auto outputs = sess.Run(Ort::RunOptions(), input_names.data(), |
| 77 | + &input_tensor, 1, output_names.data(), 1); |
| 78 | + if (outputs.size() != 1) { |
| 79 | + throw std::runtime_error("Unexpected number of outputs"); |
| 80 | + } |
| 81 | + |
| 82 | + // Step 5: Analyze model outputs |
| 83 | + |
| 84 | + const auto &output_tensor = outputs.front(); |
| 85 | + const auto output_type_and_shape_info = output_tensor.GetTensorTypeAndShapeInfo(); |
| 86 | + const auto output_shape = output_type_and_shape_info.GetShape(); |
| 87 | + |
| 88 | + if (const auto output_element_type = |
| 89 | + output_type_and_shape_info.GetElementType(); |
| 90 | + output_element_type != ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8) { |
| 91 | + throw std::runtime_error("Unexpected output element type"); |
| 92 | + } |
| 93 | + |
| 94 | + const uint8_t *output_data_raw = output_tensor.GetTensorData<uint8_t>(); |
| 95 | + |
| 96 | + // Step 6: Convert raw bytes into NSData and return as displayable UIImage |
| 97 | + |
| 98 | + NSData *output_data = [NSData dataWithBytes:output_data_raw length:(output_shape[0])]; |
| 99 | + output_image = [UIImage imageWithData:output_data]; |
| 100 | + |
| 101 | + } catch (std::exception &e) { |
| 102 | + NSLog(@"%s error: %s", __FUNCTION__, e.what()); |
| 103 | + |
| 104 | + static NSString *const kErrorDomain = @"ORTSuperResolution"; |
| 105 | + constexpr NSInteger kErrorCode = 0; |
| 106 | + if (error) { |
| 107 | + NSString *description = |
| 108 | + [NSString stringWithCString:e.what() encoding:NSASCIIStringEncoding]; |
| 109 | + *error = |
| 110 | + [NSError errorWithDomain:kErrorDomain |
| 111 | + code:kErrorCode |
| 112 | + userInfo:@{NSLocalizedDescriptionKey : description}]; |
| 113 | + } |
| 114 | + return nullptr; |
| 115 | + } |
| 116 | + |
| 117 | + if (error) { |
| 118 | + *error = nullptr; |
| 119 | + } |
| 120 | + return output_image; |
| 121 | +} |
| 122 | + |
| 123 | +@end |
0 commit comments