State of the Art Convolutional Neural Network Binary Classification
ten Minutes to Edifice a CNN Binary Image Classifier in TensorFlow
How to build a binary image classifier using convolutional neural network layers in TensorFlow/Keras
This is a short introduction to calculator vision — namely, how to build a binary image classifier using convolutional neural network layers in TensorFlow/Keras, geared mainly towards new users. This easy-to-follow tutorial is cleaved down into 3 sections:
- The data
- The model architecture
- The accuracy, ROC curve, and AUC
Requirements: Zip! All you need to follow this tutorial is this Google Colab notebook containing the data and code. Google Colab allows yous to write and run Python code in-browser without any setup, and includes gratis GPU access!
one. The Data
We're going to build a dandelion and grass image classifier. I've created a small image dataset using images from Google Images, which you tin can download and parse in the kickoff 8 cells of the tutorial.
By the terminate of those 8 lines, visualizing a sample of your image dataset will look something like this:
Annotation how some of the images in the dataset aren't perfect representations of grass or dandelions. For simplicity's sake, allow's make this okay and move on to how to easily create our training and validation dataset.
The data that we fetched before is divided into ii folders, train and valid. In those folders, the foldersdandelion and grass incorporate the images of each class. To create a dataset, let's apply the keras.preprocessing.paradigm.ImageDataGenerator grade to create our grooming and validation dataset and normalize our information. What this class does is create a dataset and automatically does the labeling for us, allowing us to create a dataset in just one line!
2. The Model Architecture
At the beginning of this section, we first import TensorFlow.
Let'south then add our CNN layers. We'll start add together a convolutional 2d layer with 16 filters, a kernel of 3x3, the input size as our epitome dimensions, 200x200x3, and the activation as ReLU.
tf.keras.layers.Conv2D(xvi, (3,three), activation='relu', input_shape=(200, 200, 3)) Afterward that, nosotros'll add a max pooling layer that halves the epitome dimension, then afterward this layer, the output will be 100x100x3.
tf.keras.layers.MaxPooling2D(two, 2) We volition stack five of these layers together, with each subsequent CNN adding more than filters.
Finally, nosotros'll flatten the output of the CNN layers, feed information technology into a fully-connected layer, and and then to a sigmoid layer for binary classification.
Here is the model that we have built:
model = tf.keras.models.Sequential([ # Note the input shape is the desired size of the image 200x200 with three bytes color # This is the first convolution tf.keras.layers.Conv2D(xvi, (3,3), activation='relu', input_shape=(200, 200, 3)), tf.keras.layers.MaxPooling2D(2, 2), # The second convolution tf.keras.layers.Conv2D(32, (3,three), activation='relu'), tf.keras.layers.MaxPooling2D(two,two), # The third convolution tf.keras.layers.Conv2D(64, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(two,2), # The fourth convolution tf.keras.layers.Conv2D(64, (iii,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), # # The fifth convolution tf.keras.layers.Conv2D(64, (3,iii), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), # Flatten the results to feed into a DNN tf.keras.layers.Flatten(), # 512 neuron hidden layer tf.keras.layers.Dense(512, activation='relu'), # Only one output neuron. It will incorporate a value from 0-1 where 0 for 1 grade ('dandelions') and 1 for the other ('grass') tf.keras.layers.Dense(i, activation='sigmoid')
Let's meet a summary of the model we have built:
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 198, 198, xvi) 448 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 99, 99, 16) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 97, 97, 32) 4640 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 48, 48, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 46, 46, 64) 18496 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 23, 23, 64) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 21, 21, 64) 36928 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, x, x, 64) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 8, eight, 64) 36928 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 4, 4, 64) 0 _________________________________________________________________ flatten (Flatten) (None, 1024) 0 _________________________________________________________________ dumbo (Dense) (None, 512) 524800 _________________________________________________________________ dense_1 (Dense) (None, ane) 513 ================================================================= Total params: 622,753 Trainable params: 622,753 Non-trainable params: 0 Next, we'll configure the specifications for model grooming. We will railroad train our model with the binary_crossentropy loss. Nosotros will utilize the RMSProp optimizer. RMSProp is a sensible optimization algorithm because information technology automates learning-rate tuning for us (alternatively, nosotros could also use Adam or Adagrad for similar results). We will add accuracy to metrics and then that the model volition monitor accuracy during training.
model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001), metrics='accurateness')
Let's train for 15 epochs:
history = model.fit(train_generator, steps_per_epoch=eight, epochs=15, verbose=ane, validation_data = validation_generator, validation_steps=8)
iii. The Accuracy, ROC Curve, and AUC
Let's evaluate the accurateness of our model:
model.evaluate(validation_generator) Now, allow'southward summate our ROC curve and plot it.
Offset, permit'south make predictions on our validation set. When using generators to make predictions, nosotros must offset turn off shuffle (as we did when nosotros created validation_generator) and reset the generator:
STEP_SIZE_TEST=validation_generator.due north//validation_generator.batch_size validation_generator.reset() preds = model.predict(validation_generator, verbose=one)
To create the ROC curve and AUC, we'll demand to compute the simulated-positive rate and the true-positive rate:
fpr, tpr, _ = roc_curve(validation_generator.classes, preds) roc_auc = auc(fpr, tpr) plt.effigy() lw = 2 plt.plot(fpr, tpr, colour='darkorange', lw=lw, label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, one], [0, 1], colour='navy', lw=lw, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating feature example') plt.fable(loc="lower right") plt.show()
The ROC curve is a probability bend plotting the true-positive rate (TPR) against the false-positive rate (FPR).
Similarly, the AUC (area under curve), every bit shown in the legend above, measures how much our model is capable of distinguishing between our 2 classes, dandelions and grass. It is too used to compare different models, which I will do in future tutorials when I present how to build an image classifier using fully-connected layers and also transfer learning with ResNet!
Finally, at the stop of the notebook, yous'll have a chance to make predictions on your own images!
I hope this gives yous a gentle introduction to building a simple binary image classifier using CNN layers. If yous are interested in similar easy-to-follow, no-nonsense tutorials like this, please check out my other stories!
Source: https://towardsdatascience.com/10-minutes-to-building-a-cnn-binary-image-classifier-in-tensorflow-4e216b2034aa
Postar um comentário for "State of the Art Convolutional Neural Network Binary Classification"