<?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 Parv Gupta on Medium]]></title>
        <description><![CDATA[Stories by Parv Gupta on Medium]]></description>
        <link>https://medium.com/@karanguptaneo?source=rss-13f12261259a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2h3kaLLUU1sn-Ssv</url>
            <title>Stories by Parv Gupta on Medium</title>
            <link>https://medium.com/@karanguptaneo?source=rss-13f12261259a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 22:03:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karanguptaneo/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[Use Computer Vision for Product Detection on a Grocery Shelf]]></title>
            <link>https://levelup.gitconnected.com/product-detection-from-grocery-shelf-9db031e0ddc1?source=rss-13f12261259a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9db031e0ddc1</guid>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Parv Gupta]]></dc:creator>
            <pubDate>Thu, 22 Apr 2021 16:55:25 GMT</pubDate>
            <atom:updated>2021-04-28T14:39:24.247Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HhbOmuyucLDD1qFmWjVp9g.jpeg" /></figure><h3>Index</h3><ol><li>Introduction</li><li>Business Problem</li><li>Mapping to Machine learning problem</li><li>Data overview</li><li>EDA</li><li>Modeling</li><li>Comparison of models</li><li>final pipelining and deployment</li><li>Future Work</li><li>References</li><li>Profile</li></ol><h3><strong>1.) Introduction</strong></h3><p>The problem revolves around the detection of products that are placed in the grocery store and we are building a model which can detect the product and gives us a rectangular bounding box around the image in the form of 2-dimensional (x,y) coordinates</p><h3><strong>2.) Business Problem</strong></h3><p>As this detection problem could be later extended to many problems like product theft alarm and product counting problem, so it became important to come up with a solution for the problem</p><h3>3.) Mapping to Machine learning problem</h3><p>So after breaking the problem we can observe that we can solve this problem by applying the Computer Vision object detection technique by which we can get the coordinates for the products which are present in the images so we will be using the state of the art object detection techniques which are built on top of convolutional neural nets and deep neural net layers to build a robust and high-performance model.</p><h3>4.) Dataset overview:</h3><p>The data consist of the images of the shelf of the grocery store and annotations of the bounding boxes in the form of text files.</p><ul><li>Using the data from <a href="https://github.com/gulvarol/grocerydataset#shelfimages">https://github.com/gulvarol/grocerydataset#shelfimages</a></li><li>Data Form: JPG Image Files</li><li>features — Images of shelves from grocery stores</li><li>ground truth: annotations of image</li></ul><p>Below are the Image properties and naming:</p><pre>&quot;C&lt;c&gt;_P&lt;p&gt;_N&lt;n&gt;_S&lt;s&gt;_&lt;i&gt;.JPG&quot;<br>        where<br>            &lt;c&gt; := camera id (1: iPhone5S, 2: iPhone4, 3: Sony Cybershot, 4: Nikon Coolpix)<br>            &lt;p&gt; := planogram id<br>            &lt;n&gt; := the rank of the top shelf on the image according to the planogram<br>            &lt;s&gt; := number of shelves on the image<br>            &lt;i&gt; := copy number</pre><h3>5.) Exploratory Data Analysis</h3><p>We will be doing some exploratory data analysis to get some insights and also to understand the data better</p><p><strong>Number of images available</strong></p><p>Getting the Number of data points available in the data set</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ce3be3610223ebfbf03a7e65e627a42/href">https://medium.com/media/3ce3be3610223ebfbf03a7e65e627a42/href</a></iframe><pre>Number of Data Points: 354</pre><p><strong>Printing Random Image from data</strong></p><p>Let&#39;s print a random image from the dataset</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64371a1073f4fddce2b1a90f19bb8e36/href">https://medium.com/media/64371a1073f4fddce2b1a90f19bb8e36/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*-5h1Ynd0u7udvM-JICNORw.png" /></figure><p><strong>Drawing Bounding Box at a random image</strong></p><p>So let&#39;s use our annotations to visualize how an annotated image looks like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ab57f2ae613bbe7519a5d72021ca98b/href">https://medium.com/media/6ab57f2ae613bbe7519a5d72021ca98b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*hET7d8pQ5xif5Eu0i0ej4g.png" /></figure><h4>Average Number of Shelve in Image</h4><p>As the images are from the grocery store and each of the images contains products lying on the shelf, each of the images can have multiple shelves so let&#39;s see what is the average number of shelf that our dataset has</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9f177825d54d4cef4dbfa401a68c828/href">https://medium.com/media/a9f177825d54d4cef4dbfa401a68c828/href</a></iframe><pre>Average Number of Shelves in Image 3.2720848056537104</pre><h4>Histogram of Number of Shelve</h4><p>In the above code snippet, we have seen that the average number of shelves in an image is around 3 but for better visualization, we will plot the histogram of the number of shelves</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd336b270b3096190a785d2a2729a870/href">https://medium.com/media/fd336b270b3096190a785d2a2729a870/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*1jjWuazWZEu0wtRWBGysZw.png" /></figure><h4>Average Number of Products in Image</h4><p>Let&#39;s see what is the average number of products that an image can contain</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c6f18e18ddb64bedb9f4c3929d05dc0/href">https://medium.com/media/4c6f18e18ddb64bedb9f4c3929d05dc0/href</a></iframe><pre>Average Number of Products in Image 37.24293785310734</pre><h4><strong>Histogram of Number of Products</strong></h4><p>For better visualization, we will again plot the histogram of several products</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/053e5e2840aec5c3cef758e70c9ce3a1/href">https://medium.com/media/053e5e2840aec5c3cef758e70c9ce3a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*YNsxk6Tek1fS5punLvaoIQ.png" /></figure><h4>Number of images Captured using each Camera</h4><p>As the images are captured using different types of cameras so let&#39;s plot a bar graph that tells us what is the number of images clicked from each camera</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d63ebaa0ec2d02d2dc43489b6ce89fe/href">https://medium.com/media/7d63ebaa0ec2d02d2dc43489b6ce89fe/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*YLSCdNbyx1pXl_qsHa1egA.png" /></figure><h4>Observations:</h4><ul><li>The average Number of Shelve is around 3</li><li>less than 90% of the images have less than or equal to 4 shelves</li><li>Average Number of Products in Image 37.24293785310734</li><li>less than 99% of images have less than 80 products in images</li><li>iPhone 5 has the largest number of image captures of any device</li></ul><blockquote>So from here We will start building our models ,we will try different models and select the model which will suite the data better.</blockquote><h3>Preparing Inputs and models</h3><p><strong>Cloning the Github Models</strong></p><p>In the below command snippet we will change the working directory to inside our workspace folder which is ‘tf2’ in our case. Then we will just clone the TensorFlow models. Then we can change the working directory to object_detection which is inside “models/research/”</p><pre>%cd /content/drive/MyDrive/tf2/<br>!git clone <a href="https://github.com/tensorflow/models/">https://github.com/tensorflow/models/</a><br>%cd /content/drive/My Drive/tf2/models/research/object_detection</pre><h4>Creating Input Pipeline</h4><ul><li>First We will create a dictionary where keys will be image and values will be annotations</li><li>Create train_labels.csv and test_labels.csv containing the Below columns:<br>filename’ , ‘width’ , ‘class’ , ‘height’ , ‘xmin’ , ‘xmax’ , ‘ymin’ , ‘ymax’</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edeea5f3a3b629b38bea8442b8f42de8/href">https://medium.com/media/edeea5f3a3b629b38bea8442b8f42de8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67cb637b656fa4eefa98dcdb68afedd3/href">https://medium.com/media/67cb637b656fa4eefa98dcdb68afedd3/href</a></iframe><h4>Creating the tensor flow records file from images and annotations</h4><ul><li>We will create a train. record and test. record below</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bf2e066a94b7b79200d52846af8fe8f/href">https://medium.com/media/3bf2e066a94b7b79200d52846af8fe8f/href</a></iframe><h4>Creating the labelmap</h4><p>In the below code snippet we are just writing a file labelmap.pbtxt which contains labels info to the training folder</p><pre>%%writefile models/research/object_detection/training/labelmap.pbtxt<br>item {<br> id: 1<br> name: ‘Product’<br>}</pre><h3>6.) Modeling</h3><h3>Modeling with efficient Det</h3><h4>Downloading the efficient_det model from Tensorflow Zoo</h4><p>There are lots of available pre-trained model in the TensorFlow zoo and we will be picking some models and fine-tune them on our dataset</p><pre>%cd /content/drive/MyDrive/tf2/model/research/object_detection/</pre><pre>!wget <a href="http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz">http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz</a></pre><pre>!tar -xvzf efficientdet_d0_coco17_tpu-32.tar.gz</pre><pre>!rm -rf efficientdet_d0_coco17_tpu-32.tar.gz</pre><h4>Saving The Config file to Training/effdet.config</h4><p>In this step we will be saving the config file for the efficient det model in the training folder and below is the command to create and save the config file</p><pre>%%writefile training/effdet.config<br>model {<br> ssd {<br> num_classes: 1<br> image_resizer {<br> keep_aspect_ratio_resizer {<br> min_dimension: 512<br> max_dimension: 512<br> pad_to_max_dimension: true<br> }<br> }<br> feature_extractor {<br> type: “ssd_efficientnet-b0_bifpn_keras”<br> conv_hyperparams {<br> regularizer {<br> l2_regularizer {<br> weight: 3.9999998989515007e-05<br> }<br> }<br> initializer {<br> truncated_normal_initializer {<br> mean: 0.0<br> stddev: 0.029999999329447746<br> }<br> }<br> activation: SWISH<br> batch_norm {<br> decay: 0.9900000095367432<br> scale: true<br> epsilon: 0.0010000000474974513<br> }<br> force_use_bias: true<br> }<br> bifpn {<br> min_level: 3<br> max_level: 7<br> num_iterations: 3<br> num_filters: 64<br> }<br> }<br> box_coder {<br> faster_rcnn_box_coder {<br> y_scale: 1.0<br> x_scale: 1.0<br> height_scale: 1.0<br> width_scale: 1.0<br> }<br> }<br> matcher {<br> argmax_matcher {<br> matched_threshold: 0.5<br> unmatched_threshold: 0.5<br> ignore_thresholds: false<br> negatives_lower_than_unmatched: true<br> force_match_for_each_row: true<br> use_matmul_gather: true<br> }<br> }<br> similarity_calculator {<br> iou_similarity {<br> }<br> }<br> box_predictor {<br> weight_shared_convolutional_box_predictor {<br> conv_hyperparams {<br> regularizer {<br> l2_regularizer {<br> weight: 3.9999998989515007e-05<br> }<br> }<br> initializer {<br> random_normal_initializer {<br> mean: 0.0<br> stddev: 0.009999999776482582<br> }<br> }<br> activation: SWISH<br> batch_norm {<br> decay: 0.9900000095367432<br> scale: true<br> epsilon: 0.0010000000474974513<br> }<br> force_use_bias: true<br> }<br> depth: 64<br> num_layers_before_predictor: 3<br> kernel_size: 3<br> class_prediction_bias_init: -4.599999904632568<br> use_depthwise: true<br> }<br> }<br> anchor_generator {<br> multiscale_anchor_generator {<br> min_level: 3<br> max_level: 7<br> anchor_scale: 4.0<br> aspect_ratios: 1.0<br> aspect_ratios: 2.0<br> aspect_ratios: 0.5<br> scales_per_octave: 3<br> }<br> }<br> post_processing {<br> batch_non_max_suppression {<br> score_threshold: 9.99999993922529e-09<br> iou_threshold: 0.5<br> max_detections_per_class: 100<br> max_total_detections: 100<br> }<br> score_converter: SIGMOID<br> }<br> normalize_loss_by_num_matches: true<br> loss {<br> localization_loss {<br> weighted_smooth_l1 {<br> }<br> }<br> classification_loss {<br> weighted_sigmoid_focal {<br> gamma: 1.5<br> alpha: 0.25<br> }<br> }<br> classification_weight: 1.0<br> localization_weight: 1.0<br> }<br> encode_background_as_zeros: true<br> normalize_loc_loss_by_codesize: true<br> inplace_batchnorm_update: true<br> freeze_batchnorm: false<br> add_background_class: false<br> }<br>}<br>train_config {<br> batch_size: 12<br> data_augmentation_options {<br> random_horizontal_flip {<br> }<br> }<br> data_augmentation_options {<br> random_scale_crop_and_pad_to_square {<br> output_size: 512<br> scale_min: 0.10000000149011612<br> scale_max: 2.0<br> } <br> }<br> <br> sync_replicas: true<br> optimizer {<br> momentum_optimizer {<br> learning_rate {<br> cosine_decay_learning_rate {<br> learning_rate_base: 0.07999999821186066<br> total_steps: 300000<br> warmup_learning_rate: 0.0010000000474974513<br> warmup_steps: 2500<br> }<br> }<br> momentum_optimizer_value: 0.8999999761581421<br> }<br> use_moving_average: false<br> }<br> fine_tune_checkpoint: “efficientdet_d0_coco17_tpu-32/checkpoint/ckpt-0”<br> num_steps: 300000<br> startup_delay_steps: 0.0<br> replicas_to_aggregate: 8<br> max_number_of_boxes: 100<br> unpad_groundtruth_tensors: false<br> fine_tune_checkpoint_type: “detection”<br> use_bfloat16: true<br> fine_tune_checkpoint_version: V2<br>}<br>train_input_reader: {<br> label_map_path: “training/labelmap.pbtxt”<br> tf_record_input_reader {<br> input_path: “train.record”<br> }<br>}</pre><pre>eval_config: {<br> metrics_set: “coco_detection_metrics”<br> use_moving_averages: false<br> batch_size: 1;<br>}</pre><pre>eval_input_reader: {<br> label_map_path: “training/labelmap.pbtxt”<br> shuffle: false<br> num_epochs: 1<br> tf_record_input_reader {<br> input_path: “test.record”<br> }<br>}</pre><h4>Training The Model</h4><p>In the Below command snippet, we are training our effdet model</p><pre>#Changing directory to inside our object detecton model<br>%cd /content/drive/My Drive/tf2/models/research/object_detection</pre><pre>#Configuring the protos<br>!protoc object_detection/protos/*.proto --python_out=.</pre><pre># training the model<br>!python model_main_tf2.py --pipeline_config_path=training/effdet.config --model_dir=training/effdet/ --alsologtostderr --num_train_steps=2000</pre><h4>Evaluation of Efficient Det</h4><p>As we have training our effdet model so we will evaulauate the model and get the result</p><pre>!python model_main_tf2.py — model_dir=training/effdet/ — pipeline_config_path=training/effdet.config — checkpoint_dir=training/effdet/</pre><h4>mAP and mAR scores after training</h4><p>Below are the scores that we got after evaluation of our model</p><pre>Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.641<br> Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.959<br> Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.834<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.641<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.020<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.200<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.699<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.699</pre><h4>Visualization loss from Tensorboard</h4><p>lets visualize the loss of our effdet model</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_kQ_qhQdlitpEncHbVTQjw.png" /></figure><h3>Modeling with Center net</h3><h4>Downloading the Center net model from Tensorflow Zoo</h4><p>Downloading the Centernet model from Tensorflow Zoo</p><pre>%cd /content/drive/MyDrive/tf2/model/research/object_detection/</pre><pre>!wget <a href="http://download.tensorflow.org/models/object_detection/tf2/20200713/centernet_hg104_512x512_coco17_tpu-8.tar.gz">http://download.tensorflow.org/models/object_detection/tf2/20200713/centernet_hg104_512x512_coco17_tpu-8.tar.gz</a></pre><pre>!tar -xvzf centernet_hg104_512x512_coco17_tpu-8.tar.gz</pre><pre>!rm -rf centernet_hg104_512x512_coco17_tpu-8.tar.gz</pre><h4>Saving The Below Config file to Training/centernet</h4><p>In this step, we will be saving the config file for the center net model in the training folder, and below is the command to create and save the config file</p><pre>%%writefile training/effdet.config</pre><pre>%%writefile training/centernet.config<br>model {<br> center_net {<br> num_classes: 1<br> feature_extractor {<br> type: “hourglass_104”<br> channel_means: 104.01361846923828<br> channel_means: 114.03422546386719<br> channel_means: 119.91659545898438<br> channel_stds: 73.60276794433594<br> channel_stds: 69.89082336425781<br> channel_stds: 70.91507720947266<br> bgr_ordering: true<br> }<br> image_resizer {<br> keep_aspect_ratio_resizer {<br> min_dimension: 512<br> max_dimension: 512<br> pad_to_max_dimension: true<br> }<br> }<br> object_detection_task {<br> task_loss_weight: 1.0<br> offset_loss_weight: 1.0<br> scale_loss_weight: 0.10000000149011612<br> localization_loss {<br> l1_localization_loss {<br> }<br> }<br> }<br> object_center_params {<br> object_center_loss_weight: 1.0<br> classification_loss {<br> penalty_reduced_logistic_focal_loss {<br> alpha: 2.0<br> beta: 4.0<br> }<br> }<br> min_box_overlap_iou: 0.699999988079071<br> max_box_predictions: 100<br> }<br> }<br>}<br>train_config {<br> batch_size: 12<br> data_augmentation_options {<br> random_horizontal_flip {<br> }<br> }<br> data_augmentation_options {<br> random_crop_image {<br> min_aspect_ratio: 0.5<br> max_aspect_ratio: 1.7000000476837158<br> random_coef: 0.25<br> }<br> }<br> data_augmentation_options {<br> random_adjust_hue {<br> }<br> }<br> data_augmentation_options {<br> random_adjust_contrast {<br> }<br> }<br> data_augmentation_options {<br> random_adjust_saturation {<br> }<br> }<br> data_augmentation_options {<br> random_adjust_brightness {<br> }<br> }<br> data_augmentation_options {<br> random_absolute_pad_image {<br> max_height_padding: 200<br> max_width_padding: 200<br> pad_color: 0.0<br> pad_color: 0.0<br> pad_color: 0.0<br> }<br> }<br> optimizer {<br> adam_optimizer {<br> learning_rate {<br> manual_step_learning_rate {<br> initial_learning_rate: 0.0010000000474974513<br> schedule {<br> step: 90000<br> learning_rate: 9.999999747378752e-05<br> }<br> schedule {<br> step: 120000<br> learning_rate: 9.999999747378752e-06<br> }<br> }<br> }<br> epsilon: 1.0000000116860974e-07<br> }<br> use_moving_average: false<br> }<br> fine_tune_checkpoint: “centernet_hg104_512x512_coco17_tpu-8/checkpoint/ckpt-0”<br> num_steps: 140000<br> max_number_of_boxes: 100<br> unpad_groundtruth_tensors: false<br> fine_tune_checkpoint_type: “fine_tune”<br> fine_tune_checkpoint_version: V2<br>}<br>train_input_reader {<br> label_map_path: “training/labelmap.pbtxt”<br> tf_record_input_reader {<br> input_path: “train.record”<br> }<br>}<br>eval_config {<br> metrics_set: “coco_detection_metrics”<br> use_moving_averages: false<br> batch_size: 1<br>}<br>eval_input_reader {<br> label_map_path: “training/labelmap.pbtxt”<br> shuffle: false<br> num_epochs: 1<br> tf_record_input_reader {<br> input_path: “test.record”<br> }<br>}</pre><h4>Training The Model</h4><p>In the Below command snippet, we are training our model</p><pre>#changing directory to inside object_detection folder<br>%cd /content/drive/My Drive/tf2/model/research/object_detection</pre><pre>#configuring the protos<br>!protoc object_detection/protos/*.proto — python_out=.</pre><pre># Training the model<br>!python model_main_tf2.py — pipeline_config_path=training/centernet.config — model_dir=training/centernet/ — alsologtostderr — num_train_steps=2000</pre><h4>Evaluating the Model</h4><p>With the below command snippet we will evaluate the centernet model</p><pre>!python model_main_tf2.py — model_dir=training/centernet — pipeline_config_path=training/centernet.config — checkpoint_dir=training/centernet/</pre><h4>mAP and mAR Scores after training</h4><p>Below are the scores of centernet model on our dataset</p><pre>Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.709<br> Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.957<br> Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.930<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000<br> Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.709<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.022<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.217<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.762<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000<br> Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.762</pre><h4>Tensorboard</h4><p>visualization of loss using Tensorboard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ySFWLAT9luCshK_nY-BcQw.png" /></figure><h3>Downloading the model from Tensorflow Zoo</h3><p>With the below command snippet we can download a Faster R-CNN model from the TensorFlow zoo</p><pre><strong>%</strong>cd <strong>/</strong>content<strong>/</strong>drive<strong>/</strong>MyDrive<strong>/</strong>tf2<strong>/</strong>model<strong>/</strong>research<strong>/</strong>object_detection<strong>/</strong></pre><pre><strong>!</strong>wget <a href="http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8.tar.gz">http:<strong>//</strong>download.tensorflow.org<strong>/</strong>models<strong>/</strong>object_detection<strong>/</strong>tf2<strong>/</strong>20200711<strong>/</strong>faster_rcnn_resnet101_v1_800x1333_coco17_gpu<strong>-</strong>8.tar.gz</a></pre><pre><strong>!</strong>tar <strong>-</strong>xvzf faster_rcnn_resnet101_v1_800x1333_coco17_gpu<strong>-</strong>8.tar.gz<strong>!</strong>rm <strong>-</strong>rf faster_rcnn_resnet101_v1_800x1333_coco17_gpu<strong>-</strong>8.tar.gz</pre><h3>Saving The Below Config file in Training/faster_rcnn.config</h3><p>Writing the faster_rcnn config file to the disk</p><pre>%%writefile training/faster_rcnn.config<br># Faster R-CNN with Resnet<br># Sync-trained on COCO (8 GPUs),<br># Initialized from Imagenet classification checkpoint<br># TF2-Compatible, *Not* TPU-Compatible</pre><pre>model {<br>  faster_rcnn {<br>    num_classes: 1<br>    image_resizer {<br>      keep_aspect_ratio_resizer {<br>        min_dimension: 800<br>        max_dimension: 1333<br>        pad_to_max_dimension: true<br>      }<br>    }<br>    feature_extractor {<br>      type: &#39;faster_rcnn_resnet101_keras&#39;<br>    }<br>    first_stage_anchor_generator {<br>      grid_anchor_generator {<br>        scales: [0.25, 0.5, 1.0, 2.0]<br>        aspect_ratios: [0.5, 1.0, 2.0]<br>        height_stride: 16<br>        width_stride: 16<br>      }<br>    }<br>    first_stage_box_predictor_conv_hyperparams {<br>      op: CONV<br>      regularizer {<br>        l2_regularizer {<br>          weight: 0.0<br>        }<br>      }<br>      initializer {<br>        truncated_normal_initializer {<br>          stddev: 0.01<br>        }<br>      }<br>    }<br>    first_stage_nms_score_threshold: 0.0<br>    first_stage_nms_iou_threshold: 0.7<br>    first_stage_max_proposals: 300<br>    first_stage_localization_loss_weight: 2.0<br>    first_stage_objectness_loss_weight: 1.0<br>    initial_crop_size: 14<br>    maxpool_kernel_size: 2<br>    maxpool_stride: 2<br>    second_stage_box_predictor {<br>      mask_rcnn_box_predictor {<br>        use_dropout: false<br>        dropout_keep_probability: 1.0<br>        fc_hyperparams {<br>          op: FC<br>          regularizer {<br>            l2_regularizer {<br>              weight: 0.0<br>            }<br>          }<br>          initializer {<br>            variance_scaling_initializer {<br>              factor: 1.0<br>              uniform: true<br>              mode: FAN_AVG<br>            }<br>          }<br>        }<br>      }<br>    }<br>    second_stage_post_processing {<br>      batch_non_max_suppression {<br>        score_threshold: 0.0<br>        iou_threshold: 0.6<br>        max_detections_per_class: 100<br>        max_total_detections: 100<br>      }<br>      score_converter: SOFTMAX<br>    }<br>    second_stage_localization_loss_weight: 2.0<br>    second_stage_classification_loss_weight: 1.0<br>  }<br>}</pre><pre>train_config: {<br>  batch_size: 2<br>  num_steps: 200000<br>  optimizer {<br>    momentum_optimizer: {<br>      learning_rate: {<br>        cosine_decay_learning_rate {<br>          learning_rate_base: 0.01<br>          total_steps: 200000<br>          warmup_learning_rate: 0.0<br>          warmup_steps: 5000<br>        }<br>      }<br>      momentum_optimizer_value: 0.9<br>    }<br>    use_moving_average: false<br>  }<br>  gradient_clipping_by_norm: 10.0<br>  fine_tune_checkpoint_version: V2<br>  fine_tune_checkpoint: &quot;faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8/checkpoint/ckpt-0&quot;<br>  fine_tune_checkpoint_type: &quot;detection&quot;<br>  data_augmentation_options {<br>    random_horizontal_flip {<br>    }<br>  }</pre><pre>data_augmentation_options {<br>    random_adjust_hue {<br>    }<br>  }</pre><pre>data_augmentation_options {<br>    random_adjust_contrast {<br>    }<br>  }</pre><pre>data_augmentation_options {<br>    random_adjust_saturation {<br>    }<br>  }</pre><pre>data_augmentation_options {<br>     random_square_crop_by_scale {<br>      scale_min: 0.6<br>      scale_max: 1.3<br>    }<br>  }<br>}</pre><pre>train_input_reader: {<br>  label_map_path: &quot;training/labelmap.pbtxt&quot;<br>  tf_record_input_reader {<br>    input_path: &quot;train.record&quot;<br>  }<br>}</pre><pre>eval_config: {<br>  metrics_set: &quot;coco_detection_metrics&quot;<br>  use_moving_averages: false<br>  batch_size: 1;<br>}</pre><pre>eval_input_reader: {<br>  label_map_path: &quot;training/labelmap.pbtxt&quot;<br>  shuffle: false<br>  num_epochs: 1<br>  tf_record_input_reader {<br>    input_path: &quot;test.record&quot;<br>  }<br>}</pre><h4>Training The Model</h4><p>The below Command snippet trains our faster R-CNN model on our dataset</p><pre>%cd /content/drive/My Drive/tf2/models/research/object_detection</pre><pre>!protoc /protos/*.proto — python_out=.</pre><pre>!python model_main_tf2.py — pipeline_config_path=training/faster_rcnn.config — model_dir=training/faster_rcnn/ — alsologtostderr — num_train_steps=2000</pre><h4>Evaluating the model</h4><p>with the below code we will be able to evaluate our faster R-CNN model</p><pre>!python model_main_tf2.py — model_dir=training/faster_rcnn — pipeline_config_path=training/faster_rcnn.config — checkpoint_dir=training/faster_rcnn/</pre><h4>Tensorboard</h4><p>Visualizing loss of faster R-CNN model our dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bBUy3O77ReSxn6qnbCeJaQ.png" /></figure><h3>7.) Comparison</h3><p>So let&#39;s look at the mAP(precision) and mAR(recall) for all models on our dataset</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*ofBzs-HiWPvo81opy611mQ.png" /></figure><p>So all our models are working good from the above table but clearly, the centernet is performing better than all the models so let&#39;s build our final pipeline using centernet</p><h3>8.) Final Pipeline and deployment</h3><p>We will deploy the app locally on colab. but in the colab we can not access the local port using our browser so we will use the ngrok API to host the app running on the port to a ngrok server, we will also use streamlit to deploy our app.</p><p>let&#39;s save the complete deployment code in the app.py file</p><pre>%%writefile app.py</pre><pre>import streamlit as st</pre><pre>st.markdown(“&lt;h1 style=’text-align: center; color: White;background-color:#e84343&#39;&gt;Welcome to Product Detection WebApp : &lt;/h1&gt;”, unsafe_allow_html=True)</pre><pre>import numpy as np</pre><pre>import os</pre><pre>import six.moves.urllib as urllib<br>import sys<br>import tarfile<br>import tensorflow as tf<br>import zipfile<br>from collections import defaultdict<br>from io import StringIO<br>from matplotlib import pyplot as plt<br>from PIL import Image<br>from IPython.display import display<br>from object_detection.utils import ops as utils_ops<br>from object_detection.utils import label_map_util<br>from object_detection.utils import visualization_utils as vis_util<br>utils_ops.tf = tf.compat.v1<br>tf.gfile = tf.io.gfile<br>category_index = label_map_util.create_category_index_from_labelmap(‘training/labelmap.pbtxt’, use_display_name=True)</pre><pre>detection_model = tf.saved_model.load(‘inference_graph/saved_model’)</pre><pre>def run_inference_for_single_image(model, image):</pre><pre>  image = np.asarray(image)</pre><pre>  input_tensor = tf.convert_to_tensor(image)</pre><pre>  input_tensor = input_tensor[tf.newaxis,…]</pre><pre>  model_fn = model.signatures[‘serving_default’]</pre><pre>  output_dict = model_fn(input_tensor)</pre><pre>  num_detections = int(output_dict.pop(‘num_detections’))</pre><pre>  output_dict = {key:value[0, :num_detections].numpy() for key,value in output_dict.items()}</pre><pre>  output_dict[‘num_detections’] = num_detections</pre><pre>  output_dict[‘detection_classes’] =       output_dict[‘detection_classes’].astype(np.int64)</pre><pre>  if ‘detection_masks’ in output_dict:</pre><pre>    detection_masks_reframed =   utils_ops.reframe_box_masks_to_image_masks(   output_dict[‘detection_masks’],   output_dict[‘detection_boxes’],image.shape[0], image.shape[1] )</pre><pre>  detection_masks_reframed = tf.cast(detection_masks_reframed &gt; 0.5,tf.uint8)</pre><pre>  output_dict[‘detection_masks_reframed’] =   detection_masks_reframed.numpy()</pre><pre>  return output_dict</pre><pre>def show_inference(model, image_path):</pre><pre>  image_np = np.array(Image.open(image_path))</pre><pre>  output_dict = run_inference_for_single_image(model, image_np)</pre><pre>  vis_util.visualize_boxes_and_labels_on_image_array(</pre><pre>  image_np,</pre><pre>  output_dict[‘detection_boxes’],</pre><pre>  output_dict[‘detection_classes’],</pre><pre>  output_dict[‘detection_scores’],</pre><pre>  category_index,</pre><pre>  instance_masks=output_dict.get(‘detection_masks_reframed’, None),</pre><pre>  use_normalized_coordinates=True,</pre><pre>  line_thickness=8)</pre><pre>  return image_np</pre><pre>def main():</pre><pre>  st.markdown(“&lt;h1 style=’text-align: center; color:     White;background-color:#e84343&#39;&gt;Please Upload the Image — &lt;/h1&gt;”, unsafe_allow_html=True)</pre><pre>  uploaded_path = st.file_uploader(“Choose an Image………” , type = “jpg”)</pre><pre>  if uploaded_path != None:</pre><pre>     st.markdown(“&lt;h1 style=’text-align: center; color: White;background-color:#e84343&#39;&gt;Generating Output …&lt;/h1&gt;”, unsafe_allow_html=True)</pre><pre>  array = show_inference(detection_model, uploaded_path)</pre><pre>  im = Image.fromarray(array)</pre><pre>  st.image(im , caption=”Output” , use_column_width = True)</pre><pre>if __name__ ==’__main__’:</pre><pre>  main() #calling the main method</pre><p>Let&#39;s deploy our app using the app.py file written above</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41678cfb476cafded3f0370034624cc4/href">https://medium.com/media/41678cfb476cafded3f0370034624cc4/href</a></iframe><p>Let&#39;s look at the app in action</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*nzpHmIBhsva13Y97MDt5_g.gif" /></figure><h3>9.) Future Work</h3><ul><li>Collecting more images from different sources of products.</li><li>Experimenting with models apart from the above object Detection model.</li><li>Adding different product classes in the models.</li></ul><h3>10.) References</h3><ul><li><a href="https://towardsdatascience.com/custom-object-detection-for-non-data-scientists-70325fef2dbb">https://towardsdatascience.com/custom-object-detection-for-non-data-scientists-70325fef2dbb</a></li><li><a href="https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html">https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html</a></li><li><a href="https://www.appliedaicourse.com/">https://www.appliedaicourse.com/</a></li><li><a href="https://github.com/tensorflow/models">https://github.com/tensorflow/models</a></li></ul><h3>11.) Profile</h3><ul><li><strong>GitHub </strong><a href="https://github.com/karanguptaneo/Product-Detection-From-Grocery-Shelf"><strong><em>Link</em></strong></a></li><li><strong>LinkedIn P</strong><a href="https://www.linkedin.com/in/parv-gupta-neo/"><strong>rofile</strong></a><strong>.</strong></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9db031e0ddc1" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/product-detection-from-grocery-shelf-9db031e0ddc1">Use Computer Vision for Product Detection on a Grocery Shelf</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stack Overflow Search Engine]]></title>
            <link>https://medium.com/swlh/stack-overflow-search-engine-ce1ce2f9fe1b?source=rss-13f12261259a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce1ce2f9fe1b</guid>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Parv Gupta]]></dc:creator>
            <pubDate>Tue, 27 Oct 2020 07:49:59 GMT</pubDate>
            <atom:updated>2022-04-30T14:56:57.192Z</atom:updated>
            <content:encoded><![CDATA[<p>GitHub <a href="https://github.com/karanguptaneo/Stack-Overflow-Search-Engine">link</a> of the project. LinkedIn <a href="https://www.linkedin.com/in/parv-gupta-neo/">profile</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kOToL45gK-TOTUmP.png" /></figure><h3><em>Overview</em></h3><blockquote>It is a necessary problem for any business to give their user the most relevant results for any search, so it became a necessary problem to solve for any organization, so we will be solving this problem using some state of the art machine learning and software engineering approaches by using the data of the stack overflow to give relevant search results to the user. One of the major key points which should be taken into the consideration is the latency requirement for the above problem, the user will not spend more than half a second to get the results so the latency requirement for this problem would be less than 0.5 seconds. So the objective of the search engine is to give the most relevant results from the available answered questions to the user using the similarity-based techniques in a computationally optimized manner. So from the design standpoint, we will be building a q/a search engine which on the run time takes the query question as text and give the most similar answers by using the text and the semantic similarity of the query as results to the user in less than 0.5 seconds.</blockquote><h3>Index</h3><p>Mapping the Business Problem</p><ol><li>Preprocessing and collection of data</li><li>Modeling with Universal-Sentence-Encoder Model</li></ol><ul><li>Loading the Universal-Sentence-encoder Model</li><li>Connecting to the Elastic Search Container</li><li>Defining the Structure of the Elastic Search Database</li><li>Indexing of the data to the elastic search</li><li>Defining the Search Function</li><li>Give Structure to the Output</li><li>Results</li></ul><p>3. Modelling Using the Bert</p><ul><li>Loading the Bert Model</li><li>Defining the Structure of the Elastic Search Database</li><li>Indexing of the data into the elastic search*</li><li>Results using Bert model</li></ul><p>4. Comparison</p><p>5. Conclusion</p><p>6. Deployment of Model</p><p>7. Model in Action</p><p>8. Future Works</p><p>9. References</p><p>10. Profile</p><h3><strong>Mapping the problem</strong></h3><p>Given a query by the user we have to return the top 10 similar results to the user on the bases of semantic as well as text-based similarity.</p><p>We will vectorize the given query into 2 vectors:</p><ul><li>For the Semantic Vector, we will be using the encoder model to generate the vector embeddings.</li><li>For the text Vector, we can use the Simple BOW to vectorize the Text.</li></ul><p>then we could compute the Similarity using the Cosine Similarity.</p><p>and combine the score using the weighted sum of semantic similarity and text-based similarity.</p><p>We will be using the Elastic Search to get the search results which will be running in a container of our docker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*jTS95t1MFPgR-RKbDy70Mg.png" /></figure><p>Datalink:<a href="https://archive.org/download/stackexchange">https://archive.org/download/stackexchange</a></p><p>We will be using the Elastic Search Which will be containerized in a Docker Instance.</p><p>We will be executing the series of commands to pull and run the elastic search image from the docker repository.</p><p>commands:</p><ul><li>docker pull docker.elastic.co/elasticsearch/elasticsearch:7.9.3</li><li>docker run -p 9200:9200 -p 9200:9300 -e “discovery.type=single-node” docker.elastic.co/elasticsearch/elasticsearch:7.9.3</li></ul><h3>Importing Some Libraries</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/965f797acba0f2158874e63dfa6c45cb/href">https://medium.com/media/965f797acba0f2158874e63dfa6c45cb/href</a></iframe><h3>1.1 ) Some Preprocessing of the Data</h3><p>We will be doing some preprocessing like removing the HTML tags, converting the text into lower case, expanding the contractions of the text</p><p>Below is the Code Snippet</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56d1ecc85e30e40361191e088609f127/href">https://medium.com/media/56d1ecc85e30e40361191e088609f127/href</a></iframe><h3>1.2) Collecting the Data from different Files.</h3><p>We will be picking the 5 most important categories for our work.</p><pre>math.meta.stackexchange.com<br>ai.stackexchange.com<br>datascience.stackexchange.com<br>data/cs.stackexchange.com<br>softwareengineering.stackexchange.com</pre><p>As the data is in the form of XML files so we will be converting the data into a python dictionary.</p><p>Each Of the posts in the XML file contains many attributes but only some of them will be of our use.</p><p>Below are the useful attributes of the XML post-</p><ul><li>PostTypeId: This tells us whether the thread contains a question or answer (1 for the question and 2 for the answer)</li><li>title: It contains the text which is the title of the thread</li><li>Body : It contains question if PostTypeId is 1 it contains answer if PostTypeId is 2</li><li>ParentId: For answers thread, it contains the id of their question</li></ul><p>We will be creating a unique id for each of the thread we create for example if the thread belongs to ‘ai’ and id is 454 then the unique id we create will be ai_454 for the thread, 1 thread will be containing the unique id, title, question, answers.</p><p>below is the code to convert each of the posts in the XML to our thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3a91b1b08deba7cec24639237d230ce/href">https://medium.com/media/d3a91b1b08deba7cec24639237d230ce/href</a></iframe><h3>2.) Modelling Using the Universal-sentence-encoder</h3><h4>2.1 ) Loading the Universal-Sentence-encoder Model</h4><ul><li>We will be using the Universal-sentence-encoder to generate our embeddings.</li><li>The universal sentence encoder model is the state of the art NLP model which encodes sentences into embeddings.</li><li>the model takes a sequence or sentence as input and returns 512-dimensional data for each row.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df5a159046c75ca3b7f5ee15ce1d5b4d/href">https://medium.com/media/df5a159046c75ca3b7f5ee15ce1d5b4d/href</a></iframe><h4>2.2) Connecting to the Elastic Search Container</h4><p>s we will be using a dockerized container on which our elastic search is running so we need to connect with our container using a port number.</p><p>Below is the python code to connect to Elastic Search</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/478406f8c7fe8a05ec2f7f5d0de10714/href">https://medium.com/media/478406f8c7fe8a05ec2f7f5d0de10714/href</a></iframe><h4>2.3) Defining the Structure of the Elastic Search Database</h4><p>As for the elastic search container we need to create a database and we have to define a structure of a database</p><ul><li>we will be having a text as well as a dense 512-dimensional vector so we will be defining our structure accordingly.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a69f2825dbace93a72710a18bd5e63f2/href">https://medium.com/media/a69f2825dbace93a72710a18bd5e63f2/href</a></iframe><h4>2.4) Indexing of the data to the elastic search</h4><p>We will feed our data to elastic search</p><ul><li>will be combining the whole ‘title’ + ‘question’ + ‘answers’ and name it as total_text</li><li>we will be creating feeding the total_text to our encoder model and get the 512 dims data</li><li>in the end, we will feed the total_text and total_text encoded vector to our database</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b7d251921e636d805b131b1d5bd838d/href">https://medium.com/media/8b7d251921e636d805b131b1d5bd838d/href</a></iframe><h4>2.5) Defining the Search Function</h4><p>Below is the Search Function which takes a query text and returns the top 10 similar results on the basis of text-based and semantic similarity.</p><ul><li>We will be using the cosine similarity to generate similarity.</li><li>As for the weighted sum of text similarity scores and semantic similarity score we will be giving higher weight to the semantic similarity.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10c3c3e7a25d220bd639c6f812f9e385/href">https://medium.com/media/10c3c3e7a25d220bd639c6f812f9e385/href</a></iframe><h4>2.6) To Give Structure to the Output</h4><p>As the output returned by the above function is slightly unstructured so we will be using the below code snippet to generate the structured output</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7600886f7cb7241fd98a05b102312a3d/href">https://medium.com/media/7600886f7cb7241fd98a05b102312a3d/href</a></iframe><h3>Results( Top 3)</h3><pre>---------------------------Answer No:1------------------------------<strong>title</strong> :  why should leaf nodes in a red-black tree be black?  question : from the property of red-black trees we know that:  all leaves (nil) are black. (all leaves are same color as the root.)(comren et al &quot;introduction to algorithms&quot;)  but what is the reason that we should enforce them as black, even though they&#39;re nill&#39;s?    <strong>subanswer 1</strong> : take a uncolored leaf node, now you can color it as either red or black. if you colored it as red then you may have chance that your immediate ancestor is also red which is contradicting(according to basic principle). if you color it as black then no problem even though the immediate ancestor is red. and also no change in the number of black nodes from root to leaf paths(i.e every path get +1). this may be the possible reason behind that. <br><strong>subanswer 2 </strong>: it&#39;s simply a part of the definition of a red-black tree. it is also necessary to maintain one of the other rules associated with red-black trees: if a node is red, then both its children are black. <br>--------------------------Answer No:2-------------------------------<strong>title</strong> :  red black tree clarification  <br><strong>question</strong> : i am quite new to red-black trees, and therefore i am having a bit of difficult time trying to understand them.one of the properties of the red-black tree is that every red vertex must have two black children, meaning one cannot have two consecutive red vertices.can someone explain to me why this is an essential property? what are the benefits of having such a property?   <br><strong>subanswer 1</strong> : the properties of a red black tree allow insertion, deletion and search in $o(log(n))$. i guess you can find a prove online somewhere. when an element is inserted or deleted. a fix-up is done, it involves rotations in a tree, so the properties still hold. this ensures the tree is quitebalanced at all times and search is quite fast at all times. <br><strong>subanswer 2</strong> : if you check the proof height-balancedness of red-black trees, you&#39;ll see that we essentially analyse the black-height $h_b$ of the tree which, by another important invariant, is the number of black nodes from the root to any leaf.the property you cite then gives us the right half of$\qquad\displaystyle h_b(t) \leq h(t) \leq 2h_b(t)$,which allows us to carry over bounds on black-height to usual height. <br>-------------------------Answer No:3--------------------------------<strong>title</strong> :  root color of a black red tree  <br><strong>question</strong> : it is required that, in a black red tree, the color for the root is always black. however, wikipedia argues that this rule can be omitted as a red root can always be changed to black but not vice versa. i get the first half, that a red root can be changed to black at any time, but in what circumstance is it impossible to change a black node to red?for instance, consider the branch: black--red--black--red--black. we can always change it to red--black--black--red--black, since a black node does not need to have red children.   <br><strong>subanswer 1</strong> : the root colour of a red-black tree carries no significance. in fact, you can save memory by not encoding it. didactically, it is meaningful to talk about a root&#39;s colour to illustrate what it means to be red or black, because it is a special case because it has no parent which is going to count it when it evaluates the sixth restriction in wikipedia&#39;s list (that the path from a particular node to a leaf should contain the same number of black nodes).as for your more general question about when changing a black node to a red one is allowed: a set of nodes can be repainted if afterwards, the black-height criterion is still satisfied. for example, if a row of the tree is saturated (if it is at depth $k$, there are $2^{k}$ nodes), then you can colour that row black. you may not colour only part of a (saturated or not) red row black. another example: in the tree $1:red \rightarrow\{ 2:black, 3:black\rightarrow\{4:red, 5:red\}\}$, you can flip the colours of $\{3,4,5\}$ and still have a legal rb tree. this is true generally for subtrees of even height and alternating colours. maybe you can think of more examples.</pre><pre>.<br>.<br>.<br>.<br>.</pre><h3>3.) Modeling Using the Bert</h3><p>Above are the results of the Universal-Encoder model now we will try using the bert model.</p><h4>3.1 ) Loading the Bert Model</h4><ul><li>We will be using the Bert Model to generate our embeddings.</li><li>Universal sentence encoder model is a Transformer based state of the art NLP model which encodes sentences into embeddings.</li><li>the model takes a sequence or sentence as input and returns 1024 dimensional data for each row.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb4fc783833e2fbdde317b8bd257556a/href">https://medium.com/media/cb4fc783833e2fbdde317b8bd257556a/href</a></iframe><h4>3.2 ) Defining the Structure of the Elastic Search Database</h4><p>As the model generates 1024 dims embeddings so we have to change the structure of the elastic search database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/619602f922bbde57f3f05bdf69b571dd/href">https://medium.com/media/619602f922bbde57f3f05bdf69b571dd/href</a></iframe><h4>3.3) Indexing of the data into the elastic search</h4><ul><li>we will be having a text as well as a dense 1024 dimensional vector for each row so we will be defining our structure accordingly.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8c09f99ee96627038efccd42e3896cb/href">https://medium.com/media/e8c09f99ee96627038efccd42e3896cb/href</a></iframe><blockquote><em>All the other code snippets will be the same as the first model.</em></blockquote><h3>Results using Bert model(Top 3)</h3><pre>-----------------------------Answer No:1----------------------------<strong>title</strong> :  why should leaf nodes in a red-black tree be black?  <strong>question</strong> : from the property of red-black trees we know that:  all leaves (nil) are black. (all leaves are same color as the root.)(comren et al &quot;introduction to algorithms&quot;)  but what is the reason that we should enforce them as black, even though they&#39;re nill&#39;s?    <strong>subanswer 1</strong> : take a uncolored leaf node, now you can color it as either red or black. if you colored it as red then you may have chance that your immediate ancestor is also red which is contradicting(according to basic principle). if you color it as black then no problem even though the immediate ancestor is red. and also no change in the number of black nodes from root to leaf paths(i.e every path get +1). this may be the possible reason behind that. <br><strong>subanswer 2</strong> : it&#39;s simply a part of the definition of a red-black tree. it is also necessary to maintain one of the other rules associated with red-black trees: if a node is red, then both its children are black. <br>---------------------------Answer No:2------------------------------<strong>title</strong> :  red black tree clarification  question : i am quite new to red-black trees, and therefore i am having a bit of difficult time trying to understand them.one of the properties of the red-black tree is that every red vertex must have two black children, meaning one cannot have two consecutive red vertices.can someone explain to me why this is an essential property? what are the benefits of having such a property?   <br><strong>subanswer 1</strong> : the properties of a red black tree allow insertion, deletion and search in $o(log(n))$. i guess you can find a prove online somewhere. when an element is inserted or deleted. a fix-up is done, it involves rotations in a tree, so the properties still hold. this ensures the tree is quitebalanced at all times and search is quite fast at all times. <br><strong>subanswer 2</strong> : if you check the proof height-balancedness of red-black trees, you&#39;ll see that we essentially analyse the black-height $h_b$ of the tree which, by another important invariant, is the number of black nodes from the root to any leaf.the property you cite then gives us the right half of$\qquad\displaystyle h_b(t) \leq h(t) \leq 2h_b(t)$,which allows us to carry over bounds on black-height to usual height.<br> -------------------------Answer No:3-------------------------------<strong>title</strong> :  root color of a black red tree  <br><strong>question</strong> : it is required that, in a black red tree, the color for the root is always black. however, wikipedia argues that this rule can be omitted as a red root can always be changed to black but not vice versa. i get the first half, that a red root can be changed to black at any time, but in what circumstance is it impossible to change a black node to red?for instance, consider the branch: black--red--black--red--black. we can always change it to red--black--black--red--black, since a black node does not need to have red children.   <br><strong>subanswer 1</strong> : the root colour of a red-black tree carries no significance. in fact, you can save memory by not encoding it. didactically, it is meaningful to talk about a root&#39;s colour to illustrate what it means to be red or black, because it is a special case because it has no parent which is going to count it when it evaluates the sixth restriction in wikipedia&#39;s list (that the path from a particular node to a leaf should contain the same number of black nodes).as for your more general question about when changing a black node to a red one is allowed: a set of nodes can be repainted if afterwards, the black-height criterion is still satisfied. for example, if a row of the tree is saturated (if it is at depth $k$, there are $2^{k}$ nodes), then you can colour that row black. you may not colour only part of a (saturated or not) red row black. another example: in the tree $1:red \rightarrow\{ 2:black, 3:black\rightarrow\{4:red, 5:red\}\}$, you can flip the colours of $\{3,4,5\}$ and still have a legal rb tree. this is true generally for subtrees of even height and alternating colours. maybe you can think of more examples.</pre><pre>.<br>.<br>.<br>.<br>.</pre><h3><strong>4.)Comparison</strong></h3><p>We have seen above that both the models results are quite similar but there is a slight difference in the latency of both the models, as the universal encoder model takes less time to generate the results</p><h3>5.)Conclusion</h3><p>We will be selecting the universal sentence encoder model for the deployment as it takes less time to compute the results.</p><h3><strong>6.)Deploying the Model</strong></h3><p>Till now we have done everything from the experimental point of view but now we have to deploy with clear and concise form, we will be using the flask API to deploy our model locally.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d42b94ff912a41c0cc535db7a024131/href">https://medium.com/media/0d42b94ff912a41c0cc535db7a024131/href</a></iframe><h3>7.)Model in Action</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*dzAhO-806R4wnItZccLIzg.gif" /></figure><h3>8.)Future Works</h3><ul><li>Feeding more data to our Model to increase the quality of results.</li><li>Training our Encoder model with programming codes.</li><li>Apply a different weighting scheme for combining the scores.</li></ul><h3>9.)References</h3><ul><li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html</a></li><li><a href="https://en.wikipedia.org/wiki/BERT_(language_model)">https://en.wikipedia.org/wiki/BERT_(language_model)</a></li><li><a href="https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder">https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder</a></li><li><a href="https://www.appliedaicourse.com/lecture/11/applied-machine-learning-online-course/4149/live-encoder-decoder-models/8/module-8-neural-networks-computer-vision-and-deep-learning">https://www.appliedaicourse.com/lecture/11/applied-machine-learning-online-course/4149/live-encoder-decoder-models/8/module-8-neural-networks-computer-vision-and-deep-learning</a></li></ul><h3>10.)Profile</h3><ul><li><strong>GitHub <em>L</em></strong><a href="https://github.com/karanguptaneo/Stack-Overflow-Search-Engine"><strong><em>ink</em></strong></a></li><li><strong>LinkedIn P</strong><a href="https://www.linkedin.com/in/parv-gupta-neo/"><strong>rofile</strong></a><strong>.</strong></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce1ce2f9fe1b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/stack-overflow-search-engine-ce1ce2f9fe1b">Stack Overflow Search Engine</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>