{"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
\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<\/figure>\n<\/p>\n