Example #1
0
def distorted_inputs_unit(roi_paths,
                          roi_expand_paths,
                          labels,
                          trainable,
                          roi_size=128,
                          roi_expand_size=256):
    filenames = roi_paths
    expandfilenames = roi_expand_paths
    labels = labels
    filename, expandfilename, label = tf.train.slice_input_producer(
        [filenames, expandfilenames, labels], shuffle=False)
    num_process_threads = 4
    images_and_labels = []
    for thread_id in range(num_process_threads):
        image_buffer = tf.read_file(filename)

        bbox = []
        image = image_preprocessing(image_buffer,
                                    bbox=bbox,
                                    train=trainable,
                                    thread_id=thread_id,
                                    image_size=roi_size)
        # image = tf.image.rgb_to_hsv(image)

        expand_image_buffer = tf.read_file(expandfilename)
        bbox = []
        expandimage = image_preprocessing(expand_image_buffer,
                                          bbox=bbox,
                                          train=trainable,
                                          thread_id=thread_id,
                                          image_size=roi_expand_size)

        images_and_labels.append([image, expandimage, label])
    batch_image, batch_expand_image, batch_label = tf.train.batch_join(
        images_and_labels,
        batch_size=FLAGS.batch_size,
        capacity=2 * num_process_threads * FLAGS.batch_size)

    images = tf.cast(batch_image, tf.float32)
    images = tf.reshape(images,
                        shape=[FLAGS.batch_size, roi_size, roi_size, 3])

    expand_images = tf.cast(batch_expand_image, tf.float32)
    # print expand_images
    expand_images = tf.reshape(
        expand_images,
        shape=[FLAGS.batch_size, roi_expand_size, roi_expand_size, 3])
    return images, expand_images, tf.reshape(batch_label, [FLAGS.batch_size])
Example #2
0
def parse_tfrecord_inception(params,
                             record,
                             width,
                             height,
                             is_training=True,
                             use_summary=False):
    '''
	Parse the records saved using the tensorflow official inception data build

	https://github.com/tensorflow/models

	'''

    image_buffer, label, bbox, label_text = parse_example_proto(record)

    image = image_preprocessing(image_buffer,
                                bbox,
                                is_training,
                                width,
                                height,
                                use_summary=use_summary)
    # [batch, height, width, channels] range(-1.0,1.0)

    label_one_hot = tf.one_hot(tf.squeeze(label, axis=-1),
                               params['num_labels'],
                               dtype=image.dtype)

    return image, label_one_hot
Example #3
0
def distorted_inputs(data, image_dir):
    #data = load_data(FLAGS.data_dir)

    filenames = [image_dir + d['filename'] for d in data]
    label_indexes = [d['label_index'] for d in data]
    filename, label_index = tf.train.slice_input_producer(
        [filenames, label_indexes], shuffle=True)

    num_preprocess_threads = 4
    images_and_labels = []
    for thread_id in range(num_preprocess_threads):
        image_buffer = tf.read_file(filename)

        bbox = []
        train = True
        image = image_preprocessing(image_buffer, bbox, train, thread_id)
        images_and_labels.append([image, label_index])

    images, label_index_batch = tf.train.batch_join(
        images_and_labels,
        batch_size=FLAGS.batch_size,
        capacity=2 * num_preprocess_threads * FLAGS.batch_size,
        allow_smaller_final_batch=True)

    height = FLAGS.input_size
    width = FLAGS.input_size
    depth = 3

    images = tf.cast(images, tf.float32)
    images = tf.reshape(images, shape=[FLAGS.batch_size, height, width, depth])

    return images, tf.reshape(label_index_batch, [FLAGS.batch_size])
Example #4
0
def distorted_inputs_unit(dataset, trainable, shuffle=True):
    filenames = dataset.images_names
    labels = dataset.labels
    filename, label = tf.train.slice_input_producer([filenames, labels], shuffle=shuffle)
    num_process_threads = 4
    images_and_labels = []
    for thread_id in range(num_process_threads):
        image_buffer = tf.read_file(filename)
        bbox = []
        image = image_preprocessing(
            image_buffer,
            bbox=bbox,
            train=trainable,
            thread_id=thread_id
        )
        # image = tf.image.rgb_to_hsv(image)
        images_and_labels.append([image, label])
    batch_image, batch_label = tf.train.batch_join(
        images_and_labels,
        batch_size=net_config.BATCH_SIZE,
        capacity=2*num_process_threads*net_config.BATCH_SIZE
    )
    height = net_config.IMAGE_W
    width = net_config.IMAGE_H
    depth = 3

    images = tf.cast(batch_image, tf.float32)
    images = tf.reshape(images, shape=[net_config.BATCH_SIZE, height, width, depth])

    return images, tf.reshape(batch_label, [net_config.BATCH_SIZE])
def distorted_inputs():
    data = load_data(FLAGS.data_dir)

    filenames = [ d['filename'] for d in data ]
    label_indexes = [ d['label_index'] for d in data ]

    filename, label_index = tf.train.slice_input_producer([filenames, label_indexes], shuffle=True)

    num_preprocess_threads = 4
    images_and_labels = []
    for thread_id in range(num_preprocess_threads):
        image_buffer = tf.read_file(filename)

        bbox = []
        train = True
        image = image_preprocessing(image_buffer, bbox, train, thread_id)
        images_and_labels.append([image, label_index])

    images, label_index_batch = tf.train.batch_join(
        images_and_labels,
        batch_size=FLAGS.batch_size,
        capacity=2 * num_preprocess_threads * FLAGS.batch_size)

    height = FLAGS.input_size
    width = FLAGS.input_size
    depth = 3

    images = tf.cast(images, tf.float32)
    images = tf.reshape(images, shape=[FLAGS.batch_size, height, width, depth])

    return images, tf.reshape(label_index_batch, [FLAGS.batch_size])
Example #6
0
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image_array = np.asarray(image)
        image_array = ip.image_preprocessing(image_array)
        steering_angle = float(
            model.predict(image_array[None, :, :, :], batch_size=1))

        throttle = controller.update(float(speed))

        print(steering_angle, throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
Example #7
0
def distorted_inputs(val_dataset):
    # data = load_data(FLAGS.data_dir)

    # filenames = [ d['filename'] for d in data ]
    # label_indexes = [ d['label_index'] for d in data ]
    # val_positive_path = '/home/give/Documents/dataset/BOT_Game/data_NY/patch/val/positive'
    # val_negative_path = '/home/give/Documents/dataset/BOT_Game/data_NY/patch/val/negative'
    filenames = val_dataset.images_names
    label_indexes = val_dataset.labels
    filename, label_index = tf.train.slice_input_producer(
        [filenames, label_indexes], shuffle=False, num_epochs=1)
    num_preprocess_threads = 1
    images_and_labels = []
    for thread_id in range(num_preprocess_threads):
        image_buffer = tf.read_file(filename)

        bbox = []
        train = True
        image = image_preprocessing(image_buffer, bbox, train, thread_id)
        images_and_labels.append([image, label_index])
    images, label_index_batch = tf.train.batch_join(
        images_and_labels,
        batch_size=FLAGS.batch_size,
        allow_smaller_final_batch=True)

    height = FLAGS.input_size
    width = FLAGS.input_size
    depth = 3

    images = tf.cast(images, tf.float32)
    images = tf.reshape(images, shape=[FLAGS.batch_size, height, width, depth])

    return images, tf.reshape(label_index_batch, [FLAGS.batch_size])
Example #8
0
def main(argv=None):
    with tf.Session() as sess:
        data_dir = FLAGS.data_dir
        files = [os.path.join(data_dir, item) for item in os.listdir(data_dir)]
        # files = random.sample(files,  800)
        images = tf.placeholder(tf.float32,
                                [None, RESIZE_FINAL, RESIZE_FINAL, 3])
        logits = inference(
            images,
            False,
            num_classes=2,
            num_blocks=[3, 4, 6, 3],  # defaults to 50-layer network
            use_bias=False,  # defaults to using batch norm
            bottleneck=True)
        init = tf.global_variables_initializer()
        resnet_variables = tf.global_variables()
        saver = tf.train.Saver(resnet_variables)
        saver.restore(sess, os.path.join(FLAGS.model_dir, FLAGS.ckpt_file))

        softmax_output = tf.nn.softmax(logits)
        if FLAGS.target:
            print('Creating output file %s' % FLAGS.target)
            output = open(os.path.join(FLAGS.data_dir, FLAGS.target), 'w')
            writer = csv.writer(output)
            writer.writerow(('file', 'label', 'score'))

        num_batches = int(math.ceil(len(files)) / MAX_BATCH_SZ)
        pg = ProgressBar(num_batches)
        # try:
        for j in range(num_batches):
            start_offset = j * MAX_BATCH_SZ
            end_offset = min((j + 1) * MAX_BATCH_SZ, len(files))

            batch_image_files = files[start_offset:end_offset]
            images_ = []
            for file in batch_image_files:
                print file
                image_buffer = tf.read_file(file)
                bbox = []
                image = image_preprocessing(image_buffer, [], False)
                images_.append(image)
            image_batch = tf.stack(images_)
            batch_results = sess.run(softmax_output,
                                     feed_dict={images: image_batch.eval()})
            batch_sz = batch_results.shape[0]

            for i in range(batch_sz):
                output_i = batch_results[i]
                best_i = np.argmax(output_i)

                best_choice = (label_list[best_i], output_i[best_i])
                if writer is not None:
                    f = batch_image_files[i]
                    writer.writerow(
                        (f, best_choice[0], '%.2f' % best_choice[1]))
            pg.update()
        pg.done()
def distorted_inputs():
    data = load_data(FLAGS.data_dir)

    # load the file list and labels
    filenames = [d['filename'] for d in data]
    label_indexes = [d['label_index'] for d in data]

    # slice_input_producer return tensors
    filename, label_index = tf.train.slice_input_producer(
        [filenames, label_indexes], shuffle=True)

    # create multiple processes to read the data
    num_preprocess_threads = 4
    images_and_labels = []
    for thread_id in range(num_preprocess_threads):
        image_buffer = tf.read_file(filename)

        bbox = []
        train = True
        image = image_preprocessing(image_buffer, bbox, train, thread_id)
        images_and_labels.append([image, label_index])

    # create batches
    images, label_index_batch = tf.train.batch_join(
        images_and_labels,
        batch_size=FLAGS.batch_size,
        capacity=2 * num_preprocess_threads * FLAGS.batch_size)

    height = FLAGS.input_size
    width = FLAGS.input_size
    depth = 3

    images = tf.cast(images, tf.float32)
    images = tf.reshape(images, shape=[FLAGS.batch_size, height, width, depth])

    return images, tf.reshape(label_index_batch, [FLAGS.batch_size])