import numpy as np import cv2 import random import settings from tqdm import tqdm from similarityMeasures import getError imageDistance = 1 def get_training_data(dataset): """ Reads in the predicted pos/rotation data as well as the ground truth """ print(dataset) images_batch = [] timestamps = [] po1 = [] po2 = [] with open(dataset) as f: for line in tqdm(f): fname, p0, p1, p2, p3, p4, p5, p6 = line.split() timestamps.append(fname) img = cv2.imread("../../../Self_loc_synthetic/dataset_real/dataset/" + fname) images_batch.append(img) po1.append(np.array((np.float(p0), np.float(p1), np.float(p2)))) po2.append(np.array((np.float(p3), np.float(p4), np.float(p5), np.float(p6)))) return (images_batch, [np.asarray(po1), np.asarray(po2)]), timestamps def limited_gen_data(source): """ Yields the input/ground truth data in batches of size 1. Can be used for evaluating the test dataset. Each input uses 3 images. The current one, the previous one and the next one. """ indices = range(len(source[0])) for i in indices: image = source[0][i] image_left = source[0][max(0, i - imageDistance)] image_right = source[0][min(i + imageDistance, len(source[0]) - 1)] pose_x = source[1][0][i] pose_q = source[1][1][i] pose_x_left = source[1][0][max(0, i - imageDistance)] pose_q_left = source[1][1][max(0, i - imageDistance)] pose_x_right = source[1][0][min(i + imageDistance, len(source[0]) - 1)] pose_q_right = source[1][1][min(i + imageDistance, len(source[0]) - 1)] if settings.saveMean: m1_2, a1_2 = getError(pose_x, pose_q, pose_x_left, pose_q_left) m2_3, a2_3 = getError(pose_x, pose_q, pose_x_right, pose_q_right) m1_3, a1_3 = getError(pose_x_left, pose_q_left, pose_x_right, pose_q_right) if m1_2 > settings.distanceThreshold or m2_3 > settings.distanceThreshold: continue if a1_2 > settings.angleThreshold or a2_3 > settings.angleThreshold: continue yield np.asarray([image_left, image, image_right]), np.asarray([pose_x_left, pose_x, pose_x_right]), np.asarray([pose_q_left, pose_q, pose_q_right]) def gen_data(source): """ Data generator that randomly shuffles the training data and yields them as single elements. """ while True: indices = list(range(len(source[0]))) random.shuffle(indices) for i in indices: image = source[0][i] image_left = source[0][max(0, i - imageDistance)] image_right = source[0][min(i + imageDistance, len(source[0]) - 1)] pose_x = source[1][0][i] pose_q = source[1][1][i] pose_x_left = source[1][0][max(0, i - imageDistance)] pose_q_left = source[1][1][max(0, i - imageDistance)] pose_x_right = source[1][0][min(i + imageDistance, len(source[0]) - 1)] pose_q_right = source[1][1][min(i + imageDistance, len(source[0]) - 1)] m1_2, a1_2 = getError(pose_x, pose_q, pose_x_left, pose_q_left) m2_3, a2_3 = getError(pose_x, pose_q, pose_x_right, pose_q_right) m1_3, a1_3 = getError(pose_x_left, pose_q_left, pose_x_right, pose_q_right) if m1_2 > settings.distanceThreshold or m2_3 > settings.distanceThreshold: continue if a1_2 > settings.angleThreshold or a2_3 > settings.angleThreshold: continue yield np.asarray([image_left, image, image_right]), np.asarray([pose_x_left, pose_x, pose_x_right]), np.asarray([pose_q_left, pose_q, pose_q_right]) def gen_data_batch(source, batchSize): """ Uses the gen_data and yields batches of the size batchSize. """ data_gen = gen_data(source) while True: image_batch = [] pose_x_batch = [] pose_q_batch = [] for _ in range(batchSize): image, pose_x, pose_q = next(data_gen) image_batch.append(image) pose_x_batch.append(pose_x) pose_q_batch.append(pose_q) yield np.asarray(image_batch), [np.asarray(pose_x_batch), np.asarray(pose_q_batch)]