【问题标题】:How to access elements inside MLMultiArray in CoreML如何在 CoreML 中访问 MLMultiArray 中的元素
【发布时间】:2018-05-29 10:46:48
【问题描述】:

我已经使用initWithDataPointer 初始化了MLMultiArray,如下代码所示:

float count = 512 * 384;
  double *tempBuffer = malloc(count * sizeof(double));
  NSError *error = NULL;
  NSArray *shape = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:512],[NSNumber numberWithInt:384], nil];
  NSArray *stride = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:1], nil];

  MLMultiArray *mlMultiArray = [[MLMultiArray alloc] initWithDataPointer:tempBuffer
                                                                   shape:shape
                                                                dataType:MLMultiArrayDataTypeDouble
                                                                 strides:stride
                                                             deallocator:^(void * _Nonnull bytes) { free(bytes); }
                                                                   error:&error];

根据此link 中提到的MLMultiArray 文档,需要使用subscript 来访问元素。

如果我以显示的方式访问元素,是否正确?

NSNumber *val = [mlMultiArray objectForKeyedSubscript:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:1], nil]];

【问题讨论】:

    标签: ios objective-c multidimensional-array machine-learning coreml


    【解决方案1】:

    我建议你使用mlMultiArray.dataPointer,将其转换为double *,然后直接访问数据缓冲区的内容。您可以计算元素 i, j, k 使用步幅的位置:

    double *ptr = (double *) mlMultiArray.dataPointer;
    NSInteger offset = i*stride[0].intValue + j*stride[1].intValue + k*stride[2].intValue;
    double val = ptr[offset];
    

    【讨论】:

    • 谢谢,即使我提到的那个也有效。不知何故,我给出了错误的步幅值
    猜你喜欢
    • 2017-11-11
    • 2022-01-23
    • 2021-01-27
    • 2021-02-09
    • 2020-04-20
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多