What is Keras?

Keras is a high-level framework for building and training deep learning models with relatively low amounts of code. It’s designed to make creating neural networks as simple as possible.

Although, it doesn’t run on its own. Keras is a front-end wrapper that runs on top of more complicated tools– TensorFlow or Theano. This means that you make your model in Keras, but behind the scenes it’s being converted to TensorFlow or Theano code.

One helpful feature of Keras is that it is designed to have best practices built in, so using the default settings is usually a safe bet. This makes it even easier for beginners.

Loading Data in TensorFlow

In order to train a Machine Learning model, you need training data. TensorFlow offers multiple methods of loading datasets. These are your three main options:

  1. Preload data into memory
  2. Feed data at request
  3. Set up a custom data pipeline

Option 1: Preload data into memory

Preload all your data into memory and pass it to TensorFlow

  • Easiest and fastest approach
  • All of the data must fit in your computer’s memory
  • Uses regular Python code, no TensorFlow

Option 2: Feed data at request

Portions of your data are fed to TensorFlow periodically, as TensorFlow requests it, instead of preloading it all at once

  • Gives you more control, but also more manual responsibility
  • Work with larger datasets
  • Normal Python code

Option 3: Set up a custom data pipeline

A data pipeline lets TensorFlow load data into memory by itself as it needs it

  • Can hold as much data as you want
  • Requires some TensorFlow code
  • Supports parallel processing (makes training larger datasets more efficient)

Use the straightforward preloading method if your dataset is smaller. Use the data pipeline if your dataset is extremely large

Building a Simple Model in TensorFlow

What are we building?

We will be building a very simple TensorFlow model that adds two numbers together. This will seem trivial, but it’s an uncomplicated way to introduce yourself to TensorFlow and making models.

How to build it

  1. Open PyCharm and create a new project
  2. Save the project with a .py extension. For example, addition.py

Now, time to code.

import os
import tensorflow as tf

First, we import the things we will need into our Python project. We add the as tf when we import TensorFlow so you don’t have to type the entire name out each time you want to access it.

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

Then, we add this line to prevent TensorFlow from flooding the screen with a bunch of log messages, like it normally does. It makes the output easier to read.

X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

Here we define the X and Y input nodes. We are defining them as placeholder nodes that will get a different value each time we run it. In the parentheses, we set their data types and give them names.

addition = tf.add(X, Y, name="addition")

This node does the actual adding. We are basically asking it to add X and Y in this line. We also must give it a name.

# create the session
with tf.Session() as session:

     result = session.run(addition, feed_dict={X: [5], Y: [3]})
     print(result)

# output: [ 8. ]

We need to create a TensorFlow session to run the addition operation on X and Y. We input values for X and Y with the feed_dict parameter.

We are feeding the model arrays because TensorFlow always works with tensors, or multi-dimensional arrays.

You can now run your code.

import os
import tensorflow as tf

# Turn off TensorFlow warning messages in program output    (limits # of log messages)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Define the nodes
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

addition = tf.add(X, Y, name="addition")

# Create the session
with tf.Session() as session:

    result = session.run(addition, feed_dict={X: [1, 5, 6], Y: [3, 4, 5]})

    print(result)

Since the inputs are arrays, you can put multiple numbers for X and Y. The output for the above should be [ 4. 9. 11. ]

This simple program may have seemed over-complicated just to do addition, but it taught you the basic structure of creating and running a TensorFlow model:

  1. Define a model
  2. Create a session
  3. Pass in data
  4. Send it to TensorFlow for execution

How to Install TensorFlow

TensorFlow is a tool to build machine learning models. Learn about it here.

Before we install TensorFlow, we have to install a programming language to use to write code in TensorFlow. TensorFlow works with multiple languages including JavaScript and Python. In this tutorial, we’re going to be downloading Python. How to install Python.

We will be installing TensorFlow with pip so make sure you have pip installed on your computer. You can check if you have pip installed by typing this line in Terminal:

pip3 --version

It should install by itself when you install Python so you shouldn’t have to worry about that.

Now we will install TensorFlow with the command below. It will install in your $HOME directory. If you don’t want this, find a tutorial that shows you how to use a virtual environment. I personally haven’t had luck with this strategy.

pip3 install --user --upgrade tensorflow 

Verify the install with this line:

python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"

What is TensorFlow?

TensorFlow is a software framework for building and deploying machine learning models.

It was developed by Google for their personal use, but they ended up deciding to make it public in late 2015. The first stable version came in 2017.

TensorFlow is open source. This means it is released under a license which allows users to do basically whatever they want with the software.

TensorFlow is low-level and can take quite a few lines of code to build stuff. Keras is a popular, high-level TensorFlow wrapper for building neural networks with much fewer lines of code.

Keras simplifies the process of coding common tasks in TensorFlow, making it much easier and quicker. Read a little bit more about Keras here.

TensorFlow
Keras

How to Install Python and PyCharm

Python is an extremely popular programming language that is commonly used in machine learning and data science. It is one of the smartest languages to learn.

PyCharm is a good IDE (Integrated Development Environment) for Python. An IDE is the software that allows you to write and test your code. There are different IDEs for different programming languages.

How to Install Python

  • Step 3: Press the big yellow download button for the latest version for your OS (It should be Python 3 or higher)
  • Step 4: Click on the downloaded file to launch the installer
  • Step 5: Set it up however you want. I suggest just choosing all of the default options by continuing to press “Continue,” but it depends on your individual situation
  • Step 6: If your on Mac, check to make sure the install was successful by opening up Terminal. Type this in:
python -v

How to Install PyCharm

  • Step 1: Go to https://www.jetbrains.com/pycharm/
  • Step 2: In the center of the screen, press the big black “Download Now” button. You should be transported to a new window that says “Download PyCharm” at the top
  • Step 3: Choose your correct OS (Windows, macOs, or Linux). Then choose the Professional (not free) or Community (free) version of PyCharm and press “Download”
  • Step 4: When the download completes, click on the file to open it
  • Step 5: Simply drag the PyCharm application to the Applications folder, as you’ve been prompted
  • Step 6: Go to your Applications folder and run PyCharm
  • Step 7: Setup PyCharm. Just press “Skip Remaining and Set Defaults” button at the bottom-left to accept the default settings

You’re all set. Now you can press “Create New Project” to get started with a new Python project

Creating a Simple Machine Learning iOS App

Description of the app

This app will take a picture of an object and tell you what that object is. For example, if you take a picture of a hot dog, it will tell you it’s a hot dog. If you take a picture of a Golden Retriever, it will tell you it’s a Golden Retriever.

What is Core ML?

Core ML allows you to integrate trained machine learning models into your iOS app. A trained model is the result of a machine learning algorithm being applied to a set of training data. Core ML models are pre-trained, meaning you don’t have to train them because they’ve already been trained. The models are static which means you are not able to use the data from your app to train the model any further.

We will be using the Inception v3 model for our app. It has been trained to recognize over 1000 categories of images including animals, foods, trees, vehicles, and more.

This tutorial assumes you have a basic understanding of iOS app development.

Creating the app

Step One: Create a new Xcode Project

Xcode is the software used to create iOS apps. If you haven’t already, you must download and set Xcode up. There are plenty of tutorials on how to do this.

  1. Afterwards, create a new Single View App, name it, and save it wherever you like.
Image result for xcode single view app

Step Two: Incorporate the Inception v3 Model into your Project

  1. Go to https://developer.apple.com/machine-learning/build-run-models/
  2. Scroll down to the CoreML Models and find “Inception v3”
  3. Press “Download Core ML Model.”
  4. Once the file downloads, drag the Inceptionv3.mlmodel file into your Xcode project file structure
  5. Make sure the check box “Copy Items if needed” is checked and press Finish (It might take a while for the model to incorporate into Xcode so be patient at first)

Step Three: Setup up your ViewController.swift

  1. Open up the ViewController.swift file in your Xcode project file structure.
import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

     override func viewDidLoad() {
          super.viewDidLoad()

     }

}

The import CoreML statement allows you to use CoreML in your code. The import Vision statement makes it easier to process images.

The UIImagePickerControllerDelegate lets you access your phone’s camera, and the UINavigationControllerDelegate lets you navigate back and forth between screens.

Step Four: Set up your Main.storyboard

  1. Click the yellow button on the top-left of the View Controller box
  2. Click the Editor tab in the menu bar of your Mac
  3. Find “Embed In” and hover on it and choose “Navigation Controller”
  4. A new gray box should appear that says “Navigation Controller.” Slide it over to the left to get it out of the way.
  1. In the top right corner, press the Library button. Type in “Bar Button Item” and drag the Bar Button Item into the top right of the View Controller
  2. Change the Bar Button Item’s icon to a camera icon
  3. Press the Library button again. Type “Image View” and drag an Image View into the View Controller. Resize it fill the entire box.
  4. Click the check box that says “Use Safe Area Layout Guides.”
  1. Connect the Bar Button Item into your ViewController.swift as an IBAction with a name like “cameraTapped”
  2. Connect the Image View into the file as an IBOutlet with a name like “imageView”

Step Five: Connect Storyboard items to variables in code

import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

     @IBOutlet weak var imageView: UIImageView!

     override func viewDidLoad() {
          super.viewDidLoad()
     }

     @IBAction func cameraTapped(_ sender: UIBarButtonItem) { 
     }

}

Step Six: Add an Image Picker to the View Controller

import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

     @IBOutlet weak var imageView: UIImageView!

     let imagePicker = UIImagePickerController()

     override func viewDidLoad() {
          super.viewDidLoad()
          
          imagePicker.delegate = self
          imagePicker.sourceType = .camera
          imagePicker.allowsEditing = false
     }

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
          if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
               imageView.image = userPickedImage
          }
          imagePicker.dismiss(animated: true, completion: nil)
     }

     @IBAction func cameraTapped(_ sender: UIBarButtonItem) {
           present(imagePicker, animated: true, completion: nil)
     }

}

All this code basically allows you to have access to the phone’s camera to take pictures through the app. When you press the Bar Button Item that we set up in your app, it will present a view that lets you take a picture.

Step Seven: Get permission to use the camera

  1. Go to the Info.plist file
  2. Click the Add button
  3. For the Key, select the “Privacy – Camera Usage Description”
  4. For the Value, write something like “We need to use your camera”

This step allows you to get access to the user’s camera by asking permission.

Now the camera functionality in your app should be fully functional.

Your phone should get something like this when you try to run it

Step Eight: Convert the Image to a CIImage

Add the guard statement to your imagePickerController function.

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
          if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
               imageView.image = userPickedImage
               guard let ciimage = CIImage(image: userPickedImage) else {
               fatalError("Could not convert to CIImage")
               }
          }
          imagePicker.dismiss(animated: true, completion: nil)
     }

CIImage is a special type of image that allows us to use the Vision and CoreML frameworks in order to get an interpretation from the model.

Step Nine: Create the Function that Interprets Images with Inceptionv3 Model

Now we’re going to add the function that will actually look at the image, analyze it, and classify it as something.

Add this detect function right above your cameraTapped IBAction

func detect(image: CIImage) {
        guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else {
            fatalError("Loading CoreML Model failed")
        }
        
        let request = VNCoreMLRequest(model: model) { (request, error) in
            guard let results = request.results as? [VNClassificationObservation] else {
                fatalError("Model failed to process image")
            }
            
            if let firstResult = results.first {
                self.navigationItem.title = "\(firstResult.identifier)"
            }
        }
        
        let handler = VNImageRequestHandler(ciImage: image)
        
        do {
            try handler.perform([request])
        }
        catch {
            print(error)
        }
        
    }

@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
        present(imagePicker, animated: true, completion: nil)
    }

First, we created a variable called model which was a reference to the Inception v3 model that we could access in our code.

Then, we created the request that basically uses the model to process input and give out relevant results.

Whatever result the request comes up with (results.first) will be displayed on the user’s screen in the navigation title (self.navigationItem.title).

Then, we put the CIImage that is a parameter of the detect function into the VNImageRequestHandler variable so we can use it in the model.

Last, we performed the request on the handler (A.K.A the image).

Step Ten: Call the detect function

Last but not least, we have to call the detect function in our code so that it actually runs on the image.

Go back to your imagePickerController function.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        if let userPickedImage = info[.originalImage] as? UIImage {
            imageView.image = userPickedImage
            
            guard let ciimage = CIImage(image: userPickedImage) else {
                fatalError("Could not convert to CIImage")
            }
            
            detect(image: ciimage)
            
        }
        
        imagePicker.dismiss(animated: true, completion: nil)
        
    }

The only line that we added is detect(image: ciimage). This line calls the detect function and the ciimage variable defined right above it is the image parameter. Whatever image you use for this parameter is the image that will be analyzed and classified by the model.

The Result

The app is now completely done and ready to run.

Remember: It is not even close to 100% accurate. It only knows about 1000 objects and it’s not a very sophisticated AI. But it’s a cool beginner project and it’s fun to see when it works.

Here’s an example. I took the photo of my dog Blue and it correctly identified him as a Silky Terrier.

Here are some more examples of objects that it recognized.

Thank you so much for reading and I hope you enjoyed this project. Come back soon for more!

What is Deep Learning?

Deep learning (DL) is a method of Machine Learning which teaches machines with as little human effort as possible. They learn from example, like humans, by digesting large amounts of data.

Unlike other other types of Machine Learning that require you to outline features to look for in the data, Deep Learning models deal directly with the data– no human intervention!

The downside to this is Deep Learning models require more data and higher computing power. Also, it takes longer to train DL models.

One of the benefits to DL models is they are more accurate. In addition, they are the best models if you don’t know the features you’re looking for in your data.

Deep Learning is accomplished using neural networks. A neural network is a computing model which is structured based on the structure of a brain.

Deep Learning is used in driverless cars, detecting cancer cells, improving worker safety around heavy machinery, in home assistants, and many other situations nowadays.

What is Machine Learning?

Machine Learning (ML) provides Artificial Intelligence machines the ability to “learn.” This is achieved by using algorithms that discover patterns and generate insights from data.

Unlike traditional programming, you’re not giving the computer detailed instructions on what to do. Instead, you give the computer data and tools that allow it to study and solve the problem without directive. The computer remembers and adapts to new information each time it is used, also known as learning.

Use ML when you can’t tell the computer explicitly what to do in each situation.

Types of Machine Learning

  • Supervised – You show the machine connection between variables and outcomes. It’s like a knowledgeable tutor.
  • Unsupervised – The machine learns through lots of observation and trial and error. Unlike supervised learning, you’re not showing the machine the correct answer. The more data you give it, the more accurate the machine will become.
  • Semi-supervised – Mix between supervised and unsupervised.
  • Reinforcement – The machine gets a reward for performing a desired action.

Why is Artificial Intelligence Important?

Artificial Intelligence (AI) basically allows us to give machines intelligence and enable them to do tasks that used to be done by humans. With this comes an unimaginable amount of benefits and drawbacks. I’ll explain some of the important ones here

AI is important because, depending on the work, it helps us to:

  • make smarter decisions
  • make work more efficient
  • cut down the amount of human effort

Examples of Benefits

Technology

  • Allows for better user experiences on applications by making them smarter, more intuitive and more personalized
    • Online shopping websites
    • Netflix
    • Social Media
    • Siri, Alexa, Cortana
  • Helps programmers because the AI models teach themselves and don’t have to be programmed normally
  • Detect and deter security threats
  • Resolve user tech problems
  • Monitor social media comments to, for example, detect people’s thoughts on a brand/company

Healthcare

  • Robots that assist in surgeries
  • Provide personalized medicine
  • Help with diagnosis
  • X-rays

Business

  • Predict fraudulent transactions
  • Quick credit scoring
  • Financial trading
  • Automated call systems