watson-developer-cloud / python-sdk

:snake: Client library to use the IBM Watson services in Python and available in pip as watson-developer-cloud
https://pypi.org/project/ibm-watson/
Apache License 2.0
1.46k stars 827 forks source link

Using buffered files to create custom visual recognition classifier #187

Closed labalekhan closed 7 years ago

labalekhan commented 7 years ago

I am using IBM data Science Experience notebooks to create a custom classifier for the visual recognition service. I have the training zip files loaded into Object storage. But when I try to create the custom classifier it fails with the error message.

"explanation": "Cannot execute learning task : Could not train classifier. Verify there are at least 10 positive training images for at least 1 class and at least 10 other unique training images."

The buffered zip files are read by the create_classifier function but it does not detect the files inside. It works fine if I use local files.

kognate commented 7 years ago

What version of python are you using?

kognate commented 7 years ago

Also, without seeing your code, it's a little difficult to comment. Can you post the notebook you're using (without credentials, of course) or at least the relevant segments?

labalekhan commented 7 years ago

I am using Python 2.

Similar request has been posted over at the node-sdk at https://github.com/watson-developer-cloud/node-sdk/issues/333 which is for the classify function.

With the Python SDK I wanted to use buffered zip files to create the custom classifier.

Below is what I am trying to do.

import swiftclient
import keystoneclient.v3 as keystoneclient
import StringIO
import io

credentials = {
// Credentials
}
auth_url = credentials['auth_url'] + '/v3'
project_name = credentials['project']
password = credentials['password']
user_domain_name = credentials['domain_name']
project_id = credentials['project_id']
user_id = credentials['user_id']
region_name = credentials['region']

conn = swiftclient.Connection(
    key=credentials['password'],
    authurl=credentials['auth_url']+"/v3",
    auth_version='3',
    os_options={
        "project_id": credentials['project_id'],
        "user_id": credentials['user_id'],
        "region_name": credentials['region']})

// get zip files stored in object storage

class1 = conn.get_object(credentials['container'], 'class1.zip')
class2 =  conn.get_object(credentials['container'], 'class2.zip')
neg = conn.get_object(credentials['container'], 'negative.zip')

class1_f = io.BytesIO()
class2_f = io.BytesIO()
neg_f = io.BytesIO() 

// write content of zip files to file like objects
class1_f.write(class1[1])
class2_f.write(class2[1])
neg_f.write(neg[1])

class1_f.pos=0
class2_f.pos=0
neg_f.pos=0

print(json.dumps
          (visual_recognition.create_classifier
           ('MyClassifier',
            class1_positive_examples=class1_f, 
            class2_positive_examples=class2_f,
            negative_examples=neg_f), indent=2))

I have also tried converting the contents of the zip file to python zipfile objects and passing that to the create_classifier function but that also did not work.

kognate commented 7 years ago

You have to upload the files slightly differently than you are doing. Here is a repo https://github.com/kognate/python-sdk-187

that repo has a notebook that shows you how to do this. It's fully supported by the sdk, you just have to provide some extra metadata and you don't have to save it locally to a file.

labalekhan commented 7 years ago

@kognate thank you, that works perfectly.