Developer Center

Resources to get you started with Algorithmia

Caffe

Updated

Welcome to hosting your Caffe model on Algorithmia!

This guide is designed as an introduction to hosting a Caffe model and publishing an algorithm even if you’ve never used Algorithmia before.

Table of Contents

Prerequisites

Before you get started hosting your model on Algorithmia there are a few things you’ll want to do first:

Save your Pre-Trained Model

You’ll want to do the training and saving of your model on your local machine, or the platform you’re using for training, before you deploy it to production on the Algorithmia platform.

After training your Caffe model, you’ll want to save the model and weights so you can upload it to Algorithmia.

Create a Data Collection

Host your data where you want and serve it to your model with Algorithmia’s Data API.

In this guide we’ll use Algorithmia’s Hosted Data Collection, but you can host it in S3 or Dropbox as well. Alternatively, if your data lies in a database, check out how we connected to a DynamoDB database.

First, you’ll want to create a data collection to host your pre-trained model.

  • Log into your Algorithmia account and create a data collection via the Data Collections page.

  • Click on “Add Collection” under the “My Collections” section.

  • After you create your collection you can set the read and write access on your data collection.

Create a data collection

For more information check out: Data Collection Types.

Note, that you can also use the Data API to create data collections and upload files.

Host Your Model File

Next, upload your model files to your newly created data collection.

  • Load model by clicking box “Drop files here to upload”

  • Note the path to your files:

    • data://username/collections_name/file_name.prototxt.txt,
    • data://username/collections_name/file_name.caffemodel

Create a data collection

Create your Algorithm

Hopefully you’ve already followed along with the Getting Started Guide for algorithm development. If not, you might want to check it out in order to understand the various permission types, how to enable a GPU environment, and use the CLI.

Once you’ve gone through the Getting Started Guide, you’ll notice that when you’ve created your algorithm, there is boilerplate code in the editor that returns “Hello” and whatever you input to the console.

The main thing to note about the algorithm is that it’s wrapped in the apply() function.

The apply() function defines the input point of the algorithm. We use the apply() function in order to make different algorithms standardized. This makes them easily chained and helps authors think about designing their algorithms in a way that makes them easy to leverage and predictable for end users.

Go ahead and remove the boilerplate code below that’s inside the apply() function on line 6, but leave the apply() function intact:

Algorithm console Python

Set your Dependencies

Now is the time to set your dependencies that your model relies on.

Here are some Caffe wheels for different versions as well as CPU and GPU wheels:

  • Caffe 0.1 (CPU): https://s3.amazonaws.com/algorithmia-wheels/caffe-0.1.0_cpu-py2-none-any.whl
  • Caffe 0.1 (GPU): https://s3.amazonaws.com/algorithmia-wheels/caffe-0.1.0_gpu-py2-none-any.whl
  • Caffe 1.05 (CPU): https://s3.amazonaws.com/algorithmia-wheels/caffe-01.05.16_b86b0aea60a_cpu-py2-none-any.whl
  • Caffe 1.05 (CGU): https://s3.amazonaws.com/algorithmia-wheels/caffe-01.05.16_b86b0aea60a_gpu-py2-none-any.whl
  • Caffe build tag 99466 (GPU): https://s3.amazonaws.com/algorithmia-wheels/caffe-27.11.17_99466_gpu-py2-none-any.whl

Click on the “Dependencies” button at the top right of the UI and list your packages under the required ones already listed and click “Save Dependencies” on the bottom right corner.

Please note that you will need to use the protobuf==3.0.0b2.post1 package to be able to run a caffe algorithm.

Set your dependencies

Load your Model

Here is where you load and run your model which will be called by the apply() function.

When you load your model, our recommendation is to preload your model in a separate function external to the apply() function.

This is because when a model is first loaded it can take time to load depending on the file size.

Then, with all subsequent calls only the apply() function gets called which will be much faster since your model is already loaded.

If you are authoring an algorithm, avoid using the ‘.my’ pseudonym in the source code. When the algorithm is executed, ‘.my’ will be interpreted as the user name of the user who called the algorithm, rather than the author’s user name.

Note that you always want to create valid JSON input and output in your algorithm. For examples see the Client Guides.

Here is an example for loading your model based on the Classifying MNIST digits based on Caffe’s tutorial.

import Algorithmia
import numpy as np
import caffe

caffe.set_mode_cpu()

client = Algorithmia.client()

def initialize_model():
    """
    Load caffe.Net model with layers
    """

    # Load model files from user collections
    model_uri = "data://user_name/caffe_demo/lenet.prototxt.txt"
    pretrained_uri = "data://user_name/caffe_demo/lenet_iter_10000.caffemodel"

    model_file = client.file(model_uri).getFile().name
    pretrained_file = client.file(pretrained_uri).getFile().name

    # Create net and load weights
    net = caffe.Net(model_file, pretrained_file, caffe.TEST, raw_scale=1, image_dims=(28, 28))

    return net

# Gets called once
net = initialize_model()

def apply(input):
    """
    Input is an image file

    Input examples:
    Data Sources via https://algorithmia.com/data, or http(s) URLs using the
    Smart Image Downloader.
    """

    client = Algorithmia.client()
    imgDataPath = client.algo("util/SmartImageDownloader").pipe(input).result["savePath"][0]
    imgAbsPath = client.file(imgDataPath).getFile().name

    image = caffe.io.load_image(imgAbsPath, color=False)
    out = net.forward_all(data=np.asarray([image]))
    probability_vector = out['prob'][0].argmax(axis=0)
    print(predicted_vector)
    prediction = net.predict([resized_image])

    return prediction

Publish your Algorithm

Last is publishing your algorithm. The best part of hosting your model on Algorithmia is that users can access it via an API that takes only a few lines of code to use! Here is what you can set when publishing your algorithm:

On the upper right hand side of the algorithm page you’ll see a purple button “Publish” which will bring up a modal:

Publish an algorithm

In this modal, you’ll see a Changes tab, a Sample I/O tab, and one called Versioning.

If you don’t recall from the Getting Started Guide how to go through the process of publishing your model, check that out before you finish publishing.

If you want to have a better idea of how a finished caffe algorithm looks like, check out: CaffeNet

For more information and detailed steps: creating and publishing your algorithm

That’s it for hosting your Caffe model on Algorithmia!