<jupyter_start><jupyter_text>KDNuggets Transfer Learning Blog PostThis 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](https://mldb.ai/signup). Import some libraries<jupyter_code>from pymldb import Connection
mldb = Connection()

import pandas as pd<jupyter_output><empty_output><jupyter_text>Inception on MLDBWe start by creating the `inception` function that we can call to run the trained Inception-V3 TensorFlow model:<jupyter_code>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"
    }
})<jupyter_output><Response [201]><jupyter_text>We can then use it to embed any image in the representation it learned. Let's try doing this to the KDNuggets logo:<jupyter_code>kdNuggets = "http://www.skytree.net/wp-content/uploads/2014/08/KDnuggets.jpg"

mldb.query("SELECT inception({url: '%s'}) as *" % kdNuggets)<jupyter_output><empty_output><jupyter_text>Loading our datasetWe now load a CSV dataset that contains links to car images from three brands:<jupyter_code>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")<jupyter_output><Response [201]><jupyter_text>Let's get a sense of how many images we have in each class:<jupyter_code>mldb.query("SELECT count(*) FROM images GROUP BY brand")<jupyter_output><empty_output><jupyter_text>We can easily run a few images through the network like this:<jupyter_code>mldb.query("SELECT inception({url: url}) AS * FROM images LIMIT 3")<jupyter_output><empty_output><jupyter_text>To create our training dataset, we run a transform procedure to apply the TensorFlow model to all images:<jupyter_code>print mldb.post("/v1/procedures", {
    "type": "transform",
    "params": {
        "inputData": """
            SELECT brand,
                   inception({url}) as *
            FROM images
        """,
        "outputDataset": "training_dataset"
    }
})<jupyter_output><Response [201]><jupyter_text>This gives us the following result:<jupyter_code>mldb.query("SELECT * FROM training_dataset LIMIT 3")<jupyter_output><empty_output><jupyter_text>Training a classifierLet's now train a model. We'll use a 50/50 split for training and testing, and use a random forest:<jupyter_code>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<jupyter_output><Response [201]><jupyter_text>Let's look at our results on the test set:<jupyter_code>pd.DataFrame(runResults["confusionMatrix"])\
    .pivot_table(index="actual", columns="predicted", fill_value=0)
pd.DataFrame.from_dict(runResults["labelStatistics"]).transpose()<jupyter_output><empty_output><jupyter_text>Creating a real-time endpointWe 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.<jupyter_code>print mldb.put("/v1/functions/brand_predictor", {
    "type": "sql.expression",
    "params": {
        "expression": """
            car_brand_cls_scorer_0(
                {
                    features: inception({url})
                }) as *
        """
    }
})<jupyter_output><Response [201]><jupyter_text>We can now call this endpoint on new images and get predictions back:<jupyter_code># 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'}})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12781130005485464, 'fraction_numerical': 0.024136039495337356, 'mean_word_length': 3.4435418359057675, 'pattern_counts': {'":': 34, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '371962', 'license': 'Apache-2.0', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1763, 'n_words': 389, 'path': 'container_files/other/KDNuggets Transfer Learning Blog Post.ipynb', 'repo_name': 'kstepanmpmg/mldb', 'size': '5469'}