<?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 Noor Hashem on Medium]]></title>
        <description><![CDATA[Stories by Noor Hashem on Medium]]></description>
        <link>https://medium.com/@noorhashem?source=rss-f784e2b8ce25------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0C2O2AYmBp7ubkoRpV7gcQ@2x.jpeg</url>
            <title>Stories by Noor Hashem on Medium</title>
            <link>https://medium.com/@noorhashem?source=rss-f784e2b8ce25------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 10:29:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@noorhashem/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[Build A Mobile App With A Brain  (Using Tensorflow & CoreML) Prt.1]]></title>
            <link>https://medium.com/@noorhashem/build-a-mobile-app-with-a-using-tensorflow-coreml-prt-1-6d0ca883e664?source=rss-f784e2b8ce25------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d0ca883e664</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Noor Hashem]]></dc:creator>
            <pubDate>Sat, 11 May 2019 15:52:53 GMT</pubDate>
            <atom:updated>2023-11-21T14:13:29.996Z</atom:updated>
            <content:encoded><![CDATA[<h3>(Archived) Build A Mobile App With A Brain🧠 (Using Tensorflow &amp; CoreML) pt.1</h3><p>Made by Dummies for Dummies</p><blockquote>This is a series documenting my work on an iOS app powered by A Custom-Trained Tensorflow model, Firebase and CoreML. My focus was to build a full-working app and explore some Machine Learning while embedding the knowledge in an idea simple enough for a beginner and one that could benefit from further research and work later on for optimization.</blockquote><p><strong>Disclaimer:</strong> This is the first Version of the app and enhancements are being made on the UX and the technical implementation.</p><p>Before talking about what I wanted to use machine learning for, I needed an interface for the user to interact with my machine learning underlying clockwork. Worth mentioning is the fact that I had <strong>Neither </strong>the info about <strong>Machine Learning</strong> and using any models except just definitions nor <strong>developing that interface </strong>(which would be iOS) before starting that project. The project took me a net of 2 <strong>months of part-time focus (you can pull it in one with max focus) </strong>besides my college studying schedule, exams, deadlines and of course failed trials. The three months were for deciding on the idea that will merge both experiences, research the implementation details, study the concepts and the tools then implement ( also including the procrastination 🙄)</p><h3>Step 1: The idea 💡</h3><p>Picking the idea was by limiting down which format of data I wanted to work on with my machine learning models. I picked images because I have always been interested in Computer Vision and Object Recognition.</p><p>My inspiration was an application used for aiding the visually impaired, it’s called <strong>Be My Eyes</strong>. I loved the idea, but I was thinking of working on Mobile accessibility features that allow the visually impaired to be as self-reliant as possible without the need to communicate with volunteers to read him/her what’s in front of them.</p><p>That’s when I decided the <strong>three features</strong> I will include in my app :</p><p>1- An OCR feature to extract the text from an image captured by the phone’s camera and voice it over for the person in real-time.</p><p>2- I was appalled by the fact that some bank notes don’t have braille on them yet to distinguish them. the available options where a standalone device that scans the banknote and speaks it or they simply folded them differently.</p><p>By then, I decided that a simple challenge to start with can be to classify the Egyptian banknotes and voice them to the user from a captured photo in real-time. Starting simple gives you the room to contemplate the process thoroughly and up close. So it was a classification problem to handle now but through imagery.</p><p>3- An extra similar feature to the bank notes but for object detection, this wasn’t so essential but I wanted to try it anyway.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FEdzhPSVAQDg%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DEdzhPSVAQDg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FEdzhPSVAQDg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ff6af4c8d2755fab7b6bcab0a11b37a/href">https://medium.com/media/8ff6af4c8d2755fab7b6bcab0a11b37a/href</a></iframe><p><strong>Note</strong> : It still looks ugly! I know. More work on the UI needed.</p><h3>Step 2: The Research 🧐</h3><p>At that point, I realized my app was a smart ass now that needs to decide for itself which actions to take and speak. And what better way to help here than a mighty Neural Network? …neural…neurons? …as close to a brain as we can get!</p><p>I started researching what’s the best way to classify images efficiently. by efficient, I mean the best accuracy I can get with the least time and resource-consumption training procedures ( I only had a <strong>Lenovo Z510i with a 4GBs of Nvidia RAM</strong>). That’s when, of course, I came across Convolutional Neural Networks. they are designed to handle the immense amounts of data and classify the images into categories by taking decision paths through multiple layers till they boil it down to a few categories with percentages that your image might fall into. And they are also specifically designed to handle huge numbers of training data. Fits with images…A LOT of them!</p><p>Treading into neural networks intuition easily was possible thanks to those amazing two resources</p><ol><li><strong>Neural Networks and Deep learning by Michael A.Nielsen book:</strong></li></ol><p>This book is fantastic, it’s intuitive, simple and tackles the math and linear algebra behind neural networks.</p><p><a href="http://neuralnetworksanddeeplearning.com/">Neural networks and deep learning</a></p><p>2.<strong> 3Blue1Brown’s Playlist</strong> :</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FaircAruvnKk%3Fstart%3D32%26feature%3Doembed%26start%3D32&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DaircAruvnKk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaircAruvnKk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a17f80521caf69358f0868e886890068/href">https://medium.com/media/a17f80521caf69358f0868e886890068/href</a></iframe><p>this playlist was the perfect visual explanation.</p><p>it offers both the concepts + mathematics, and you can always dig deeper or follow up with a more detailed course.</p><p><strong>For Dedicated courses</strong>, I relied on those two :</p><p><a href="http://cs231n.stanford.edu/index.html">CS231n: Convolutional Neural Networks for Visual Recognition</a></p><p>and Andrew Ng’s Machine Learning course was my starter kit, it has two chapters on neural networks and he has also made a series of new courses dedicated to Deep Learning in specific (Neural Networks are considered a deep learning paradigm).</p><p><a href="https://www.coursera.org/learn/machine-learning">Supervised Machine Learning: Regression and Classification</a></p><p>The course is very beginner friendly and well-informative, especially when it comes to intuition, my favorite and most important part while learning something new.</p><p>After learning the concepts comes the next step</p><h3>Picking your superpowers (aka) Tools 🐱‍💻</h3><p>There is plenty of frameworks to train neural networks. I will mention the most popular four I found during research :</p><ol><li>Tensorflow</li><li>PyTorch</li><li>Keras</li><li>Caffe</li></ol><p>These two videos by Siraj Raval ( Follow Him, Recommended!) Points the main differences between each of the four frameworks:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FMDP9FfsNx60%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DMDP9FfsNx60&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FMDP9FfsNx60%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffc2d3b2f572785046c5389618337cde/href">https://medium.com/media/ffc2d3b2f572785046c5389618337cde/href</a></iframe><p>I Picked Tensorflow because it allowed me to export my model in a Lighter form compatible with mobile devices TFLite, You can learn more about TFLite from here: <a href="https://www.tensorflow.org/lite/">Link</a></p><p>After Picking the framework, comes the setup of this framework on your workstation.</p><p>I Used both Windows 10 and Ubuntu 16.04 (I shifted between both if anything wasn’t supported)</p><h3>Step 3 : The Tools + Setting Up The Workstation 🔧</h3><ol><li>OS: Windows 10 + Ubuntu 16.04 LTS ( as mentioned )</li><li><strong>(If you already own a Macbook, skip this!)</strong> VMWare Workstation Pro 14.0.0 ( For Running a version of MacOS for the iOS app development) Why I picked iOS development while not having a MacOS system? because I simply wanted to try it! and it worked!</li><li>Anaconda Package Manager (Python 3.5)</li><li>Tensorflow GPU 1.7 (You can use Tensorflow without GPU-enabled if you don’t have a NVIDIA gfx card and it will still work)</li><li>cuDNN 7 (a Library) and CUDA 9.0 (a Platform) both provided by NVIDIA for accelerating the performance of these frameworks aforementioned by using GPU-accelerated computing on NVIDIA graphics card.</li><li><strong>(If you already own a Macbook, skip this!)</strong>MacOS High Sierra 10.13.1 image</li><li>Xcode 9.2 for developing the iOS app.</li></ol><p>Follow this tutorial for installing Anaconda + Python3 on your Windows 10 (It applies to newer versions too) and install tensorflow-gpu from Anaconda.</p><p>You can set it up right away or use a virtual environment to separate it from your Default workspace.</p><ol><li>Directly using this command in your Anaconda Prompt pip install tensorflow-gpu</li><li>Creating a virtual machine then installing Tensorflow :</li></ol><pre>$ conda create -n nameofenv python=3.5</pre><pre>#then install Tensorflow-GPU using this command</pre><pre>$nameofenv pip install tensorflow-gpu</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FRplXYjxgZbw%3Fstart%3D626%26feature%3Doembed%26start%3D626&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DRplXYjxgZbw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FRplXYjxgZbw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/db4504a9a9bdcc69337803494fb154ff/href">https://medium.com/media/db4504a9a9bdcc69337803494fb154ff/href</a></iframe><p>For Ubuntu, You will make sure you downloaded your latest NVIDIA gfx card driver for Ubuntu and download the cuDNN &amp; CUDA packages for Ubuntu then install Tensorflow GPU from your terminal using this command:</p><p>pip install tensorflow-gpu</p><blockquote><strong>Note </strong>: When installing make sure you’ve added the CUDA directory on your system to your PATH ( as mentioned in the video)</blockquote><p><strong>On Windows</strong> : search for Environment Variables on your windows search and choose the ‘Path’ variable and add the CUDA directory likewise</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*i-H2nrnrHwMYKq-JFJiooA.png" /></figure><p><strong>On Ubuntu </strong>:</p><p>This is how you do it on Ubuntu → Go to your HOME directory and edit your ‘bashrc’ file</p><pre>$ gedit ~/.bashrc #will open the gedit for you to add the lines</pre><pre>export PATH=&quot;$PATH:/path/to/dir&quot;</pre><pre>#save the file then in terminal write:</pre><pre>$ source ~/.bashrc</pre><p>For a detailed Tutorial with another option, check <a href="https://medium.com/@zhanwenchen/install-cuda-and-cudnn-for-tensorflow-gpu-on-ubuntu-79306e4ac04e">this</a>.</p><h3>Step 4 : Gathering The Money 🤑….Dataset 😏</h3><p>After Setting everything up, We then need the data to train our model for detecting the currency. I took photos using my phone camera (Better be high quality) and took photos of different Egyptian currencies in different positions and lighting conditions (i.e. while holding it in hand ( since this will mostly be the way you capture it using the app) and on different surfaces with a lot of stuff in the background and with less stuff, among other currencies…etc)</p><p>I then gathered all the photos of every currency in a separate folder like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RZEsUNMbPPvX_smTNAJJqA.png" /></figure><p><strong>Examples of the photos </strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*bPV46femlB8Sz9Nh5lWeew.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*oihZ05Y1njeI9h8HdI5N1Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*mi1fPDc2_vyffD1d15SDFA.jpeg" /></figure><h3>Step 5 : Let the magic begins!</h3><p>I relied on this tutorial from Google’s CodeLabs</p><p><a href="https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/">TensorFlow For Poets</a></p><p>I cloned their repo from Github and used the scripts to train my classifier for currencies.</p><blockquote><strong>Note : </strong>To clone the repo, you need to install the git package on your Anaconda’s Virtual environment</blockquote><p><strong>First:</strong> Activate it from your Anaconda Prompt</p><p>$ activate yourenvname</p><p><strong>Second:</strong> install Git for Anaconda</p><p>$(yourenvname) conda install -c anaconda git</p><p><strong>Third:</strong> Clone</p><pre>$ git clone <a href="https://github.com/googlecodelabs/tensorflow-for-poets-2">https://github.com/googlecodelabs/tensorflow-for-poets-2</a></pre><p>Now you have all the scripts you would need to train, You just need to</p><ol><li>choose the model to train on from Google’s pool of pre-trained models.</li><li>adjust some parameters for running the script.</li></ol><h3>A. Picking a model</h3><p>There is a couple of pre-trained networks for you to work with but they vary in terms of <strong>accuracy</strong> and <strong>size of model</strong> and <strong>speed of detection</strong>.</p><p>Find Them <a href="https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models">Here</a></p><p>I picked <a href="https://ai.googleblog.com/2017/06/mobilenets-open-source-models-for.html">MobileNet_v1_0.50_224 </a>because I wanted a lighter model and its accuracy was acceptable for my application with the proper training steps and higher image qualities.</p><blockquote>The MobileNet is configurable in two ways:</blockquote><blockquote>Input image resolution: 128,160,192, or 224px. <strong>Unsurprisingly, feeding in a higher resolution image takes more processing time, but results in better classification accuracy.</strong></blockquote><p>After downloading your model, add it in a directory titled as “models” ( create it) and add it in your “tf_files” directory in your GitHub repository clone on your computer. Then add your images directory after categorizing the photos to your tf_files directory. ( You can put them anywhere you want, but it’s advised to have all your data and models in a single workspace for easier accessibility)</p><h3>B. ReTraining The Model</h3><p>Before starting the training, we will open a tensor board session to monitor the training process and save the training status in log files in our log directory.</p><pre>tensorboard --logdir tf_files/training_summaries &amp;</pre><p>then follow the URL it gives you for monitoring. it should be something like this <a href="http://DESKTOP-DU3A4PK:6006">http://your-pc-name:6006</a></p><p>Navigate to the repository folder using your anaconda prompt, then go to the scripts folder to run the retrain.py script by writing the following to your prompt.</p><p>but first, we need to set up two environment variables</p><pre>IMAGE_SIZE=224<br>ARCHITECTURE=&quot;mobilenet_0.50_${IMAGE_SIZE}&quot;</pre><p>to do this in our Anaconda Environment immediately when you activate it, we will navigate to our anaconda environment directory and create a bat file following these <a href="https://conda.io/docs/user-guide/tasks/manage-environments.html">instructions</a> in our anaconda prompt.</p><pre>cd %CONDA_PREFIX%<br>mkdir .\etc\conda\activate.d<br>mkdir .\etc\conda\deactivate.d<br>type NUL &gt; .\etc\conda\activate.d\env_vars.bat<br>type NUL &gt; .\etc\conda\deactivate.d\env_vars.bat</pre><p>then we will edit the env_vars file with our environment variables above as follows.</p><pre>set IMAGE_SIZE=224<br>set ARCHITECTURE=&quot;mobilenet_0.50_${IMAGE_SIZE}&quot;</pre><blockquote><strong>Tip :</strong> You can</blockquote><blockquote>1. navigate to your anaconda directory where it is installed on your system and then to the envrionments directory (C:\users\yourusername\Anaconda3\envs\yourenvname\etc\conda) and create the activate.d , deactivate.d directories and env_vars.bat file with your variables inside the activate.d directory.</blockquote><blockquote>2. instead of setting your environment variables you can run the script and edit in your flags while writing your command on runtime.</blockquote><p>Initiating the training is as simple as shoving this command to your terminal</p><pre>python -m scripts.retrain --bottleneck_dir=tf_files/bottlenecks --how_many_training_steps=<strong>500</strong> --model_dir=tf_files/models/ --summaries_dir=tf_files/training_summaries/&quot;<strong>${ARCHITECTURE}</strong>&quot; --output_graph=tf_files/retrained_graph.pb --output_labels=tf_files/retrained_labels.txt --architecture=&quot;<strong>${ARCHITECTURE}</strong>&quot; --image_dir=tf_files/<strong>money_photos</strong></pre><p>The things I <strong>Bolded-out </strong>are the ones that you can tweak depending on your inputs and case. You can either change them in your command as I mentioned in #2 in the last tip, or you can set them up in your environment variables. The flags should be</p><p><strong>training_steps:</strong> The first retraining command iterates only 500 times. You can improve it by training for more iterations. You can remove</p><p>--how_many_training_steps to use the default 4,000 iterations.</p><p><strong>ARCHITECTURE</strong>: it’s set up in your env_var file, or you can replace it in the command above by adding mobilenet_0.50_224 between the double quotes.</p><p><strong>image_dir:</strong> path pointing to the directory where you have your training set photos categorized in folders.</p><p><strong>model_dir:</strong> path to where you have the model you downloaded.</p><p><strong>output_graph:</strong> path to where you want to export your trained model after training.</p><p><strong>output_labels:</strong> path to where you want to export your training labels after training.</p><p>The training will start and you will get something similar to this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*w-_8ni_BW_GcSbDqRI_etw.png" /></figure><p>You will get the <strong>training accuracy</strong>, <strong>validation accuracy</strong> and<strong> cross entropy</strong> (Your error). We want to maximize the first two as much as possible and minimize our error ofc.</p><p>by the end of the training steps you specify, you should view your training status and see if the accuracy and error are acceptable to you or you’d like to retrain again. That’s what I got by the end of my training session.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*ppHktqBoqKWrKY0r_OED_Q.png" /></figure><p>a training accuracy of 100%, validation of 95% and error = 0.002313</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-f3MHQqQSjiDKsc-zwZQxg.png" /></figure><p>Congratulations! 🎉 You have a trained model and you’re gonna find the model file and the labels in the output directory specified in our flags, in our case: the tf_files directory ( unless you changed the output directories). The model will be a file: retrained_graph.pb and the labels: retrained_labels.txt.</p><p>Keep them safe as we will need them in our next step.</p><p>You can test your model on photos not in your training set and see the results using the “label_image script” in the repo you cloned.</p><pre>python -m scripts.label_image <br> --graph=tf_files/retrained_graph.pb  <br> --image=tf_files/your_photos_dir/&#39;Ten Pounds&#39;/1.jpg</pre><p>You will get an output of classification categories by percentages for your selected photo.</p><h3>C. Converting The Model To Tf-Lite Format</h3><p>To convert the trained model into a lite version that the mobile can handle and process, we first use an optimization tool provided by Tensorflow, that removes some of the unnecessary nodes in your network that won’t affect the inference operation and also speed up the process. it basically encapsulates some operations and removes some that won’t affect that much and are not supported by our mobile phones.</p><p>We’d run the optimization script on our trained model file as follows</p><pre>python -m tensorflow.python.tools.optimize_for_inference <br>  --input=tf_files/retrained_graph.pb <br>  --output=tf_files/optimized_graph.pb <br>  --input_names=&quot;input&quot; <br>  --output_names=&quot;final_result&quot;</pre><p>Now after optimizing the model, we will make the final transformation into a TFLite model by running the model converter script</p><p>By setting your environment variable IMAGE_SIZE = 224 and specifying the path to your trained model and the output path for the .lite file that will be the outcome of the conversion process, you’re ready to hit ENTER</p><pre>IMAGE_SIZE=224<br>tflite_convert<br>  --graph_def_file=tf_files/retrained_graph.pb <br>  --output_file=tf_files/optimized_graph.lite <br>  --input_format=TENSORFLOW_GRAPHDEF <br>  --output_format=TFLITE <br>  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3<br>  --input_array=input<br>  --output_array=final_result<br>  --inference_type=FLOAT<br>  --input_data_type=FLOAT</pre><p>after the conversion is done you’d find a .lite file in your specified output path and this is the model we’re gonna use in our iOS app.</p><p>Phew! Mission accomplished!… The first part only, don’t get too excited 🤪. But you can relax a bit to celebrate the first steps, do some in-depth research if needed for the concepts you have encountered until I post the next part of this series where I discuss the process of building the iOS app in-detail with the three features I introduced at the beginning and how to utilize the model we trained and deploy it in our app.</p><p><strong>Part Two of the series : Coming Soon! (iOS app development and model deployment)</strong></p><p>Link to the project on Github <a href="https://github.com/noorhashem/ios-app-with-brains">Here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d0ca883e664" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>