Welcome to Algorithmia! This guide is designed as an introduction to hosting your OpenCV model and publishing it as an algorithm, even if you’ve never used Algorithmia before!
Create a Data Collection
Here you’ll want to create a data collection to host your model.
To use the Data API, log into your Algorithmia account and create a data collection via the Data Collections page.
Click on “Add Collection” under the “My Collections” section on your data collections page.
After you create your collection you can set the read and write access on your data collection. For more information check out: Data Collection Types
Let’s create a data collection where we’ll save our model.
Training a model
Before we create our algorithm, let’s first train our model on the handwriting digits dataset and save it to the DataAPI.
This requires version
The last line will save your model to the data collection you’ve created.
Create your Algorithm
Creating your algorithm is easy!
- To add an algorithm, simply click “Add Algorithm” from the user profile icon.
- Name your algorithm, select the language, choose permissions and make the code either open or closed source.
Note: There is also a checkbox for ‘Standard Execution Environment’ or ‘Advanced GPU’. For deep learning models you will want to check ‘Advanced GPU’.
Note: Make sure that your version of python is the same between your development environment and the algorithm. There may be version conflicts otherwise.
Set your dependencies
Now is the time to set the dependencies your model relies on.
- Click on the “Dependencies” button at the top right of the UI and add
opencv-python==184.108.40.206under the required ones already listed and click “Save Dependencies” on the bottom right corner.
Load your model
Here is where you load your model, which will be called by the
apply() function when you pass input to the algorithm using our API.
Our recommendation is to preload your model in a separate function before the apply(). The reasoning behind this is because when your model is first loaded it can take some time to load depending on the file size. However, with all subsequent calls only the apply() function gets called which will be much faster since your model is already loaded!
Note that you always want to create valid JSON input and output in your algorithm. For example this algorithm takes a JSON blob passing in a csv file hosted on Algorithmia, Amazon S3, or Dropbox.
We’ll deploy our newly trained model. For testing the model we have, we’ll crop a single zero digit from the dataset for testing.
Preloading your OpenCV model is a great step for improving your overall performance. By downloading your OpenCV model file when the container first starts, it means that any subsequent requests to the algorithm won’t have the same loading overhead!
Here’s an example of what that looks like:
Image pre-processing functions
Before we can pass the image to the model, we need to pre-process the user provided image.
The first function deskews the image, and the second function finds the HOG descriptor of the image.
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:
In this modal, you’ll see a Changes tab, a Sample I/O tab, and one called Versioning.
Changes shows you your commit history and release notes.
Sample I/O is where you’ll create your sample input and output for the user to try under Try the API in the Run tab. When you add a sample input, make sure to test it out with all the inputs that you accept since users will be able to test your algorithm with their own inputs.
Under the Versioning tab, you can select whether your algorithm will be for public use or private use as well as set the royalty. The algorithm can either be royalty-free or charge per-call. If you opt to have the algorithm charge a royalty, as the author, you will earn 70% of the royalty cost.
Check out Algorithm Pricing for more information on how much algorithms will cost to run.
Under Semantic Versioning you can choose which kind of release your change should fall under: Major, Minor, or Revision.
If you are satisfied with your algorithm and settings, go ahead and hit publish. Congratulations, you’re an algorithm developer!
For more information and detailed steps: creating and publishing your algorithm
This guide is based on the OpenCV tutorial here.
If you run into any trouble, please contact us at firstname.lastname@example.org.