TensorFlow(TF)toCoreMLConverter
Dependenciestensorflow>=1.5.0
coremltools>=0.8
numpy>=1.6.2
protobuf>=3.1.0
six>=1.10.0
InstallationInstallFromSourceTogetthelatestversionoftheconverter,clonethisrepoandinstallfromsource.Thatis,
git clone https://github.com/tf-coreml/tf-coreml.gitcd tf-coremlToinstallasapackagewithpip,eitherrun(attherootdirectory):
pip install -e .orrun:
python setup.py bdist_wheelThiswillgenerateapipinstallablewheelinsidethedistdirectory.
InstallFromPyPIToinstallthePypipackage:
pip install -U tfcoremlUsageSeeiPythonnotebooksinthedirectoryexamples/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.
SupportedOpsListofTensorFlowopsthataresupportedcurrently(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)
Limitationstfcoremlconverterhasthefollowingconstraints:
TFgraphmustbecyclefree(cyclesaregenerallycreatedduetocontrolflowopslikeif,while,map,etc.)
MusthaveNHWCordering(Batchsize,Height,Width,Channels)forimagefeaturemaptensors
Musthavetensorswithranklessthanorequalto4(len(tensor.shape)<=4)
TheconverterproducesCoreMLmodelwithfloatvalues.AquantizedTFgraph(suchasthestyletransfernetworklinkedabove)getsconvertedtoafloatCoreMLmodel
RunningUnitTestsInordertorununittests,youneedpytest.
pip install pytestToaddanewunittest,addittothetests/folder.Makesureyounamethefilewitha'test'astheprefix.Torunallunittests,navigatetothetests/folderandrun
pytestDirectories"tfcoreml":thetfcoremlpackage
"examples":examplestousethisconverter
"tests":unittests
"utils":generalscriptsforgraphinspection
LicenseApacheLicense2.0
评论