{"id":27077,"date":"2024-05-29T12:45:30","date_gmt":"2024-05-29T04:45:30","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=27077"},"modified":"2024-05-29T12:45:30","modified_gmt":"2024-05-29T04:45:30","slug":"tf%e4%b8%8a%e6%9e%b6%e8%8b%b9%e6%9e%9c%e5%95%86%e5%9f%8e%e6%a8%a1%e5%bc%8f%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2024\/05\/29\/tf%e4%b8%8a%e6%9e%b6%e8%8b%b9%e6%9e%9c%e5%95%86%e5%9f%8e%e6%a8%a1%e5%bc%8f%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"tf\u4e0a\u67b6\u82f9\u679c\u5546\u57ce\u6a21\u5f0f\u4ecb\u7ecd"},"content":{"rendered":"

TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u4eba\u5de5\u667a\u80fd\u6846\u67b6\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u3002\u5728\u79fb\u52a8\u8bbe\u5907\u7684\u5e94\u7528\u5f00\u53d1\u4e2d\uff0cTensorFlow\u4e5f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TensorFlow\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e0a\u67b6\u5230\u82f9\u679c\u5546\u57ce\u4e2d\u3002<\/p>\n

\u5728\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e0a\u67b6\u5230\u82f9\u679c\u5546\u57ce\u4e2d\uff0c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n

1. \u521b\u5efa\u4e00\u4e2aXcode\u9879\u76ee<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aXcode\u9879\u76ee\u3002\u5728Xcode\u4e2d\uff0c\u9009\u62e9File -> New -> Project\uff0c\u7136\u540e\u9009\u62e9iOS -> Application -> Single View Application\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9009\u62e9\u4e00\u4e9b\u9879\u76ee\u8bbe\u7f6e\uff0c\u4f8b\u5982\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\u548c\u7ec4\u7ec7\u6807\u8bc6\u7b26\u7b49\u3002<\/p>\n

2. \u96c6\u6210TensorFlow\u6846\u67b6<\/p>\n

\u5728Xcode\u9879\u76ee\u4e2d\uff0c\u9700\u8981\u5c06TensorFlow\u6846\u67b6\u96c6\u6210\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u9996\u5148\uff0c\u9700\u8981\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5TensorFlow\uff1a<\/p>\n

“`<\/p>\n

pip install tensorflow<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u9700\u8981\u5c06TensorFlow\u6846\u67b6\u6587\u4ef6\u6dfb\u52a0\u5230Xcode\u9879\u76ee\u4e2d\u3002\u5728Xcode\u4e2d\uff0c\u9009\u62e9File -> Add Files to “Project Name”\uff0c\u7136\u540e\u9009\u62e9TensorFlow.framework\u6587\u4ef6\u3002\u5728\u6dfb\u52a0\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u9009\u62e9\u201cCopy items if needed\u201d\u9009\u9879\u3002<\/p>\n

3. \u521b\u5efaCore ML\u6a21\u578b<\/p>\n

Core ML\u662f\u82f9\u679c\u7684\u673a\u5668\u5b66\u4e60\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8f6c\u6362\u4e3aiOS\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u7528\u7684\u683c\u5f0f\u3002\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u9700\u8981\u4f7f\u7528TensorFlow\u6765\u8bad\u7ec3\u4e00\u4e2a\u6a21\u578b\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aCore ML\u6a21\u578b\u3002<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u8bad\u7ec3\u4e00\u4e2a\u6a21\u578b\u3002\u53ef\u4ee5\u4f7f\u7528Python\u548cTensorFlow\u6765\u8bad\u7ec3\u6a21\u578b\u3002\u5728\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u9700\u8981\u5c06\u6a21\u578b\u4fdd\u5b58\u4e3a.pb\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u4fdd\u5b58\u6a21\u578b\uff1a<\/p>\n

“`<\/p>\n

import tensorflow as tf<\/p>\n

# train your model here<\/p>\n

# save the model<\/p>\n

with tf.Session() as sess:<\/p>\n

saver = tf.train.Saver()<\/p>\n

saver.save(sess, ‘model.pb’)<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u9700\u8981\u4f7f\u7528Core ML\u5de5\u5177\u5c06\u6a21\u578b\u8f6c<\/p>\n

<\/figure>\n<\/p>\n

\u6362\u4e3aCore ML\u6a21\u578b\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u6a21\u578b\u8f6c\u6362\u4e3aCore ML\u6a21\u578b\uff1a<\/p>\n

“`<\/p>\n

coremltools.converters.tensorflow.convert(‘model.pb’,<\/p>\n

input_names=[‘input’],<\/p>\n

output_names=[‘output’],<\/p>\n

output_feature_names=[‘output:0’],<\/p>\n

input_shapes={‘input’: [1, 28, 28, 1]},<\/p>\n

image_input_names=[‘input’])<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u547d\u4ee4\u4e2d\uff0c\u9700\u8981\u6307\u5b9a\u8f93\u5165\u548c\u8f93\u51fa\u7684\u540d\u79f0\u548c\u5f62\u72b6\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u5165\u662f\u4e00\u4e2a28×28\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u8f93\u51fa\u662f\u4e00\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u5206\u5e03\u3002<\/p>\n

4. \u96c6\u6210Core ML\u6a21\u578b<\/p>\n

\u5728Xcode\u9879\u76ee\u4e2d\uff0c\u9700\u8981\u5c06Core ML\u6a21\u578b\u6587\u4ef6\u6dfb\u52a0\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u53ef\u4ee5\u5c06Core ML\u6a21\u578b\u6587\u82f9\u679c\u5e02\u573a\u4e0a\u67b6<\/a>\u4ef6\u76f4\u63a5\u62d6\u653e\u5230Xcode\u9879\u76ee\u4e2d\u3002\u5728\u6dfb\u52a0\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u9009\u62e9\u201cCopy items if needed\u201d\u9009\u9879\u3002<\/p>\n

\u7136\u540e\uff0c\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u52a0\u8f7dCore ML\u6a21\u578b\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u6a21\u578b\uff1a<\/p>\n

“`<\/p>\n

import CoreML<\/p>\n

model = CoreML.MLModel(‘model.mlmodel’)<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u6307\u5b9aCore ML\u6a21\u578b\u6587\u4ef6\u7684\u540d\u79f0\u3002\u5728\u52a0\u8f7d\u6a21\u578b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8fdb\u884c\u63a8\u7406\uff1a<\/p>\n

“`<\/p>\n

import UIKit<\/p>\n

# get the input image<\/p>\n

image = UIImage(named: ‘input.jpg’)<\/p>\n

# create the input for the model<\/p>\n

input = CoreML.ImageType(<\/p>\n

pixels: image?.resize(to: CGSize(width: 28, height: 28)).pixelData() ?? [],<\/p>\n

size: CGSize(width: 28, height: 28),<\/p>\n

pixelFormatType: kCVPixelFormatType_OneComponent8)<\/p>\n

# perform the inference<\/p>\n

output = try? model.prediction(input: input)<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u9700\u8981\u5c06\u8f93\u5165\u56fe\u50cf\u8f6c\u6362\u4e3aCore ML\u6a21\u578b\u7684\u8f93\u5165\u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9\u6a21\u578b\u3002\u5728\u63a8\u7406\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f93\u51fa\u6765\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n

5. \u4e0a\u67b6\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u5728\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u540e\uff0c\u9700\u8981\u5c06\u5176\u4e0a\u67b6\u5230\u82f9\u679c\u5546\u57ce\u4e2d\u3002\u9996\u5148\uff0c\u9700\u8981\u5728\u82f9\u679c\u5f00\u53d1\u8005\u4e2d\u5fc3\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8fID\uff0c\u5e76\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u8bc1\u4e66\u3002\u7136\u540e\uff0c\u9700\u8981\u5728Xcode\u4e2d\u5c06\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3a.ipa\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u4e0a\u4f20\u5230\u82f9\u679c\u5546\u57ce\u4e2d\u3002<\/p>\n

\u5728\u4e0a\u4f20\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u4e00\u4e9b\u5143\u6570\u636e\uff0c\u4f8b\u5982\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\u3001\u63cf\u8ff0\u548c\u56fe\u6807\u7b49\u3002\u5728\u4e0a\u4f20\u5b8c\u6210\u540e\uff0c\u82f9\u679c\u5546\u57ce\u4f1a\u5ba1\u6838\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u51b3\u5b9a\u662f\u5426\u5c06\u5176\u4e0a\u67b6\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528TensorFlow\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e0a\u67b6\u5230\u82f9\u679c\u5546\u57ce\u4e2d\u3002\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aXcode\u9879\u76ee\uff0c\u5e76\u5c06TensorFlow\u6846\u67b6\u96c6\u6210\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u7136\u540e\uff0c\u9700\u8981\u4f7f\u7528TensorFlow\u8bad\u7ec3\u4e00\u4e2a\u6a21\u578b\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aCore ML\u6a21\u578b\u3002\u6700\u540e\uff0c\u9700\u8981\u5c06Core ML\u6a21\u578b\u96c6\u6210\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e0a\u67b6\u5230\u82f9\u679c\u5546\u57ce\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u4eba\u5de5\u667a\u80fd\u6846\u67b6\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u3002\u5728\u79fb\u52a8\u8bbe\u5907\u7684\u5e94\u7528\u5f00\u53d1\u4e2d\uff0cTensorFlow\u4e5f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TensorFlow\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e0a\u67b6\u5230\u82f9\u679c<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[360,116,6509,16791,25652],"topic":[],"class_list":["post-27077","post","type-post","status-publish","format-standard","hentry","category-appsjsd","tag-360","tag-116","tag-6509","tag-appapp","tag-25652"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/27077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=27077"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/27077\/revisions"}],"predecessor-version":[{"id":27096,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/27077\/revisions\/27096"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=27077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=27077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=27077"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=27077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}