In this tutorial, you will learn about contrastive loss and how it can be used to train more accurate siamese neural networks. We will implement contrastive loss using Keras and TensorFlow.
Previously, I authored a three-part series on the fundamentals of siamese neural networks:
- Building image pairs for siamese networks with Python
- Siamese networks with Keras, TensorFlow, and Deep Learning
- Comparing images for similarity using siamese networks, Keras, and TenorFlow
This series covered the fundamentals of siamese networks, including:
- Generating image pairs
- Implementing the siamese neural network architecture
- Using binary cross-entry to train the siamese network
But while binary cross-entropy is certainly a valid choice of loss function, itās not the only choice (or even the best choice).
State-of-the-art siamese networks tend to use some form of either contrastive loss or triplet loss when training ā these loss functions are better suited for siamese networks and tend to improve accuracy.
By the end of this guide, you will understand how to implement siamese networks and then train them with contrastive loss.
To learn how to train a siamese neural network with contrastive loss, just keep reading.
Looking for the source code to this post?
Jump Right To The Downloads SectionContrastive Loss for Siamese Networks with Keras and TensorFlow
In the first part of this tutorial, we will discuss what contrastive loss is and, more importantly, how it can be used to more accurately and effectively train siamese neural networks.
Weāll then configure our development environment and review our project directory structure.
We have a number of Python scripts to implement today, including:
- A configuration file
- Helper utilities for generating image pairs, plotting training history, and implementing custom layers
- Our contrastive loss implementation
- A training script
- A testing/inference script
Weāll review each of these scripts; however, some of them have been covered in my previous guides on siamese neural networks, so when appropriate Iāll refer you to my other tutorials for additional details.
Weāll also spend a considerable amount of time discussing our contrastive loss implementation, ensuring you understand what itās doing, how it works, and why we are utilizing it.
By the end of this tutorial, you will have a fully functioning contrastive loss implementation that is capable of training a siamese neural network.
What is contrastive loss? And how can contrastive loss be used to train siamese networks?
In our previous series of tutorials on siamese neural networks, we learned how to train a siamese network using the binary cross-entropy loss function:
Binary cross-entropy was a valid choice here because what weāre essentially doing is 2-class classification:
- Either the two images presented to the network belong to the same class
- Or the two images belong to different classes
Framed in that manner, we have a classification problem. And since we only have two classes, binary cross-entropy makes sense.
However, there is actually a loss function much better suited for siamese networks called contrastive loss:
Paraphrasing Harshvardhan Gupta, we need to keep in mind that the goal of a siamese network isnāt to classify a set of image pairs but instead to differentiate between them. Essentially, contrastive loss is evaluating how good a job the siamese network is distinguishing between the image pairs. The difference is subtle but incredibly important.
To break this equation down:
- The value is our label. It will be if the image pairs are of the same class, and it will be if the image pairs are of a different class.
- The variable is the Euclidean distance between the outputs of the sister network embeddings.
- The max function takes the largest value of and the margin, , minus the distance.
Weāll be implementing this loss function using Keras and TensorFlow later in this tutorial.
If you would like more mathematically motivated details on contrastive loss, be sure to refer to Hadsell et al.ās paper, Dimensionality Reduction by Learning an Invariant Mapping.
Configuring your development environment
This series of tutorials on siamese networks utilizes Keras and TensorFlow. If you intend on following this tutorial on the previous two parts in this series, I suggest you take the time now to configure your deep learning development environment.
You can utilize either of these two guides to install TensorFlow and Keras on your system:
Either tutorial will help you configure your system with all the necessary software for this blog post in a convenient Python virtual environment.
Having problems configuring your development environment?
All that said, are you:
- Short on time?
- Learning on your employerās administratively locked system?
- Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?
- Ready to run the code right now on your Windows, macOS, or Linux system?
Then join PyImageSearch Plus today!
Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are pre-configured to run on Google Colabās ecosystem right in your web browser! No installation required.
And best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux!
Project structure
Todayās tutorial on contrastive loss on siamese networks builds on my three previous tutorials that cover the fundamentals of building image pairs, implementing and training siamese networks, and using siamese networks for inference:
- Building image pairs for siamese networks with Python
- Siamese networks with Keras, TensorFlow, and Deep Learning
- Comparing images for similarity using siamese networks, Keras, and TensorFlow
Weāll be building on the knowledge we gained from those guides (including the project directory structure itself) today, so consider the previous guides required reading before continuing today.
Once youāve gotten caught up, we can proceed to review our project directory structure:
$ tree . --dirsfirst . āāā examples ā āāā image_01.png ā āāā image_02.png ā āāā image_03.png ... ā āāā image_13.png āāā output ā āāā contrastive_siamese_model ā ā āāā assets ā ā āāā variables ā ā ā āāā variables.data-00000-of-00001 ā ā ā āāā variables.index ā ā āāā saved_model.pb ā āāā contrastive_plot.png āāā pyimagesearch ā āāā config.py ā āāā metrics.py ā āāā siamese_network.py ā āāā utils.py āāā test_contrastive_siamese_network.py āāā train_contrastive_siamese_network.py 6 directories, 23 files
Inside the
module youāll find four Python files:pyimagesearch
: Contains our configuration of important variables, including batch size, epochs, output file paths, etc.config.py
: Holds our implementation of themetrics.py
functioncontrastive_loss
: Contains the siamese network model architecturesiamese_network.py
utils.py
: Includes helper utilities, including a function to generate image pairs, compute the Euclidean distance as a layer inside of a CNN, and a training history plotting function
We then have two Python driver scripts:
: Trains our siamese neural network using contrastive loss and serializes the training history and model weights/architecture to disk inside thetrain_contrastive_siamese_network.py
directoryoutput
: Loads our trained siamese network from disk and applies it to image pairs from inside thetest_contrastive_siamse_network.py
directoryexamples
Again, I cannot stress the importance of reviewing my previous series of tutorials on siamese networks. Doing so is an absolute requirement before continuing here today.
Implementing our configuration file
Our configuration file holds important variables used to train our siamese network with contrastive loss.
Open up the
file in your project directory structure, and letās take a look inside:config.py
# import the necessary packages import os # specify the shape of the inputs for our network IMG_SHAPE = (28, 28, 1) # specify the batch size and number of epochs BATCH_SIZE = 64 EPOCHS = 100 # define the path to the base output directory BASE_OUTPUT = "output" # use the base output path to derive the path to the serialized # model along with training history plot MODEL_PATH = os.path.sep.join([BASE_OUTPUT, "contrastive_siamese_model"]) PLOT_PATH = os.path.sep.join([BASE_OUTPUT, "contrastive_plot.png"])
Line 5 sets our
dimensions. Weāll be working with the MNIST digits dataset, which has 28×28 grayscale (i.e., single channel) images.IMG_SHAPE
We then set our BATCH_SIZE
and number of EPOCHS
to train before. These parameters were experimentally tuned.
Lines 16-19 define the output file paths for both our serialized model and training history.
For more details on the configuration file, refer to my tutorial on Siamese networks with Keras, TensorFlow, and Deep Learning.
Creating our helper utility functions
In order to train our siamese network model, weāll need three helper utilities:
: Generates a set of image pairs from the MNIST dataset that will serve as our training setmake_pairs
: A custom layer implementation that computes the Euclidean distance between two volumes inside of a CNNeuclidean_distance
plot_training
: Plots the training and validation contrastive loss over the course of the training process
Letās start off with our imports:
# import the necessary packages import tensorflow.keras.backend as K import matplotlib.pyplot as plt import numpy as np
We then have our make_pairs
function, which I discussed in detail in my Building image pairs for siamese networks with Python tutorial (make sure you read that guide before continuing):
def make_pairs(images, labels): # initialize two empty lists to hold the (image, image) pairs and # labels to indicate if a pair is positive or negative pairImages = [] pairLabels = [] # calculate the total number of classes present in the dataset # and then build a list of indexes for each class label that # provides the indexes for all examples with a given label numClasses = len(np.unique(labels)) idx = [np.where(labels == i)[0] for i in range(0, numClasses)] # loop over all images for idxA in range(len(images)): # grab the current image and label belonging to the current # iteration currentImage = images[idxA] label = labels[idxA] # randomly pick an image that belongs to the *same* class # label idxB = np.random.choice(idx[label]) posImage = images[idxB] # prepare a positive pair and update the images and labels # lists, respectively pairImages.append([currentImage, posImage]) pairLabels.append([1]) # grab the indices for each of the class labels *not* equal to # the current label and randomly pick an image corresponding # to a label *not* equal to the current label negIdx = np.where(labels != label)[0] negImage = images[np.random.choice(negIdx)] # prepare a negative pair of images and update our lists pairImages.append([currentImage, negImage]) pairLabels.append([0]) # return a 2-tuple of our image pairs and labels return (np.array(pairImages), np.array(pairLabels))
Iāve already covered this function in detail previously, but the gist here is that:
- In order to train siamese networks, we need examples of positive and negative image pairs
- A positive pair is two images that belong to the same class (i.e., two examples of the digit ā8ā)
- A negative pair is two images that belong to different classes (i.e., one image containing a ā1ā and the other image containing a ā3ā)
- The
function accepts an input set ofmake_pairs
images
and associatedlabels
and then constructs the positive and negative image pairs
The next function, euclidean_distance
, accepts a 2-tuple of vectors
and then computes the Euclidean distance between them, utilizing Keras/TensorFlow functions such that the Euclidean distance can be computed inside the siamese neural network:
def euclidean_distance(vectors): # unpack the vectors into separate lists (featsA, featsB) = vectors # compute the sum of squared distances between the vectors sumSquared = K.sum(K.square(featsA - featsB), axis=1, keepdims=True) # return the euclidean distance between the vectors return K.sqrt(K.maximum(sumSquared, K.epsilon()))
Finally, we have a helper utility, plot_training
, which accepts a plotPath
, plots our training and validation contrastive loss over the course of training, and then saves the plot to disk:
def plot_training(H, plotPath): # construct a plot that plots and saves the training history plt.style.use("ggplot") plt.figure() plt.plot(H.history["loss"], label="train_loss") plt.plot(H.history["val_loss"], label="val_loss") plt.title("Training Loss") plt.xlabel("Epoch #") plt.ylabel("Loss") plt.legend(loc="lower left") plt.savefig(plotPath)
Letās move on to implementing the siamese network architecture itself.
Implementing our siamese network architecture
Our siamese neural network architecture is essentially a basic CNN:
# import the necessary packages from tensorflow.keras.models import Model from tensorflow.keras.layers import Input from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Dropout from tensorflow.keras.layers import GlobalAveragePooling2D from tensorflow.keras.layers import MaxPooling2D def build_siamese_model(inputShape, embeddingDim=48): # specify the inputs for the feature extractor network inputs = Input(inputShape) # define the first set of CONV => RELU => POOL => DROPOUT layers x = Conv2D(64, (2, 2), padding="same", activation="relu")(inputs) x = MaxPooling2D(pool_size=(2, 2))(x) x = Dropout(0.3)(x) # second set of CONV => RELU => POOL => DROPOUT layers x = Conv2D(64, (2, 2), padding="same", activation="relu")(x) x = MaxPooling2D(pool_size=2)(x) x = Dropout(0.3)(x) # prepare the final outputs pooledOutput = GlobalAveragePooling2D()(x) outputs = Dense(embeddingDim)(pooledOutput) # build the model model = Model(inputs, outputs) # return the model to the calling function return model
You can refer to my tutorial on Siamese networks with Keras, TensorFlow, and Deep Learning for more details on the model architecture and implementation.
Implementing contrastive loss with Keras and TensorFlow
With our helper utilities and model architecture implemented, we can move on to defining the
function in Keras/TensorFlow.contrastive_loss
For reference, here is the equation for the contrastive loss function that weāll be implementing in Keras/TensorFlow code:
The full implementation of contrastive loss is concise, spanning only 18 lines, including comments:
# import the necessary packages import tensorflow.keras.backend as K import tensorflow as tf def contrastive_loss(y, preds, margin=1): # explicitly cast the true class label data type to the predicted # class label data type (otherwise we run the risk of having two # separate data types, causing TensorFlow to error out) y = tf.cast(y, preds.dtype) # calculate the contrastive loss between the true labels and # the predicted labels squaredPreds = K.square(preds) squaredMargin = K.square(K.maximum(margin - preds, 0)) loss = K.mean(y * squaredPreds + (1 - y) * squaredMargin) # return the computed contrastive loss to the calling function return loss
Line 5 defines our contrastive_loss
function, which accepts three arguments, two of which are required and the third optional:
y
: The ground-truth labels from our dataset. A value of
indicates that the two images in the pair are of the same class, while a value of1
indicates that the images belong to two different classes.0
: The predictions from our siamese network (i.e., distances between the image pairs).preds
: Margin used for the contrastive loss function (typically this value is set tomargin
).1
Line 9 ensures our ground-truth labels are of the same data type as our
. Failing to perform this explicit casting may result in TensorFlow erroring out when we try to perform mathematical operations on preds
y
and preds
.
We then proceed to compute the contrastive loss by:
- Taking the square of the
preds
(Line 13) - Computing the
squaredMargin
, which is the square of the maximum value of either0
ormargin - preds
(Line 14) - Computing the final
loss
(Line 15)
The computed contrastive loss
value is then returned to the calling function.
I suggest you review the āWhat is contrastive loss? And how can contrastive loss be used to train siamese networks?ā section above and compare our implementation to the equation so you can better understand how contrastive loss is implemented.
Creating our contrastive loss training script
We are now ready to implement our training script! This script is responsible for:
- Loading the MNIST digits dataset from disk
- Preprocessing it and constructing image pairs
- Instantiating the siamese neural network architecture
- Training the siamese network with contrastive loss
- Serializing both the trained network and training history plot to disk
The majority of this code is identical to our previous post on Siamese networks with Keras, TensorFlow, and Deep Learning, so while Iām still going to cover our implementation in full, Iām going to defer a detailed discussion to the previous post (and of course, pointing out the details along the way).
Open up the
file in your project directory structure, and letās get to work:train_contrastive_siamese_network.py
# import the necessary packages from pyimagesearch.siamese_network import build_siamese_model from pyimagesearch import metrics from pyimagesearch import config from pyimagesearch import utils from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Input from tensorflow.keras.layers import Lambda from tensorflow.keras.datasets import mnist import numpy as np
Lines 2-11 import our required Python packages. Note how we are importing the
submodule of metrics
pyimagesearch
, which contains our
implementation.contrastive_loss
From there we can load the MNIST dataset from disk:
# load MNIST dataset and scale the pixel values to the range of [0, 1] print("[INFO] loading MNIST dataset...") (trainX, trainY), (testX, testY) = mnist.load_data() trainX = trainX / 255.0 testX = testX / 255.0 # add a channel dimension to the images trainX = np.expand_dims(trainX, axis=-1) testX = np.expand_dims(testX, axis=-1) # prepare the positive and negative pairs print("[INFO] preparing positive and negative pairs...") (pairTrain, labelTrain) = utils.make_pairs(trainX, trainY) (pairTest, labelTest) = utils.make_pairs(testX, testY)
Line 15 loads the MNIST dataset with the pre-supplied training and testing splits.
We then preprocess the dataset by:
- Scaling the input pixel intensities in the images from the range [0, 255] to [0, 1] (Lines 16 and 17)
- Adding a channel dimension (Lines 20 and 21)
- Constructing our image pairs (Lines 25 and 26)
Next, we can instantiate the siamese network architecture:
# configure the siamese network print("[INFO] building siamese network...") imgA = Input(shape=config.IMG_SHAPE) imgB = Input(shape=config.IMG_SHAPE) featureExtractor = build_siamese_model(config.IMG_SHAPE) featsA = featureExtractor(imgA) featsB = featureExtractor(imgB) # finally, construct the siamese network distance = Lambda(utils.euclidean_distance)([featsA, featsB]) model = Model(inputs=[imgA, imgB], outputs=distance)
Lines 30-34 create our sister networks:
- We start by creating two inputs, one for each image in the image pair (Lines 30 and 31).
- We then build the sister network architecture, which acts as our feature extractor (Line 32).
- Each image in the pair will be passed through our feature extractor, resulting in a vector that quantifies each image (Lines 33 and 34).
Using the 48-d vector generated by the sister networks, we proceed to compute the euclidean_distance
between our two vectors (Line 37) ā this distance serves as our output from the siamese network:
- The smaller the distance is, the more similar the two images are.
- The larger the distance is, the less similar the images are.
Line 38 defines the model
by specifying imgA
and imgB
, our two images in the image pair, as inputs, and our distance
layer as the output.
Finally, we can train our siamese network using contrastive loss:
# compile the model print("[INFO] compiling model...") model.compile(loss=metrics.contrastive_loss, optimizer="adam") # train the model print("[INFO] training model...") history = model.fit( [pairTrain[:, 0], pairTrain[:, 1]], labelTrain[:], validation_data=([pairTest[:, 0], pairTest[:, 1]], labelTest[:]), batch_size=config.BATCH_SIZE, epochs=config.EPOCHS) # serialize the model to disk print("[INFO] saving siamese model...") model.save(config.MODEL_PATH) # plot the training history print("[INFO] plotting training history...") utils.plot_training(history, config.PLOT_PATH)
Line 42 compiles our model architecture using the
function.contrastive_loss
We then proceed to train the model using our training/validation image pairs (Lines 46-50) and then serialize the model to disk (Line 54) and plot the training history (Line 58).
Training a siamese network with contrastive loss
We are now ready to train our siamese neural network with contrastive loss using Keras and TensorFlow.
Make sure you use the āDownloadsā section of this guide to download the source code, helper utilities, and contrastive loss implementation.
From there, you can execute the following command:
$ python train_contrastive_siamese_network.py [INFO] loading MNIST dataset... [INFO] preparing positive and negative pairs... [INFO] building siamese network... [INFO] compiling model... [INFO] training model... Epoch 1/100 1875/1875 [==============================] - 81s 43ms/step - loss: 0.2038 - val_loss: 0.1755 Epoch 2/100 1875/1875 [==============================] - 80s 43ms/step - loss: 0.1756 - val_loss: 0.1571 Epoch 3/100 1875/1875 [==============================] - 80s 43ms/step - loss: 0.1619 - val_loss: 0.1394 Epoch 4/100 1875/1875 [==============================] - 81s 43ms/step - loss: 0.1548 - val_loss: 0.1356 Epoch 5/100 1875/1875 [==============================] - 81s 43ms/step - loss: 0.1501 - val_loss: 0.1262 ... Epoch 96/100 1875/1875 [==============================] - 81s 43ms/step - loss: 0.1264 - val_loss: 0.1066 Epoch 97/100 1875/1875 [==============================] - 80s 43ms/step - loss: 0.1262 - val_loss: 0.1100 Epoch 98/100 1875/1875 [==============================] - 82s 44ms/step - loss: 0.1262 - val_loss: 0.1078 Epoch 99/100 1875/1875 [==============================] - 81s 43ms/step - loss: 0.1268 - val_loss: 0.1067 Epoch 100/100 1875/1875 [==============================] - 80s 43ms/step - loss: 0.1261 - val_loss: 0.1107 [INFO] saving siamese model... [INFO] plotting training history...
Each epoch took ~80 seconds on my 3 GHz Intel Xeon W processor. Training would be even faster with a GPU.
Our training history can be seen in Figure 7. Notice how our validation loss is actually lower than our training loss, a phenomenon that I discuss in this tutorial.
Having our validation loss lower than our training loss implies that we can ātrain harderā to improve our siamese network accuracy, typically by relaxing regularization constraints, deepening the model, and using a more aggressive learning rate.
But for now, our training model is more than sufficient.
Implementing our contrastive loss test script
The final script we need to implement is
. This script is essentially identical to the one covered in our previous tutorial on Comparing images for similarity using siamese networks, Keras, and TensorFlow, so while Iāll still cover the script in its entirety today, Iāll defer a detailed discussion to my previous guide.test_contrastive_siamese_network.py
Letās get started:
# import the necessary packages from pyimagesearch import config from pyimagesearch import utils from tensorflow.keras.models import load_model from imutils.paths import list_images import matplotlib.pyplot as plt import numpy as np import argparse import cv2
Lines 2-9 import our required Python packages.
Weāll be using
to load our serialized siamese network from disk. The load_model
function will be used to grab image paths and facilitate building sample image pairs.list_images
Letās move on to our command line arguments:
# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--input", required=True, help="path to input directory of testing images") args = vars(ap.parse_args())
The only command line argument we need is --input
, the path to our directory containing sample images we want to build pairs from (i.e., the examples
directory in our project directory).
Speaking of building image pairs, letās do that now:
# grab the test dataset image paths and then randomly generate a # total of 10 image pairs print("[INFO] loading test dataset...") testImagePaths = list(list_images(args["input"])) np.random.seed(42) pairs = np.random.choice(testImagePaths, size=(10, 2)) # load the model from disk print("[INFO] loading siamese model...") model = load_model(config.MODEL_PATH, compile=False)
Line 20 grabs the paths to all images in our --input
directory. We then randomly generate a total of 10 pairs of images (Line 22).
Line 26 loads our trained siamese network from disk.
With the siamese network loaded from disk, we can now compare images:
# loop over all image pairs for (i, (pathA, pathB)) in enumerate(pairs): # load both the images and convert them to grayscale imageA = cv2.imread(pathA, 0) imageB = cv2.imread(pathB, 0) # create a copy of both the images for visualization purpose origA = imageA.copy() origB = imageB.copy() # add channel a dimension to both the images imageA = np.expand_dims(imageA, axis=-1) imageB = np.expand_dims(imageB, axis=-1) # add a batch dimension to both images imageA = np.expand_dims(imageA, axis=0) imageB = np.expand_dims(imageB, axis=0) # scale the pixel values to the range of [0, 1] imageA = imageA / 255.0 imageB = imageB / 255.0 # use our siamese model to make predictions on the image pair, # indicating whether or not the images belong to the same class preds = model.predict([imageA, imageB]) proba = preds[0][0]
Line 29 loops over all pairs
. For each pair, we:
- Load the two images from disk (Lines 31 and 32)
- Clone the images such that we can visualize/draw on them (Lines 35 and 36)
- Add a channel dimension to both images, a requirement for inference (Lines 39 and 40)
- Add a batch dimension to the images, again, a requirement for inference (Lines 43 and 44)
- Scale the pixel intensities from the range [0, 255] to [0, 1], just like we did during training
The image pairs are then passed through our siamese network on Lines 52 and 53, resulting in the computed Euclidean distance between the vectors generated by the sister networks.
Again, keep in mind that the smaller the distance is, the more similar the two images are. Conversely, the larger the distance, the less similar the images are.
The final code block handles visualizing the two images in the pair along with their computed distance:
# initialize the figure fig = plt.figure("Pair #{}".format(i + 1), figsize=(4, 2)) plt.suptitle("Distance: {:.2f}".format(proba)) # show first image ax = fig.add_subplot(1, 2, 1) plt.imshow(origA, cmap=plt.cm.gray) plt.axis("off") # show the second image ax = fig.add_subplot(1, 2, 2) plt.imshow(origB, cmap=plt.cm.gray) plt.axis("off") # show the plot plt.show()
Congratulations on implementing an inference script for siamese networks! For more details on this implementation, refer to my previous tutorial, Comparing images for similarity using siamese networks, Keras, and TensorFlow.
Making predictions using our siamese network with contrastive loss model
Letās put our
script to work. Make sure you use the āDownloadsā section of this tutorial to download the source code, pre-trained model, and example images.test_contrastive_siamse_network.py
From there, you can run the following command:
$ python test_contrastive_siamese_network.py --input examples [INFO] loading test dataset... [INFO] loading siamese model...
Looking at Figure 8, youāll see that we have sets of example image pairs presented to our siamese network trained with contrastive loss.
Images that are of the same class have lower distances while images of different classes have larger classes.
You can thus set a threshold value, T, to act as a cutoff on distance. If the computed distance, D, is < T, then the image pair must belong to the same class. Otherwise, if D >= T, then the images are different classes.
Setting the threshold T should be done empirically through experimentation:
- Train the network.
- Compute distances for image pairs.
- Manually visualize the pairs and their corresponding differences.
- Find a cutoff value that maximizes correct classifications and minimizes incorrect ones.
In this case, setting T=0.16 would be an appropriate threshold, since it allows us to correctly mark all image pairs that belong to the same class, while all image pairs of different classes are treated as such.
What's next? I recommend PyImageSearch University.
30+ total classes • 39h 44m video • Last updated: 12/2021
★★★★★ 4.84 (128 Ratings) • 3,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
Thatās not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And thatās exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here youāll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 30+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 30+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser ā works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this tutorial, you learned about contrastive loss, including how itās a better loss function than binary cross-entropy for training siamese networks.
What you need to keep in mind here is that a siamese network isnāt specifically designed for classification. Instead, itās utilized for differentiation, meaning that it should not only be able to tell if an image pair belongs to the same class or not but whether the two images are identical/similar or not.
Contrastive loss works far better in this situation.
I recommend you experiment with both binary cross-entropy and contrastive loss when training your own siamese neural networks, but I think youāll find that overall, contrastive loss does a much better job.
To download the source code to this post (and be notified when future tutorials are published here on PyImageSearch), simply enter your email address in the form below!
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!
Comment section
Hey, Adrian Rosebrock here, author and creator of PyImageSearch. While I love hearing from readers, a couple years ago I made the tough decision to no longer offer 1:1 help over blog post comments.
At the time I was receiving 200+ emails per day and another 100+ blog post comments. I simply did not have the time to moderate and respond to them all, and the sheer volume of requests was taking a toll on me.
Instead, my goal is to do the most good for the computer vision, deep learning, and OpenCV community at large by focusing my time on authoring high-quality blog posts, tutorials, and books/courses.
If you need help learning computer vision and deep learning, I suggest you refer to my full catalog of books and courses ā they have helped tens of thousands of developers, students, and researchers just like yourself learn Computer Vision, Deep Learning, and OpenCV.
Click here to browse my full catalog.