{"id":31308,"date":"2024-05-29T12:45:30","date_gmt":"2024-05-29T04:45:30","guid":{"rendered":"https:\/\/h5app.yimenapp.cn\/h5app\/?p=31308"},"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:\/\/h5app.yimenapp.cn\/h5app\/2024\/sjapp\/31308\/","title":{"rendered":"tf\u4e0a\u67b6\u82f9\u679c\u5546\u57ce\u6a21\u5f0f\u4ecb\u7ecd"},"content":{"rendered":"<p>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<p>\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<p>1. \u521b\u5efa\u4e00\u4e2aXcode\u9879\u76ee<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aXcode\u9879\u76ee\u3002\u5728Xcode\u4e2d\uff0c\u9009\u62e9File -&gt; New -&gt; Project\uff0c\u7136\u540e\u9009\u62e9iOS -&gt; Application -&gt; 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<p>2. \u96c6\u6210TensorFlow\u6846\u67b6<\/p>\n<p>\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>&#8220;`<\/p>\n<p>pip install tensorflow<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\uff0c\u9700\u8981\u5c06TensorFlow\u6846\u67b6\u6587\u4ef6\u6dfb\u52a0\u5230Xcode\u9879\u76ee\u4e2d\u3002\u5728Xcode\u4e2d\uff0c\u9009\u62e9File -&gt; Add Files to &#8220;Project Name&#8221;\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<p>3. \u521b\u5efaCore ML\u6a21\u578b<\/p>\n<p>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<p>\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>&#8220;`<\/p>\n<p>import tensorflow as tf<\/p>\n<p># train your model here<\/p>\n<p># save the model<\/p>\n<p>with tf.Session() as sess:<\/p>\n<p>    saver = tf.train.Saver()<\/p>\n<p>    saver.save(sess, &#8216;model.pb&#8217;)<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\uff0c\u9700\u8981\u4f7f\u7528Core ML\u5de5\u5177\u5c06\u6a21\u578b\u8f6c<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.myapp.ltd\/ag\/5\/img\/25.jpg\" \/><\/figure>\n<\/p>\n<p>\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>&#8220;`<\/p>\n<p>coremltools.converters.tensorflow.convert(&#8216;model.pb&#8217;,<\/p>\n<p>                                          input_names=[&#8216;input&#8217;],<\/p>\n<p>                                          output_names=[&#8216;output&#8217;],<\/p>\n<p>                                          output_feature_names=[&#8216;output:0&#8217;],<\/p>\n<p>                                          input_shapes={&#8216;input&#8217;: [1, 28, 28, 1]},<\/p>\n<p>                                          image_input_names=[&#8216;input&#8217;])<\/p>\n<p>&#8220;`<\/p>\n<p>\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&#215;28\u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u8f93\u51fa\u662f\u4e00\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u5206\u5e03\u3002<\/p>\n<p>4. \u96c6\u6210Core ML\u6a21\u578b<\/p>\n<p>\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<a href=\"https:\/\/www.applebyme.cn\/\">\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<p>\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>&#8220;`<\/p>\n<p>import CoreML<\/p>\n<p>model = CoreML.MLModel(&#8216;model.mlmodel&#8217;)<\/p>\n<p>&#8220;`<\/p>\n<p>\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>&#8220;`<\/p>\n<p>import UIKit<\/p>\n<p># get the input image<\/p>\n<p>image = UIImage(named: &#8216;input.jpg&#8217;)<\/p>\n<p># create the input for the model<\/p>\n<p>input = CoreML.ImageType(<\/p>\n<p>    pixels: image?.resize(to: CGSize(width: 28, height: 28)).pixelData() ?? [],<\/p>\n<p>    size: CGSize(width: 28, height: 28),<\/p>\n<p>    pixelFormatType: kCVPixelFormatType_OneComponent8)<\/p>\n<p># perform the inference<\/p>\n<p>output = try? model.prediction(input: input)<\/p>\n<p>&#8220;`<\/p>\n<p>\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<p>5. \u4e0a\u67b6\u5e94\u7528\u7a0b\u5e8f<\/p>\n<p>\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<p>\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<p>\u603b\u7ed3<\/p>\n<p>\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":"<p>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":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[16,11,9215,20173,29578],"topic":[],"class_list":["post-31308","post","type-post","status-publish","format-standard","hentry","category-sjapp","tag-16","tag-11","tag-9215","tag-appapp","tag-29578"],"_links":{"self":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/31308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/comments?post=31308"}],"version-history":[{"count":1,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/31308\/revisions"}],"predecessor-version":[{"id":31327,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/posts\/31308\/revisions\/31327"}],"wp:attachment":[{"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/media?parent=31308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/categories?post=31308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/tags?post=31308"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/h5app.yimenapp.cn\/h5app\/wp-json\/wp\/v2\/topic?post=31308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}