Skip to main content
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 1/21
Creating AI projects without being an
expert
Professional- quality AI on projects, the easy way!
Nasrudin Salim
Past
Software engineering, internal products -
Data Scientist @ EventPenguins | VIPQueue | LiquidityMagnate
EWallets, Food Delivery, events management and algorithmic trading
Now
Full-Time
Data Scientist @ Canopy.Cloud - Fintech, wealth aggregation platfor
m
Gig culture - Involved in other startups on free time
Blockchain ewallet, FuturesRobo Advisor startup, - Lead Consultant/
Architect
Biotech, DNA Medical blockchain project, and chatbots - medical ins
titution - Consultant.
Co-Founder @ Chatoyance, Fintech, Financial strategy optimization.
Seeded 6 Figure sum. Trading the market with deep learning applied
on risk management and strategy. Serve a News Sentiment analysis o
n articles API.
DL is applied on predicting a strategy's risk, rather than on the m
arket, reducing complexity of prediction.
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 2/21
Some requirements before we start
Basic python knowledge
Some data lying around
What are typical applications of AI
for typical local businesses?
Most businesses do not need chatbots, reinforcement learning/roboti
cs,
but what is practical?
Prediction -
What is going to happen?
Customer Churn Prediction
- Traffic Usage Data
- Lead Conversion Data
- Customer Churn Records
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 3/21
In [4]:
display(HTML(churn_data_know.head().to_html()))
Image Classification -
What is this?
Clothes and Fashion
Labelling images of clothes
Label: Tribal Print Bodycon
#
Customer_ID
Sessions Cell_ID District_ID Com
93296 2770289.0 14.0 0.0 4096.0 0.0
7022 41859563.0 53.0 0.0 4018.0 1.0
38831 2076101.0 16.0 0.0 4096.0 0.0
8161 42562614.0 3.0 0.0 4096.0 0.0
11827 1509741.0 16.0 0.0 4096.0 0.0
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 4/21
Label: Triangle Patterned Dolman Cardigan
The fashion dataset you might have lying
around
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 5/21
In [6]:
fashion_images.sample(7)
Natural Language Processing
What is being talked about?
Automated data entry
Sentiment Analysis
Text Classifiation
News Articles, good, bad? ( Sentiment)
Labelling Text Data ( Categorizing)
What you can do to bridge the gap without a team of
Out[6]:
image_n
255049 img/Surplice_Halter_Jumpsuit/img_00000002.jpg
213623 img/Cutout_Shoulder_Knit_Dress/img_00000027.j
2661 img/Striped_Floral_Blouse/img_00000095.jpg
124210 img/On_Fleek_Muscle_Tee/img_00000010.jpg
236314 img/Geo_Print_Strappy_Maxi_Dress/img_0000000
259588 img/Tribal_Print_Babydoll_Dress/img_00000001.jp
158598 img/Floral_Print_Drawstring_Shorts/img_0000000.
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 6/21
Data Scientists
Achieve close to or professional results
Google AutoML
H2O
DataRobot
Google AutoML
AutoML Vision
AutoML Natural Language
AutoML Engine
I do not work for Google!
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 7/21
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 8/21
In [7]:
fashion_images.head()
In [8]:
fashion_images.shape
I think I need to lower this down to train faster, as this is a demo
Generalize my labels and limit samples.
In [9]:
def clean_image_name_fashion(description:str) -> str:
item = description.split("/")[1]
item = item.replace("_"," ").replace("-"," ")
return item.split(" ")[-1]
fashion_images['label'] = fashion_images['image_name'].apply(clean_i
mage_name_fashion)
fashion_images.sort_values("label",inplace=True)
fashion_images.reset_index(inplace=True,drop=True)
Out[7]:
image_name
0 img/Sheer_Pleated-Front_Blouse/img_00000001.jpg
1 img/Sheer_Pleated-Front_Blouse/img_00000002.jpg
2 img/Sheer_Pleated-Front_Blouse/img_00000003.jpg
3 img/Sheer_Pleated-Front_Blouse/img_00000004.jpg
4 img/Sheer_Pleated-Front_Blouse/img_00000005.jpg
Out[8]:
(289222, 1)
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.h… 9/21
In [10]:
fashion_images.head()
Split your data to Train, Test and Validation
Train + Validation = 70%
Test = 30%
Google AutoML will handle Train, Test and Cross Validation for us
automatically.
Keep a test dataset to verify model accuracy after Training
Separate Labels from data
Take 200 of each type for Training
Take 60 of each for Testing
Remove types where we do not have 260 of
Out[10]:
image_name
0 img/Hooded_Cotton_Canvas_Anorak/img_00000039.jpg
1 img/Hooded_Cotton_Canvas_Anorak/img_00000013.jpg
2 img/Hooded_Cotton_Canvas_Anorak/img_00000014.jpg
3 img/Hooded_Cotton_Canvas_Anorak/img_00000015.jpg
4 img/Hooded_Cotton_Canvas_Anorak/img_00000016.jpg
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 10/21
In [11]:
fashion_training_data = []
fashion_testing_data = []
for label, have_enough_data in (fashion_images['label'].value_counts
()>=260).iteritems():
if not have_enough_data:
continue
# Get the subset of that label
subset = fashion_images.loc[fashion_images['label']==label]
# Takes 260 samples
random_samples = subset.sample(260, replace=False)
# Puts first 200 into fashion_training_data
fashion_training_data.append(random_samples.iloc[:200])
# Puts last 60 into fashion_testing_data
fashion_testing_data.append(random_samples.iloc[200:260])
fashion_training_data_df = pd.concat(fashion_training_data)
fashion_testing_data_df = pd.concat(fashion_testing_data)
In [12]:
fashion_training_data_df.shape[0]
In [13]:
fashion_testing_data_df.shape[0]
Out[12]:
6600
Out[13]:
1980
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 11/21
In [14]:
fashion_training_data_df['label'].value_counts()
Out[14]:
Parka 200
Blazer 200
Poncho 200
Jersey 200
Blouse 200
Flannel 200
Trunks 200
Hoodie 200
Leggings 200
Tee 200
Jumpsuit 200
Sweater 200
Henley 200
Culottes 200
Shorts 200
Coat 200
Dress 200
Down 200
Kimono 200
Sweatpants 200
Jeggings 200
Romper 200
Jacket 200
Bomber 200
Skirt 200
Tank 200
Sweatshorts 200
Cutoffs 200
Top 200
Joggers 200
Cardigan 200
Jeans 200
Chinos 200
Name: label, dtype: int64
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 12/21
Copy Files Into different subfolders
To send training data into Google AutoML Vision
In [15]:
import os
import shutil
In [ ]:
try:
os.mkdir("/mnt/HardDrive/dataset/fashion_img_samples")
except FileExistsError:
pass
for image_number, row in enumerate(fashion_training_data_df.itertupl
es()):
folder_name = "/mnt/HardDrive/dataset/fashion_img_samples/{}".fo
rmat(row.label)
if not os.path.exists(folder_name):
os.mkdir(folder_name)
source_file = os.path.abspath("/mnt/HardDrive/dataset/{}".format
(row.image_name))
destination_file = os.path.abspath("/mnt/HardDrive/dataset/fashi
on_img_samples/{}/img_{}.jpg".format(row.label, str(image_number)))
# Move the file here
shutil.copy(source_file, destination_file)
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 13/21
Zip Upload, Train
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 14/21
Verifying our model's accuracy with our test
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 15/21
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 16/21
Applying the model programmatically
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 17/21
In [104]:
import sys
from google.cloud import automl_v1beta1
from google.cloud.automl_v1beta1.proto import service_pb2
def get_prediction(file_fp, project_id= PROJECT_ID, model_id=MODEL_I
D):
with open(file_fp, 'rb') as ff:
content = ff.read()
prediction_client = automl_v1beta1.PredictionServiceClient()
name = 'projects/{}/locations/us-central1/models/{}'.format(
project_id, model_id)
payload = {'image': {'image_bytes': content }}
params = {}
request = prediction_client.predict(name, payload, params)
for prediction_payload in request.payload:
return prediction_payload.display_name # waits till req
uest is returned
In [105]:
prediction = get_prediction(fashion_testing_data_df['image_path'].il
oc[2])
print(prediction)
In [112]:
fashion_testing_data_df['label'] = fashion_testing_data_df['label'].
str.lower()
Predicting for each image in our test samples
dress
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 18/21
In [107]:
fashion_testing_data_df['predicted_label'] = fashion_testing_data_df
['image_path'].apply(get_prediction)
In [173]:
fashion_testing_data_df[['image_name','label','predicted_label','ima
ge_path']].head()
What is the accuracy?
In [174]:
from sklearn.metrics import accuracy_score
print("The accuracy score is {}".format(
accuracy_score(fashion_testing_data_df['label'], fashion_testing
_data_df['predicted_label'])))
How many labels were there?
Out[173]:
image_nam
87857 img/Rose_Print_Babydoll_Dress/img_00000008.jp
122060 img/Strapless_Paisley_Print_Dress/img_00000057
93330 img/Tribal_Print_Cutout_Back_Dress/img_0000002
122833 img/Ribbed_Knit_Striped_Dress/img_00000041.jpg
64691 img/Belted_Tribal_Print_Maxi_Dress/img_0000004
The accuracy score is 0.8702020202020202
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 19/21
In [181]:
print("Therlen(fashion_testing_data_df['label'].value_counts()))
print(fashion_testing_data_df['label'].value_counts())
33
henley 60
jeggings 60
jersey 60
jumpsuit 60
culottes 60
bomber 60
sweatshorts 60
kimono 60
coat 60
skirt 60
blazer 60
cutoffs 60
top 60
joggers 60
trunks 60
jeans 60
down 60
tee 60
tank 60
parka 60
sweater 60
romper 60
sweatpants 60
chinos 60
flannel 60
shorts 60
dress 60
hoodie 60
cardigan 60
blouse 60
leggings 60
jacket 60
poncho 60
Name: label, dtype: int64
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 20/21
Via API -
https://cloud.google.com/vision/automl/docs/tutorial
(https://cloud.google.com/vision/automl/docs/tutorial)
Import DataSet
Create Model
Train Model
Evaluate Model
Use Model for Prediction
03/09/2018 Professional AI Projects the Easy Way slides
file:///Users/soohian/Desktop/Aug%2031%20Data%20Science%20event/NuLabAug292018/Professional%20AI%20Projects%20the%20Easy%20Way.slides.… 21/21
Questions?