tf-coreml TensorFlow 转换到 CoreML 的转换器开源项目

我要开发同款
匿名用户2019年01月02日
42阅读
开发技术Python
所属分类人工智能、机器学习/深度学习
授权协议Apache

作品详情

tfcoreml

TensorFlow(TF)toCoreMLConverter

Dependencies

tensorflow>=1.5.0

coremltools>=0.8

numpy>=1.6.2

protobuf>=3.1.0

six>=1.10.0

InstallationInstallFromSource

Togetthelatestversionoftheconverter,clonethisrepoandinstallfromsource.Thatis,

git clone https://github.com/tf-coreml/tf-coreml.gitcd tf-coreml

Toinstallasapackagewithpip,eitherrun(attherootdirectory):

pip install -e .

orrun:

python setup.py bdist_wheel

Thiswillgenerateapipinstallablewheelinsidethedistdirectory.

InstallFromPyPI

ToinstallthePypipackage:

pip install -U tfcoreml

Usage

SeeiPythonnotebooksinthedirectoryexamples/forexamplesofhowtousetheconverter.

ThefollowingargumentsarerequiredbytheCoreMLconverter:

pathtothefrozen.pbgraphfiletobeconverted

pathwherethe.mlmodelshouldbewritten

alistofoutputtensornamespresentintheTFgraph

adictionaryofinputnamesandtheirshapes(aslistofintegers).Thisisonlyrequiredifinputtensors'shapesarenotfullydefinedinthefrozen.pbfile(e.g.theycontainNoneor?)

Notethatthefrozen.pbfilecanbeobtainedfromthecheckpointandgraphdeffilesbyusingthetensorflow.python.tools.freeze_graphutility.FordetailsoffreezingTFgraphs,pleaserefertotheTensorFlowdocumentationandthenotebooksindirectoryexamples/inthisrepo.Therearescriptsintheutils/directoryforvisualizingandwritingoutatextsummaryofagivenfrozenTFgraph.Thiscouldbeusefulindeterminingtheinput/outputnamesandshapes.AnotherusefultoolforvisualizingfrozenTFgraphsisNetron.

Thereareadditionalargumentsthattheconvertercantake.Fordetails,refertothefullfunctiondefinitionhere.

Forexample:

Wheninputshapesarefullydeterminedinthefrozen.pbfile:

import tfcoreml as tf_convertertf_converter.convert(tf_model_path = 'my_model.pb',                     mlmodel_path = 'my_model.mlmodel',                     output_feature_names = ['softmax:0'])

Wheninputshapesarenotfullyspecifiedinthefrozen.pbfile:

import tfcoreml as tf_convertertf_converter.convert(tf_model_path = 'my_model.pb',                     mlmodel_path = 'my_model.mlmodel',                     output_feature_names = ['softmax:0'],                     input_name_shape_dict = {'input:0' : [1, 227, 227, 3]})

Followingtopicsarediscussedinthejupyternotebooksundertheexamples/folder:

inception_v1_preprocessing_steps.ipynb:Howtogenerateaclassifiermodelwithimageinputtypesandtheimportanceofproperlysettingthepreprocessingparameters.

inception_v3.ipynb:Howtostripthe"DecodeJpeg"opfromtheTFgraphtoprepareitforCoreMLconversion.

linear_mnist_example.ipynb:HowtogetafrozengraphfromthecheckpointandgraphdescriptionfilesgeneratedbytraininginTF.

ssd_example.ipynb:HowtoextractaportionoftheTFgraphthatcanbeconverted,fromtheoverallgraphthatmayhaveunsupportedops.

style_transfer_example.ipynb:HowtoeditaCoreMLmodeltogetanimageoutputtype(bydefaulttheoutputsareMultiArrays).

custom_layer_examples.ipynb:AfewexamplestodemonstratetheprocessofaddingcustomCoreMLlayersforunsupportedTFops.

SupportedOps

ListofTensorFlowopsthataresupportedcurrently(seetfcoreml/_ops_to_layers.py):

Abs

Add

ArgMax

AvgPool

BatchNormWithGlobalNormalization

BatchToSpaceND*

BiasAdd

ConcatV2,Concat

Const

Conv2D

Conv2DBackpropInput

CropAndResize*

DepthToSpace

DepthwiseConv2dNative

Elu

Exp

ExtractImagePatches

FusedBatchNorm

Identity

Log

LRN

MatMul

Max*

Maximum

MaxPool

Mean*

Min*

Minimum

MirrorPad

Mul

Neg

OneHot

Pad

Placeholder

Pow*

Prod*

RealDiv

Reciprocal

Relu

Relu6

Reshape*

ResizeNearestNeighbor

ResizeBilinear

Rsqrt

Sigmoid

Slice*

Softmax

SpaceToBatchND*

SpaceToDepth

Split*

Sqrt

Square

SquaredDifference

StridedSlice*

Sub

Sum*

Tanh

Transpose*

Notethatcertainparameterizationsoftheseopsmaynotbefullysupported.Foropsmarkedwithanasterisk,onlyveryspecificusagepatternsaresupported.Inaddition,thereareseveralotherops(notlistedabove)thatareskippedbytheconverterastheygenerallyhavenoeffectduringinference.Kindlyrefertothefilestfcoreml/_ops_to_layers.pyandtfcoreml/_layers.pyforfulldetails.Forunsupportedopsorconfigurations,thecustomlayerfeatureofCoreMLcanbeused.Fordetails,refertotheexamples/custom_layer_examples.ipynbnotebook.

Scriptsforconvertingseveralofthefollowingpretrainedmodelscanbefoundattests/test_pretrained_models.py.Othermodelswithsimilarstructuresandsupportedopscanbeconverted.BelowisalistofpubliclyavailableTensorFlowfrozenmodelsthatcanbeconvertedwiththisconverter:

Inceptionv1(Slim)

Inceptionv2(Slim)

Inceptionv3(Slim)

Inceptionv4(Slim)

Inceptionv3(non-Slim)*

Inception/ResNetv2(Slim)

MobileNetvariations(Slim):

Imagesize:128(1,2,3,4)

Imagesize:160(1,2,3,4)

Imagesize:192(1,2,3,4)

Imagesize:224(1,2,3,4)

Imagestylizationnetwork+

MobilenetSSD*

*ConvertingthesemodelsrequiresextrastepstoextractsubgraphsfromtheTFfrozengraphs.Seeexamples/fordetails.<br>+ThereareknownissuesrunningimagestylizationnetworkonGPU.(SeeIssue#26)

Limitations

tfcoremlconverterhasthefollowingconstraints:

TFgraphmustbecyclefree(cyclesaregenerallycreatedduetocontrolflowopslikeif,while,map,etc.)

MusthaveNHWCordering(Batchsize,Height,Width,Channels)forimagefeaturemaptensors

Musthavetensorswithranklessthanorequalto4(len(tensor.shape)<=4)

TheconverterproducesCoreMLmodelwithfloatvalues.AquantizedTFgraph(suchasthestyletransfernetworklinkedabove)getsconvertedtoafloatCoreMLmodel

RunningUnitTests

Inordertorununittests,youneedpytest.

pip install pytest

Toaddanewunittest,addittothetests/folder.Makesureyounamethefilewitha'test'astheprefix.Torunallunittests,navigatetothetests/folderandrun

pytest

Directories

"tfcoreml":thetfcoremlpackage

"examples":examplestousethisconverter

"tests":unittests

"utils":generalscriptsforgraphinspection

License

ApacheLicense2.0

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论