This page is part of the documentation for the Machine Learning Database.
It is a static snapshot of a Notebook which you can play with interactively by trying MLDB online now.
It's free and takes 30 seconds to get going.
This is the companion notebook to the MLDB.ai guest blog post on KGNuggets.
The post will soon be published. If you want to try an interactive version of this notebook, simply signup for a free account.
from pymldb import Connection
mldb = Connection()
import pandas as pd
We start by creating the inception
function that we can call to run the trained Inception-V3 TensorFlow model:
print mldb.put('/v1/functions/inception', {
"type": 'tensorflow.graph',
"params": {
"modelFileUrl": 'archive+'+
'http://public.mldb.ai/models/inception_dec_2015.zip'+
'#tensorflow_inception_graph.pb',
"inputs": 'fetcher(url)[content] AS "DecodeJpeg/contents"',
"outputs": "pool_3"
}
})
We can then use it to embed any image in the representation it learned. Let's try doing this to the KDNuggets logo:
kdNuggets = "http://www.skytree.net/wp-content/uploads/2014/08/KDnuggets.jpg"
mldb.query("SELECT inception({url: '%s'}) as *" % kdNuggets)
We now load a CSV dataset that contains links to car images from three brands:
print mldb.post("/v1/procedures", {
"type": "import.text",
"params": {
"dataFileUrl": "https://public.mldb.ai/datasets/car_brand_images/cars_urls.csv",
"outputDataset": "images"
}
})
mldb.query("SELECT * FROM images LIMIT 3")
Let's get a sense of how many images we have in each class:
mldb.query("SELECT count(*) FROM images GROUP BY brand")
We can easily run a few images through the network like this:
mldb.query("SELECT inception({url: url}) AS * FROM images LIMIT 3")
To create our training dataset, we run a transform procedure to apply the TensorFlow model to all images:
print mldb.post("/v1/procedures", {
"type": "transform",
"params": {
"inputData": """
SELECT brand,
inception({url}) as *
FROM images
""",
"outputDataset": "training_dataset"
}
})
This gives us the following result:
mldb.query("SELECT * FROM training_dataset LIMIT 3")
Let's now train a model. We'll use a 50/50 split for training and testing, and use a random forest:
rez = mldb.post("/v1/procedures", {
"type": "classifier.experiment",
"params": {
"experimentName": "car_brand_cls",
"inputData": """
SELECT
{* EXCLUDING(brand)} as features,
brand as label
FROM training_dataset
""",
"mode": "categorical",
"modelFileUrlPattern": "file:///mldb_data/car_brand_cls.cls",
"configuration": {
"type": "bagging",
"weak_learner": {
"type": "boosting",
"weak_learner": {
"type": "decision_tree",
"max_depth": 5,
"update_alg": "gentle",
"random_feature_propn": 0.6
},
"min_iter": 5,
"max_iter": 30
},
"num_bags": 15
}
}
})
runResults = rez.json()["status"]["firstRun"]["status"]["folds"][0]["resultsTest"]
print rez
Let's look at our results on the test set:
pd.DataFrame(runResults["confusionMatrix"])\
.pivot_table(index="actual", columns="predicted", fill_value=0)
pd.DataFrame.from_dict(runResults["labelStatistics"]).transpose()
We create a function of type sql.expression
that will represent our pipeline and that we call brand_predictor
. It takes the URL to an image, passes it through the inception
model to extract the features, and then into the car_brand_cls_scorer_0
function that represents our trained model and that was created at the previous step.
print mldb.put("/v1/functions/brand_predictor", {
"type": "sql.expression",
"params": {
"expression": """
car_brand_cls_scorer_0(
{
features: inception({url})
}) as *
"""
}
})
We can now call this endpoint on new images and get predictions back:
# good tesla: http://www.automobile-propre.com/wp-content/uploads/2016/09/tesla-premiere-livraison-france-657x438.jpg
# good tesla: http://insideevs.com/wp-content/uploads/2016/03/JL82776-750x500.jpg
# good bmw: http://www.bmwhk.com/content/dam/bmw/common/all-models/1-series/5-door/2015/images-and-videos/bmw-1-series-wallpaper-1920x1200-03-R.jpg/jcr:content/renditions/cq5dam.resized.img.485.low.time1448014414633.jpg
mldb.get("/v1/functions/brand_predictor/application",
data={'input':
{'url': 'http://insideevs.com/wp-content/uploads/2016/03/JL82776-750x500.jpg'}})
You can now look at the full Transfer Learning with Tensorflow demo, or check out the other Tutorials and Demos.