Skip to main content
SAGE
Search form
  • 00:09

    SOUBHIK BARARI: Now that we know how training and predictionwork in neural networks and also howto perform both of these things on a network in Keras,let's work through an end to end pipeline involving trainingand prediction.And this time instead of using dummy data we'regoing to use real world data.Specifically, we're going to takeimages of handwritten digits and label them

  • 00:29

    SOUBHIK BARARI [continued]: with impressive accuracy.You can find all of this in notebook number two.So just like in the previous notebook,we're going to import NumPy, but in this onewe're also going to import matplotlib,as I mentioned before, which is a Python's plotting library.So instead of just looking at abstract matricesof our images, we can actually see them as plots.

  • 00:52

    SOUBHIK BARARI [continued]: Additionally, we're going to import a bunch of thingsfrom Keras including the mnist datasetand the rest of the model and the layers stuff from before.Run the Ember blog.Again, ignore the warning messagesif there are any for you.OK.So we're going to go ahead and load up the mnist digits

  • 01:14

    SOUBHIK BARARI [continued]: dataset, which is actually already splitinto Drake training and validation data, like so.OK.And we're actually going to go ahead and plot a few examplesusing matplotlib.Hopefully you've seen some examples of matplotlib codebefore, but if not, you basically

  • 01:34

    SOUBHIK BARARI [continued]: start by defining your figure, things like the figure sizeand the title, and then you go ahead and attachindividual plots to the figure.In our case, we're going to attach a threeby three grid of plots that show the first nineimages in our dataset, in the training data set specifically,and there are ground truth labels.

  • 01:56

    SOUBHIK BARARI [continued]: OK.So we go ahead and run that.We can see each of these first nine images,they're pretty small images, but you can obviously still makeout what digits they are.And we also have the ground truth labelsfor what digit they are.So since these inputs are two dimensional images of pixels,

  • 02:18

    SOUBHIK BARARI [continued]: we're actually going to have to doa little bit of pre-processing to fit thisinto the input layer of the neural network we'regoing to use.So what we're doing in this code blockhere is we're collapsing each image matrix into a onedimensional vector, and we're actuallynormalizing its grayscale pixel value to be between 0 and 1.So just like before, since we're working with classification

  • 02:40

    SOUBHIK BARARI [continued]: and we have 10 classes, we need 10 output neurons.So we have to convert each zero to nine responsevalue to a categorical vector.So hopefully you remember how to do thisfrom the previous notebook.So let's do this together.So we're going to call the new training response

  • 03:02

    SOUBHIK BARARI [continued]: vector y train categorical and we'regoing to use the two categorical function to dothe transformation.And same thing for the validation responses.OK.

  • 03:23

    SOUBHIK BARARI [continued]: And we're just going to save the number of classesthat we're working with, which is 10 classes.All right.Let's make sure that we did that right.OK.So we've transformed it and we save the number of classes.And again, we're going to create our neural network

  • 03:46

    SOUBHIK BARARI [continued]: from scratch.So we start by creating the base object, whichis of the sequential class, and we're justgoing to add a single hidden layer and an output layer.So we see that the output layer has already been coded in,so let's add the hidden layer in together.OK.So digitpredictor.add and it's going to be of the dense class

  • 04:10

    SOUBHIK BARARI [continued]: just like before.The number of neurons here, we're justgoing to make the number of pixels,and the input dimensions are justgoing to be the number of pixels as well since that'sthe dimensionality of our input.And the activation function, we're just

  • 04:31

    SOUBHIK BARARI [continued]: going to make relu like before.OK.Let's see if that compiles.So now we're going to train our model on the entire 60,000image training set that we have here.We're going to specify a batch size of 1,000so you all can do a multiplication.That means that there'll be 600 batches per epoch, which will

  • 04:55

    SOUBHIK BARARI [continued]: cover the whole training set.So let's go ahead and let this run.And brace yourself because this might take a while.OK.So it's starting the first epoch here and it's going.OK.Our model is finally done training now, as you can see.

  • 05:15

    SOUBHIK BARARI [continued]: Hopefully that only took a couple of minutesat most for you.Now, let's go ahead and evaluate our modelon the validation set provided for us.OK, let's do this together.So we're going to call this the validation scoresand that is equal to the model, or in this case,the digit predictor evaluating the validation set

  • 05:44

    SOUBHIK BARARI [continued]: and also being fed in the categorical vector.We're just going to make this not verbose.We're not going to let it give usany input just for cleanliness.And we're going to have it print the final accuracy,

  • 06:05

    SOUBHIK BARARI [continued]: .format validation scores.So we want just the accuracy, so it'sgoing to be the second element or the first index.OK.So let's go ahead and run that.Moment of truth.

  • 06:34

    SOUBHIK BARARI [continued]: And we see that the validation accuracy for our modelis 96% or 97%, which is very high.So now we want to actually see imageby image what our model predicts on the validation set.In general with neural networks it'sa good idea to closely inspect the predictionson a few validation inputs just to make sure things haven't

  • 06:56

    SOUBHIK BARARI [continued]: gone horribly wrong.So we're going to get all the predictions first.So going to do this together, so y_pred equalsdigit_predictor.predict_classes.That is the function we want to call here, predict classes.We feed it the validation inputs and thenwe reshape the output to be a vertical vector.

  • 07:20

    SOUBHIK BARARI [continued]: OK.So we go ahead and run that.Might take just a minute or two hereas it's doing it over the thousands of imagesthat are on the validation set.OK.And once that's done here is the code

  • 07:41

    SOUBHIK BARARI [continued]: to visualize the predictions on a set of the validation data.And we're going to visualize that in another grid.OK.So we go ahead and run that and wecan see each of the predictions on these validation images.All right.So we can see that it actually does really well.

  • 08:04

    SOUBHIK BARARI [continued]: So it mispredicts this five here, which actually even looksa little bit like a six, so it's not surprisingthat it predicts it was a six.This three here, which also kind of looksa little bit like an eight, the neural networkpredicts that it's an eight.But other than that it gets all of these initial imagescorrect.

  • 08:27

    SOUBHIK BARARI [continued]: And if you want you can actually increase the columns and rowshere and visualize more examples,so visualize them a few more of the predictions on the images.And that wraps it up.So hopefully this notebook has demonstrated to youexactly how you can create an endto end Keras pipeline for some simple but very usefulimage recognition.

Video Info

Series Name: Deep Learning in Python

Episode: 6

Publisher: SAGE Publications Ltd

Publication Year: 2019

Video Type:Tutorial

Methods: Deep learning, Python

Keywords: accuracy; coding; data management; data preparation; data transformations; evaluation and assessment; neural networks; prediction; programming and scripting languages ... Show More

Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

Abstract

Soubhik Barari, PhD student in Political Science, IQSS, at Harvard University, discusses how to use Python's Keras package to create an end-to-end pipeline for image recognition, including how to setup the neural network and run the training set, how to evaluate the model using the validation set, and how to inspect the predictions.

Looks like you do not have access to this content.

Deep Learning in Python: End-to-End Keras Pipeline for Image Recognition

Soubhik Barari, PhD student in Political Science, IQSS, at Harvard University, discusses how to use Python's Keras package to create an end-to-end pipeline for image recognition, including how to setup the neural network and run the training set, how to evaluate the model using the validation set, and how to inspect the predictions.

Copy and paste the following HTML into your website