<jupyter_start><jupyter_text><jupyter_code># Full tutorial with explanation at https://medium.com/@johnson.doc/create-a-world-class-deep-learning-model-using-google-drive-and-deploy-it-as-an-app-7edc6695bbac 
# Installing TuriCreate. This is our main package. TuriCreate is open sourced. 
# You can get more info about TuriCreate at https://github.com/apple/turicreate

!pip install turicreate<jupyter_output>Requirement already satisfied: turicreate in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: pandas>=0.19.0 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Collecting mxnet<1.0.0,>=0.11 (from turicreate)
  Using cached mxnet-0.12.1-py2.py3-none-manylinux1_x86_64.whl
Requirement already satisfied: pillow>=3.3.0 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Requirement already satisfied: coremltools==0.8 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Requirement already satisfied: requests>=2.9.1 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Requirement already satisfied: prettytable==0.7.2 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Requirement already satisfied: decorator>=4.0.9 in /usr/local/lib/python3.6/dist-packages (from turicreate)
Requirement already satisfied: numpy>=1.9.0 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.19.0->turicreate)
Requirement already satisfied: pytz>=20[...]<jupyter_text><jupyter_code># Finding the directory we are in
!ls<jupyter_output>datalab  drive<jupyter_text>Next code block will mount your google drive. This will help you to access files stored in your google drive. Druing installation you see a prompt with a link -Please, open the following URL in a web browser: https://accounts.google.com/o ....Click the link. Copy and paste the code from the link in the form field and press enter. Sometime You may have to run this code block twice. I think this is a bug in colab.<jupyter_code>!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
# Mount your google drive to access files from it 
!mkdir -p drive
!google-drive-ocamlfuse drive
# The image folder that I am using for this tutorial is located in my google drive under 'app' folder
# So we need to change directory to the 'app' folder

import os
os.chdir("drive/app")
# make sure that we are in the right folder 
!ls
# importing turicreate as tc

import turicreate as tc
# Loading the images from ImageFolder in my google drive. This 'ImageFolder' has 2 folders
# 'Cat' and 'Dog'
# Each folder has about 1000 images
# Original images were obtained from https://www.kaggle.com/c/dogs-vs-cats/data
# First the whole data set from kaggle website was downloaded to my desktop. 
# After that I created a folder called 'ImageFolder' in my desktop with sub folders 'Cat' & 'Dog'
# Then I copied the first 1000 images of cats and dogs to the individual folders
# Later the 'ImageFolder' was uploaded to the 'app' folder in my google drive

data = tc.image_analysis.load_images('ImageFolder', with_path=True)<jupyter_output><empty_output><jupyter_text>It took 5 seconds to process the files.<jupyter_code># Labelling each images in the SFrame using the folder name

data['label'] = data['path'].apply(lambda path: 'dog' if '/Dog' in path else 'cat')
#Displaying the first 5 rows in data to make sure that we got everything right 

data.head()
# Saving the data frame to our google drive 
data.save('cats-dogs.sframe')
# Splitting the image data set into training (80%) and testing (20%) data
train_data, test_data = data.random_split(0.8)
# Switching to the root directory in our google virtual machine
os.chdir("/usr")
# installing other packahes to use GPU
!apt install libnvrtc8.0
# uninstalling previosu versions of mxnet
!pip uninstall -y mxnet
# Installing the latest version of mxnet to work with turi create
!pip install mxnet==1.1.0
# making sure that mxnet is working
# If you get an erroe here, you need to fix it before going forward
import mxnet as m2
# Automatically pick the right model based on your data.
# Note: Because the dataset is large, model creation may take hours.

model = tc.image_classifier.create(train_data, target='label')<jupyter_output>Resizing images...
Performing feature extraction on resized images...
Completed  512/1591
Completed 1024/1591
Completed 1536/1591
Completed 1591/1591
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>Took only about 7 minutes to do this with GPU!<jupyter_code># storing the prediction 

predictions = model.predict(test_data)
# Evaluating the model 

metrics = model.evaluate(test_data)
print(metrics['accuracy'])
# Printing confusion matrix

print ("Confusion Matrix : \n%s" % metrics['confusion_matrix'])
# Save the model for later use in Turi Create
model.save('mymodel.model')
# Export for use in Core ML
model.export_coreml('MyCustomImageClassifier.mlmodel')
!ls
# Downloading the model for use in Xcode

from google.colab import files

files.download('MyCustomImageClassifier.mlmodel')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09377174669450243, 'fraction_numerical': 0.022268615170494086, 'mean_word_length': 4.969885773624092, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 32, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '377804', 'license': 'MIT', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1779, 'n_words': 642, 'path': 'Image_Classification.ipynb', 'repo_name': 'dhruvaop/Image-Classification-using-Machine-Learning', 'size': '5748'}