import numpy as np import cv2 import random 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 f: fname, p0, p1, p2, p3, p4, p5, p6 = line.split() timestamps.append(fname) img = np.load("path/to/txt_files/with/data/" + 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""" indices = range(len(source[0])) for i in indices: image = source[0][i] pose_x_rel = source[1][0][i] pose_q_rel = source[1][1][i] yield image, pose_x_rel, pose_q_rel 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] pose_x_rel = source[1][0][i] pose_q_rel = source[1][1][i] yield image, pose_x_rel, pose_q_rel def gen_data_batch(source, batchSize, im_shape): """ 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) if image.shape[0] != im_shape[1] or image.shape[1] != im_shape[2]: image = cv2.resize(image, (im_shape[2], im_shape[1])) 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)]