Skip to content

Feature 106 visual recognition retraining #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Change Log
==========
## Version 0.7.0
_2016-07-29_

* New: Visual Recognition: Added retraining functionality.
* New: Visual Recognition: Use byteArray data to classify, detect faces and recognize text.
* Fix: Updated integration testing.


## Version 0.6.1
_2016-07-17_

Expand Down
Binary file modified Config.json.enc
Binary file not shown.
123 changes: 67 additions & 56 deletions Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;
Expand All @@ -35,60 +36,70 @@ void Start()
LogSystem.InstallDefaultReactors();

// Get all classifiers
Log.Debug("ExampleVisualRecognition", "Attempting to get all classifiers");
if (!m_VisualRecognition.GetClassifiers(OnGetClassifiers))
Log.Debug("ExampleVisualRecognition", "Getting classifiers failed!");
//
// Find classifier by name
m_VisualRecognition.FindClassifier(m_classifierName, OnFindClassifier);

// Find classifier by ID
if (!m_VisualRecognition.GetClassifier(m_classifierID, OnGetClassifier))
Log.Debug("ExampleVisualRecognition", "Getting classifier failed!");

// Delete classifier by ID
if (!m_VisualRecognition.DeleteClassifier(m_classifierToDelete, OnDeleteClassifier))
Log.Debug("ExampleVisualRecognition", "Deleting classifier failed!");

// Train classifier
string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
string m_negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
if (!m_VisualRecognition.TrainClassifier("unity-test-classifier5", "giraffe", m_positiveExamplesPath, m_negativeExamplesPath, OnTrainClassifier))
Log.Debug("ExampleVisualRecognition", "Train classifier failed!");

// Classify get
if (!m_VisualRecognition.Classify(m_imageURL, OnClassify))
Log.Debug("ExampleVisualRecognition", "Classify image failed!");

// Classify post image
string m_imagesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
string[] m_owners = { "IBM", "me" };
string[] m_classifierIDs = { "default" };
if (!m_VisualRecognition.Classify(OnClassify, m_imagesPath, m_owners, m_classifierIDs, 0.5f))
Log.Debug("ExampleVisualRecognition", "Classify image failed!");


// Detect faces get
if (!m_VisualRecognition.DetectFaces(m_imageURL, OnDetectFaces))
Log.Debug("ExampleVisualRecogntiion", "Detect faces failed!");

// Detect faces post image
string m_faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
if (!m_VisualRecognition.DetectFaces(OnDetectFaces, m_faceExamplePath))
Log.Debug("ExampleVisualRecognition", "Detect faces failed!");



// Recognize text get
if (!m_VisualRecognition.RecognizeText(m_imageTextURL, OnRecognizeText))
Log.Debug("ExampleVisualRecognition", "Recognize text failed!");

// Recognize text post image
string m_textExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/from_platos_apology.png";
if (!m_VisualRecognition.RecognizeText(OnRecognizeText, m_textExamplePath))
Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
//// Find classifier by name
//Log.Debug("ExampleVisualRecognition", "Attempting to find classifier by name");
//m_VisualRecognition.FindClassifier(OnFindClassifier, m_classifierName);

//// Find classifier by ID
//Log.Debug("ExampleVisualRecognition", "Attempting to find classifier by ID");
//if (!m_VisualRecognition.GetClassifier(OnGetClassifier, m_classifierID))
// Log.Debug("ExampleVisualRecognition", "Getting classifier failed!");

//// Delete classifier by ID
//Log.Debug("ExampleVisualRecognition", "Attempting to delete classifier");
//if (!m_VisualRecognition.DeleteClassifier(OnDeleteClassifier, m_classifierToDelete))
// Log.Debug("ExampleVisualRecognition", "Deleting classifier failed!");

//// Train classifier
//Log.Debug("ExampleVisualRecognition", "Attempting to train classifier");
//string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
//string m_negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
//Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
//positiveExamples.Add("giraffe", m_positiveExamplesPath);
//if (!m_VisualRecognition.TrainClassifier(OnTrainClassifier, "unity-test-classifier-example", positiveExamples, m_negativeExamplesPath))
// Log.Debug("ExampleVisualRecognition", "Train classifier failed!");

//// Classify get
//Log.Debug("ExampleVisualRecognition", "Attempting to get classify via URL");
//if (!m_VisualRecognition.Classify(m_imageURL, OnClassify))
// Log.Debug("ExampleVisualRecognition", "Classify image failed!");

//// Classify post image
//Log.Debug("ExampleVisualRecognition", "Attempting to classify via image on file system");
//string m_imagesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
//string[] m_owners = { "IBM", "me" };
//string[] m_classifierIDs = { "default" };
//if (!m_VisualRecognition.Classify(OnClassify, m_imagesPath, m_owners, m_classifierIDs, 0.5f))
// Log.Debug("ExampleVisualRecognition", "Classify image failed!");

//// Detect faces get
//Log.Debug("ExampleVisualRecognition", "Attempting to detect faces via URL");
//if (!m_VisualRecognition.DetectFaces(m_imageURL, OnDetectFaces))
// Log.Debug("ExampleVisualRecogntiion", "Detect faces failed!");

//// Detect faces post image
//Log.Debug("ExampleVisualRecognition", "Attempting to detect faces via image");
//string m_faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
//if (!m_VisualRecognition.DetectFaces(OnDetectFaces, m_faceExamplePath))
// Log.Debug("ExampleVisualRecognition", "Detect faces failed!");

//// Recognize text get
//Log.Debug("ExampleVisualRecognition", "Attempting to recognizeText via URL");
//if (!m_VisualRecognition.RecognizeText(m_imageTextURL, OnRecognizeText))
// Log.Debug("ExampleVisualRecognition", "Recognize text failed!");

//// Recognize text post image
//Log.Debug("ExampleVisualRecognition", "Attempting to recognizeText via image");
//string m_textExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/from_platos_apology.png";
//if (!m_VisualRecognition.RecognizeText(OnRecognizeText, m_textExamplePath))
// Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
}

private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers)
private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, string data)
{
if (classifiers != null && classifiers.classifiers.Length > 0)
{
Expand All @@ -103,7 +114,7 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers)
}
}

private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier)
private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
{
if (classifier != null)
{
Expand All @@ -115,7 +126,7 @@ private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier)
}
}

private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
{
if (classifier != null)
{
Expand All @@ -127,7 +138,7 @@ private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
}
}

private void OnDeleteClassifier(bool success)
private void OnDeleteClassifier(bool success, string data)
{
if (success)
{
Expand All @@ -139,7 +150,7 @@ private void OnDeleteClassifier(bool success)
}
}

private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
{
if (classifier != null)
{
Expand All @@ -151,7 +162,7 @@ private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
}
}

private void OnClassify(ClassifyTopLevelMultiple classify)
private void OnClassify(ClassifyTopLevelMultiple classify, string data)
{
if (classify != null)
{
Expand All @@ -173,7 +184,7 @@ private void OnClassify(ClassifyTopLevelMultiple classify)
}
}

private void OnDetectFaces(FacesTopLevelMultiple multipleImages)
private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data)
{
if (multipleImages != null)
{
Expand All @@ -196,7 +207,7 @@ private void OnDetectFaces(FacesTopLevelMultiple multipleImages)
}
}

private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages)
private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages, string data)
{
if (multipleImages != null)
{
Expand Down
Binary file not shown.
Binary file modified Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip
100644 → 100755
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
}
}
```

##### Training classifiers
Train a new classifier by uploading image data. Two compressed zip files containing at least two positive example files or one positive and one negative example file. The prefix of the positive example file is used as the classname for the new classifier `<Class Name>_positive_examples`. Negative examples zip must be named `negative_examples`. After a successful call, training the classifier takes a few minutes.

Expand All @@ -466,6 +467,36 @@ private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
}
}
```

##### Updating a classifier
Update an existing classifier by adding new classes, or by adding new images to existing classes. To update the existing classifier, use several compressed `.zip` files, including files containing positive or negative images `.jpg` or `.png`. You must supply at least one compressed file, with additional positive or negative examples.

Compressed files containing positive examples are used to add or create `classes` that define the updated classifier. The prefix that you specify for each positive example parameter is used as the class name within the classifier. The `_positive_examples` suffix is required. There is no limit on the number of positive example files you can upload in a single call.
The compressed file containing negative examples is not used to create a class within the created classifier, but does define what the new classifier is not. Negative example files should contain images that do not depict the subject of any of the positive examples. You can only specify one negative example file in a single call.

```cs
private VisualRecognition m_VisualRecognition = new VisualRecognition();

void Start()
{
string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/<Class Name>_positive_examples.zip";
if(!m_VisualRecognition.UpdateClassifier(OnUpdateClassifier, "<ClassifierID>", "<Classifier Name>", "<Class Name>", m_positiveExamplesPath))
Log.Debug("ExampleVisualRecognition", "Update classifier failed!");
}

private void OnUpdateClassifier(GetClassifiersPerClassifierVerbose classifier)
{
if(classifier != null)
{
Log.Debug("ExampleVisualRecognition", "Classifier is retraining! " + classifier);
}
else
{
Log.Debug("ExampleVisualRecognition", "Failed to update classifier!");
}
}
```

##### Deleting classifiers
Delete a classifier by Classifier ID

Expand Down
2 changes: 1 addition & 1 deletion Scripts/Connection/RESTConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

// uncomment to enable debugging
#define ENABLE_DEBUGGING
//#define ENABLE_DEBUGGING

using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;
Expand Down
Loading