Train

Clarifai provides many different models that 'see' the world differently. A model contains a group of concepts. A model will only see the concepts it contains.

There are times when you wish you had a model that sees the world the way you see it. The API allows you to do this. You can create your own model and train it with your own images and concepts. Once you train it to see how you would like it to see, you can then use that model to make predictions.

You do not need many images to get started. We recommend starting with 10 and adding more as needed. Before you train your first model you will have needed to create an application. From there you will be able to change your Base Workflow to optimize custom training using the knowledge base from select public models.

inputs outputs

Add Images With Concepts

To get started training your own model, you must first add images that already contain the concepts you want your model to see.

js
python
java
csharp
objective-c
php
cURL
app.inputs.create({
url: "https://samples.clarifai.com/puppy.jpg",
concepts: [
{
id: "boscoe",
value: true
}
]
});
Response JSON
{
"status": {
"code": 10000,
"description": "Ok"
},
"inputs": [
{
"id": "e82fd13b11354d808cc48dc8f94ec3a9",
"created_at": "2016-11-22T17:16:00Z",
"data": {
"image": {
"url": "https://samples.clarifai.com/puppy.jpeg"
},
"concepts": [
{
"id": "boscoe",
"name": "boscoe",
"app_id": "f09abb8a57c041cbb94759ebb0cf1b0d",
"value": 1
}
]
},
"status": {
"code": 30000,
"description": "Download complete"
}
}
]
}

Create A Model

Once your images with concepts are added, you are now ready to create the model. You'll need a name for the model and you'll also need to provide it with the concepts you added above.

Take note of the model id that is returned in the response. You'll need that for the next two steps.

js
python
java
csharp
objective-c
php
cURL
app.models.create(
"pets",
[
{ "id": "boscoe" }
]
).then(
function(response) {
// do something with response
},
function(err) {
// there was an error
}
);
Response JSON
{
"status": {
"code": 10000,
"description": "Ok"
},
"model": {
"name": "pets",
"id": "a10f0cf48cf3426cbb8c4805e246c214",
"created_at": "2016-11-22T17:17:36Z",
"app_id": "f09abb8a57c041cbb94759ebb0cf1b0d",
"output_info": {
"message": "Show output_info with: GET /models/{model_id}/output_info",
"type": "concept",
"output_config": {
"concepts_mutually_exclusive": false,
"closed_environment": false
}
},
"model_version": {
"id": "e7bcd534b61b4874a3ab69fba974c012",
"created_at": "2016-11-22T17:17:36Z",
"status": {
"code": 21102,
"description": "Model not yet trained"
}
}
}
}

Train The Model

Now that you've added images with concepts, then created a model with those concepts, the next step is to train the model. When you train a model, you are telling the system to look at all the images with concepts you've provided and learn from them. This train operation is asynchronous. It may take a few seconds for your model to be fully trained and ready.

Keep note of the model_version id in the response. We'll need that for the next section when we predict with the model.

js
python
java
csharp
objective-c
php
cURL
app.models.train("{model_id}").then(
function(response) {
// do something with response
},
function(err) {
// there was an error
}
);
// or if you have an instance of a model
model.train().then(
function(response) {
// do something with response
},
function(err) {
// there was an error
}
);
Response JSON
{
"status": {
"code": 10000,
"description": "Ok"
},
"model": {
"name": "pets",
"id": "a10f0cf48cf3426cbb8c4805e246c214",
"created_at": "2016-11-22T17:17:36Z",
"app_id": "f09abb8a57c041cbb94759ebb0cf1b0d",
"output_info": {
"message": "Show output_info with: GET /models/{model_id}/output_info",
"type": "concept",
"output_config": {
"concepts_mutually_exclusive": false,
"closed_environment": false
}
},
"model_version": {
"id": "d1b38fd2251148d08675c5542ef00c7b",
"created_at": "2016-11-22T17:21:13Z",
"status": {
"code": 21103,
"description": "Custom model is currently in queue for training, waiting on inputs to process."
}
}
}
}

Predict With The Model

Now that we have a trained model. We can start making predictions with it. In our predict call we need to specify three items. The model id, model_version id and the input we want a prediction for.

Note: you can repeat the above steps as often as you like. By adding more images with concepts and training, you can get the model to predict exactly how you want it to.

js
python
java
csharp
objective-c
php
cURL
let app = new Clarifai.App({apiKey: 'YOUR_API_KEY'});
app.models.predict({id:'MODEL_ID', version:'MODEL_VERSION_ID'}, "https://samples.clarifai.com/metro-north.jpg").then(
function(response) {
// do something with response
},
function(err) {
// there was an error
}
);
Response JSON
{
"status": {
"code": 10000,
"description": "Ok"
},
"outputs": [
{
"id": "e8b6eb27de764f3fa8d4f7752a3a2dfc",
"status": {
"code": 10000,
"description": "Ok"
},
"created_at": "2016-11-22T17:22:23Z",
"model": {
"name": "pets",
"id": "a10f0cf48cf3426cbb8c4805e246c214",
"created_at": "2016-11-22T17:17:36Z",
"app_id": "f09abb8a57c041cbb94759ebb0cf1b0d",
"output_info": {
"message": "Show output_info with: GET /models/{model_id}/output_info",
"type": "concept",
"output_config": {
"concepts_mutually_exclusive": false,
"closed_environment": false
}
},
"model_version": {
"id": "d1b38fd2251148d08675c5542ef00c7b",
"created_at": "2016-11-22T17:21:13Z",
"status": {
"code": 21100,
"description": "Model trained successfully"
}
}
},
"input": {
"id": "e8b6eb27de764f3fa8d4f7752a3a2dfc",
"data": {
"image": {
"url": "https://samples.clarifai.com/puppy.jpeg"
}
}
},
"data": {
"concepts": [
{
"id": "boscoe",
"name": "boscoe",
"app_id": "f09abb8a57c041cbb94759ebb0cf1b0d",
"value": 0.98308545
}
]
}
}
]
}