In this Keras tutorial, you’ll discover how easy it is to get started with deep learning and Python. You will use the Keras deep learning library to train your first neural network on a custom image data set, and you will also implement your first convolutional neural network (CNN).

I’m going to bring you a new and different Keras tutorial.

I’m going to teach you how to take advantage of these pre-compiled data sets, rather than teach you how to use custom data sets to train your first neural networks and convolutional neural networks, to be honest, your goal is to apply deep learning to your own data sets, not the built-in Keras, am I right?

The introduction

Today’s Keras tutorial is designed for practitioners — this is how practitioners apply deep learning.

In the first half of this article, we went through the first five steps,

  1. Install Keras and other dependencies on your system
  2. Load data from disk
  3. Create training and test branches
  4. Define your Keras model architecture
  5. Compile your Keras model

Today’s topic is step 6.7.8, which includes

6. Train your training data model

7. Evaluate the model on test data

8. Prediction using trained Keras model

6. Fit your CARAS model into the data

Figure 8: In step 6 of the KARAS tutorial, we use our training data and compilation model to train a deep learning model.

Now that our Keras model is compiled, we can “fit” (i.e. train) it to our training data:

# train the neural network
H = model.fit(trainX, trainY, validation_data=(testX, testY),
	epochs=EPOCHS, batch_size=32)
Copy the code

We discussed all the inputs except the batch_size size. Batch_size Controls the size of each data group to pass through the network. Larger Gpus will be able to accommodate larger batch sizes. I recommend starting at 32 or 64 and working your way up.

7. Evaluate your Keras model

Figure 9: After we fit the model, we can use our test data to make predictions and generate classification reports

We have trained our actual model, but now we need to evaluate it on test data.

It is important that we evaluate the test data so that we can get an unbiased (or as close to unbiased as possible) representation of how well our model handles untrained data.

To evaluate our Keras model, we can use a combination of the model’s.predict prediction method and classfication_report from SciKit-Learning:

# evaluate the network print("[INFO] evaluating network..." ) predictions = model.predict(testX, batch_size=32) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=lb.classes_)) # plot the training loss and accuracy N = np.arange(0, EPOCHS) plt.style.use("ggplot") plt.figure() plt.plot(N, H.history["loss"], label="train_loss") plt.plot(N, H.history["val_loss"], label="val_loss") plt.plot(N, H.history["acc"], label="train_acc") plt.plot(N, H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy (Simple NN)") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["plot"])Copy the code

When running this script, you will notice that our Keras neural network will begin training, and once the training is complete, we will evaluate the network on our test set:

$ python train_simple_nn.py --dataset animals --model output/simple_nn.model \ --label-bin output/simple_nn_lb.pickle --plot output/simple_nn_plot.png Using TensorFlow backend. [INFO] loading images... [INFO] training network... Train on 2250 samples, validate on 750 samples Epoch 1/75 2250/2250 [==============================] - 1s - loss: 1.1033-ACC: 0.3636 - val_loss: 1.0811 - val_ACC: 0.3707 Epoch 2/75 2250/2250 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s - loss: 1.0882 acc: 0.3862 - val_loss: 1.1292 - val_acc: 0.3227 Epoch 3/75 2250/2250 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s - loss: 1.0713 acc: 0.4067 - val_loss: 1.0525 - val_ACC: 0.3907... Epoch 73/75 2250/2250 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s - loss: 0.7780 acc: 0.6067 - val_loss: 0.8438 - val_acc: 0.5813 Epoch 74/75 2250/2250 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s - loss: 0.7805 acc: 0.5978 - val_loss: 0.8463 - val_acc: 0.5893 Epoch 75/75 2250/2250 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s - loss: 0.7765 acc: 0.6262-val_loss: 0.8144-val_ACC: 0.6133 [INFO] Evaluating network... Precision recall F1-Score support cats 0.58 0.50 0.54 236 dogs 0.49 0.50 0.49 236 panda 0.73 0.81 0.77 278 AVG/total 0.61 0.61 0.61 750 [INFO] serializing network and label binarizer...Copy the code

The network is small, and when combined with a small piece of data, it takes only 2 seconds per session on my CPU.

Here you can see that our network is gaining 61% accuracy.

Since we have a one in three chance of randomly selecting the right label for a given image, we know that our network has actually learned patterns that can be used to distinguish between the three classes.

We also saved a drawing:

  • Training loss
  • Confirm the loss
  • The training accuracy
  • Verify the accuracy of the

… Make sure that we can easily find that our results overfit or underfit.

Figure 10: Our simple neural network training script (created with Keras) generates an accuracy/loss graph to help us find deficiencies/overfits

Looking at our chart, we see that a small amount of overfitting began to occur in the past epoch~45, where our training and validation losses began to diverge and a significant gap appeared.

Finally, we can save our model to disk so we can reuse it without having to retrain it:

# save the model and label binarizer to disk print("[INFO] serializing network and label binarizer..." ) model.save(args["model"]) f = open(args["label_bin"], "wb") f.write(pickle.dumps(lb)) f.close()Copy the code

8. Predict new data using Keras model

At this point, our model is trained – but what if we want to make predictions about the image after our network has been trained?

  • So what do we do?
  • How do we load models from disk?
  • How can images be loaded and preprocessed for classification?

In the predict.py script, I’ll demonstrate how to use it, so open it and insert the following code:

# import the necessary packages from keras.models import load_model import argparse import pickle import cv2 # construct  the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image we are going to classify") ap.add_argument("-m", "--model", required=True, help="path to trained Keras model") ap.add_argument("-l", "--label-bin", required=True, help="path to label binarizer") ap.add_argument("-w", "--width", type=int, default=28, help="target spatial dimension width") ap.add_argument("-e", "--height", type=int, default=28, help="target spatial dimension height") ap.add_argument("-f", "--flatten", type=int, default=-1, help="whether or not we should flatten the image") args = vars(ap.parse_args())Copy the code

First, we will import the required packages and modules.

Whenever you write a script to load the Keras model from disk, you need to explicitly import load_model from keras.models. OpenCV will be used for annotation and display. The pickle module will be used to load our label binalizers.

Next, let’s parse the command line arguments:

  1. –image: We enter the path of the image.
  2. –model: path to the KRAS model that we trained and serialized.
  3. –label-bin: Path to the serialized label binalizer.
  4. –width: width of our CNN input shape. Remember, you can’t just specify anything here. You need to specify the width of the model design.
  5. –height: Enter the height of the CNN image. The specified height must also match the input shape of the network.
  6. –flatten : Should we flatten the picture? By default, we don’t flatten the image. If you need to pan the image, you should pass 1 for this parameter.

Next, let’s load the image and resize it according to the command line arguments:

# load the input image and resize it to the target spatial dimensions
image = cv2.imread(args["image"])
output = image.copy()
image = cv2.resize(image, (args["width"], args["height"]))
Copy the code

Then, if needed, we make the image flatten.

# check to see if we should flatten the image and add a batch
# dimension
if args["flatten"] > 0:
	image = image.flatten()
	image = image.reshape((1, image.shape[0]))
 
# otherwise, we must be working with a CNN -- don't flatten the
# image, simply add the batch dimension
else:
	image = image.reshape((1, image.shape[0], image.shape[1],
		image.shape[2]))
Copy the code

Flat image standard fully connected network is simple (lines 30-32).

In the CNN example, we also added batch sizes, but we didn’t flatuate the image (lines 33-38). The next section looks at an example — CNN.

Let’s load the model + label binarizer into memory and make a prediction:

# load the model and label binarizer print("[INFO] loading network and label binarizer..." ) model = load_model(args["model"]) lb = pickle.loads(open(args["label_bin"], "rb").read()) # make a prediction on the image preds = model.predict(image) # find the class label index with the largest corresponding # probability i = preds.argmax(axis=1)[0] label = lb.classes_[i]Copy the code

Our model and label binalizers are loaded through lines 42 and 43.

We can predict the input image by calling model.predict (line 46).

What does a preds array look like?

(Pdb) PreDS Array ([[5.4622066E-01, 4.5377851E-01, 7.7963534E-07]], DType = FLOAT32)Copy the code

The 2D array contains the index of the images in the batch (there is only one index here, because only one image is passed into the NN for classification) and the percentage corresponding to each class label, as shown by querying the variables in my Python debugger:

  • Cats: 54.6%
  • Dogs: 45.4%
  • Panda: ~ 0%

In other words, when our network “thinks” it sees “cats”, it must “know” it doesn’t see “pandas”.

Line 50 looks for the maximum index (the 0th “cat” index).

Line 51 extracts the “cat” string tag from the tag binalizer.

Easy?

Now let’s show the results:

# draw the class label + probability on the output image text = "{}: {:.2f}%". Format (label, preds[0][I] * 100) cv2.puttext (Output, text, (10, 30), cv2.font_hershey_simplex, 0.7, (0, 0) 255), 2) # show the output image cv2.imshow("Image", output) cv2.waitKey(0)Copy the code

We format the text string at line 54. This includes predicted values in label and percentage format.

Then we put text on the Output image (lines 55 and 56).

Finally, we display the output image on the screen and wait until the user presses any key in lines 59 and 60 (watch Homer Simpson try to locate the “any” key).

Our prediction script is fairly straightforward.

Once you have downloaded the code using the Downloads section of this tutorial, you can open the terminal and try running the trained network on your custom image:

$ python predict.py --image images/cat.jpg --model output/simple_nn.model \
	--label-bin output/simple_nn_lb.pickle --width 32 --height 32 --flatten 1
Using TensorFlow backend.
[INFO] loading network and label binarizer...
Copy the code

Make sure you copy/paste or type the entire command (including the command-line arguments) from within the folder relative to the script. If you’re having problems with command-line arguments, read this blog post.

Figure 11: In our KARAS tutorial, a cat was correctly classified using a simple neural network

Here, you can see that our simple Keras neural network has classified the input image as “cats” with a 55.87% probability, even though the cat’s face is partially obscured by a piece of bread.

9. Bonus: Training the first convolutional neural network with Keras

Admittedly, using standard feedforward neural networks to classify images is not a wise choice.

Instead, we should take advantage of a convolutional neural network (CNN) designed to operate on the raw pixel intensity of the image, and learn the discriminant filters that can be used to classify the image with high precision.

The model we will discuss today is a smaller variant of VGGNET, which I call “SmallVGGNet”.

The vGGnet-like model has two common characteristics:

  1. Just use the 3 by 3 convolution.
  2. Before applying destructive pool operations, the convolutional layers are stacked deeper on top of each other in the network architecture.

Now let’s implement SmallVGGNet.

Open the smallvggnet.py file and insert the following code:

# import the necessary packages
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras import backend as K
Copy the code

As you can see from the input in lines 2-10, everything SmallVGGNet needs comes from Keras. I encourage you to familiarize yourself with each of the Keras documentation and my deep learning manual.

We then define the SmallVGGNet class and build method:

class SmallVGGNet:
	@staticmethod
	def build(width, height, depth, classes):
		# initialize the model along with the input shape to be
		# "channels last" and the channels dimension itself
		model = Sequential()
		inputShape = (height, width, depth)
		chanDim = -1
 
		# if we are using "channels first", update the input shape
		# and channels dimension
		if K.image_data_format() == "channels_first":
			inputShape = (depth, height, width)
			chanDim = 1
Copy the code

Our class is defined in line 12, and the only build method is defined in line 14.

Build takes four parameters: width for the input image, height for the input image, height, depth, and class number.

Depth can also be thought of as the number of channels. Our image is in RGB color space, so we pass a depth of 3 when we call the Build method.

First, we initialize a sequential model (line 17).

Then, we determine the channel ordering. Keras supports “channels_last” (TensorFlow) and “channels_first” (Theano) sorting. Lines 18-25 allow our model to support any type of back end.

Now, let’s add some layers to the network:

# CONV => RELU => POOL layer set model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShape)) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) Model. The add (MaxPooling2D (pool_size = (2, 2))) model. The add (Dropout (0.25))Copy the code

This block adds the first CONV => RELU => POOL

Our first CONV layer has 32 filters of size 3×3.

It is important that we specify the first-level InputShape, because all subsequent hierarchical dimensions will be evaluated using the down-triggered method.

We will use ReLU (Rectilinear unit) activation functions in this network architecture. There are many activation methods, and I encourage you to familiarize yourself with the popular methods in Python’s “Deep Learning for Computer Vision,” which discuss the pros and cons.

Batch standardization, maximum pooling, and drop-outs are also applied.

Batch standardization is used to standardize the activation of a given input volume before passing it to the next layer in the network. It has proven to be very effective in reducing the amount of time required to train CNN as well as stabilizing the training itself.

The pool layer has the primary function of gradually reducing the space size (i.e. width and height) of the input volume to the layer. In CNN architecture, it is common to insert pool layers between successive CONV layers.

Dropout is an interesting concept that can’t be ignored. To force networks to be more stable, we can apply Dropout, the process of disconnecting random neuron connections between layers. This process has been shown to reduce overfitting, improve accuracy, and allow our network to better generalize over unfamiliar images. As shown in this parameter, 25% of node connections are randomly disconnected (quits) between layers during each training iteration.

Note: If you are new to deep learning, this may be a different language for you. Just like learning a new spoken language, it takes time, study and practice.

(CONV= > Relu) * 2 = >POOL:

# (CONV => RELU) * 2 => POOL layer set model.add(Conv2D(64, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) model.add(Conv2D(64, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) Model. The add (MaxPooling2D (pool_size = (2, 2))) model. The add (Dropout (0.25))Copy the code

Note that our filter size remains the same (3X3, which is a common problem such as networking); However, we increase the total number of filters from 32 to 64.

This is followed by a (CONV= > Relu= >POOL) * 3-tier collection:

# (CONV => RELU) * 3 => POOL layer set model.add(Conv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) model.add(Conv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) model.add(Conv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=chanDim)) Model. The add (MaxPooling2D (pool_size = (2, 2))) model. The add (Dropout (0.25))Copy the code

Again, notice how all CONV layers learn 3×3 filters, but the total number of filters learned by CONV layers increases from 64 to 128. As you go deeper in CNN (and as the volume of input gets smaller and smaller), it is common practice to increase the total number of filters.

Finally we have a set of FC= > Relu layers:

# first (and only) set of FC => RELU layers model.add(Flatten()) model.add(Dense(512)) model.add(Activation("relu")) Model. Add (BatchNormalization()) Model. Add (Dropout(0.5)) # Softmax classifier Model. Add (Dense(classes)) model.add(Activation("softmax")) # return the constructed network architecture return modelCopy the code

The fully connected layer is represented as dense Keras. The last layer is fully connected to the three outputs (because we have three classes in our dataset). The SoftMax layer returns the class probability for each tag.

Now that SmallVGGNet is implemented, let’s write a driver script that will be used to train it on our animal data set.

Most of the code here will be similar to the previous example, but I will:

  • Review the entire script completely
  • And eliminate all differences

Open the train_vgg.py script and let’s get started:

# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
 
# import the necessary packages
from pyimagesearch.smallvggnet import SmallVGGNet
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import pickle
import cv2
import os
Copy the code

The input is the same as our previous training script, with two exceptions:

Us through the from pyimagesearch. Smallvggnet import smallvggnet import is SmallVGGNetfrom rather than keras. Models import Sequential, scroll up slightly, To see the SmallVGGNet Implementation.

We will augment our data with ImageDataGenerator. Data augmentation is almost always recommended and leads to better generalizable models. Data enhancement involves adding random rotations, shifts, clipping, and scaling to existing training data. You won’t see a bunch of new.png and.jpg files — it’s done on the fly as the script executes.

At this point, you should be aware of the other imports. If not, please refer to the list above.

Let’s parse the command-line arguments:

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
	help="path to input dataset of images")
ap.add_argument("-m", "--model", required=True,
	help="path to output trained model")
ap.add_argument("-l", "--label-bin", required=True,
	help="path to output label binarizer")
ap.add_argument("-p", "--plot", required=True,
	help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
Copy the code

We have four command-line argument parses:

  • –dataset: Path to the image data set on disk. This could be the path of an animal/or another data set organized in the same way.
  • –model: Our model will be serialized and output to disk. This parameter contains the path to the output model file. Be sure to name your models accordingly so that you don’t overwrite any previously trained models (such as simple neural network models).
  • –labelbinThe: DataSet tag is serialized to disk for easy traceback in other scripts. This is the path to the output label binarizer file.
  • -plot: Output the path to the training drawing image file. We will examine this graph to check for over/under fitting of the data. Each time you change the training model using parameters, you should specify a different drawing file name on the command line so that you have a drawing history in your notebook or notebook file that corresponds to your training notes. This tutorial makes deep learning look easy, but keep in mind that I went through several iterations before deciding on all the parameters to share with you in this script.

Let’s load and preprocess our data:

# initialize the data and labels print("[INFO] loading images..." ) data = [] labels = [] # grab the image paths and randomly shuffle them imagePaths = sorted(list(paths.list_images(args["dataset"]))) random.seed(42) random.shuffle(imagePaths) # loop over the input images  for imagePath in imagePaths: # load the image, resize it to 64x64 pixels (the required input # spatial dimensions of SmallVGGNet), and store the image in the # data list image = cv2.imread(imagePath) image = cv2.resize(image, (64, 64)) data.append(image) # extract the class label from the image path and update the # labels list label = imagePath.split(os.path.sep)[-2] labels.append(label) # scale the raw pixel intensities to the range [0, 1] data = np.array(data, dtype="float") / 255.0 labels = Np.array (labels)Copy the code

As with a simple neural network script, here we:

  1. Initialize our list of data and tags (lines 35 and 36).
  2. Grab images and shuffle cards randomly (lines 31-41). Before we sort and shuffle,paths.list_imageFunction will easily find all images in our input dataset directory.
  3. Start looping through all of the data set (line 44)imagepaths.

As we loop over each image path, we do:

  1. Load the image into memory (line 48).
  2. Adjust the image to64 x 64, this isSmallVGGNetDimensions of input space required (line 49). One key difference is that we don’t flatten our neural network data because it’s convolution.
  3. Will be resizedimageAppended to theData (Line 50).
  4. fromimagepathClass to extract images fromlabelAnd add it toLabels are listedTable (lines 54 and 55).

In line 58, we scale pixel intensity in array form from the range [0, 255] to [0, 1].

We also convert the label list to Numpy array format (line 59).

We will then split the data and binary the labels:

# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data,
	labels, test_size=0.25, random_state=42)
 
# convert the labels from integers to vectors (for 2-class, binary
# classification you should use Keras' to_categorical function
# instead as the scikit-learn's LabelBinarizer will not return a
# vector)
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
Copy the code

We performed 75/25 training and test splits on the data (lines 63 and 64). One experiment I encourage you to try is to divide the training into 80/20 classes and see if the results change significantly.

Tag binarization occurs on lines 70-72. This allows for a hot encoding and a label binalizer to be serialized to the pickle file later in the script.

Now for data augmentation:

# construct the image generator for data augmentation aug = ImageDataGenerator(rotation_range=30, Width_shift_range =0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") # initialize our VGG-like Convolutional Neural Network model = SmallVGGNet.build(width=64, height=64, depth=3, classes=len(lb.classes_))Copy the code

In lines 75-77, we initialize our image data generator to perform image enhancement.

Image enhancement allows us to construct “additional” training data from existing training data by randomly rotating, shifting, clipping, scaling, and flipping.

Data augmentation is usually a critical step:

  1. Avoid overfitting
  2. Make sure the model is well generalized

I recommend that you always perform data augmentation unless you have a clear reason not to.

To build our SmallVGGNet, we simply call Smallvggnet.build, passing the necessary parameters (lines 80 and 81).

Let’s compile and train our model:

# initialize our initial learning rate, # of epochs to train for, # and batch size INIT_LR = 0.01 EPOCHS = 75 BS = 32 # initialize the model and optimizer (you'll want to use # binary_crossentropy for 2-class classification) print("[INFO] training network..." ) opt = SGD(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS), validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS, epochs=EPOCHS)Copy the code

First, we establish our learning rate, number of times, and batch size (lines 85-87).

We then initialize our stochastic gradient Descent (SGD) optimizer (line 92).

We are now ready to compile and train our model (lines 93-99). Because we are performing data enhancement, we call it model.FIT_generator (not Model.fit). We must take the generator’s training data as the first argument. The generator will generate batch augmented training data based on the setup we made earlier.

Finally, we will evaluate our model, plot the loss/accuracy curve, and save the model:

# evaluate the network print("[INFO] evaluating network..." ) predictions = model.predict(testX, batch_size=32) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=lb.classes_)) # plot the training loss and accuracy N = np.arange(0, EPOCHS) plt.style.use("ggplot") plt.figure() plt.plot(N, H.history["loss"], label="train_loss") plt.plot(N, H.history["val_loss"], label="val_loss") plt.plot(N, H.history["acc"], label="train_acc") plt.plot(N, H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy (SmallVGGNet)") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy")  plt.legend() plt.savefig(args["plot"]) # save the model and label binarizer to disk print("[INFO] serializing network and label binarizer..." ) model.save(args["model"]) f = open(args["label_bin"], "wb") f.write(pickle.dumps(lb)) f.close()Copy the code

We predict the test set, then use Scikit-Learning to calculate and print our ClassiFICation_report (lines 103-105).

Draw loss/accuracy curves using Matplotlib – lines 108 to 118 show my typical drawing Settings. Line 119 saves the graph to disk.

Finally, we save our model and label binalizers to disk (lines 123-126).

Let’s keep training our model.

Make sure you have used the Downloads section of this blog post to download the source code and sample dataset.

Open a terminal from there and execute the following command:

$ python train_vgg.py --dataset animals --model output/smallvggnet.model \ --label-bin output/smallvggnet_lb.pickle \ --plot output/smallvggnet_plot.png Using TensorFlow backend. [INFO] loading images... [INFO] training network... Epoch 1/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 3 s - loss: 1.3783 acc: 0.5165 - val_loss: 2.3654 - val_acc: 0.3133 Epoch 2/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 2 s - loss: 1.0382 acc: 0.5998 - val_loss: 2.7962 - val_acc: 0.3173 Epoch 3/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 2 s - loss: 0.9366 acc: 0.6018-val_loss: 2.2790-val_ACC: 0.3173... Epoch 73/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 2 s - loss: 0.4402 acc: 0.8044 - val_loss: 0.4975 - val_acc: 0.7880 Epoch 74/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 2 s - loss: 0.4306 acc: 0.8055 - val_loss: 0.6150 - val_acc: 0.7520 Epoch 75/75 70/70 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 2 s - loss: 0.4179 acc: 0.8110-val_loss: 0.5624 - val_ACC: 0.7653 [INFO] Evaluating network... Precision recall F1-Score Support cats 0.62 0.84 0.71 236 dogs 0.75 0.50 0.60 236 panda 0.95 0.92 0.93 278 AVG/total 0.77 0.76 750 [INFO] serializing network and label binarizer...Copy the code

When you paste a command, make sure you have all the command line arguments to avoid “use” errors. If the command line arguments are new, be sure to read them before continuing.

Training on the CPU takes some time – each of the EPOCH 75’s takes more than a minute. Training will take more than an hour.

A GPU will complete this process in minutes, as it only takes 2 seconds per era, as demonstrated!

Let’s look at the training diagram generated in the output/directory:

Figure 12: Our deep learning of Keras accuracy/loss plots shows that we achieved 78% accuracy on the animal data using the SmallVGNet model

As our results show, you can see that we achieved 78% accuracy on the animal data set using the convolutional neural network, significantly higher than the 61% accuracy achieved using the standard fully connected network.

We can also use our newly trained Keras CNN as an example:

$ python predict.py --image images/panda.jpg --model output/smallvggnet.model \ --label-bin output/smallvggnet_lb.pickle  --width 64 --height 64 Using TensorFlow backend. [INFO] loading network and label binarizer...Copy the code

Figure 13: Our deep learning with KARAS tutorial has shown how can we confidently recognize a panda in an image

Here at CNN, we’re pretty confident it’s a panda. Me too, but I just wish he’d stop staring at me!

Let’s try a cute beagle:

$ python predict.py --image images/dog.jpg --model output/smallvggnet.model \
	--label-bin output/smallvggnet_lb.pickle --width 64 --height 64
Using TensorFlow backend.
[INFO] loading network and label binarizer...
Copy the code

Figure 14: Hounds are identified as dogs, using KARAS, TysFooSt and Python. Our KARAS tutorial already covers the basics for deep learning, but it only scratches the surface of the field

A comparison dog was part of my family and childhood. I’m glad this picture of a beagle I found online was recognized as a dog!

I can use a similar CNN to find my hounds dog photos on my computer.

In fact, in Google Photos, if you type “dog” into the search box, images of dogs in your photo library will be returned — I’m sure CNN is already a feature of the image search engine. Image search engines aren’t the only use case for CNN — I bet your brain is already coming up with all sorts of ideas for applying deep learning.

conclusion

In today’s tutorial, you learned how to get started with Keras, deep learning, and Python.

Specifically, you learned seven key steps to using Keras and your own custom dataset:

  1. How do I load data from disk
  2. How do I create training and test branches
  3. How do YOU define the Keras model architecture
  4. How do I compile and write the Keras model
  5. How to train your training data model
  6. How to evaluate the model of test data
  7. How to make predictions using trained Keras models

You also learned how to implement convolutional neural networks that allow you to achieve greater accuracy than standard fully connected networks.


Keras Tutorial: How to Get started with Keras, Deep Learning, and Python

Translation: Xu Dabai