image_train = graphlab.SFrame('https://static.turi.com/datasets/coursera/deep_learning/image_train_data')
image_test = graphlab.SFrame('https://static.turi.com/datasets/coursera/deep_learning/image_test_data')

"""
Explanation: Deep Learning Basics
Today we'll walk through building a deep learning network for image recognition <a href='http://yann.lecun.com/exdb/mnist/'>CIFAR-10 dataset</a>. The CIFAR data-set represents real-world data that is already formatted and labeled, so we can focus on building our network today instead of cleaning the data.
<img src="images/cifar.png"></img>
We're going to walk through 4 steps today:
- Loading the Data
- Training our Model
- Evaluating the results of the model
- Look at ways to improveing the performance of our model

<img src="images/load.png"></img>
We've download a subset of the CIFAR-10 data set for you. We load the data into an SFame, which is a powerful and scalable data structure that is used by many of the models in GraphLab Create.
End of explanation
"""


image_train

graphlab.image_analysis.resize(image_train['image'], 128, 128, 3).show()

"""
Explanation: Exploring the data
End of explanation
"""


raw_pixel_model = graphlab.logistic_classifier.create(image_train, features=['image_array'], target='label')

"""
Explanation: <img src="images/train.png"></img>
We now use the neuralnet_classifier provided by GraphLab Create to create a neural network for our data set. The create method picks a default network architecture for you based on the dataset.
End of explanation
"""


graphlab.image_analysis.resize(image_test[0:1]['image'], 128, 128, 3).show()

image_test[0:1]['label']

raw_pixel_model.predict(image_test[0:1])

"""
Explanation: <img src="images/evaluate.png"></img>
In order to ensure that the deep learning model is acutally learning how to recognize the data, instead of memorizing features, we want to validate it with a dataset it hasn't seen before.
Making a single prediction
End of explanation
"""


raw_pixel_model.evaluate(image_test)

"""
Explanation: Evaluating the model
End of explanation
"""


# if you wanted to do this on your own
# step 1: deep_learning_model = graphlab.load_model('imagenet_model/')
# step 2: image_train['deep_features'] = deep_learning_model.extract_features(image_train)

deep_model = graphlab.neuralnet_classifier.create(image_train, features=['image'], target='label')

"""
Explanation: <img src="images/linear.png" width="50%" height="50%"></img>

<img src="images/improve.png"></img>
Our network does well on the data, but we'd like to do better. How can we improve it?
End of explanation
"""


graphlab.image_analysis.resize(image_test[0:1]['image'], 128, 128, 3).show()

image_test[0:1]['label']

deep_model.predict(image_test[0:1])

deep_model.evaluate(image_test)


"""
Explanation: Evaluating the Improved Model
End of explanation
"""


deep_features_model = graphlab.logistic_classifier.create(image_train, features=['deep_features'], target='label')

"""
Explanation: <img src="images/quadratic.png" width="50%" height="50%"></img>
Improving the model further with Deep Features
End of explanation
"""


deep_features_model.evaluate(image_test)


"""
Explanation: Evaluating the deep model
End of explanation
"""


nearest_neighbors_model = graphlab.nearest_neighbors.create(image_train, features=['deep_features'],
                                                            label='id')

def get_nearest_neighbors(image):
    ans = nearest_neighbors_model.query(image)
    return image_train.filter_by(ans['reference_label'],'id')

"""
Explanation: <img src="images/AA1.png"></img>

Finding Similar Images
<img src="images/workflow3.png"></img>
End of explanation
"""


cat = image_train[18:19]
graphlab.image_analysis.resize(cat['image'],128,128,3).show()

graphlab.image_analysis.resize(get_nearest_neighbors(cat)['image'],128,128,3).show()

"""
Explanation: <img src="images/workflow4.png"></img>
Similar Images: Cat Photo
End of explanation
"""


car = image_train[8:9]
graphlab.image_analysis.resize(car['image'], 128, 128, 3).show()

graphlab.image_analysis.resize(get_nearest_neighbors(car)['image'],256,256,3).show()

"""
Explanation: Similar Images: Car Photo
End of explanation
"""


