|
| 1 | +import tensorflow as tf |
| 2 | +import numpy as np |
| 3 | +import threading |
| 4 | +import PIL.Image as Image |
| 5 | +from functools import partial |
| 6 | +from multiprocessing import Pool |
| 7 | +import cv2 |
| 8 | + |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 | +from modules import * |
| 14 | + |
| 15 | + |
| 16 | +HEIGHT=192 |
| 17 | +WIDTH=256 |
| 18 | +NUM_OBJECTS = 10 |
| 19 | +NUM_THREADS = 4 |
| 20 | + |
| 21 | + |
| 22 | +class RecordReader(): |
| 23 | + def __init__(self): |
| 24 | + return |
| 25 | + |
| 26 | + def getBatch(self, filename_queue, options, min_after_dequeue = 1000, random=True): |
| 27 | + reader = tf.TFRecordReader() |
| 28 | + _, serialized_example = reader.read(filename_queue) |
| 29 | + |
| 30 | + features = tf.parse_single_example( |
| 31 | + serialized_example, |
| 32 | + # Defaults are not specified since both keys are required. |
| 33 | + features={ |
| 34 | + #'height': tf.FixedLenFeature([], tf.int64), |
| 35 | + #'width': tf.FixedLenFeature([], tf.int64), |
| 36 | + 'image_raw': tf.FixedLenFeature([], tf.string), |
| 37 | + 'image_path': tf.FixedLenFeature([], tf.string), |
| 38 | + 'depth': tf.FixedLenFeature([HEIGHT * WIDTH], tf.float32), |
| 39 | + 'info': tf.FixedLenFeature([4 * 4 + 4], tf.float32), |
| 40 | + 'objects': tf.FixedLenFeature([NUM_OBJECTS * 13], tf.float32), |
| 41 | + }) |
| 42 | + |
| 43 | + # Convert from a scalar string tensor (whose single string has |
| 44 | + # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape |
| 45 | + # [mnist.IMAGE_PIXELS]. |
| 46 | + image = tf.decode_raw(features['image_raw'], tf.uint8) |
| 47 | + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 |
| 48 | + image = tf.reshape(image, [HEIGHT, WIDTH, 3]) |
| 49 | + |
| 50 | + |
| 51 | + depth = features['depth'] |
| 52 | + depth = tf.reshape(depth, [HEIGHT, WIDTH, 1]) |
| 53 | + |
| 54 | + # normal = features['normal'] |
| 55 | + # normal = tf.reshape(normal, [HEIGHT, WIDTH, 3]) |
| 56 | + # normal = tf.nn.l2_normalize(normal, dim=2) |
| 57 | + |
| 58 | + objects = tf.reshape(features['objects'], [NUM_OBJECTS, 13]) |
| 59 | + |
| 60 | + outputWidth = WIDTH / options.outputStride |
| 61 | + outputHeight = HEIGHT / options.outputStride |
| 62 | + |
| 63 | + if options.axisAligned or options.useAnchor: |
| 64 | + centers = objects[:, :3] |
| 65 | + sizes = objects[:, 3:6] |
| 66 | + towards = objects[:, 6:9] |
| 67 | + up = objects[:, 9:12] |
| 68 | + right = tf.cross(towards, up) |
| 69 | + right /= tf.maximum(tf.norm(right, axis=-1, keep_dims=True), 1e-4) |
| 70 | + directionsArray = np.array([[1, 1, 1], |
| 71 | + [1, 1, -1], |
| 72 | + [1, -1, 1], |
| 73 | + [1, -1, -1], |
| 74 | + [-1, 1, 1], |
| 75 | + [-1, 1, -1], |
| 76 | + [-1, -1, 1], |
| 77 | + [-1, -1, -1]]) |
| 78 | + directions = tf.expand_dims(tf.constant(directionsArray.reshape(-1), shape=directionsArray.shape), 0) |
| 79 | + cornerPoints = tf.expand_dims(centers, 1) + tf.matmul(tf.expand_dims(sizes, 1) / 2 * directionsArray, tf.stack([towards, up, right], axis=1)) |
| 80 | + maxs = tf.reduce_max(cornerPoints, axis=1) |
| 81 | + mins = tf.reduce_min(cornerPoints, axis=1) |
| 82 | + sizes = maxs - mins |
| 83 | + #centers = (mins + maxs) / 2 |
| 84 | + objects = tf.concat([centers, sizes, objects[:, 6:]], axis=1) |
| 85 | + pass |
| 86 | + |
| 87 | + |
| 88 | + numObjects = tf.reduce_sum(tf.cast(tf.greater(objects[:, 12], 0), dtype=tf.int32)) |
| 89 | + info = features['info'] |
| 90 | + |
| 91 | + objects = objects[:numObjects] |
| 92 | + |
| 93 | + if options.useAnchor: |
| 94 | + centers = objects[:, :3] - objects[:, 3:6] / 2 * tf.expand_dims(tf.constant([1, 0, 0], dtype=tf.float32), 0) |
| 95 | + else: |
| 96 | + centers = objects[:, :3] |
| 97 | + pass |
| 98 | + |
| 99 | + U = (centers[:, 2] / centers[:, 0] * info[0] + info[2]) / info[16] |
| 100 | + V = tf.clip_by_value((-centers[:, 1] / centers[:, 0] * info[5] + info[6]) / info[17], 0, 1) |
| 101 | + |
| 102 | + if True: |
| 103 | + validMask = tf.logical_and(tf.logical_and(tf.logical_and(tf.greater_equal(U, 0), tf.less_equal(U, 1)), tf.logical_and(tf.greater_equal(V, 0), tf.less_equal(V, 1))), tf.greater(centers[:, 0], 0)) |
| 104 | + objects = tf.boolean_mask(objects, validMask) |
| 105 | + U = tf.boolean_mask(U, validMask) |
| 106 | + V = tf.boolean_mask(V, validMask) |
| 107 | + numObjects = tf.reduce_sum(tf.cast(validMask, tf.int32)) |
| 108 | + pass |
| 109 | + |
| 110 | + U = tf.clip_by_value(U, 0, 1) |
| 111 | + V = tf.clip_by_value(V, 0, 1) |
| 112 | + |
| 113 | + gridU = tf.clip_by_value(tf.cast((U * outputWidth), tf.int32), 0, outputWidth - 1) |
| 114 | + gridV = tf.clip_by_value(tf.cast((V * outputHeight), tf.int32), 0, outputHeight - 1) |
| 115 | + indices = gridV * outputWidth + gridU |
| 116 | + local_scores = tf.reshape(tf.maximum(tf.unsorted_segment_max(tf.ones([numObjects]), indices, num_segments=outputWidth * outputHeight), 0), (outputHeight, outputWidth, 1)) |
| 117 | + class_prob = tf.one_hot(tf.cast(objects[:, 12], tf.int32), depth=40, axis=-1) |
| 118 | + local_classes = tf.reshape(tf.maximum(tf.unsorted_segment_max(class_prob, indices, num_segments=outputWidth * outputHeight), 0), (outputHeight, outputWidth, -1)) |
| 119 | + |
| 120 | + if options.useAnchor: |
| 121 | + mins = objects[:, :3] - objects[:, 3:6] / 2 * tf.expand_dims(tf.constant([1, -1, 1], dtype=tf.float32), 0) |
| 122 | + maxs = objects[:, :3] - objects[:, 3:6] / 2 * tf.expand_dims(tf.constant([1, 1, -1], dtype=tf.float32), 0) |
| 123 | + minU = (mins[:, 2] / mins[:, 0] * info[0] + info[2]) / info[16] |
| 124 | + minV = (-mins[:, 1] / mins[:, 0] * info[5] + info[6]) / info[17] |
| 125 | + maxU = (maxs[:, 2] / maxs[:, 0] * info[0] + info[2]) / info[16] |
| 126 | + maxV = (-maxs[:, 1] / maxs[:, 0] * info[5] + info[6]) / info[17] |
| 127 | + boxes = tf.stack([U, V, maxU - minU, maxV - minV], axis=-1) |
| 128 | + boxes = tf.reshape(tf.maximum(tf.unsorted_segment_max(boxes, indices, num_segments=outputWidth * outputHeight), 0), (outputHeight, outputWidth, 4)) |
| 129 | + |
| 130 | + anchorW = tf.fill((outputHeight, outputWidth, 1), 1.0 / outputWidth) |
| 131 | + anchorH = tf.fill((outputHeight, outputWidth, 1), 1.0 / outputHeight) |
| 132 | + anchors = tf.stack([tf.tile(tf.expand_dims(tf.range(outputWidth, dtype=tf.float32), 0), (outputHeight, 1)) / outputWidth, tf.tile(tf.expand_dims(tf.range(outputHeight, dtype=tf.float32), 1), (1, outputWidth)) / outputHeight], axis=-1) |
| 133 | + anchors = tf.concat([anchors, anchorW, anchorH], axis=-1) |
| 134 | + |
| 135 | + #local_parameters = tf.reshape(tf.unsorted_segment_sum(objects[:, :12], indices, num_segments=outputWidth * outputHeight), (outputHeight, outputWidth, 12)) |
| 136 | + depths = tf.clip_by_value(tf.reshape(tf.maximum(tf.unsorted_segment_max(tf.stack([objects[:, 0] - objects[:, 3] / 2, objects[:, 0] + objects[:, 3] / 2], axis=-1), indices, num_segments=outputWidth * outputHeight), 0), (outputHeight, outputWidth, 2)), 0, 10) |
| 137 | + local_parameters = tf.concat([(boxes[:, :, :2] - anchors[:, :, :2]) / anchors[:, :, 2:4], tf.minimum(boxes[:, :, 2:4] / anchors[:, :, 2:4], options.outputStride * 2), depths], axis=-1) |
| 138 | + local_parameters *= local_scores |
| 139 | + local_parameters = tf.concat([local_parameters, tf.zeros(local_parameters.shape)], axis=-1) |
| 140 | + #local_parameters = tf.reshape(tf.unsorted_segment_sum(objects[:, :12], indices, num_segments=outputWidth * outputHeight), (outputHeight, outputWidth, 12)) |
| 141 | + else: |
| 142 | + local_parameters = tf.reshape(tf.unsorted_segment_sum(objects[:, :12], indices, num_segments=outputWidth * outputHeight), (outputHeight, outputWidth, 12)) |
| 143 | + pass |
| 144 | + |
| 145 | + objects = tf.reshape(tf.concat([objects, tf.zeros((NUM_OBJECTS - numObjects, 13))], axis=0), (NUM_OBJECTS, 13)) |
| 146 | + |
| 147 | + if random: |
| 148 | + image_inp, object_inp, depth_gt, num_objects_gt, image_path, info, local_scores_gt, local_parameters_gt, local_classes_gt = tf.train.shuffle_batch([image, objects, depth, numObjects, features['image_path'], features['info'], local_scores, local_parameters, local_classes], batch_size=options.batchSize, capacity=min_after_dequeue + (NUM_THREADS + 2) * options.batchSize, num_threads=NUM_THREADS, min_after_dequeue=min_after_dequeue) |
| 149 | + else: |
| 150 | + image_inp, object_inp, depth_gt, num_objects_gt, image_path, info, local_scores_gt, local_parameters_gt, local_classes_gt = tf.train.batch([image, objects, depth, numObjects, features['image_path'], features['info'], local_scores, local_parameters, local_classes], batch_size=options.batchSize, capacity=min_after_dequeue + (NUM_THREADS + 2) * options.batchSize, num_threads=1) |
| 151 | + pass |
| 152 | + global_gt_dict = {'object': object_inp, 'depth': depth_gt, 'num_objects': num_objects_gt} |
| 153 | + local_gt_dict = {'score': local_scores_gt, 'object': local_parameters_gt, 'image_path': image_path, 'info': info, 'num_objects': num_objects_gt, 'class': local_classes_gt} |
| 154 | + return image_inp, global_gt_dict, local_gt_dict |
0 commit comments