<jupyter_start><jupyter_text>Using deep features to build an image classifier Fire up GraphLab Create(See [Getting Started with SFrames](../Week%201/Getting%20Started%20with%20SFrames.ipynb) for setup instructions)<jupyter_code>import graphlab
# Limit number of worker processes. This preserves system memory, which prevents hosted notebooks from crashing.
graphlab.set_runtime_config('GRAPHLAB_DEFAULT_NUM_PYLAMBDA_WORKERS', 8)<jupyter_output>[INFO] graphlab.cython.cy_server: GraphLab Create v2.1 started. Logging: C:\Users\e150003\AppData\Local\Temp\graphlab_server_1519881755.log.0<jupyter_text>Load a common image analysis datasetWe will use a popular benchmark dataset in computer vision called CIFAR-10.  (We've reduced the data to just 4 categories = {'cat','bird','automobile','dog'}.)This dataset is already split into a training set and test set.<jupyter_code>image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')<jupyter_output><empty_output><jupyter_text>Exploring the image data<jupyter_code>graphlab.canvas.set_target('ipynb')
image_train['image'].show()<jupyter_output><empty_output><jupyter_text>Train a classifier on the raw image pixelsWe first start by training a classifier on just the raw pixels of the image.<jupyter_code>raw_pixel_model = graphlab.logistic_classifier.create(
    image_train,target='label',
    features=['image_array'])<jupyter_output>PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.<jupyter_text>Make a prediction with the simple model based on raw pixels<jupyter_code>image_test[0:3]['image'].show()
image_test[0:3]['label']
raw_pixel_model.predict(image_test[0:3])<jupyter_output><empty_output><jupyter_text>The model makes wrong predictions for all three images.  Evaluating raw pixel model on test data<jupyter_code>raw_pixel_model.evaluate(image_test)<jupyter_output><empty_output><jupyter_text>The accuracy of this model is poor, getting only about 46% accuracy.  Can we improve the model using deep featuresWe only have 2005 data points, so it is not possible to train a deep neural network effectively with so little data.  Instead, we will use transfer learning: using deep features trained on the full ImageNet dataset, we will train a simple model on this small dataset.<jupyter_code>len(image_train)<jupyter_output><empty_output><jupyter_text>Computing deep features for our imagesThe two lines below allow us to compute deep features.  This computation takes a little while, so we have already computed them and saved the results as a column in the data you loaded. (Note that if you would like to compute such deep features and have a GPU on your machine, you should use the GPU enabled GraphLab Create, which will be significantly faster for this task.)<jupyter_code># deep_learning_model = graphlab.load_model('http://s3.amazonaws.com/GraphLab-Datasets/deeplearning/imagenet_model_iter45')
# image_train['deep_features'] = deep_learning_model.extract_features(image_train)<jupyter_output><empty_output><jupyter_text>As we can see, the column deep_features already contains the pre-computed deep features for this data.<jupyter_code>image_train.head()<jupyter_output><empty_output><jupyter_text>Given the deep features, let's train a classifier<jupyter_code>deep_features_model = graphlab.logistic_classifier.create(
    image_train,
    features=['deep_features'],
    target='label')<jupyter_output>PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.<jupyter_text>Apply the deep features model to first few images of test set<jupyter_code>image_test[0:3]['image'].show()
deep_features_model.predict(image_test[0:3])<jupyter_output><empty_output><jupyter_text>The classifier with deep features gets all of these images right!  Compute test_data accuracy of deep_features_modelAs we can see, deep features provide us with significantly better accuracy (about 78%)<jupyter_code>deep_features_model.evaluate(image_test)<jupyter_output><empty_output><jupyter_text>Quizzes  1. What’s the least common category in the training data?<jupyter_code>len(image_train[image_train['label'] == 'bird'])
len(image_train[image_train['label'] == 'dog'])
len(image_train[image_train['label'] == 'cat'])
len(image_train[image_train['label'] == 'automobile'])<jupyter_output><empty_output><jupyter_text>2. Of the images below, which is the nearest ‘cat’ labeled image in the training data to the the first image in the test data (image_test[0:1])?<jupyter_code>knn_model = graphlab.nearest_neighbors.create(
    image_train,
    features=['deep_features'],
    label='id')
image_test[0:1]['image'].show()
knn_imgs = knn_model.query(image_test[0:1])
knn_imgs
def get_images_from_ids(query_result):
    return image_train.filter_by(
        query_result['reference_label'], 'id')
get_images_from_ids(knn_imgs[0])['image'].show()<jupyter_output><empty_output><jupyter_text>3. Of the images below, which is the nearest ‘dog’ labeled image in the training data to the the first image in the test data (image_test[0:1])?<jupyter_code>image_test[0:1]['image'].show()
knn_imgs = knn_model.query(image_test[0:1], k=10)
knn_imgs.head()
knn_imgs_df = get_images_from_ids(knn_imgs)
knn_imgs_df.head()
knn_imgs_df[knn_imgs_df['label'] == 'dog'][0:1]['image'].show()<jupyter_output><empty_output><jupyter_text>4. For the first image in the test data, in what range is the mean distance between this image and its 5 nearest neighbors that were labeled ‘cat’ in the training data?<jupyter_code>knn_imgs = knn_model.query(image_test[0:1], k=5)
knn_imgs
def get_images_from_ids(query_result):
    df = image_train.filter_by(
        query_result['reference_label'], 'id')
    df['distance'] = query_result['distance']
    return df
knn_imgs_df = get_images_from_ids(knn_imgs)
knn_imgs_df.head()
knn_imgs_df['distance'].mean()<jupyter_output><empty_output><jupyter_text>5. For the first image in the test data, in what range is the mean distance between this image and its 5 nearest neighbors that were labeled ‘dog’ in the training data?<jupyter_code>knn_imgs = knn_model.query(image_test[0:1], k=None)
knn_imgs_df = get_images_from_ids(knn_imgs)
knn_imgs_df_dogs = knn_imgs_df[knn_imgs_df['label'] == 'dog']
knn_imgs_df_dogs[0:5]
knn_imgs_df_dogs[0:5]['distance'].mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0983556170278162, 'fraction_numerical': 0.013523897341324727, 'mean_word_length': 7.375804375804376, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '185014', 'license': 'Unlicense', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2091, 'n_words': 570, 'path': 'Coursera_Machine_Learning/1_Machine_Learning_Foundations_A_Case_Study_Approach/Week 6 - Deep Learning/Deep_Features_for_Image_Classification.ipynb', 'repo_name': 'dangkhoa1992/Advanced-Machine-Learning-Specialization', 'size': '6507'}