Package: transferit
transferitThis repository contains a Python package that can help you train an image
classification model using transfer learning and serve the model with TensorFlow Serving
and Docker.The repo contains sample images you can use to train the model to tell a certain kind of
collectible playing card, namelyMagic: The Gatheringcards from other objects. These,
however, only serve as an example. You can easily use the transferit package to train
models on your own data.InstallationInstall from PyPI:pip3installtransferitTo get the example data and run the examples, you will need to check out the repository:gitclonegit@github.com:sorenlind/transferit.gitQuick startClone the repoInstall the package either from source or from PyPIDownload theCaltech 256
datasetand store the.tarfile inside thedata/rawfolder.Run the01 Create model.ipynbnotebook in thenotebooksfolder.Build the docker image:docker build -t transferit .Start the container:docker run -t --rm -p 8501:8501 transferitRun the02 API Example usage.ipynbnotebook in thenotebooksfolder.IntroductionThe package provides a command line application,transferitwhich can help you prepare
date for training and evaluation as well as training the model. Finally, it can also
wrap or package the trained model in way that makes it compatible with TensorFlow
Serving. Thetransferitcommand has four sub commands as briefly explained below:create-class: Copy and resize images in a specified folder (and its subfolders) to
another folder. This is handy if, for example you are training a binary image
classifier and you have a library of various kinds of images which you will use for
the negative class and a smaller set of custom images that you will use for the
positive class. Running this command twice (once for the positive and once for the
negative) class can create a complete data set for you.split: Creates a train / dev split using a dataset already prepared using thecreate-classsub command.train: Train the actual model using the training and dev data created using thesplitsub command.wrap: Wrap a trained model to make it compatible with TensorFlow Serving and ready
to be copied to a docker image.In addition to running the command line application you can also call the relevant
functions from your Python code such as a Jupyter notebook. Thenotebooksfolder
contains a notebook01 Create model.ipynbwhich runs through the entire process of
preparing data, training a model and wrapping it for serving. Note that before you can
run the notebook, you will have to download theCaltech 256
datasetand store the.tarfile inside thedata/rawfolder.Preparing imagestransferitcreate-class./raw/256_ObjectCategories/./prepared/full/negative--n-max3000transferitcreate-class./raw/cards/./prepared/full/positiveCreating train / dev splittransferitsplit./prepared/full/./prepared/Training modeltransferittrain./prepared/train/./prepared/dev/./models/naked/Wrapping up model for TF Servingtransferitwrapmodels/naked/models_best_loss.hdf5./models/wrapped/00000001/-cNegativePositiveCreating Dockerfile from the templateThe repository contains a template for a Dockerfile calledDockerfile.template. You
can create a copy of this simply calledDockerfileand edit it to match your setup. If
you have been running the Jupyter notebook to train and wrap a model, you do not need to
make any changes to the Dockerfile.Serving model locally using DockerBuild the image:dockerbuild-ttransferit.Once you have built the image, you can serve the model in a container as follows:docker run -t --rm -p 8501:8501 transferitOnce the container is running, you can access it as shown in the example below. Thenotebooksfolder contains a notebook called02 API Example usage.ipynbwhich has
similar code and classifies two images from the dev dataset.importbase64importjsonimportrequestsURL="http://localhost:8501/v1/models/transferit:classify"HEADERS={"content-type":"application/json"}withopen(img_filename,mode="rb")asfile:img=file.read()jpeg_bytes=base64.b64encode(img).decode("utf-8")body={"signature_name":"serving_default","examples":[{"x":{"b64":jpeg_bytes},}],}json_response=requests.post(URL,data=json.dumps(body),headers=HEADERS)json_response.status_code