Don's Machine Learning https://donml.io/ My Machine Learning Journey Tue, 06 Dec 2022 03:48:35 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.4 https://donml.io/wp-content/uploads/2022/09/cropped-logo-32x32.png Don's Machine Learning https://donml.io/ 32 32 Serverless Deep Learning – AWS https://donml.io/2022/11/27/serverless-deep-learning-aws/ https://donml.io/2022/11/27/serverless-deep-learning-aws/#respond Sun, 27 Nov 2022 20:24:01 +0000 https://donml.io/?p=1234 What will be covered in this post: This week we will create a clothes classification service in the cloud to identify images we upload and send. We will use AWS Lambda to serve our model. We can upload an image and send the URL to Lambda, which returns our predicted class. We will use our […]

The post Serverless Deep Learning – AWS appeared first on Don's Machine Learning.

]]>
What will be covered in this post:

This week we will create a clothes classification service in the cloud to identify images we upload and send.

We will use AWS Lambda to serve our model. We can upload an image and send the URL to Lambda, which returns our predicted class.

We will use our previous clothes classification model, convert it to TensorFlow lite, package it up using Docker, and then upload the image to serve from AWS in the cloud.

AWS Lambda

Lambda allows us to run code on AWS without worrying about creating services or instances, hence the “serverless” name. First, we will create a new function inside Lambda and choose the “Author from scratch” option. Next, we will give our function a name, select the runtime language we want to use and the architecture, and then hit “Create function.”


AWS Lambda

Once the function is created, we will see the screen with information about our function. Since we chose Python as our runtime language, the function contains “lambda-function.py.” We will modify this for our demonstration. Finally, we will have our function print the event and return “PONG” when it is triggered.

Now we will click on the “Test” button and name our test event “test” and “save.” Now we have to click the “Deploy” button for our changes to take effect. When we click “Test” again, we will see the response.


Lambda Test Results

Using TensorFlow lite allows us to reduce our cost for storage and speeds up our initialization.

Tensorflow lite only focuses on inference or, in other words, prediction. Therefore, Tensorflow lite can’t be used to train a model.

The first step for this process will be to load a previously trained model to deploy. This Jupyter notebook shows the steps to convert a Keras model to tflite.


tensorflow-model

Preparing the Lambda code

First, we will extract the code from our Jupyter notebook, converting the model from Keras to tflite and creating a script.

We will use nbconvert for this extraction.

jupyter nbconvert --to script 'tensorflow-model.ipynb'

Now we have a file named ‘tensorflow-model.py.’ First, we will clean it up by removing everything except the last section of the notebook. Next, we will create a function to predict on a URL and return the zipped dictionary. Finally, we will also rename the file ‘lambda-function’ as this will be the function we create on AWS lambda.

import tflite_runtime.interpreter as tflite
from keras_image_helper import create_preprocessor

interpreter = tflite.Interpreter(model_path='clothing-model.tflite')
interpreter.allocate_tensors()

input_index = interpreter.get_input_details()[0]['index']
output_index = interpreter.get_output_details()[0]['index']

preprocessor = create_preprocessor('xception', target_size=(299, 299))

classes = [
    'dress',
    'hat',
    'longsleeve',
    'outwear',
    'pants',
    'shirt',
    'shoes',
    'shorts',
    'skirt',
    't-shirt'
]

#url = 'http://bit.ly/mlbookcamp-pants'

def predict(url):
    X = preprocessor.from_url(url)

    interpreter.set_tensor(input_index, X)
    interpreter.invoke()
    preds = interpreter.get_tensor(output_index)

    return dict(zip(classes, preds[0]))

def lambda_handler(event, context):
    url = event['url']
    result = predict(url)
    return result

Preparing a Docker image

First, we will create a file named Dockerfile, and we can use prepackaged images from AWS lambda.

We want to use the lambda/python base image, and under ‘Image tags,’ we can find the image we would like to use. We will use an image with Python 3.8.

In our Dockerfile, we will pull the python image and install the dependencies we used in our Jupyter Notebook that installed the keras-image-hander and the tflite_runtime package. After that, we will copy our tflite model and the lambda_function.py file to the current directory. Last we will run a command that invokes the lambda_handler in our lambda_function script.

FROM public.ecr.aws/lambda/python:3.8

RUN pip install keras-image-helper
RUN pip install --extra-index-url \
	https://google-coral.github.io/py-repo/ tflite-runtime

COPY clothing-model.tflite .
COPY lambda_function.py .

CMD[ "lambda_function.lambda_handler" ]

Now that our docker file is completed, we will build the docker container using the following:

docker build -t clothing-model .

The ‘.’ means we use the Dockerfile from the current directory. This will pull all the packages down into the container.

We can now test our Docker container locally using the following:

docker run -it --rm -p 8080:8080 clothing-model:latest

This will start the Docker image, and we can create a test script for our Docker image.

Inside the test.py file, we will import requests and then assign the function invocation to the ‘URL’ variable, the URL for our docker image locally. Next, we will assign the URL of the image of the pants to the ‘data’ variable. Finally, our results will contain the request in JSON format.

import requests

#url = 'http://localhost:8080/2015-03-31/functions/function/invocations'
url = 'https://000000000.execute-api.us-east-1.amazonaws.com/test/predict'

data = {'url': 'http://bit.ly/mlbookcamp-pants'}

result = requests.post(url, json=data).json()

print(result)

Now we run the test.py file, and we see our results.



Now we have built and tested our Docker image, so we will now create a lambda function and deploy our Docker image. We will deploy the Docker image to AWS ECR (Elastic Container Registry). Finally, we will create our repository with the command line. If you haven’t done so, you will have to run the ‘pip install awscli.’

We can run the following:

'aws ecr create-repository --repository-name clothing-tflite-images'

Which will create a repository on AWS. Now we will log in to that repository using the following:

aws ecr get-login --no-include-email

This will return the password, which I will not expose here. To use the password immediately without needing an extra step, we can run the following command:

$(aws ecr get-login --no-include-email)

Which will retrieve the password and then log you in with one step.

Using the URL that AWS assigned to us when creating the repository for our Docker image, we can use that information to execute a lengthy command to push our docker image to the repository.

ACCOUNT=090321278467
REGION=us-east-1
REGISTRY=clothing-tflite-images
PREFIX=${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com/${REGISTRY}

TAG=clothing-model-exception-v4-001
REMOTE_URI=${PREFIX}:${TAG}

To view the address:

echo ${REMOTE_URI}

Now we can tag and push our Docker image to the repository using the following:

docker tag clothing-model:latest ${REMOTE_URI}
docker push ${REMOTE_URI}

Now when we look at our AWS ECR repository page, we will see the “clothing-tflite-images” repository.


Repository page

We now have our docker container loaded onto the AWS platform. It is time to create a function on AWS that will link to the docker container. Let’s choose “Container image,” give it a name, in this case, “clothing-classification,” and the next step is choosing our Docker image from a “Container Image URI” using the “Browse Images” button. Finally, we are ready to create the function using the “Create function” button.


Creating a Container Image Lambda Function


Our “clothing-classification” function has been created, and now we can test it by clicking on the “Test” tab. We previously used the URL of the image of the pants, and we will also use that here, and now we can click the “Test” button. We get an error stating that the task timed out after roughly 3 seconds, which is the default time out and isn’t sufficient for our purposes, so we will change that using the “Configuration” tab. Then we will click the “Edit” button, increase the time to 30 seconds, add a little more memory, and click “Save.” Finally, we return to the “Test” tab and rerun our test.


Time out error

Configuration Tab


We have a successful test that shows our predictions. It took roughly 11 seconds to initialize, load, and run the first time we tested. The second time of testing, it only takes about 2 seconds.



API Gateway: exposing the lambda function

Our next step will be to expose our lambda function as a web service, and we will use an API gateway. We can search for “API gateway” to find this service. We will choose “REST API.”


AWS API Gateway

If this is your first time, an example API will be shown. We will select “New API,” name our API and then click “Create API.” Clicking the “Actions” drop-down button will allow us to choose “Create Resource.”


AWS REST API GATEWAY

Create Resource

In the “Resource Name” field, enter a name. I will use “predict” here to use the same convention we have been using in the zoomcamp course. Then click the “Create Resource” button.

We will have to add a method to our resource, and we can do that by clicking on the “Actions” button and selecting “Create Method,” and then choosing “POST” as our method type. Now we will select “Lambda Function” and enter “clothing-classification” as the Lambda Function we will use.


AWS API Gateway Post Setup Method

We will see a Permission window, and we can select “OK.” Then we will see a diagram of the Method-Request-Response process.

We have to set up the test using our diagram’s “Test” link. Then, in the request body, we will again enter our JSON URL information for the image of the pants. Then we can click the “Test” Lightning bolt button.



Adding our code to the Request Body

The results are the body of the response showing our predictions.


Results

It is time to deploy this API. Using the “Action” button again, we can select “Deploy API.” Using “New Stage,” we will use the test as our “Stage name” and then click on “Deploy.”


Deploy API

We are now presented with the URL where our function resides. The URL doesn’t contain our endpoint, “/predict”, so we will add that to the end of the URL. Then, we will copy and paste that function into our test.py file and hash out our local URL. When we run the test.py file, we will see our predictions.


import requests

#url = 'http://localhost:8080/2015-03-31/functions/function/invocations'
url = 'https://0000000000.execute-api.us-east-1.amazonaws.com/test/predict'

data = {'url': 'http://bit.ly/mlbookcamp-pants'}

result = requests.post(url, json=data).json()

print(result)

Now our lambda function is exposed as a web service.


The post Serverless Deep Learning – AWS appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/11/27/serverless-deep-learning-aws/feed/ 0
Neural networks and deep learning https://donml.io/2022/11/20/neural-networks-and-deep-learning/ https://donml.io/2022/11/20/neural-networks-and-deep-learning/#respond Sun, 20 Nov 2022 18:23:15 +0000 https://donml.io/?p=1176 8. Neural Networks and Deep Learning 8. Neural networks and deep learning¶ This week, we’ll learn about neural nets and build a model for classifying images of clothes 8.1 Fashion classification 8.1b Setting up the Environment on Saturn Cloud 8.2 TensorFlow and Keras 8.3 Pre-trained convolutional neural networks 8.4 Convolutional neural networks 8.5 Transfer learning […]

The post Neural networks and deep learning appeared first on Don's Machine Learning.

]]>
8. Neural Networks and Deep Learning

The post Neural networks and deep learning appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/11/20/neural-networks-and-deep-learning/feed/ 0
Deploying Machine Learning Models https://donml.io/2022/10/13/deploying-machine-learning-models/ https://donml.io/2022/10/13/deploying-machine-learning-models/#respond Thu, 13 Oct 2022 05:56:07 +0000 https://donml.io/?p=1168 You have created a well-tuned, performing model. Now what? People can’t access it while it sits on your PC in a Jupyter Notebook. It has to be deployed so that it can be utilized. That is what this week was all about, deploying machine learning models. Let’s first import all of the libraries we will […]

The post Deploying Machine Learning Models appeared first on Don's Machine Learning.

]]>
You have created a well-tuned, performing model. Now what? People can’t access it while it sits on your PC in a Jupyter Notebook. It has to be deployed so that it can be utilized. That is what this week was all about, deploying machine learning models.

Let’s first import all of the libraries we will need to create and train the model.

import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score

import urllib.request

Now we can import our data. Alexey Grigorev, from Data Talks Club has the data on his GitHub account.

url = 'https://raw.githubusercontent.com/alexeygrigorev/mlbookcamp-code/master/chapter-03-churn-prediction/WA_Fn-UseC_-Telco-Customer-Churn.csv'

filename = 'data-week-3.csv'

df = pd.read_csv(url)

Now that we have our data let’s clean up the data by replacing spaces with ‘_’. With the categorical features, we will do another replacement of spaces with ‘_’. During our exploration, we noticed that the ‘totalcharges’ feature contained ‘NAN’s, which we can’t have, so we will fill those with a ‘0’. The last action we will take is to turn our target variable, ‘churn’ into an integer using the ‘astype(int)‘ function

df.columns = df.columns.str.lower().str.replace(' ', '_')

categorical_columns = list(df.dtypes[df.dtypes == 'object'].index)

for c in categorical_columns:
    df[c] = df[c].str.lower().str.replace(' ', '_')

df.totalcharges = pd.to_numeric(df.totalcharges, errors='coerce')
df.totalcharges = df.totalcharges.fillna(0)

df.churn = (df.churn == 'yes').astype(int)

It is time to split our data into train, validation, and test datasets using ‘train_test_split‘. First, we split our data into 80% and 20% and assign each to df_full_train and df_test, respectively. Second, we split our df_train_full dataset into 67% and 33%, respectively. Since this was a training class, the random_state was used so that we were all working with the same splits. We have also reset the index on each dataset with the ‘.reset_index’ function just for appearances, it isn’t required. We then assign y_train and y_val the target variable, again ‘churn’, and then we delete the target variable from our train and val set.

df_train_full, df_test = train_test_split(df, test_size=0.2, random_state=11)

df_train_full = df_train_full.reset_index(drop=True)
df_test = df_test.reset_index(drop=True)

df_train, df_val = train_test_split(df_train_full, test_size=0.33, random_state=11)

df_train = df_train.reset_index(drop=True)
df_val = df_val.reset_index(drop=True)

y_train = df_train.churn.values
y_val = df_val.churn.values

del df_train['churn']
del df_val['churn']

We will assign our different types of features to two variables here. ‘Categorical’ will contain all features that are considered categorical, i.e. ‘gender’ will be assigned as male=0, female=1, ‘seniorcitizen’ will be no=0, yes=1. The other features are numerical so therefore will be added to the numerical variable.

categorical = ['gender', 'seniorcitizen', 'partner', 'dependents',
               'phoneservice', 'multiplelines', 'internetservice',
               'onlinesecurity', 'onlinebackup', 'deviceprotection',
               'techsupport', 'streamingtv', 'streamingmovies',
               'contract', 'paperlessbilling', 'paymentmethod']
numerical = ['tenure', 'monthlycharges', 'totalcharges']

Let’s create a function that will turn our categorical and numerical features into a dictionary and assign that dictionary to the variable ‘cat’. Next, we will assign the Dictionary Vectorizer, ‘DictVectorizer’ function, which will turn our dictionary into a vector, to ‘dv’. The ‘fit’ method

def train(df, y, C=1.0):
    cat = df[categorical + numerical].to_dict(orient='rows')
    
    dv = DictVectorizer(sparse=False)
    dv.fit(cat)

    X = dv.transform(cat)

    model = LogisticRegression(solver='liblinear', C=C)
    model.fit(X, y)

    return dv, model


def predict(df, dv, model):
    cat = df[categorical + numerical].to_dict(orient='rows')
    
    X = dv.transform(cat)

    y_pred = model.predict_proba(X)[:, 1]

    return y_pred

The post Deploying Machine Learning Models appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/10/13/deploying-machine-learning-models/feed/ 0
Evaluation Metrics for Classification https://donml.io/2022/09/27/evaluation-metrics-for-classification/ https://donml.io/2022/09/27/evaluation-metrics-for-classification/#respond Tue, 27 Sep 2022 22:05:24 +0000 https://donml.io/?p=1164 Evaluation Metrics for Classification 4. Evaluation Metrics for Classification¶ Last week we trained a model for churn. How do we know if it’s good? The fourth week of Machine Learning Zoomcamp is about different metrics to evaluate a binary classifier. These measures include accuracy, confusion table, precision, recall, ROC curves(TPR, FRP, random model, and ideal […]

The post Evaluation Metrics for Classification appeared first on Don's Machine Learning.

]]>
Evaluation Metrics for Classification

The post Evaluation Metrics for Classification appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/27/evaluation-metrics-for-classification/feed/ 0
Machine Learning for Classification https://donml.io/2022/09/22/machine-learning-for-classification/ https://donml.io/2022/09/22/machine-learning-for-classification/#respond Thu, 22 Sep 2022 05:37:51 +0000 https://donml.io/?p=1160 Machine Learning for Classification 3. Machine Learning for Classification¶ 3.1 Churn prediction project 3.2 Data preparation 3.3 Setting up the validation framework 3.4 EDA 3.5 Feature importance: Churn rate and risk ratio 3.6 Feature importance: Mutual information 3.7 Feature importance: Correlation 3.8 One-hot encoding 3.9 Logistic regression 3.10 Training logistic regression with Scikit-Learn 3.11 Model […]

The post Machine Learning for Classification appeared first on Don's Machine Learning.

]]>
Machine Learning for Classification

The post Machine Learning for Classification appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/22/machine-learning-for-classification/feed/ 0
Machine Learning for Regression https://donml.io/2022/09/16/machine-learning-for-regression/ https://donml.io/2022/09/16/machine-learning-for-regression/#respond Fri, 16 Sep 2022 20:39:55 +0000 https://donml.io/?p=1156 2022-09-14-2-Machine-Learning-for-Regression 2. Machine Learning for Regression¶ 2.1 Car price prediction project 2.2 Data preparation 2.3 Exploratory data analysis 2.4 Setting up the validation framework 2.5 Linear regression 2.6 Linear regression: vector form 2.7 Training linear regression: Normal equation 2.8 Baseline model for car price prediction project 2.9 Root mean squared error 2.10 Using RMSE on […]

The post Machine Learning for Regression appeared first on Don's Machine Learning.

]]>
2022-09-14-2-Machine-Learning-for-Regression

The post Machine Learning for Regression appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/16/machine-learning-for-regression/feed/ 0
Introduction to Numpy https://donml.io/2022/09/14/introduction-to-numpy/ https://donml.io/2022/09/14/introduction-to-numpy/#respond Wed, 14 Sep 2022 19:20:37 +0000 https://donml.io/?p=1153 Introduction to NumPy Machine Learning Zoomcamp¶ 1.7 Introduction to NumPy¶ Plan: Creating arrays Multi-dimentional array Randomly generated arrays Element-wise operations Comparison operations Logical operations Summarizing operations In [5]: import numpy as np In [6]: np Out[6]: <module 'numpy' from '/home/don/miniconda3/lib/python3.8/site-packages/numpy/__init__.py'> Creating arrays¶ In [8]: np.zeros(10) Out[8]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) In [11]: np.ones(10) […]

The post Introduction to Numpy appeared first on Don's Machine Learning.

]]>
Introduction to NumPy

The post Introduction to Numpy appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/14/introduction-to-numpy/feed/ 0
A Beginners Pandas Introduction https://donml.io/2022/09/14/a-beginners-pandas-introduction/ https://donml.io/2022/09/14/a-beginners-pandas-introduction/#respond Wed, 14 Sep 2022 17:29:40 +0000 https://donml.io/?p=1142 Introduction to Pandas Machine Learning Zoomcamp¶ 1.9 Introduction to Pandas¶ Plan: Data Frames Series Index Accessing elements Element-wise operations Filtering String operations Summarazing operations Missing values Grouping Getting the NumPy arrays In [1]: import numpy as np import pandas as pd Data Frames¶ In [11]: data = [ ['Nissan', 'Stanza', 1991, 138, 4, 'MANUAL', 'sedan', 2000], ['Hyundai', […]

The post A Beginners Pandas Introduction appeared first on Don's Machine Learning.

]]>
Introduction to Pandas

The post A Beginners Pandas Introduction appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/14/a-beginners-pandas-introduction/feed/ 0
My Journey So Far https://donml.io/2022/09/10/my-data-science-journey/ https://donml.io/2022/09/10/my-data-science-journey/#respond Sat, 10 Sep 2022 20:26:04 +0000 https://donml.io/?p=17     I have been on the quest to become proficient in data science and machine learning. I don’t have a background in computer science and have no real experience with programming. I have, at times, fiddled with programming but nothing more than the basics. I am determined to become proficient in data science. I […]

The post My Journey So Far appeared first on Don's Machine Learning.

]]>

   

I have been on the quest to become proficient in data science and machine learning. I don’t have a background in computer science and have no real experience with programming. I have, at times, fiddled with programming but nothing more than the basics.


I am determined to become proficient in data science. I believe I have a good cross-section of skills and the curiosity and desire to continue learning, to become a great data scientist. So my quest continues.


I have recently started a free program from DataTalks named Bookcamp at Data Talks Club. This four-month intensive program has graded homework, mid-term and capstone projects, and a certificate.


I will document my progress through the course and the new skills and challenges I face.

The post My Journey So Far appeared first on Don's Machine Learning.

]]>
https://donml.io/2022/09/10/my-data-science-journey/feed/ 0