我的张量流模型在c ++ iOS上无法正常工作

时间:2018-06-18 06:17:10

标签: c++ ios tensorflow convolutional-neural-network

我正在尝试与CNN进行角色识别移动应用。我的模型预测python上的%99,6准确度。但是当我试图在iOS应用程序中使用与c ++相同的模型时,它无法预测任何值。

我从cnn模型的图像中获取pixelBuffer值:

    pixelBuffer(width: width, height: height,
                       pixelFormatType: kCVPixelFormatType_32BGRA,
                       colorSpace: CGColorSpaceCreateDeviceRGB(),
                       alphaInfo: .noneSkipFirst)

func pixelBuffer(width: Int, height: Int, pixelFormatType: OSType,
                 colorSpace: CGColorSpace, alphaInfo: CGImageAlphaInfo) -> CVPixelBuffer? {
    var maybePixelBuffer: CVPixelBuffer?
    let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
                 kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue]
    let status = CVPixelBufferCreate(kCFAllocatorDefault,
                                     width,
                                     height,
                                     pixelFormatType,
                                     attrs as CFDictionary,
                                     &maybePixelBuffer)

    guard status == kCVReturnSuccess, let pixelBuffer = maybePixelBuffer else {
        return nil
    }

    CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
    let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)

    guard let context = CGContext(data: pixelData,
                                  width: width,
                                  height: height,
                                  bitsPerComponent: 8,
                                  bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
                                  space: colorSpace,
                                  bitmapInfo: alphaInfo.rawValue)
        else {
            return nil
    }

    UIGraphicsPushContext(context)
    context.translateBy(x: 0, y: CGFloat(height))
    context.scaleBy(x: 1, y: -1)
    self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
    UIGraphicsPopContext()

    CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
    return pixelBuffer}

然后我用这个设置创建我的模型:

    static NSString* model_file_name = @"inference_1_1_0";
    static NSString* model_file_type = @"pb";
    static NSString* labels_file_name = @"labels";
    static NSString* labels_file_type = @"txt";
    std::unique_ptr<tensorflow::Session> tf_session;
    std::vector<std::string> labels;

    const int wanted_input_width = 38;
    const int wanted_input_height = 45;
    const int wanted_input_channels = 3;
    const float input_mean = 117.0f;
    const float input_std = 1.0f;
    const std::string input_layer_name = "input_input";
    const std::string output_layer_name = "output_node0";

但我不知道我应该为input_meaninput_std使用什么。也许他们可能有问题。 我从Tensorflow Example

获得了runCNN方法的默认设置

我真的不知道我的图像是错误的,还是我应该更改tensorflow的任何默认设置

感谢。

  • 我是否编写了自定义代码:是
  • OS平台和发行版:Mac OS 10.13.3 High Sierra
  • 从(源或二进制)安装的TensorFlow:source
  • TensorFlow版本:Mac上的1.1.0(iOS上的Tensorflow-Experimental)
  • Python版本:3
  • Bazel版本:0.13.0
  • CUDA / cuDNN:N / A
  • GPU型号和内存:N / A
  • 重现的确切命令:当我试图用我的模型进行预测时,它总是预测错误。(预测)

0 个答案:

没有答案