The code below illustrates how to invoke a model built with the watson recontion service.
You need three things to make this work.
The documentatation for the python api is very poor. the solution here was developed by looking at the source on github.
import watson_developer_cloud
from watson_developer_cloud import WatsonService
from watson_developer_cloud import VisualRecognitionV3
We first create an instance of the VisualRecognitionV3 class.
visual_recognition = VisualRecognitionV3(
api_key = '1fc969d38 your key here 7f7d3d27334',
version = '2016-05-20'
)
classifier_id = 'galaxies_1872954591'
We can use "get_classifier()" to set and return information about the classifier
visual_recognition.get_classifier(classifier_id)
visual_recognition.list_classifiers()
Now let's classify an image. we will try number 12 in the test set. it should be elliptical.
To invoke the classifier we will pass the url of the image.
i = 12
base_image_url = "https://s3-us-west-2.amazonaws.com/learn-galaxies/"
image_url = base_image_url + "bigtest/t{}.jpg".format(i)
print(image_url)
The simplest way to invoke the classifier it to pass it a json parameter file containing the url and classifer id.
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
import json
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
The classifier return a dict that contains a list of images, each of which contains a list of classifiers and each of those contain the ordered list of clasifications of the image together with the score. we have only one image and one classifier, so the following will get what we want.
x['images'][0]['classifiers'][0]['classes'][0]
the following cells compute the confusion matrix for the test set and for the train set. looking at the results we see that the classifier sometimes fails to classify an image. we call that result "none".
results = []
for i in range(0,40):
image_url = base_image_url + "bigtest/t{}.jpg".format(i)
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
try:
results.append(x['images'][0]['classifiers'][0]['classes'][0])
except:
results.append({'class': 'none', 'score': 0.0})
results
mydict = {'spiral': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'barred spiral': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'elliptical': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'irregular': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0}}
keys = []
for i in range(0,10):
keys.append('barred spiral')
for i in range(10,20):
keys.append('elliptical')
for i in range(20,30):
keys.append('irregular')
for i in range(30,40):
keys.append('spiral')
for i in range(0,40):
key = keys[i]
result = results[i]['class']
mydict[key][result]+= 1
import pandas as pd
df = pd.DataFrame.from_dict(mydict)
df.transpose()
nms = ['spiral', 'barred spiral', 'elliptical', 'irregular', 'none']
for r in nms:
if r != 'none':
total = 0.0
for c in nms:
total += df[r][c]
for c in nms:
df[r][c] = round(100.0*df[r][c]/total)
df.transpose()
testdict = {'spiral': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'barred spiral': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'elliptical': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0},
'irregular': {'spiral':0.0, 'barred spiral':0.0, 'elliptical':0.0, 'irregular':0.0, 'none': 0.0}}
for i in range(1,20):
image_url = base_image_url + "bigbarred/bs{}.jpg".format(i)
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
try:
y = x['images'][0]['classifiers'][0]['classes'][0]
except:
y = {'class': 'none', 'score': 0.0}
testdict['barred spiral'][y['class']]+=1
for i in range(1,20):
image_url = base_image_url + "bigelliptical/e{}.jpg".format(i)
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
try:
y = x['images'][0]['classifiers'][0]['classes'][0]
except:
y = {'class': 'none', 'score': 0.0}
testdict['elliptical'][y['class']]+=1
for i in range(1,20):
image_url = base_image_url + "bigirregular/i{}.jpg".format(i)
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
try:
y = x['images'][0]['classifiers'][0]['classes'][0]
except:
y = {'class': 'none', 'score': 0.0}
testdict['irregular'][y['class']]+=1
for i in range(1,20):
image_url = base_image_url + "bigspiral/s{}.jpg".format(i)
param = { "url": image_url, "classifier_ids":['galaxies_1872954591']}
js = json.dumps(param)
x = visual_recognition.classify(parameters=js)
try:
y = x['images'][0]['classifiers'][0]['classes'][0]
except:
y = {'class': 'none', 'score': 0.0}
testdict['spiral'][y['class']]+=1
tdf = pd.DataFrame.from_dict(testdict)
for r in nms:
if r != 'none':
total = 0.0
for c in nms:
total += tdf[r][c]
for c in nms:
tdf[r][c] = round(100.0*tdf[r][c]/total)
tdf.transpose()
x