Add multiple Images , neural network
In this lesson we’re going to see how to add multiple images to train the neuron with. In addition to the vertical white line image we used in the previous lesson, we’re adding also the horizontal white line, so that the neuron will be trained for both of them.
In this lesson you will learn:
- How to Add Multiple images
- How to Train the neuron with multiple images
1. Add multiple images
Adding multiple images to the input is a simple operation. In the previous lesson we’ve just added the “vertical.png” image to train the neuron, and we preprocessed it (by dividing the image per 255, and by flattening it), using this code:
# 1. Input # 1.1 Image preprocessing img = cv2.imread("images/vertical.png", cv2.IMREAD_GRAYSCALE) img = img / 255 img_flattened = img.flatten()
Now we do the same loading and preprocessing operations also for the second image “horizontal.png”, and then we put them together into a new array called dataset.
# 1. Input # 1.1 Image preprocessing img = cv2.imread("images/vertical.png", cv2.IMREAD_GRAYSCALE) img = img / 255 img_flattened = img.flatten() img2 = cv2.imread("images/horizontal.png", cv2.IMREAD_GRAYSCALE) img2 = img2 / 255 img2_flattened = img2.flatten() dataset = np.array([img_flattened, img2_flattened])
In this way we can easily access all the images we have by accessing the dataset.
There is one extra thing to add. If think about it, we’re loading two different images, one it’s the vertical line and the second one is the horizontal line. We need to find a way to tell the neuron that one is vertical and one is horizontal.
We decided a few lessons back that we would associate the value 0 to the vertical and the number 1 to the horizontal. So we now add a new variable called “labels”, and in the exact order in which we loaded the images, we also put the correspondent label.
labels = np.array([0, 1])
Considering the first image is vertical, the first number we put is 0, the second image is horizontal, so we put 1 as second number.
That’s all we have to do to add new images.
Let’s now change the neuron so that it takes the dataset and labels.
2. Train the neuron with multiple images
We need to make the changes where before we were multiplying the flattened image with the weights, and right now we should repeat the same operation for all the images inside dataset.
To be able to do the same operations for all the images we can just loop through dataset and labels together, so on the line 38 that we have the image and its label associated.
In addition to this, on line 37 we added temporary_weights, to avoid changing the original weights while looping through the images in dataset. We want to update the weights only after one full round of training and not after each single image.
# 2. Weights weights = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]) for i in range(100): print("Round: ", i + 1) temporary_weights = weights.copy() for image, label in zip(dataset, labels): convolution = sum(image * weights) print("Image") print(image)
Then at the end of the dataset’s loop we can finally update all the weights, by change the value of weights, putting copying the temporary_weights value inside.
# 3. Activation function result = sigmoid(convolution) # 5. Error error = result - label print("Label", label) print("Error", error) # 6. Adjustment adjustment = error * sigmoid_der(result) print("Adjustment", adjustment) temporary_weights -= np.dot(image, adjustment) weights = temporary_weights.copy() print("Weights", weights) print()
This code above is able to train a single neuron for all the images included inside dataset.
- Train YOLO to detect a custom object (online with free GPU)
- YOLO object detection using Opencv with Python
- Detecting colors (Hsv Color Space) – Opencv with Python
- How to install Python 3 and Opencv 4 on Windows
- Check if two images are equal with Opencv and Python
- YOLO V3 – Install and run Yolo on Nvidia Jetson Nano (with GPU)
- How to install Dlib for Python 3 on Windows
- Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25