<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hassan Ijaz on Medium]]></title>
        <description><![CDATA[Stories by Hassan Ijaz on Medium]]></description>
        <link>https://medium.com/@hassan-ml?source=rss-46a55dc3b2cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mbEeYAtm1DSxfo-ikDY30A.jpeg</url>
            <title>Stories by Hassan Ijaz on Medium</title>
            <link>https://medium.com/@hassan-ml?source=rss-46a55dc3b2cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:04:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hassan-ml/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Grokking SageMaker Algorithms]]></title>
            <link>https://medium.com/@hassan-ml/grokking-sagemaker-algorithms-891ed142ee4c?source=rss-46a55dc3b2cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/891ed142ee4c</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[sagemaker]]></category>
            <dc:creator><![CDATA[Hassan Ijaz]]></dc:creator>
            <pubDate>Sat, 17 Aug 2024 15:32:20 GMT</pubDate>
            <atom:updated>2024-08-17T16:09:52.553Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y92wFKb47X7li946jBr8kg.png" /><figcaption>Decision Tree for which SageMaker Built-In Algorithm to use</figcaption></figure><h3>Algorithms</h3><h4>Supervised</h4><ol><li><strong>Linear Learner </strong>is a straightforward algorithm that tackles regression and classification tasks by identifying a linear relationship between input features and the output. It’s particularly useful for scenarios where this relationship is relatively simple, such as estimating house prices or used car values based on a few key features.</li><li><strong>XGBoost</strong> is a highly effective algorithm that excels in both regression and classification tasks. It works by combining the outputs of multiple simple decision trees to create a stronger, more accurate model through a process called boosting. This method is particularly useful for handling complex relationships in data. For instance, if you’re predicting customer churn or credit risk, XGBoost can help by refining its predictions through successive trees to minimize errors. It’s a great choice when your problem involves complex data patterns, like predicting house prices based on various features such as location and square footage.</li><li><strong>LightGBM</strong> is an advanced algorithm designed for both classification and regression tasks, similar to XGBoost. It builds decision trees to predict outcomes, but it’s optimized for speed and efficiency, making it especially useful when dealing with large datasets or when training time is a concern. LightGBM achieves this by using a technique called Gradient-based One-Side Sampling (GOSS) and Exclusive Feature Bundling (EFB), which allow it to process data faster while maintaining high accuracy. For example, if you’re working on a problem like predicting click-through rates for online ads across millions of users, LightGBM can handle the large volume of data quickly and efficiently.</li><li><strong>CatBoost</strong> is an algorithm well-suited for handling categorical features directly without needing extensive preprocessing like one-hot encoding. It builds an ensemble of decision trees, using a technique called gradient boosting to improve accuracy by reducing errors iteratively. CatBoost is particularly effective when working with datasets that have many categorical variables, such as customer demographics in a marketing campaign.</li><li><strong>KNN</strong> (K-Nearest Neighbors) is a simple, non-parametric algorithm. Instead of building a model, KNN works by comparing a new data point to a set of existing data points and assigning it to the category or predicting a value based on the majority or average of its nearest neighbors (the “K” closest points). KNN is particularly useful for problems where the decision boundary is irregular or where relationships are local rather than global. However, KNN is less suitable for large datasets because it requires comparing each new data point to all existing data points.</li><li><strong>AutoGluon-Tabular</strong> is an automated machine learning (AutoML) tool designed to handle tabular data for both classification and regression tasks. It simplifies the process of model selection, training, and tuning by automatically testing multiple models and combining them into an ensemble to achieve the best possible performance. It is particularly useful when you need to quickly build a high-performing model without deep expertise in machine learning.</li><li><strong>Blazing Text</strong> is a fast and efficient algorithm for natural language processing (NLP) tasks. It’s primarily used for tasks like text classification, word embedding, and sentence classification. BlazingText implements the Word2Vec algorithm for generating word embeddings and can handle large-scale text data efficiently by leveraging the power of GPU acceleration. For example, if you’re working on sentiment analysis of customer reviews, BlazingText can quickly classify text as positive, negative, or neutral based on the content of the reviews.</li><li><strong>Seq2Seq</strong> is a deep learning model designed for tasks where input sequences are mapped to output sequences. It’s commonly used in natural language processing (NLP) for tasks like machine translation, where a sentence in one language is translated into another. For example, translating an English sentence into French can be effectively handled by a Seq2Seq model, which learns the relationships between sequences of words.</li><li><strong>NTMs</strong> are advanced models that combine the learning capability of neural networks with the flexible memory system of a Turing machine. This allows them to learn algorithms and perform tasks that require both computation and memory, such as sorting, pattern recognition, or even simple forms of reasoning. They are useful for problems that require working with sequences and utilizing external memory to store and retrieve information.</li><li><strong>Image Classification </strong>involves assigning a label to an image based on its content. This is one of the most common tasks in computer vision. Convolutional Neural Networks (CNNs) are typically used for this purpose. For example, classifying images of animals into categories like ‘cat’, ‘dog’, or ‘bird’ is a typical image classification task.</li><li><strong>Object Detection</strong> goes beyond classification by identifying and localizing objects within an image. Algorithms like YOLO (You Only Look Once) or Faster R-CNN are commonly used. An example application is in autonomous vehicles, where the model detects and identifies various objects like pedestrians, cars, and traffic signs in real-time.</li><li><strong>Semantic Segmentation</strong> involves classifying each pixel in an image into a category. This is useful for tasks that require precise identification of different regions within an image, such as medical imaging (e.g., segmenting tumors in MRI scans) or autonomous driving (e.g., differentiating between road, sidewalk, and other objects).</li><li><strong>DeepAR</strong> is a probabilistic forecasting model designed for time series data. It uses RNNs (Recurrent Neural Networks) to predict future values of a time series, considering the entire history of data. This model is particularly useful for forecasting tasks in finance, retail, or any domain where understanding and predicting future trends based on historical data is crucial, such as predicting product demand or stock prices.</li></ol><h4>Unsupervised</h4><ol><li><strong>Random Cut Forests</strong> (RCF) is an anomaly detection algorithm that works well on streaming data. It identifies anomalies by constructing a forest of trees, where each tree represents the data as a sequence of random cuts. RCF is particularly useful for detecting unexpected patterns or outliers in real-time data, such as identifying fraudulent transactions or unusual server behavior in a network.</li><li><strong>K-Means</strong> is a clustering algorithm that partitions data into k distinct clusters based on feature similarity. It works by iteratively assigning each data point to the nearest cluster centroid and then updating the centroids. K-Means is widely used in market segmentation, image compression, and other scenarios where grouping similar data points is essential, like grouping customers with similar purchasing behaviors.</li><li><strong>PCA</strong> is a dimensionality reduction technique that transforms high-dimensional data into a lower-dimensional form while preserving as much variance as possible. It does this by identifying the principal components (directions of maximum variance) in the data. PCA is commonly used in exploratory data analysis, noise reduction, and data visualization, especially when dealing with large datasets with many features, like gene expression data in biology.</li><li><strong>Factorization Machines</strong> (FM) are a general-purpose model designed for recommendation systems, predictive analytics, and tasks involving sparse data. They can capture interactions between variables by factorizing them into latent factors. FMs are particularly effective in recommendation engines, such as predicting user preferences for products based on user-item interaction data, and can also be used for regression and classification tasks involving high-dimensional sparse data, like click-through rate prediction in advertising.</li></ol><h4>Specialized</h4><ol><li><strong>Object2Vec</strong> is a general-purpose embedding algorithm that learns dense, low-dimensional representations (embeddings) of objects. These objects could be anything from text, images, or even complex entities like products or users. The embeddings capture the similarities and relationships between objects, making Object2Vec particularly useful in recommendation systems, where you might want to recommend similar products or content to users based on these embeddings. It can also be applied in tasks like entity matching or clustering.</li><li><strong>RL Toolkit</strong> refers to a collection of tools and libraries designed to facilitate the development, training, and deployment of reinforcement learning (RL) models. These toolkits often include environments, algorithms, and utilities for RL tasks. They are particularly useful for developing and experimenting with RL models in various applications, such as gaming (e.g., training agents to play video games), robotics (e.g., teaching robots to perform tasks), and automated decision-making systems (e.g., optimizing business strategies). Popular RL toolkits include OpenAI Gym, Stable Baselines, and Ray RLlib.</li></ol><h3>Hyperparameters</h3><p>When deploying machine learning models in Amazon SageMaker, especially in preparation for AWS certification, understanding how to tweak hyperparameters is key to optimizing model performance. Each model type has its own set of hyperparameters that directly influence how the model learns from data, how it generalizes to new data, and how efficiently it trains.</p><h4>Tree-Based Models (XGBoost, LightGBM, CatBoost)</h4><p>Tree-based models like XGBoost, LightGBM, and CatBoost are commonly used for tasks such as classification and regression. Some critical hyperparameters to focus on include:</p><ul><li><strong>max_depth</strong>: Controls the maximum depth of each tree. Deeper trees can model more complex patterns but may lead to overfitting. Tuning this parameter helps balance model complexity and generalization.</li><li><strong>learning_rate (or </strong><strong>eta)</strong>: Determines the step size at each iteration of learning. A smaller learning rate can improve accuracy but requires more boosting rounds (n_estimators or num_round), increasing training time.</li><li><strong>n_estimators/</strong><strong>num_round/</strong><strong>num_iterations</strong>: Refers to the number of trees in the model. Increasing this can improve performance but also raises the risk of overfitting and extends training time.</li><li><strong>subsample</strong> and <strong>colsample_bytree</strong>: These parameters control the fraction of the dataset and features used to train each tree, respectively, helping to prevent overfitting by introducing randomness.</li><li><strong>gamma, </strong><strong>lambda, and </strong><strong>alpha</strong>: These are regularization parameters that control the model’s complexity, helping to prevent overfitting by penalizing overly complex models.</li><li><strong>min_child_weight</strong>: In XGBoost, this controls the minimum sum of instance weights needed in a child, which helps prevent the model from learning overly specific patterns.</li><li><strong>scale_pos_weight</strong>: Particularly useful in imbalanced classification problems, this parameter balances the positive and negative classes, which is essential for tasks like fraud detection.</li><li><strong>max_bin</strong> (LightGBM): Defines the maximum number of bins used for discretizing continuous features. A higher max_bin value can increase model accuracy but at the cost of training time.</li><li><strong>num_leaves</strong> (LightGBM): Determines the number of leaves in a tree, which directly controls the model’s capacity to capture complex patterns.</li></ul><h4>Neural Network-Based Models (Seq2Seq, DeepAR)</h4><p>Neural network models in SageMaker, such as Seq2Seq and DeepAR are powerful tools for tasks involving sequence data, time series forecasting, and complex tabular data. Key hyperparameters for these models include:</p><ul><li><strong>epochs</strong>: This defines how many times the entire dataset is passed through the network during training. More epochs can improve learning, but too many can lead to overfitting.</li><li><strong>batch_size</strong>: Determines how many samples are processed before the model&#39;s weights are updated. Smaller batch sizes give a more accurate gradient estimate but are computationally expensive.</li><li><strong>learning_rate</strong>: Just as in tree-based models, the learning rate in neural networks dictates the step size during optimization. It’s one of the most critical parameters to tune, affecting convergence speed and model accuracy.</li><li><strong>dropout</strong> and <strong>dropout_prob</strong>: Dropout is a regularization technique used to prevent overfitting by randomly setting a fraction of input units to zero during training.</li><li><strong>hidden_size</strong> and <strong>num_layers</strong>: These define the architecture of the neural network, with larger hidden sizes and more layers allowing the model to capture more complex relationships but also increasing the risk of overfitting.</li><li><strong>recurrent_dropout</strong> (specific to RNNs like in DeepAR and Seq2Seq): This type of dropout is applied within recurrent layers to prevent overfitting in models that process sequences.</li><li><strong>context_length</strong> and <strong>prediction_length</strong> (DeepAR): These parameters are crucial for time-series models, determining how much historical data is used for training and the length of the forecast horizon.</li></ul><h4>Specialized Models (BlazingText, KNN, PCA, Factorization Machines)</h4><p>SageMaker also supports specialized models that are tailored for specific tasks:</p><ul><li><strong>BlazingText</strong>: Used for natural language processing tasks, important hyperparameters include embedding_dim, which controls the size of the word vectors, and window_size, which defines the context window for word predictions.</li><li><strong>K-Nearest Neighbors (KNN)</strong>: Key hyperparameters for KNN include k, which determines the number of neighbors to consider for predictions, and distance_metric, which defines how similarity is measured (e.g., Euclidean distance).</li><li><strong>PCA (Principal Component Analysis)</strong>: For dimensionality reduction tasks, n_components is the most critical parameter, as it controls how many principal components are retained, influencing both the model’s performance and its interpretability.</li><li><strong>Factorization Machines</strong>: Useful for tasks like recommendation systems, hyperparameters such as factorization_rank control the model’s complexity, while l1_reg and l2_reg provide regularization to prevent overfitting.</li></ul><h4>Hyperparameter Optimization in SageMaker</h4><p>SageMaker’s built-in Hyperparameter Optimization (HPO) feature allows you to automate the process of tuning these parameters. By defining ranges for critical hyperparameters and using optimization strategies like Bayesian or Random Search, you can efficiently explore the hyperparameter space to identify the settings that yield the best model performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=891ed142ee4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Harnessing SageMaker DeepAR+ for Bitcoin Price Predictions]]></title>
            <link>https://medium.com/@hassan-ml/harnessing-sagemaker-deepar-for-bitcoin-price-predictions-230ec0e85da8?source=rss-46a55dc3b2cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/230ec0e85da8</guid>
            <category><![CDATA[forecast]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[sagemaker]]></category>
            <dc:creator><![CDATA[Hassan Ijaz]]></dc:creator>
            <pubDate>Sun, 14 Jul 2024 04:38:56 GMT</pubDate>
            <atom:updated>2024-07-14T04:45:34.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x-t3q2iSBEa9NH3mGUYXVg.png" /></figure><p>This tutorial is part 3 of Machine Learning on AWS Series. You can see the previous tutorial <a href="https://medium.com/@hassan-ml/receipt-vision-build-a-receipt-reader-using-aws-textract-and-comprehend-03e21197240c">Here</a></p><h3>Pre-Requisites</h3><ul><li>Basic python knowledge</li><li>An AWS account (If you don’t have one go to <a href="https://aws.amazon.com/">aws.amazon.com</a> and sign up for a free account)</li><li>Basic AWS knowledge (Optional but recommended)</li></ul><h3>Project Overview</h3><p>In this project we will use SageMaker to download historical bitcoin price data and then use DeepAR+ algorithm (built in to SageMaker) to make predictions about the bitcoin price.</p><ol><li>SageMaker: Amazon SageMaker is a fully managed service that provides developers and data scientists with the ability to build, train, and deploy machine learning models quickly. It offers a comprehensive set of tools and features to simplify each step of the ML workflow, including data preparation, model training, and model deployment.</li><li>DeepAR+: Amazon SageMaker DeepAR+ is a forecasting algorithm designed to handle large datasets of time series data. It uses deep learning to provide accurate forecasts by modeling complex patterns and trends across multiple time series, making it suitable for various applications such as finance, retail, and supply chain management.</li></ol><h3>Setup S3 Bucket</h3><p>We need to create an S3 bucket which will hold our training data and the trained model.</p><ul><li>Navigate to S3 in AWS Management Console.</li><li>Click on the “Create bucket” button.</li><li>Enter a unique bucket name like<strong> </strong>bitcoin-price-predictor-data (bucket names must be unique across all existing bucket names in Amazon S3).</li></ul><h3>Setup IAM Role</h3><p>Our SageMaker notebook instance will require permissions to access SageMaker and S3 resources.</p><ul><li>Navigate to IAM in AWS Management Console.</li><li>Create a new Role and attach S3FullAccess and SageMakerFullAccess policies to it. (In a production setting we would restrict these to just the bare minimum permission needed)</li></ul><h3>Create SageMaker Notebook</h3><ul><li>Navigate to SageMaker in AWS Management Console</li><li>Click “Create notebook instance”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*czrdg7X-ukjxoPfJBteaAA.png" /></figure><ul><li>Give the notebook a name like ‘BitcoinPricePredictorNotebook’</li><li>Assign it the IAM role previously created</li><li>Rest of the options can be left at default settings</li><li>Click ‘Create notebook instance’ again</li><li>Once the notebook is created you can “Start” it and click “Open Jupyter”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*m74S7SrlZUaNLMvxVOiFxw.png" /></figure><h3>Download Training Data</h3><ul><li>Create a new Code cell in Jupyter notebook and install the following dependencies</li></ul><pre>!pip install yfinance pandas boto3</pre><ul><li>We will be using the great <a href="https://pypi.org/project/yfinance/">yfinance</a> library which lets us download historical stock information.</li><li>Next we will download historical Bitcoin data from the start of the year 2015 till 2024–07–11</li></ul><pre>import yfinance as yf<br>import pandas as pd<br>import json<br><br># Download historical Bitcoin data<br>btc_data = yf.download(&#39;BTC-USD&#39;, start=&#39;2015-01-01&#39;, end=&#39;2024-07-11&#39;)<br>print(btc_data.head())  <br></pre><ul><li>The downloaded data should look something like this</li></ul><pre>                 Open        High         Low       Close   Adj Close  \<br>Date                                                                     <br>2015-01-01  320.434998  320.434998  314.002991  314.248993  314.248993   <br>2015-01-02  314.079010  315.838989  313.565002  315.032013  315.032013   <br>2015-01-03  314.846008  315.149994  281.082001  281.082001  281.082001   <br>2015-01-04  281.145996  287.230011  257.612000  264.195007  264.195007   <br>2015-01-05  265.084015  278.341003  265.084015  274.473999  274.473999   <br><br>              Volume  <br>Date                  <br>2015-01-01   8036550  <br>2015-01-02   7860650  <br>2015-01-03  33054400  <br>2015-01-04  55629100  <br>2015-01-05  43962800</pre><h3>Transform Training Data</h3><ul><li>Next we need to convert the data into a JSON format expected by DeepAR+ with the start date of the first reading and an array of closing costs since then (since we are interested in learning the patterns of and predicting future closing costs)</li></ul><pre># Prepare the data<br>btc_data.reset_index(inplace=True)<br>btc_data[&#39;Date&#39;] = btc_data[&#39;Date&#39;].apply(lambda x: x.strftime(&#39;%Y-%m-%d&#39;))<br><br># Convert the data to the required JSON format<br>data = {<br>    &quot;start&quot;: btc_data[&#39;Date&#39;].iloc[0],<br>    &quot;target&quot;: btc_data[&#39;Close&#39;].tolist()<br>}<br><br># Save to JSON file (each object should be in a new line)<br>with open(&#39;bitcoin_historical_data.json&#39;, &#39;w&#39;) as f:<br>    f.write(json.dumps(data) + &#39;\n&#39;)<br><br># Verify the first few lines of the JSON file<br>with open(&#39;bitcoin_historical_data.json&#39;, &#39;r&#39;) as f:<br>    for _ in range(5):<br>        print(f.readline())</pre><ul><li>The code above should result in a JSON that looks like this</li></ul><pre>{&quot;start&quot;: &quot;2015-01-01&quot;, &quot;target&quot;: [314.2489929199219, 315.0320129394531, 281.0820007324219, 264.19500732421875, 274.4739990234375, 286.1889953613281, 294.3370056152344, 283.3489990234375, 290.4079895019531, 274.7959899902344, 265.6600036621094, 267.7959899902344, 225.86099243164062, 178.10299682617188, 209.843994140625, 208.0970001220703, 199.25999450683594, 210.33900451660156, 214.86099243164062, 211.31500244140625, ...</pre><h3>Upload Train data to S3</h3><ul><li>The training data should be saved in S3 so it does not need to be downloaded for future training sessions (replace the S3 bucket name with the name of the bucket you created).</li></ul><pre>import boto3<br><br># Initialize S3 client<br>s3 = boto3.client(&#39;s3&#39;)<br><br># Define your bucket name and the prefix (folder path)<br>bucket_name = &#39;bitcoin-price-predictor-data&#39;<br>prefix = &#39;deepar-bitcoin-forecast/train&#39;<br><br># Upload the JSON data<br>s3.upload_file(&#39;bitcoin_historical_data.json&#39;, bucket_name, f&#39;{prefix}/bitcoin_historical_data.json&#39;)<br>print(f&#39;Data uploaded to s3://{bucket_name}/{prefix}/bitcoin_historical_data.json&#39;)</pre><ul><li>Navigate to your S3 bucket and verify that the JSON file has been uploaded succesfully</li></ul><h3>Train DeepAR+ Model</h3><ul><li>With all this setup we are now finally ready to train the model. Use the following code (but replace the bucket name), to grab the forecasting-deepar container image and train it on the data previously saved in S3. Training will approximately take 5–10 minutes.</li></ul><pre>import sagemaker<br>from sagemaker import get_execution_role<br>from sagemaker.image_uris import retrieve<br><br># Initialize SageMaker session and role<br>sagemaker_session = sagemaker.Session()<br>role = get_execution_role()<br><br># Define the S3 bucket and prefix<br>bucket = &#39;bitcoin-price-predictor-data&#39;<br>prefix = &#39;deepar-bitcoin-forecast&#39;<br><br># Get the DeepAR+ container image<br>image_uri = retrieve(&#39;forecasting-deepar&#39;, sagemaker_session.boto_region_name)<br><br># Define the DeepAR+ estimator<br>estimator = sagemaker.estimator.Estimator(<br>    image_uri=image_uri,<br>    role=role,<br>    instance_count=1,<br>    instance_type=&#39;ml.c4.xlarge&#39;,<br>    volume_size=5,<br>    max_run=3600,<br>    input_mode=&#39;File&#39;,<br>    output_path=f&#39;s3://{bucket}/{prefix}/output&#39;,<br>    sagemaker_session=sagemaker_session<br>)<br><br># Set hyperparameters<br>estimator.set_hyperparameters(<br>    time_freq=&#39;D&#39;,  # daily frequency<br>    epochs=50,<br>    prediction_length=30,  # predict next 30 days<br>    context_length=30,<br>    num_layers=2,<br>    num_cells=40,<br>    likelihood=&#39;gaussian&#39;,<br>    mini_batch_size=64,<br>    learning_rate=0.001,<br>    dropout_rate=0.05,<br>)<br><br># Define the input data format<br>train_data = sagemaker.inputs.TrainingInput(f&#39;s3://{bucket}/{prefix}/train/bitcoin_historical_data.json&#39;, content_type=&#39;json&#39;)<br><br># Fit the model<br>estimator.fit({&#39;train&#39;: train_data})</pre><ul><li>Verify the training logs did not throw an error and that a model file now exists in your S3 bucket</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*nUXITxG_V6p6E0tBjRLS7A.png" /></figure><h3>Deploy SageMaker Endpoint</h3><ul><li>To make inference against the trained model SageMaker gives us the ability to deploy it to an endpoint which can be sent inference requests to. Use the following code to do that (once again verify the S3 paths are correct)</li></ul><pre>import sagemaker<br>from sagemaker import get_execution_role<br><br># Initialize SageMaker session<br>sagemaker_session = sagemaker.Session()<br>role = get_execution_role()<br>model_key = &#39;deepar-bitcoin-forecast/output/forecasting-deepar-2024-07-13-04-06-28-228/output/model.tar.gz&#39;<br><br># Define the model artifacts<br>model_artifact = f&#39;s3://bitcoin-price-predictor-data/deepar-bitcoin-forecast/output/forecasting-deepar-2024-07-13-04-06-28-228/output/model.tar.gz&#39;<br><br># Create the model<br>from sagemaker.model import Model<br>deepar_model = Model(model_data=model_artifact,<br>                     role=role,<br>                     image_uri=sagemaker.image_uris.retrieve(&quot;forecasting-deepar&quot;, sagemaker_session.boto_region_name))<br><br># Deploy the model to an endpoint<br>predictor = deepar_model.deploy(initial_instance_count=1,<br>                                instance_type=&#39;ml.m5.large&#39;,<br>                                endpoint_name=&#39;deepar-bitcoin-endpoint&#39;)</pre><h3>Infer Future Bitcoin Prices</h3><ul><li>Finally we are ready to start predicting future bitcoin prices. In the following code we invoke the deployed enpoint to fetch prediction for the future 30 days</li></ul><pre>import boto3<br>import json<br>import pandas as pd<br>import matplotlib.pyplot as plt<br><br># Use the specified endpoint name<br>endpoint_name = &#39;deepar-bitcoin-endpoint&#39;<br><br># Create a SageMaker runtime client<br>runtime_client = boto3.client(&#39;runtime.sagemaker&#39;)<br><br># Prepare the input data for prediction (recent data)<br>recent_data = btc_data[&#39;Close&#39;].tolist()[-30:]<br><br># Ensure the index is in datetime format<br>btc_data.index = pd.to_datetime(btc_data.index)<br><br># Format the data as required by DeepAR<br>input_data = {<br>    &quot;instances&quot;: [<br>        {<br>            &quot;start&quot;: str(btc_data.index[-30].date()),  # Start date of the recent data<br>            &quot;target&quot;: recent_data  # Recent data<br>        }<br>    ]<br>}<br><br># Convert input data to JSON format<br>payload = json.dumps(input_data)<br><br># Make the prediction<br>response = runtime_client.invoke_endpoint(<br>    EndpointName=endpoint_name,<br>    ContentType=&#39;application/json&#39;,<br>    Body=payload<br>)<br><br># Parse the response<br>result = json.loads(response[&#39;Body&#39;].read().decode())<br><br># Print the entire response to understand its structure<br>print(&quot;Full response from the endpoint:&quot;)<br>print(json.dumps(result, indent=4))<br><br># Extract predictions based on the actual response structure<br>if &#39;predictions&#39; in result and len(result[&#39;predictions&#39;]) &gt; 0:<br>    if &#39;quantiles&#39; in result[&#39;predictions&#39;][0]:<br>        predictions = result[&#39;predictions&#39;][0][&#39;quantiles&#39;][&#39;0.5&#39;]  # Median prediction<br>    elif &#39;mean&#39; in result[&#39;predictions&#39;][0]:<br>        predictions = result[&#39;predictions&#39;][0][&#39;mean&#39;]<br>    else:<br>        predictions = []<br>else:<br>    predictions = []<br><br># Print the predictions<br>print(&quot;Predicted prices for the next 30 days:&quot;)<br>for i, prediction in enumerate(predictions):<br>    print(f&quot;Day {i+1}: {prediction}&quot;)<br><br># Convert predictions to a DataFrame for better visualization<br>if predictions:<br>    prediction_dates = pd.date_range(start=btc_data.index[-1] + pd.Timedelta(days=1), periods=len(predictions), freq=&#39;D&#39;)<br>    prediction_df = pd.DataFrame(data={&#39;Date&#39;: prediction_dates, &#39;Predicted Close&#39;: predictions})<br><br>    # Plot the historical and predicted prices<br>    plt.figure(figsize=(14, 7))<br>    plt.plot(btc_data[&#39;Close&#39;], label=&#39;Historical Prices&#39;)<br>    plt.plot(prediction_df[&#39;Date&#39;], prediction_df[&#39;Predicted Close&#39;], label=&#39;Predicted Prices&#39;, linestyle=&#39;--&#39;)<br>    plt.xlabel(&#39;Date&#39;)<br>    plt.ylabel(&#39;Price&#39;)<br>    plt.title(&#39;Bitcoin Price Prediction&#39;)<br>    plt.legend()<br>    plt.show()<br>else:<br>    print(&quot;No predictions were made. Please check the response structure.&quot;)</pre><ul><li>I got the following predictions for the next 30 days</li></ul><pre>Predicted prices for the next 30 days:<br>Day 1: 52501.43359375<br>Day 2: 54837.73046875<br>Day 3: 56198.109375<br>Day 4: 56217.41015625<br>Day 5: 55887.828125<br>Day 6: 56257.8046875<br>Day 7: 56906.1796875<br>Day 8: 56187.9453125<br>Day 9: 56758.234375<br>Day 10: 56161.765625<br>Day 11: 54751.31640625<br>Day 12: 54658.140625<br>Day 13: 54198.4140625<br>Day 14: 55457.953125<br>Day 15: 53713.32421875<br>Day 16: 54059.24609375<br>Day 17: 53099.015625<br>Day 18: 52433.3203125<br>Day 19: 51978.546875<br>Day 20: 50440.765625<br>Day 21: 50154.13671875<br>Day 22: 50394.96484375<br>Day 23: 49662.32421875<br>Day 24: 49925.25<br>Day 25: 48535.6015625<br>Day 26: 48221.78515625<br>Day 27: 50349.7734375<br>Day 28: 50746.55078125<br>Day 29: 50819.0<br>Day 30: 51720.92578125</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*X-uOG-FXWok1MvoXYKEiFA.png" /></figure><p><em>Title Background: “200520” by takawohttp://openprocessing.org/sketch/857874License CreativeCommons Attribution NonCommercial ShareAlikehttps://creativecommons.org/licenses/by-nc-sa/3.0</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=230ec0e85da8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Receipt Vision: Build a receipt reader using AWS Textract and Comprehend]]></title>
            <link>https://medium.com/@hassan-ml/receipt-vision-build-a-receipt-reader-using-aws-textract-and-comprehend-03e21197240c?source=rss-46a55dc3b2cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/03e21197240c</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ocr]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Hassan Ijaz]]></dc:creator>
            <pubDate>Fri, 28 Jun 2024 20:11:47 GMT</pubDate>
            <atom:updated>2024-07-14T04:47:09.808Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yOmV5tNIxctHhaNA7_PuPA.png" /></figure><p>This tutorial is part 2 of Machine Learning on AWS Series. You can see the previous tutorial <a href="https://medium.com/@hassan-ml/build-a-smart-photo-organizer-with-aws-rekognition-865300375f1f">Here</a></p><h3>Pre-Requisites</h3><ul><li>Basic python knowledge</li><li>An AWS account (If you don’t have one go to <a href="https://aws.amazon.com/">aws.amazon.com</a> and sign up for a free account)</li><li>Basic AWS knowledge (Optional but recommended)</li></ul><h3>Project Overview</h3><p>In this project we will use OCR (optical character recognition) abilities of AWS Textract to read user uploaded images of receipts and parse them using AWS Comprehend.</p><p>Other than S3 and Lambda which we have used previously, we will be using 2 new AWS services for this project:</p><ol><li><strong>AWS Textract</strong> is a machine learning service that automatically extracts text, handwriting, and data from scanned documents. It goes beyond simple optical character recognition (OCR) by also identifying the structure of documents, such as forms and tables, making it easy to convert paper-based data into digital formats for analysis and processing.</li><li><strong>AWS Comprehend</strong> is a natural language processing (NLP) service that uses machine learning to find insights and relationships in text. It can perform tasks such as entity recognition, sentiment analysis, key phrase extraction, language detection, and text categorization.</li></ol><h3>Set Up S3 Buckets</h3><p>We will create two S3 Buckets. One for uploading receipt images to and the second which will store the parsed receipt data from Textract.</p><ul><li>Sign in to the AWS Management Console.</li><li>In the AWS Management Console, go to the “Services” menu and select “S3” under “Storage”.</li></ul><p><strong>Create Receipt Uploads Bucket</strong></p><ul><li>Click on the “Create bucket” button.</li><li>Enter a unique bucket name like<strong> </strong>receipt-uploads (bucket names must be unique across all existing bucket names in Amazon S3).</li><li>Choose the same region where you will deploy your Lambda function.</li><li>Ensure all options are checked to block public access.</li><li>Click on the “Create bucket” button at the bottom.</li><li>Click on the “Permissions” tab and enter the following policy so that Textract can access images in this bucket.</li></ul><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Principal&quot;: {<br>                &quot;Service&quot;: &quot;textract.amazonaws.com&quot;<br>            },<br>            &quot;Action&quot;: &quot;s3:GetObject&quot;,<br>            &quot;Resource&quot;: &quot;arn:aws:s3:::receipt-uploads/*&quot;<br>        }<br>    ]<br>}</pre><p><strong>Create the Result Bucket</strong></p><ul><li>Follow the same steps to another bucket with a name like receipt-results. , except this bucket does not need a bucket policy attached.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*pFl3IMQO0HT6mXYvifexSw.png" /></figure><h3>Set Up IAM Role</h3><p>We need to set up an IAM Role so that our Lambda function can access S3, CloudWatch, Comprehend and Textract. This time we will be attaching a custom policy which is more restrictive.</p><p><strong>Create Custom Policy</strong></p><ul><li>Navigate to the IAM Console.</li><li>Click on “Policies” and the “Create Policy” button.</li><li>Switch to the JSON tab and paste the following policy in there (replace the bucket names to match your bucket names)</li></ul><pre>{<br> &quot;Version&quot;: &quot;2012-10-17&quot;,<br> &quot;Statement&quot;: [<br>  {<br>   &quot;Effect&quot;: &quot;Allow&quot;,<br>   &quot;Action&quot;: [<br>    &quot;comprehend:DetectEntities&quot;,<br>    &quot;comprehend:DetectSentiment&quot;,<br>    &quot;comprehend:DetectSyntax&quot;<br>   ],<br>   &quot;Resource&quot;: &quot;*&quot;<br>  },<br>  {<br>   &quot;Effect&quot;: &quot;Allow&quot;,<br>   &quot;Action&quot;: [<br>    &quot;textract:AnalyzeDocument&quot;,<br>    &quot;textract:DetectDocumentText&quot;<br>   ],<br>   &quot;Resource&quot;: &quot;*&quot;<br>  },<br>  {<br>   &quot;Effect&quot;: &quot;Allow&quot;,<br>   &quot;Action&quot;: [<br>    &quot;s3:GetObject&quot;,<br>    &quot;s3:PutObject&quot;<br>   ],<br>   &quot;Resource&quot;: [<br>    &quot;arn:aws:s3:::hijaz-receipt-uploads/*&quot;,<br>    &quot;arn:aws:s3:::hijaz-receipt-results/*&quot;<br>   ]<br>  },<br>  {<br>   &quot;Effect&quot;: &quot;Allow&quot;,<br>   &quot;Action&quot;: [<br>    &quot;logs:CreateLogGroup&quot;,<br>    &quot;logs:CreateLogStream&quot;,<br>    &quot;logs:PutLogEvents&quot;<br>   ],<br>   &quot;Resource&quot;: [<br>    &quot;arn:aws:logs:*:*:log-group:/aws/lambda/*&quot;<br>   ]<br>  }<br> ]<br>}</pre><ul><li>Give the policy and name like “LambdaTextractS3CloudWatchPolicy” and click “Create Policy”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cqMvcK64xb5IRZAP9FA9Qw.png" /></figure><p><strong>Create Role</strong></p><ul><li>In IAM Console, click on “Roles” and click “Create Role” button.</li><li>Choose “AWS Service” and select “Lambda” as the service.</li><li>Click “Next” to go to “Permissions”</li><li>Search for and attach the custom policy we just created “LambdaTextractS3CloudWatchPolicy”</li><li>Enter the role name, “lambda-receipt-processor-role”.</li><li>Review the permissions and click “Create role”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3o3H3D5iHi1RfyBCps5DFA.png" /></figure><h3>Set Up the Lambda Function</h3><ul><li>Go to the AWS Lambda console.</li><li>Click “Create function”.</li><li><strong>Function name</strong>: ReceiptProcessor</li><li><strong>Runtime</strong>: Python 3.x</li><li><strong>Permissions</strong>: Choose the IAM role created earlier (lambda-receipt-processor-role).</li><li>Click “Create function”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*x-uNd0PGciRvLXyHe4tt_Q.png" /></figure><ul><li>Enter the following code for your function (replace the results bucket name with your result bucket)</li></ul><pre>import json<br>import boto3<br>import logging<br>import urllib.parse<br>import uuid<br>from dateutil import parser<br>import re<br><br># Set up logging<br>logger = logging.getLogger()<br>logger.setLevel(logging.INFO)<br><br># Initialize AWS clients<br>s3_client = boto3.client(&#39;s3&#39;)<br>textract_client = boto3.client(&#39;textract&#39;)<br>comprehend_client = boto3.client(&#39;comprehend&#39;)<br><br>def lambda_handler(event, context):<br>    try:<br>        # Log the event received from S3<br>        logger.info(f&quot;Received event: {json.dumps(event)}&quot;)<br><br>        # Get the bucket name and document key from the event<br>        bucket = event[&#39;Records&#39;][0][&#39;s3&#39;][&#39;bucket&#39;][&#39;name&#39;]<br>        key = event[&#39;Records&#39;][0][&#39;s3&#39;][&#39;object&#39;][&#39;key&#39;]<br>        logger.info(f&quot;Bucket: {bucket}, Key: {key}&quot;)<br><br>        # URL-encode the object key to handle spaces and special characters<br>        encoded_key = urllib.parse.quote(key)<br>        logger.info(f&quot;Encoded Key: {encoded_key}&quot;)<br><br>        # Skip processing if the file is in the result bucket<br>        if bucket == &#39;your-result-bucket&#39;:<br>            logger.info(&quot;Skipping processing for output bucket.&quot;)<br>            return<br><br>        # Call Textract to analyze the document<br>        response = textract_client.analyze_document(<br>            Document={&#39;S3Object&#39;: {&#39;Bucket&#39;: bucket, &#39;Name&#39;: encoded_key}},<br>            FeatureTypes=[&#39;TABLES&#39;, &#39;FORMS&#39;]<br>        )<br><br>        # Extract the text in order<br>        text_blocks = extract_text_blocks(response)<br>        combined_text = &quot;\n&quot;.join(text_blocks)<br><br>        # Call Comprehend to analyze the text<br>        comprehend_response = comprehend_client.detect_entities(<br>            Text=combined_text,<br>            LanguageCode=&#39;en&#39;<br>        )<br><br>        # Extract the structured data<br>        extracted_data = extract_data(text_blocks, comprehend_response)<br>        logger.info(f&quot;Extracted Data: {extracted_data}&quot;)<br><br>        # Save the extracted data to another S3 bucket<br>        save_to_s3(extracted_data, key)<br><br>        return {<br>            &#39;statusCode&#39;: 200,<br>            &#39;body&#39;: json.dumps(&#39;Receipt processed successfully!&#39;)<br>        }<br><br>    except Exception as e:<br>        logger.error(f&quot;An error occurred: {str(e)}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 500,<br>            &#39;body&#39;: json.dumps(&#39;An internal error occurred.&#39;)<br>        }<br><br>def extract_text_blocks(response):<br>    text_blocks = []<br>    for block in response[&#39;Blocks&#39;]:<br>        if block[&#39;BlockType&#39;] == &#39;LINE&#39;:<br>            text_blocks.append(block[&#39;Text&#39;])<br>    return text_blocks<br><br>def extract_data(text_blocks, comprehend_response):<br>    data = {&#39;ReceiptId&#39;: str(uuid.uuid4())}<br>    entities = comprehend_response[&#39;Entities&#39;]<br>    vendor_name = None<br>    total_amount = None<br>    date = None<br><br>    for entity in entities:<br>        if entity[&#39;Type&#39;] == &#39;ORGANIZATION&#39; and vendor_name is None:<br>            vendor_name = entity[&#39;Text&#39;]<br>        elif entity[&#39;Type&#39;] == &#39;DATE&#39; and date is None:<br>            date = entity[&#39;Text&#39;]<br><br>    # Extract total amount more accurately<br>    for line in text_blocks:<br>        if &#39;total&#39; in line.lower():<br>            parts = line.split()<br>            for part in parts:<br>                if part.replace(&#39;.&#39;, &#39;&#39;, 1).isdigit():<br>                    total_amount = part<br>                    break<br><br>    data[&#39;Vendor&#39;] = vendor_name if vendor_name else &#39;N/A&#39;<br>    data[&#39;Total&#39;] = total_amount if total_amount else &#39;N/A&#39;<br>    data[&#39;Date&#39;] = extract_date(text_blocks)<br><br>    return data<br><br>def extract_date(text_blocks):<br>    date_patterns = [<br>        r&#39;\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b&#39;,  # Matches dates like MM/DD/YY or MM/DD/YYYY<br>    ]<br>    date_regex = re.compile(&#39;|&#39;.join(date_patterns))<br>    <br>    for line in text_blocks:<br>        match = date_regex.search(line)<br>        if match:<br>            date_str = match.group(0)<br>            try:<br>                date = parser.parse(date_str, fuzzy=False)<br>                logger.info(f&quot;Parsed date: {date.strftime(&#39;%Y-%m-%d&#39;)} from line: {line}&quot;)<br>                if date.year &gt; 1900 and date.year &lt; 2100:<br>                    return date.strftime(&#39;%Y-%m-%d&#39;)<br>            except ValueError:<br>                logger.info(f&quot;Failed to parse date from line: {line}&quot;)<br>                continue<br>    return &#39;N/A&#39;<br><br>def save_to_s3(data, original_key):<br>    result_bucket = &#39;hijaz-receipt-results&#39;  # Replace with your result bucket name<br>    result_key = &#39;results/&#39; + original_key.split(&#39;/&#39;)[-1].replace(&#39;.jpg&#39;, &#39;.json&#39;).replace(&#39;.png&#39;, &#39;.json&#39;)<br>    <br>    s3_client.put_object(<br>        Bucket=result_bucket,<br>        Key=result_key,<br>        Body=json.dumps(data),<br>        ContentType=&#39;application/json&#39;<br>    )</pre><ul><li>Set the timeout and memory settings according to your needs (e.g., timeout of 5 minutes and 512 MB of memory).</li><li>Click “Save”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6_7EVmD0SJw-whASmOLppQ.png" /></figure><h3>Add Trigger</h3><ul><li>In the “Configuration” tab, click on “Triggers”.</li><li>Click “Add trigger”.</li><li>From the “Trigger configuration” dropdown, select “S3”.</li></ul><p><strong>Set Up the Trigger</strong></p><ul><li><strong>Bucket</strong>: Select your receipt-uploads bucket.</li><li><strong>Event type</strong>: Choose All object create events.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*szSGSFVIsM71T8XPreyxxA.png" /></figure><h3>Add CloudWatch Log Group</h3><ul><li>Go to the CloudWatch Console.</li><li>In the navigation pane, click on “Logs”.</li><li>Create the log group/aws/lambda/ReceiptProcessor (if it does not already exist)</li></ul><h3>Test the Service</h3><ul><li>Get a jpg or png image of a receipt like the one below and upload it to the uploads S3 bucket you created</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*zo2pgbhRuanilmO5AuS89w.jpeg" /></figure><ul><li>Verify in CloudWatch logs that there are no errors.</li><li>In the results S3 bucket you should see the parsed JSON data from the receipt you uploaded</li></ul><pre>{<br>  &quot;ReceiptId&quot;: &quot;99dc4740-a2e4-423a-906b-8dc302092f3e&quot;, <br>  &quot;Vendor&quot;: &quot;Walmart&quot;, <br>  &quot;Total&quot;: &quot;23.19&quot;, <br>  &quot;Date&quot;: &quot;2017-11-13&quot;<br>}</pre><ul><li>The code can be made more complete by reading individual items, tax and adapting it for different receipt types.</li></ul><p>In the next part of this series we <a href="https://medium.com/@hassan-ml/harnessing-sagemaker-deepar-for-bitcoin-price-predictions-230ec0e85da8">predict Bitcoin prices using SageMaker DeepAR+</a></p><p><em>Title Background: “200520” by takawohttp://openprocessing.org/sketch/857874License CreativeCommons Attribution NonCommercial ShareAlikehttps://creativecommons.org/licenses/by-nc-sa/3.0</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=03e21197240c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a Smart Photo Organizer with AWS Rekognition]]></title>
            <link>https://medium.com/@hassan-ml/build-a-smart-photo-organizer-with-aws-rekognition-865300375f1f?source=rss-46a55dc3b2cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/865300375f1f</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[rekognition]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Hassan Ijaz]]></dc:creator>
            <pubDate>Wed, 26 Jun 2024 20:23:24 GMT</pubDate>
            <atom:updated>2024-07-14T04:48:39.445Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4yR7RVcIL6XvgdrTHXhUpg.png" /></figure><p>This is part 1 of machine learning on AWS series, which will cover various AWS ML services using a hands on project based approach.</p><h3>Pre-Requisites</h3><ul><li>Basic python knowledge</li><li>An AWS account (If you don’t have one go to <a href="https://aws.amazon.com/">aws.amazon.com</a> and sign up for a free account)</li><li>Basic AWS knowledge (Optional but recommended)</li></ul><h3>Project Overview</h3><p>We will be using 3 main AWS services for this project:</p><ol><li><strong>AWS Rekognition</strong> is a powerful image and video analysis service that uses deep learning to identify objects, people, text, scenes, and activities, and to detect any inappropriate content.</li><li><strong>Amazon S3</strong> (Simple Storage Service) is a scalable, high-speed, web-based cloud storage service designed to store and retrieve any amount of data from anywhere on the internet.</li><li><strong>AWS Lambda</strong> is a serverless compute service that automatically runs your code in response to events, scaling seamlessly from a few requests per day to thousands per second.</li></ol><p>In this project we will use the abilities of Rekognition service to auto tag photos uploaded in S3.</p><h3>Create Roles for Access</h3><p>Access for different services in AWS is managed through roles. Since our core logic will live inside the Lambda serverless function, we need to create a role which gives it access to Rekognition, S3 and CloudWatch.</p><ul><li>Log in to AWS management console and navigate to IAM Dashboard</li><li>Click on “Roles” in the left Nav</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*dwzPpgdHafYeGDCtKm-ajA.png" /></figure><ul><li>Click on “Create Role”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*lpw6tRYikC894To4L3wiHA.png" /></figure><ul><li>Under “Trusted entity type” choose “AWS Service”, under the “Use Case” dropdown select “Lambda” and click Next</li><li>Filter and select “S3FullAccess”, “<a href="https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/policies/details/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FCloudWatchLogsFullAccess">CloudWatchLogsFullAccess</a>” and “AmazonRekognitionFullAccess” permission policies</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*PPmo1MlSFKT9d8WnUG6R0w.png" /></figure><ul><li>Click Next and assign the role a name, “PhotoOrganizerRole”</li><li>Finally finish the flow by clicking “Create Role”</li><li>(The permissions applied for this tutorial are overly permissive but for an actual production setup we would only restrict permissions to the abilities actually needed by the Lambda function)</li></ul><h3>Create S3 Bucket</h3><ul><li>Navigate to S3 in AWS Management Console and click “Create Bucket”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*_cSYJJfYnOkz0UsVgyJudw.png" /></figure><ul><li>Give it a unique name such as “smart-photo-organizer-bucket” (S3 bucket names are globally unique) and click Create (leave the other options at default values)</li><li>After the bucket is created go to permissions tab and paste in the following permissions, these will allow Rekognition and Lambda to read our images from the bucket</li></ul><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Principal&quot;: {<br>                &quot;Service&quot;: &quot;rekognition.amazonaws.com&quot;<br>            },<br>            &quot;Action&quot;: &quot;s3:GetObject&quot;,<br>            &quot;Resource&quot;: &quot;arn:aws:s3:::smart-photo-organizer-bucket/*&quot;<br>        },<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Principal&quot;: {<br>                &quot;Service&quot;: &quot;lambda.amazonaws.com&quot;<br>            },<br>            &quot;Action&quot;: [<br>                &quot;s3:GetObject&quot;,<br>                &quot;s3:GetObjectVersion&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;arn:aws:s3:::smart-photo-organizer-bucket/*&quot;<br>        }<br>    ]<br>}</pre><h3>Create Lambda Function</h3><p>Next we will create the Lambda function.</p><ul><li>Navigate to AWS Lambda in the management console and click “Create Function”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ewH8zFQvXnBt2Ym1HlQI0w.png" /></figure><ul><li>Select “Author from scratch”.</li><li>Enter a function name, “SmartPhotoOrganizer”.</li><li>Choose “Python 3.x” as the runtime (or your preferred runtime).</li><li>Under “Permissions”, select “Use an existing role”.</li><li>Choose the role you created earlier that has full Rekognition and S3 access.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*pzUrdl30SCmNOdO10ybSqQ.png" /></figure><ul><li>Click create function and paste the following code in the IDE that shows up (the code is heavily commented to explain what each section is doing)</li></ul><pre>import json<br>import boto3<br>import logging<br>import urllib.parse<br><br># Set up logging to capture logs for debugging and monitoring<br>logger = logging.getLogger()<br>logger.setLevel(logging.INFO)<br><br># Initialize the S3 and Rekognition clients using boto3<br>s3_client = boto3.client(&#39;s3&#39;)<br>rekognition_client = boto3.client(&#39;rekognition&#39;)<br><br>def lambda_handler(event, context):<br>    try:<br>        # Log the received event for debugging purposes<br>        logger.info(f&quot;Received event: {json.dumps(event)}&quot;)<br><br>        # Extract the bucket name and object key from the event<br>        bucket = event[&#39;Records&#39;][0][&#39;s3&#39;][&#39;bucket&#39;][&#39;name&#39;]<br>        key = event[&#39;Records&#39;][0][&#39;s3&#39;][&#39;object&#39;][&#39;key&#39;]<br>        logger.info(f&quot;Bucket: {bucket}, Key: {key}&quot;)<br><br>        # URL-encode the object key to handle spaces and special characters<br>        encoded_key = urllib.parse.quote(key)<br>        logger.info(f&quot;Encoded Key: {encoded_key}&quot;)<br><br>        # Check if the file is a JPEG or PNG by examining the file extension<br>        if not (encoded_key.lower().endswith(&#39;.jpg&#39;) or encoded_key.lower().endswith(&#39;.jpeg&#39;) or encoded_key.lower().endswith(&#39;.png&#39;)):<br>            logger.error(f&quot;Invalid image format for file: {encoded_key}&quot;)<br>            return {<br>                &#39;statusCode&#39;: 400,<br>                &#39;body&#39;: json.dumps(&#39;Invalid image format. Only JPEG and PNG are supported.&#39;)<br>            }<br><br>        # Get the object tags from S3<br>        tagging_response = s3_client.get_object_tagging(Bucket=bucket, Key=encoded_key)<br>        existing_tags = {tag[&#39;Key&#39;]: tag[&#39;Value&#39;] for tag in tagging_response[&#39;TagSet&#39;]}<br>        logger.info(f&quot;Existing tags for {encoded_key}: {existing_tags}&quot;)<br><br>        # Check if the object has already been processed<br>        if &#39;Processed&#39; in existing_tags:<br>            logger.info(f&quot;File {encoded_key} is already processed and tagged. Skipping...&quot;)<br>            return {<br>                &#39;statusCode&#39;: 200,<br>                &#39;body&#39;: json.dumps(&#39;Image already processed.&#39;)<br>            }<br><br>        # Call Rekognition to detect labels in the image<br>        response = rekognition_client.detect_labels(<br>            Image={<br>                &#39;S3Object&#39;: {<br>                    &#39;Bucket&#39;: bucket,<br>                    &#39;Name&#39;: encoded_key<br>                }<br>            },<br>            MaxLabels=10  # Limit the number of labels to detect<br>        )<br><br>        # Extract the detected labels from the Rekognition response<br>        labels = response[&#39;Labels&#39;]<br>        label_names = [label[&#39;Name&#39;] for label in labels]<br>        logger.info(f&quot;Detected labels for {encoded_key}: {label_names}&quot;)<br><br>        # Create tags from the detected labels and add a &#39;Processed&#39; tag<br>        tags = [{&#39;Key&#39;: f&#39;Label{i + 1}&#39;, &#39;Value&#39;: label[&#39;Name&#39;]} for i, label in enumerate(labels)]<br>        tags.append({&#39;Key&#39;: &#39;Processed&#39;, &#39;Value&#39;: &#39;True&#39;})<br><br>        # Ensure the total number of tags does not exceed 10<br>        if len(tags) &gt; 10:<br>            tags = tags[:9]  # Limit to 9 tags to accommodate the &#39;Processed&#39; tag<br><br>        # Add the tags to the S3 object<br>        s3_client.put_object_tagging(<br>            Bucket=bucket,<br>            Key=encoded_key,<br>            Tagging={<br>                &#39;TagSet&#39;: tags<br>            }<br>        )<br><br>        # Return a success response<br>        return {<br>            &#39;statusCode&#39;: 200,<br>            &#39;body&#39;: json.dumps(&#39;Image processed and tagged successfully!&#39;)<br>        }<br><br>    # Handle exceptions for NoSuchKey, meaning the object was not found in S3<br>    except s3_client.exceptions.NoSuchKey as e:<br>        logger.error(f&quot;NoSuchKeyException: {str(e)}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 404,<br>            &#39;body&#39;: json.dumps(&#39;S3 object not found.&#39;)<br>        }<br><br>    # Handle exceptions for invalid image formats recognized by Rekognition<br>    except rekognition_client.exceptions.InvalidImageFormatException as e:<br>        logger.error(f&quot;InvalidImageFormatException: {str(e)}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 400,<br>            &#39;body&#39;: json.dumps(&#39;Invalid image format.&#39;)<br>        }<br><br>    # Handle exceptions for invalid S3 object metadata recognized by Rekognition<br>    except rekognition_client.exceptions.InvalidS3ObjectException as e:<br>        logger.error(f&quot;InvalidS3ObjectException: {str(e)}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 400,<br>            &#39;body&#39;: json.dumps(&#39;Invalid S3 object. Check object key, region, and/or access permissions.&#39;)<br>        }<br><br>    # Handle any other exceptions that may occur<br>    except Exception as e:<br>        logger.error(f&quot;An error occurred: {str(e)}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 500,<br>            &#39;body&#39;: json.dumps(&#39;An internal error occurred.&#39;)<br>        }</pre><ul><li>Click on “Deploy”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*wQk7Rz6qS_91rszTbXQ7ZA.png" /></figure><h3>Create Cloud Watch Log Group</h3><ul><li>Navigate to CloudWatch Logs in AWS Management Console</li><li>Click on “Logs” in the left-hand menu.</li></ul><p>Click on “Actions” and select “Create log group” ( /aws/lambda/SmartPhotoOrganizer does not already exist)</p><ul><li>Enter /aws/lambda/SmartPhotoOrganizer as the log group name.</li><li>Click “Create log group”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0F-0HeNWMsa4HhSBe9b3Jw.png" /></figure><h3>Connect S3 Bucket to Lambda</h3><ul><li>Navigate to Configuration tab for your Lambda function and click Triggers</li><li>Click “Add Trigger”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EK9ACPMJfx33LO9G0_42Wg.png" /></figure><ul><li>Select “S3” as the trigger.</li><li>Configure the trigger by selecting the bucket you created earlier.</li><li>Choose the event type “All object create events”.</li><li>Click “Add” to set up the trigger.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kjrie9AS7i45a50PywVBYg.png" /></figure><h3>Test the Photo Organizer</h3><ul><li>Navigate to your S3 bucket and upload a jpg or a png image file</li><li>You should be able to look at the logs under /aws/lambda/SmartPhotoOrganizer log group in Cloud Watch log group, ensure there are no errors and labels from Rekognition are being fetched</li><li>Verify in S3 tags have been applied to your image.</li><li>This is just one way photos can be organized, another option would be moving images to other appropriate buckets based on the labels returned, for example auto sorting cat and dog images in separate buckets.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nTaT8JDyAPhSfsP7qO7wjw.png" /></figure><p>In the next part of this series we <a href="https://medium.com/@hassan-ml/receipt-vision-build-a-receipt-reader-using-aws-textract-and-comprehend-03e21197240c">build a Receipt Reader using Textract and Comprehend</a></p><p><em>Title Background: “200520” by takawohttp://openprocessing.org/sketch/857874License CreativeCommons Attribution NonCommercial ShareAlikehttps://creativecommons.org/licenses/by-nc-sa/3.0</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=865300375f1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>