我正在尝试与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_mean
和input_std
使用什么。也许他们可能有问题。
我从Tensorflow Example
我真的不知道我的图像是错误的,还是我应该更改tensorflow的任何默认设置
感谢。