Reputation: 361
I am trying to train a model for facial keypoints detection from a series of frames from videos. Since these frames have almost identical position for the keypoints, the model is producing the same output for every image I put in for prediction. I was trying to use ImageDataGenerator
to rotate the images in order for them to be different from each other, however, I can't seem to make it work.
Originally when I call fit
on the model, I have the option to split the training data into train and validation, but I don't understand how to use the validation_split
option in ImageDataGenerator
. Could someone explain how to use it, or maybe suggest me a way to use this class?
Right now I have a tensor of size [total_images, width, height, channels]
and its corresponding [total_images, output]
. How do I use ImageDataGenerator
to rotate the images, and also separate them into training and validation data?
Upvotes: 1
Views: 587
Reputation: 361
Turned out I can create this myself using transformation matrices. To rotate the images correctly in OpenCV, I used this code (modified the transformation matrix to keep all the corners of the image while rotating)
Code credits: Cristian Perez Brokate you can find the explanation of the math behind this implementation. rotate_bound
is exactly the same as what I found, rotate_points
is the modified version of rotate_box
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
And to rotate the coordinates of the points accordingly, I used this code:
def rotate_points(image, points, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
v = np.ones((points.shape[0], points.shape[1] + 1))
v[:,:-1] = points
return np.dot(M, v.T).T
Upvotes: 1