Exemple #1
0
def img_preprocessing_op(image_op):
    """
    Creates preprocessing operations that are going to be applied on a single frame.

    You can do any preprocessing (masking, normalization/scaling of inputs, augmentation, etc.) by using tensorflow
    operations. Here I provided some examples commented in the code. You can find more built-in image operations at
    https://www.tensorflow.org/api_docs/python/tf/image .

    :param image_op:
    :return:
    """
    with tf.name_scope("img_preprocessing"):
        # Convert from RGB to greyscale.
        # image_op = tf.image.rgb_to_grayscale(image_op)

        # Crop
        #image_op = tf.image.resize_image_with_crop_or_pad(image_op, 60, 60)

        # Resize operation requires 4D tensors (i.e., batch of images).
        # Reshape the image so that it looks like a batch of one sample: [1,60,60,1]
        #image_op = tf.expand_dims(image_op, 0)
        # Resize
        #image_op = tf.image.resize_bilinear(image_op, np.asarray([32,32]))
        # Reshape the image: [32,32,1]
        #image_op = tf.squeeze(image_op, 0)

        # Normalize (zero-mean unit-variance) the image locally, i.e., by using statistics of the
        # image not the whole data or sequence.
        # image_op = tf.image.per_image_standardization(image_op)

        # Flatten image
        # y.set_shape(image_op.get_shape())
        # image_op_ = tf.TensorArray(
        #     dtype=tf.float32, size=0, dynamic_size=True)

        image_op_ = np.ones(shape=[image_op.shape[0], 80, 80, 3],
                            dtype=np.float32)

        for i in range(image_op.shape[0]):
            skeleton = Skeleton(image_op[i])
            skeleton.resizePixelCoordinates()
            image_op_[i] = skeleton.toImage(80, 80)
        # return skeletonimage [0,255]
        return image_op_.astype(np.float32)
def main(unused_argv):
    # Create a list of TFRecord input files.
    filenames = [
        os.path.join(config['data_dir'], config['file_format'] % i)
        for i in config['file_ids']
    ]
    # Create data loading operators. This will be represented as a node in the computational graph.
    batch_rgb_op, batch_skeleton_op, batch_labels_op, batch_seq_len_op = input_pipeline(
        filenames, config)
    # TODO: your model can take batch_rgb, batch_labels and batch_seq_len ops as an input.

    # Create tensorflow session and initialize the variables (if any).
    sess = tf.Session()
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)
    # Create threads to prefetch the data.
    # https://www.tensorflow.org/programmers_guide/reading_data#creating_threads_to_prefetch_using_queuerunner_objects
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    """
    # Training Loop
    # The input pipeline creates input batches for config['num_epochs'] epochs,
    # You can iterate over the training data by using coord.should_stop() signal.
    try:
        while not coord.should_stop():
            # TODO: Model training

    except tf.errors.OutOfRangeError:
        print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
    finally:
        # When done, ask the threads to stop.
        coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)
    # Close session
    sess.close()
    """

    # Instead of running training, fetch a sample and save one frame.
    # Fetch a batch of samples.
    batch_rgb, batch_skeleton, batch_labels, batch_seq_len = sess.run(
        [batch_rgb_op, batch_skeleton_op, batch_labels_op, batch_seq_len_op])

    # Print
    print("# Samples: " + str(len(batch_rgb)))
    print("Sequence lengths: " + str(batch_seq_len))
    print("Sequence labels: " + str(batch_labels))

    # Note that the second dimension will give maximum-length in the batch, i.e., the padded sequence length.
    print("Sequence type: " + str(type(batch_rgb)))
    print("Sequence shape: " + str(batch_rgb.shape))

    # Fetch first clips 10th frame.
    img = batch_rgb[0][9]
    print("Image shape: " + str(img.shape))

    # Create a skeleton object.
    skeleton = Skeleton(batch_skeleton[0][10])
    # Resize the pixel coordinates.
    skeleton.resizePixelCoordinates()
    # Draw skeleton image.
    skeleton_img = skeleton.toImage(img.shape[0], img.shape[1])

    imsave(config['model_dir'] + 'rgb_test_img.png', img)
    imsave(config['model_dir'] + 'skeleton_test_img.png', skeleton_img)
Exemple #3
0
    cnt = 0
    for batch in data_iterator:
        b_size = batch["rgb"].shape[0]
        num_samples += b_size

        for i in range(b_size):

            length = batch['length'][i]
            tmp = []

            print(length)

            for l in range(length):
                img_rgb = batch['rgb'][i][l]
                skeleton = Skeleton(batch['skeleton'][i][l])
                skeleton.resizePixelCoordinates()
                tmp.append(skeleton.toImage(img_rgb.shape[0], img_rgb.shape[1]))
        
            skeleton_img = np.array(tmp).astype(np.float32)

            print(skeleton_img.shape)
            cnt +=1
            print (cnt)

            sample = {
                "rgb"          : np.array(batch['rgb'][i][:length][:,:,:,::-1]),
                "depth"        : np.array(batch['depth'][i][:length]),
                "segmentation" : np.array(batch['segmentation'][i][:length]),
                "skeleton"     : np.array(batch['skeleton'][i][:length]),
                "skeleton_img" : skeleton_img,
                "length"      : np.array(batch['length'][i]),