示例#1
0
def load_examples():
    if a.input_dir is None or not os.path.exists(a.input_dir):
        raise Exception("input_dir does not exist")

    # synchronize seed for image operations so that we do the same operations to both
    # input and output images
    # seed = random.randint(0, 2**31 - 1)
    decode = tf.image.decode_png

    ### Read inputs
    input_paths = sorted(glob.glob(os.path.join(a.input_dir, "*.png")))
    # print(input_paths)

    with tf.name_scope("load_input_images"):
        path_queue = tf.train.string_input_producer(input_paths,
                                                    shuffle=a.mode == "train")
        reader = tf.WholeFileReader()
        paths, contents = reader.read(path_queue)
        raw_input = decode(contents, channels=1)

        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        # assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
        # with tf.control_dependencies([assertion]):
        raw_input = tf.identity(raw_input)

        raw_input.set_shape([350, 256, 1])

        input_images = preprocess(raw_input)

    def get_name(path):
        name, _ = os.path.splitext(os.path.basename(path))
        return name

    input_dir = a.input_dir

    if len(input_paths) == 0:
        raise Exception("input_dir contains no image files")

    ### Read targets
    target_dir = a.target_dir
    target_paths = sorted(glob.glob(os.path.join(target_dir, "*.png")))

    if len(target_paths) == 0:
        raise Exception("input_dir contains no image files")

    with tf.name_scope("load_target_images"):
        targets_path_queue = tf.train.string_input_producer(
            target_paths, shuffle=a.mode == "train")
        targets_reader = tf.WholeFileReader()
        targets_paths, targets_contents = targets_reader.read(
            targets_path_queue)
        raw_input = decode(targets_contents, channels=1)

        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        # assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
        # with tf.control_dependencies([assertion]):
        raw_input = tf.identity(raw_input)

        raw_input.set_shape([29, 21, 1])

        target_images = preprocess(raw_input)

    input_paths_batch, inputs_batch, targets_batch = tf.train.batch(
        [paths, input_images, target_images], batch_size=a.batch_size)
    steps_per_epoch = int(math.ceil(len(input_paths) / a.batch_size))

    return Examples(input_paths=input_paths_batch,
                    inputs=inputs_batch,
                    targets=targets_batch,
                    count=len(input_paths),
                    steps_per_epoch=steps_per_epoch)
def main(unused_argv):
    os.environ["CUDA_VISIBLE_DEVICES"] = cmd_args.cuda_device

    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.curr_idx)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.gfile.FastGFile(cmd_args.config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directory if necessary.
    if not os.path.exists(cmd_args.output_dir):
        os.makedirs(cmd_args.output_dir, exist_ok=True)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        reader = tf.WholeFileReader()
        _, value = reader.read(filename_queue)
        image_tf = tf.image.decode_image(value, channels=3)

        count = 0
        with tf.Session(config=sess_config) as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            extractor_fn = MakeExtractor(sess, config)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                try:
                    im = sess.run(image_tf)
                except tf.errors.NotFoundError as e:
                    count += 1
                    tf.logging.warning(
                        f"{'*' * 15} Decode error {e} - {count}")
                    continue

                if len(im.shape) == 4 or im.shape[-1] != 3:
                    tf.logging.warning(
                        f"{'*' * 15} Skipping image {i} which probably is a GIF or broken image"
                    )
                    continue

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + _DELF_EXT
                out_desc_parent_dir = os.path.join(
                    cmd_args.output_dir, '/'.join(out_desc_filename[:3]))
                if not os.path.exists(out_desc_parent_dir):
                    os.makedirs(out_desc_parent_dir)

                out_desc_fullpath = os.path.join(out_desc_parent_dir,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = extractor_fn(im)

                feature_io.WriteToFile(out_desc_fullpath, locations_out,
                                       feature_scales_out, descriptors_out,
                                       attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
    def make_data_tensor(self, train=True):
        if train:
            folders = self.metatrain_character_folders
            # number of tasks, not number of meta-iterations. (divide by metabatch size to measure)
            num_total_batches = 200000
        else:
            folders = self.metaval_character_folders
            num_total_batches = 600

        # make list of files
        print('Generating filenames')
        if self.datasource == 'cifar':
            if not train:
                all_filenames = []
                from datetime import datetime
                start = datetime.now()
                for i in range(num_total_batches):
                    if (i + 1) % 5000 == 0:
                        print('Generated {} tasks...'.format((i + 1)))
                    sampled_character_folders = random.sample(
                        folders, self.num_classes)
                    random.shuffle(sampled_character_folders)

                    if self.mode == 'pretrain':
                        sampled_character_folders = folders

                    labels_and_images = get_images(
                        sampled_character_folders,
                        range(self.num_classes),
                        nb_samples=self.num_samples_per_class,
                        shuffle=False)
                    # make sure the above isn't randomized order
                    labels = [li[0] for li in labels_and_images]
                    filenames = [li[1] for li in labels_and_images]
                    all_filenames.extend(filenames)

            # temp
            # save and reload pickle for fast iteration
            else:
                with open("cifar_filenames_25way1shot.pkl", 'rb') as file:
                    # pickle.dump(all_filenames, file)
                    all_filenames = pickle.load(file)
                # quit()
                labels = list(
                    np.concatenate(np.array(self.num_samples_per_class *
                                            [list(range(self.num_classes))]).T,
                                   axis=0))
        elif self.datasource == 'miniimagenet':

            if self.mode == 'pretrain':

                import glob
                all_filenames = glob.glob('data/miniImagenet/train/n*/*')
                all_labels = list(
                    np.concatenate(np.array(600 * [list(range(64))]).T,
                                   axis=0))

            elif not train:

                all_filenames = []
                from datetime import datetime
                start = datetime.now()
                for i in range(num_total_batches):
                    if (i + 1) % 5000 == 0:
                        print('Generated {} tasks...'.format((i + 1)))
                    sampled_character_folders = random.sample(
                        folders, self.num_classes)
                    random.shuffle(sampled_character_folders)

                    if self.mode == 'pretrain':
                        sampled_character_folders = folders

                    labels_and_images = get_images(
                        sampled_character_folders,
                        range(self.num_classes),
                        nb_samples=self.num_samples_per_class,
                        shuffle=False)
                    # make sure the above isn't randomized order
                    labels = [li[0] for li in labels_and_images]
                    filenames = [li[1] for li in labels_and_images]
                    all_filenames.extend(filenames)
            # temp
            # save and reload pickle for fast iteration
            else:
                with open("miniimagenet_filenames_5way1shot.pkl",
                          'rb') as file:
                    # pickle.dump(all_filenames, file)
                    all_filenames = pickle.load(file)
                # quit()
                labels = list(
                    np.concatenate(np.array(self.num_samples_per_class *
                                            [list(range(self.num_classes))]).T,
                                   axis=0))
        else:
            all_filenames = []
            from datetime import datetime
            start = datetime.now()
            for i in range(num_total_batches):
                if (i + 1) % 5000 == 0:
                    print('Generated {} tasks...'.format((i + 1)))
                sampled_character_folders = random.sample(
                    folders, self.num_classes)
                random.shuffle(sampled_character_folders)

                # temp
                # sampled_character_folders = folders

                labels_and_images = get_images(
                    sampled_character_folders,
                    range(self.num_classes),
                    nb_samples=self.num_samples_per_class,
                    shuffle=False)
                # make sure the above isn't randomized order
                labels = [li[0] for li in labels_and_images]
                filenames = [li[1] for li in labels_and_images]
                all_filenames.extend(filenames)

        # make queue for tensorflow to read from
        if self.mode == 'pretrain':
            filename_queue = tf.train.string_input_producer(
                tf.convert_to_tensor(all_filenames), shuffle=True, seed=42)
            label_queue = tf.train.input_producer(
                tf.convert_to_tensor(all_labels), shuffle=True, seed=42)
            print('Generating image processing ops')
            image_reader = tf.WholeFileReader()
            _, image_file = image_reader.read(filename_queue)
            # if self.datasource == 'miniimagenet':
            image = tf.image.decode_jpeg(image_file, channels=3)
            image.set_shape((self.img_size[0], self.img_size[1], 3))
            image = tf.reshape(image, [self.dim_input])
            image = tf.cast(image, tf.float32) / 255.0
            num_preprocess_threads = 1
            min_queue_examples = 256
            images, labels = tf.train.batch(
                [image, label_queue.dequeue()],
                batch_size=self.batch_size,
                num_threads=num_preprocess_threads,
                capacity=min_queue_examples + 3 * self.batch_size,
            )
            labels = tf.one_hot(labels, self.num_classes)
            return images, labels

        filename_queue = tf.train.string_input_producer(
            tf.convert_to_tensor(all_filenames), shuffle=False)
        print('Generating image processing ops')
        image_reader = tf.WholeFileReader()
        _, image_file = image_reader.read(filename_queue)
        if self.datasource == 'miniimagenet':
            image = tf.image.decode_jpeg(image_file, channels=3)
            image.set_shape((self.img_size[0], self.img_size[1], 3))
            image = tf.reshape(image, [self.dim_input])
            image = tf.cast(image, tf.float32) / 255.0
        elif self.datasource == 'cifar':
            image = tf.image.decode_png(image_file, channels=3)
            image.set_shape((self.img_size[0], self.img_size[1], 3))
            image = tf.reshape(image, [self.dim_input])
            image = tf.cast(image, tf.float32) / 255.0
        else:
            image = tf.image.decode_png(image_file)
            image.set_shape((self.img_size[0], self.img_size[1], 1))
            image = tf.reshape(image, [self.dim_input])
            image = tf.cast(image, tf.float32) / 255.0
            image = 1.0 - image  # invert
        num_preprocess_threads = 1  # TODO - enable this to be set to >1
        min_queue_examples = 256
        examples_per_batch = self.num_classes * self.num_samples_per_class
        batch_image_size = self.batch_size * examples_per_batch
        print('Batching images')
        images = tf.train.batch(
            [image],
            batch_size=batch_image_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_image_size,
        )
        all_image_batches, all_label_batches = [], []
        print('Manipulating image data to be right shape')
        for i in range(self.batch_size):
            image_batch = images[i * examples_per_batch:(i + 1) *
                                 examples_per_batch]

            if self.datasource == 'omniglot':
                # omniglot augments the dataset by rotating digits to create new classes
                # get rotation per class (e.g. 0,1,2,0,0 if there are 5 classes)
                rotations = tf.multinomial(tf.log([[1., 1., 1., 1.]]),
                                           self.num_classes)
            label_batch = tf.convert_to_tensor(labels)
            new_list, new_label_list = [], []
            for k in range(self.num_samples_per_class):
                class_idxs = tf.range(0, self.num_classes)
                class_idxs = tf.random_shuffle(class_idxs)

                true_idxs = class_idxs * self.num_samples_per_class + k
                new_list.append(tf.gather(image_batch, true_idxs))
                if self.datasource == 'omniglot':  # and FLAGS.train:
                    new_list[-1] = tf.stack([
                        tf.reshape(
                            tf.image.rot90(tf.reshape(
                                new_list[-1][ind],
                                [self.img_size[0], self.img_size[1], 1]),
                                           k=tf.cast(
                                               rotations[0, class_idxs[ind]],
                                               tf.int32)), (self.dim_input, ))
                        for ind in range(self.num_classes)
                    ])
                new_label_list.append(tf.gather(label_batch, true_idxs))
            new_list = tf.concat(
                new_list, 0
            )  # has shape [self.num_classes*self.num_samples_per_class, self.dim_input]
            new_label_list = tf.concat(new_label_list, 0)
            all_image_batches.append(new_list)
            all_label_batches.append(new_label_list)
        all_image_batches = tf.stack(all_image_batches)
        all_label_batches = tf.stack(all_label_batches)
        all_label_batches = tf.one_hot(all_label_batches, self.num_classes)
        return all_image_batches, all_label_batches
def load_examples():
    if a.input_dir is None or not os.path.exists(a.input_dir):
        raise Exception("input_dir does not exist")

    input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
    decode = tf.image.decode_jpeg
    if len(input_paths) == 0:
        input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
        decode = tf.image.decode_png

    if len(input_paths) == 0:
        raise Exception("input_dir contains no image files")

    def get_name(path):
        name, _ = os.path.splitext(os.path.basename(path))
        return name

    # if the image names are numbers, sort by the value rather than asciibetically
    # having sorted inputs means that the outputs are sorted in test mode
    if all(get_name(path).isdigit() for path in input_paths):
        input_paths = sorted(input_paths, key=lambda path: int(get_name(path)))
    else:
        input_paths = sorted(input_paths)

    with tf.name_scope("load_images"):
        path_queue = tf.train.string_input_producer(input_paths, shuffle=False)
        reader = tf.WholeFileReader()
        paths, contents = reader.read(path_queue)
        raw_input = decode(contents)
        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
        with tf.control_dependencies([assertion]):
            raw_input = tf.identity(raw_input)

        raw_input.set_shape([None, None, 3])


        # break apart image pair and move to range [-1, 1]
        width = tf.shape(raw_input)[1] # [height, width, channels]
        images = preprocess(raw_input[:,:,:])
        inputs=images




    # synchronize seed for image operations so that we do the same operations to both
    # input and output images
    seed = random.randint(0, 2**31 - 1)
    def transform(image):
        r = image


        r = tf.image.resize_images(r, [a.scale_size, a.scale_size], method=tf.image.ResizeMethod.AREA)

        return r

    with tf.name_scope("input_images"):
        input_images = transform(inputs)



    paths_batch, inputs_batch, targets_batch = tf.train.batch([paths, input_images, input_images], batch_size=a.batch_size)
    steps_per_epoch = int(math.ceil(len(input_paths) / a.batch_size))

    return Examples(
        paths=paths_batch,
        inputs=inputs_batch,
        count=len(input_paths),
        steps_per_epoch=steps_per_epoch,
    )
示例#5
0
    def save_model(self, saver, sess, step):
        """
        save model with path error checking
        """
        if not os.path.exists(os.path.join(self.model_path, 'Checkpoints')):
            os.makedirs(os.path.join(self.model_path, 'Checkpoints'))
        my_path = os.path.join(self.model_path, 'Checkpoints\my_model.ckpt')
        saver.save(sess, os.path.join(os.getcwd(), my_path), global_step=step)



if __name__ == "__main__":
    
    filenames= [('C:/Users/Daniel/Desktop/myc_e2f_data/figureD/distribution_myc_ef_2_%d.png' %i) for i in range(1,501)]
    filename_queue = tf.train.string_input_producer(filenames, num_epochs=None)
    reader = tf.WholeFileReader()
    _, content = reader.read(filename_queue)
    
    image = tf.image.decode_png(content, channels=3)
    image = tf.cast(image, tf.float32)
    resized_image= tf.image.resize_images(image, [64, 64])
    result = tf.train.shuffle_batch([resized_image], batch_size=300, capacity=500, min_after_dequeue=100)
    
    #print(result)
    #pdb.set_trace()
    
    train_size=500
    valid_size=300
    
    with tf.Session() as sess:
        sess.run(tf.local_variables_initializer())
示例#6
0
def single_JPEGimage_reader(filename_queue):
    image_reader = tf.WholeFileReader()
    _, image_file = image_reader.read(filename_queue)
    image = (tf.to_float(tf.image.decode_jpeg(image_file, channels=3)))
    image = tf.image.resize_images(image,[HEIGHT,WIDTH],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    return image
# read csv
csv_file = tf.train.string_input_producer(["./label.csv"], shuffle=True)
csv_reader = tf.TextLineReader()
_, line = csv_reader.read(csv_file)

image_file, label_decoded = tf.decode_csv(line, record_defaults=[[""], [""]])
image_decoded = tf.image.decode_jpeg(tf.read_file(image_file), channels=1)
image_cast = tf.cast(image_decoded, tf.float32)
image = tf.reshape(image_cast,
                   [IMAGE_WIDTH, IMAGE_HEIGHT, 1])  # 64 by 64 , grayscale

test_batch = int(12500 / BATCH_SIZE)
test_image_list = [
    './resize_test/' + file_name for file_name in os.listdir('./resize_test/')
]
test_image_reader = tf.WholeFileReader()
test_image_name = tf.train.string_input_producer(test_image_list)
_, value = test_image_reader.read(test_image_name)
test_image_decode = tf.cast(tf.image.decode_jpeg(value, channels=1),
                            tf.float32)
test_image = tf.reshape(test_image_decode, [IMAGE_WIDTH, IMAGE_HEIGHT, 1])

# make batch file
image_batch, label_batch, test_batch_x = tf.train.shuffle_batch(
    [image, label_decoded, test_image],
    batch_size=BATCH_SIZE,
    num_threads=4,
    capacity=50000,
    min_after_dequeue=10000)

# start session
示例#8
0
ran_lst = []
PARTION_SIZE = 1000

if __name__ == "__main__":
    md5_label_f = open("../data/md5_ctype", "r")
    for items in md5_label_f.readlines():
        try:
            md5, ctype = items.strip().split("\t")
            if len(md5) > 16:
                md5_lst += [md5]
                md5_dic[md5] = ctype
            elif md5 not in md5_dic:
                continue
            else:
                logger.warning("wrong length of md5ctype:%s" % items.strip())
        except:
            logger.warning("wrong formats of md5ctype:%s" % items.strip())

    random.shuffle(md5_lst)
    index = 0
    while index < len(md5_lst):
        ran_lst += [md5_lst[index: min(index+PARTION_SIZE, len(md5_lst))]]
        index += PARTION_SIZE

    print len(ran_lst)
    for ran_part in ran_lst:
        for md5 in ran_part:
            ctype = md5_dic[md5]
            key, value = tf.WholeFileReader().read(tf.train.string_input_producer([".".join(("../data/" + md5, "jpg"))]))
            tf_image = tf.image.decode_png(value) # use png or jpg decoder based on your files.
示例#9
0
def create_input_pipeline(files,
                          batch_size,
                          n_epochs,
                          shape,
                          crop_shape=None,
                          crop_factor=1.0,
                          n_threads=2):
    """Creates a pipefile from a list of image files.
    Includes batch generator/central crop/resizing options.
    The resulting generator will dequeue the images batch_size at a time until
    it throws tf.errors.OutOfRangeError when there are no more images left in
    the queue.

    Parameters
    ----------
    files : list
        List of paths to image files.
    batch_size : int
        Number of image files to load at a time.
    n_epochs : int
        Number of epochs to run before raising tf.errors.OutOfRangeError
    shape : list
        [height, width, channels]
    crop_shape : list
        [height, width] to crop image to.
    crop_factor : float
        Percentage of image to take starting from center.
    n_threads : int, optional
        Number of threads to use for batch shuffling
    """

    # We first create a "producer" queue.  It creates a production line which
    # will queue up the file names and allow another queue to deque the file
    # names all using a tf queue runner.
    # Put simply, this is the entry point of the computational graph.
    # It will generate the list of file names.
    # We also specify it's capacity beforehand.
    producer = tf.train.string_input_producer(files, capacity=len(files))

    # We need something which can open the files and read its contents.
    reader = tf.WholeFileReader()

    # We pass the filenames to this object which can read the file's contents.
    # This will create another queue running which dequeues the previous queue.
    keys, vals = reader.read(producer)

    # And then have to decode its contents as we know it is a jpeg image
    imgs = tf.image.decode_jpeg(
        vals, channels=3 if len(shape) > 2 and shape[2] == 3 else 0)

    # We have to explicitly define the shape of the tensor.
    # This is because the decode_jpeg operation is still a node in the graph
    # and doesn't yet know the shape of the image.  Future operations however
    # need explicit knowledge of the image's shape in order to be created.
    imgs.set_shape(shape)

    # Next we'll centrally crop the image to the size of 100x100.
    # This operation required explicit knowledge of the image's shape.
    if shape[0] > shape[1]:
        rsz_shape = [
            int(shape[0] / shape[1] * crop_shape[0] / crop_factor),
            int(crop_shape[1] / crop_factor)
        ]
    else:
        rsz_shape = [
            int(crop_shape[0] / crop_factor),
            int(shape[1] / shape[0] * crop_shape[1] / crop_factor)
        ]
    rszs = tf.image.resize_images(imgs, rsz_shape)
    crops = (tf.image.resize_image_with_crop_or_pad(rszs, crop_shape[0],
                                                    crop_shape[1])
             if crop_shape is not None else imgs)

    # Now we'll create a batch generator that will also shuffle our examples.
    # We tell it how many it should have in its buffer when it randomly
    # permutes the order.
    min_after_dequeue = len(files) // 100

    # The capacity should be larger than min_after_dequeue, and determines how
    # many examples are prefetched.  TF docs recommend setting this value to:
    # min_after_dequeue + (num_threads + a small safety margin) * batch_size
    capacity = min_after_dequeue + (n_threads + 1) * batch_size

    # Randomize the order and output batches of batch_size.
    batch = tf.train.shuffle_batch([crops],
                                   enqueue_many=False,
                                   batch_size=batch_size,
                                   capacity=capacity,
                                   min_after_dequeue=min_after_dequeue,
                                   num_threads=n_threads)

    # alternatively, we could use shuffle_batch_join to use multiple reader
    # instances, or set shuffle_batch's n_threads to higher than 1.

    return batch
示例#10
0
def read_data(filename_queue):
    reader = tf.WholeFileReader()
    _, image = reader.read(filename_queue)
    image = tf.image.per_image_standardization(
        tf.image.decode_jpeg(image, channels=1))
    return tf.image.random_flip_left_right(image)
示例#11
0
def load_images(data_root):
    filename_queue = tf.train.string_input_producer(data_root)
    image_reader = tf.WholeFileReader()
    key, image_file = image_reader.read(filename_queue)
    image = tf.image.decode_jpeg(image_file)
    return image, key
示例#12
0
def main(_):
  os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu_id

  if not tf.gfile.Exists(FLAGS.output_dir):
    tf.gfile.MakeDirs(FLAGS.output_dir)

  with tf.Graph().as_default() as g:
    with open(FLAGS.input_fname, 'r') as f:
      filenames = [line.split(',')[0][:-4] for line in f.readlines()]
      filenames = [
          os.path.join(FLAGS.image_dir, name) for name in filenames \
              if not os.path.exists(os.path.join(FLAGS.output_dir, name + '.npy'))
      ]

    filename_queue = tf.train.string_input_producer(filenames)
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    image = tf.image.decode_jpeg(value, channels=3)
    image_size = resnet_v1.resnet_v1.default_image_size
    processed_image = vgg_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False
    )
    processed_images, keys = tf.train.batch(
        [processed_image, key],
        FLAGS.batch_size,
        num_threads=8, capacity=8*FLAGS.batch_size*5,
        allow_smaller_final_batch=True
    )

    # Create the model
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
      net, end_points = resnet_v1.resnet_v1_101(
          processed_images, num_classes=1000, is_training=False
      )
      init_fn = slim.assign_from_checkpoint_fn(
          FLAGS.checkpoint_dir, slim.get_model_variables()
      )
      pool5 = g.get_operation_by_name('resnet_v1_101/pool5').outputs[0]
      pool5 = tf.transpose(pool5, perm=[0, 3, 1, 2])  # (batch_size, 2048, 1, 1)

      with tf.Session() as sess:
        init_fn(sess)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        try:
          for step in tqdm(
              xrange(len(filenames) / FLAGS.batch_size + 1), ncols=70
          ):
            if coord.should_stop():
              break
            file_names, pool5_value = sess.run([keys, pool5])
            for i in xrange(len(file_names)):
              np.save(
                  os.path.join(
                      FLAGS.output_dir,
                      os.path.basename(file_names[i]) + '.npy'
                  ),
                  pool5_value[i].astype(np.float32)
              )
        except tf.errors.OutOfRangeError:
          print "Done feature extraction -- epoch limit reached"
        finally:
          coord.request_stop()

        coord.join(threads)
示例#13
0
import tensorflow as tf
from PIL import Image

IMAGE_PATH = '../data/Face00001.png'
LABEL_PATH = '../data/Label.csv'
IMAGE_LIST = [IMAGE_PATH]
LABEL_LIST = [LABEL_PATH]
IMAGE_WIDTH = 49
IMAGE_HEIGHT = 61

label_queue = tf.train.string_input_producer(LABEL_LIST)
image_queue = tf.train.string_input_producer(IMAGE_LIST)

reader_csv = tf.TextLineReader()
reader_image = tf.WholeFileReader()

label_key, label_value = reader_csv.read(label_queue)
key, value = reader_image.read(image_queue)

image_decoded = tf.image.decode_png(value)
label_decoded = tf.decode_csv(label_value, record_defaults=[[0]])

x = tf.cast(image_decoded, tf.float32)
y_ = tf.cast(label_decoded, tf.float32)
y_ = tf.reshape(y_, [-1, 1])

w1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32]))
b1 = tf.Variable(tf.zeros([32]))
x_image = tf.reshape(x, [-1, IMAGE_WIDTH, IMAGE_HEIGHT, 1])
conv1 = tf.nn.conv2d(x_image, w1, strides=[1, 1, 1, 1], padding='SAME')
h1 = tf.nn.relu(conv1 + b1)
示例#14
0
def read_in_data(file_path):
    ##################################### READ IN DATA #####################################
    # Make a queue of file names including all the JPEG images files in the relative
    # image directory.
    file_names = tf.train.match_filenames_once(file_path + "/*.jpg")
    filename_queue = tf.train.string_input_producer(file_names)

    # Used to read the entire file
    file_reader = tf.WholeFileReader()

    # Read a whole file from the queue. Not entirely sure how this works but it returns
    # a key and value, that we can use to iterate through the entire file.
    key, value = file_reader.read(filename_queue)

    #Array to hold our image data and also our labels
    data = []
    lab = []

    with tf.Session() as sess:

        tf.initialize_all_variables().run()

        # Coordinate the loading of image files.
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        seen = set()

        # print len(file_names.eval())
        # print len(set(file_names.eval()))

        for i in range(len(file_names.eval())):
            # I think what happens when you call eval on either key or value, it will iterate both key and
            # val to the next item in the queue. I tried calling key.eval and value.eval both and that f****d it up.
            # Key here is the file's name. Value is actually the file.
            file_name = key.eval()
            labels = file_name.decode().split("-")

            if (i % 100 == 0 and i != 0):
                print i

            if file_name in seen:
                print("seen:") + file_name
            else:
                seen.add(file_name)

            #  [male, female]
            # labels[1][1] is race and sex
            # if labels[1][1] == 'M': #'AM' stands for Asian Male, but I only have AM and AF currently in the file
            #     lab.append([1, 0])
            # else: # 0 for girls
            #     lab.append([0, 1])

            # [Happy Open, Happy Closed, Neutral, Angry, Fear]
            image_emotion = labels[4][:-4].split('.')[0]
            if (image_emotion == 'HO'):
                lab.append([1, 0, 0, 0, 0])
            elif (image_emotion == 'HC'):
                lab.append([0, 1, 0, 0, 0])
            elif (image_emotion == 'N'):
                lab.append([0, 0, 1, 0, 0])
            elif (image_emotion == 'A'):
                lab.append([0, 0, 0, 1, 0])
            elif (image_emotion == 'F'):
                lab.append([0, 0, 0, 0, 1])
            else:
                print('ERROR: UNEXPECTED LABEL RECEIVED')
                sys.exit()

            # Read the in the image and decode it. It might be possible here to use "value" instead of,
            # but I'm not sure. You guys can mess with it.
            my_img = tf.image.decode_jpeg(tf.read_file(file_name), channels=1)
            image = tf.image.resize_images(my_img, 31, 32)
            leh = tf.reshape(image, [1, 992])
            data.append(leh.eval()[0])

        coord.request_stop()
        coord.join(threads)

    print("Done reading images.")
    return data, lab
示例#15
0
def get_loader(root,
               batch_size,
               scale_size,
               data_format,
               split=None,
               is_grayscale=False,
               seed=None):
    dataset_name = os.path.basename(root)

    if dataset_name in ['CelebA'] and split:
        root = os.path.join(root, 'splits', split)

    print("Root is {}".format(root))

    for ext in ["jpg", "png"]:
        paths = glob("{}/*.{}".format(root, ext))

        if ext == "jpg":
            tf_decode = tf.image.decode_jpeg
        elif ext == "png":
            tf_decode = tf.image.decode_png

        if len(paths) != 0:
            break

    with Image.open(paths[0]) as img:
        w, h = img.size
        shape = [h, w, 3]

    filename_queue = tf.train.string_input_producer(list(paths),
                                                    shuffle=False,
                                                    seed=seed)
    reader = tf.WholeFileReader()
    filename, data = reader.read(filename_queue)
    image = tf_decode(data, channels=3)

    if is_grayscale:
        image = tf.image.rgb_to_grayscale(image)
    image.set_shape(shape)

    min_after_dequeue = 5000
    capacity = min_after_dequeue + 3 * batch_size

    queue = tf.train.shuffle_batch([image],
                                   batch_size=batch_size,
                                   num_threads=4,
                                   capacity=capacity,
                                   min_after_dequeue=min_after_dequeue,
                                   name='synthetic_inputs')

    if dataset_name in ['CelebA']:
        queue = tf.image.crop_to_bounding_box(queue, 50, 25, 128, 128)
        queue = tf.image.resize_nearest_neighbor(queue,
                                                 [scale_size, scale_size])
    else:
        queue = tf.image.resize_nearest_neighbor(queue,
                                                 [scale_size, scale_size])

    if data_format == 'NCHW':
        queue = tf.transpose(queue, [0, 3, 1, 2])
    elif data_format == 'NHWC':
        pass
    else:
        raise Exception("[!] Unkown data_format: {}".format(data_format))

    return tf.to_float(queue)
示例#16
0
    def load_train_batch(self):
        """Load a batch of training instances.
        """
        opt = self.opt

        # Load the list of training files into queues
        file_list = self.format_file_list(opt.dataset_dir, 'train')

        # file_list['image_file_list']
        # '/userhome/34/h3567721/dataset/kitti/kitti_raw_eigen/2011_09_30_drive_0034_sync_03/0000000558.jpg'

        # file_list['cam_file_list']
        # '/userhome/34/h3567721/dataset/kitti/kitti_raw_eigen/2011_09_30_drive_0034_sync_03/0000000558_cam.txt'

        image_paths_queue = tf.train.string_input_producer(
            file_list['image_file_list'], shuffle=False)
        cam_paths_queue = tf.train.string_input_producer(
            file_list['cam_file_list'], shuffle=False)

        # Load images
        img_reader = tf.WholeFileReader()
        _, image_contents = img_reader.read(image_paths_queue)
        image_seq = tf.image.decode_jpeg(image_contents)

        # tgt_image: (128, 416, 3)
        # src_image_stack: (128, 416, 3)
        tgt_image, src_image_stack = \
            self.unpack_image_sequence(
                image_seq, opt.img_height, opt.img_width, opt.num_source)

        # Load camera intrinsics
        cam_reader = tf.TextLineReader()
        _, raw_cam_contents = cam_reader.read(cam_paths_queue)

        rec_def = []
        for i in range(9):
            rec_def.append([1.])

        # rec_def: [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0]]

        raw_cam_vec = tf.decode_csv(raw_cam_contents, record_defaults=rec_def)
        raw_cam_vec = tf.stack(raw_cam_vec)
        intrinsics = tf.reshape(raw_cam_vec, [3, 3])

        # ------------------------------------------------------

        # Form training batches
        seed = random.randint(0, 2**31 - 1)

        min_after_dequeue = 2048
        # 2048 * 32 * 4 = 262144
        capacity = min_after_dequeue + opt.num_threads * opt.batch_size

        # src_image_stack: (4,128,416,6)
        # tgt_image: (4,128,416,3)
        # intrinsics: (4,3,3)
        src_image_stack, tgt_image, intrinsics = \
            tf.train.shuffle_batch([src_image_stack, tgt_image, intrinsics], opt.batch_size,
                                    capacity, min_after_dequeue, opt.num_threads, seed)

        # Data augmentation
        image_all = tf.concat([tgt_image, src_image_stack], axis=3)

        image_all, intrinsics = self.data_augmentation(image_all, intrinsics,
                                                       opt.img_height,
                                                       opt.img_width)

        tgt_image = image_all[:, :, :, :3]  # (4,128,416,3)
        src_image_stack = image_all[:, :, :, 3:]  # (4,128,416,6)

        # intrinsics_mscale (4,4,3,3)
        intrinsics = self.get_multi_scale_intrinsics(intrinsics,
                                                     opt.num_scales)

        return tgt_image, src_image_stack, intrinsics
示例#17
0
def load_examples():
    # 判断input路径是否存在
    if a.input_dir is None or not os.path.exists(a.input_dir):
        raise Exception("input_dir does not exist")
    '''
    glob.glob(pathname)
        pathname是一个字符串,允许出现“*”、“?”、"["、"]"等通配符,不能含有“~”
        这个函数根据pathname筛选匹配的路径
        这个函数是通过配合使用os.listdir()与fnmatch.fnmatch()实现的,而非调用一个子shell
    '''

    # 匹配出图片路径
    input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
    decode = tf.image.decode_jpeg
    if len(input_paths) == 0:
        input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
        decode = tf.image.decode_png

    if len(input_paths) == 0:
        raise Exception("input_dir contains no image files")
    '''
    os.path.basename(path)
        获取路径最后的文件名,当路径以“\”结尾时,返回空字符串
    os.path.splitext(path)
        分离文件路径的文件名和扩展名
    '''
    def get_name(path):
        name, _ = os.path.splitext(os.path.basename(path))
        return name

    '''
    string.isdigit()
        判断字符串是否全为数字组成
    all()
        当元素全为True时返回True,否则返回False
    ang()
        当元素全为False时返回True,否则返回False
    '''

    # 排序
    # if the image names are numbers, sort by the value rather than asciibetically
    # having sorted inputs means that the outputs are sorted in test mode
    if all(get_name(path).isdigit() for path in input_paths):
        input_paths = sorted(input_paths, key=lambda path: int(get_name(path)))
    else:
        input_paths = sorted(input_paths)

    with tf.name_scope("load_images"):
        # 使用文件名队列读取数据
        path_queue = tf.train.string_input_producer(input_paths,
                                                    shuffle=a.mode == "train")
        reader = tf.WholeFileReader()
        paths, contents = reader.read(path_queue)
        # decode图片,转换到[0,1]区间
        raw_input = decode(contents)
        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        # 断言图片必须有三通道
        assertion = tf.assert_equal(tf.shape(raw_input)[2],
                                    3,
                                    message="image does not have 3 channels")
        with tf.control_dependencies([assertion]):
            raw_input = tf.identity(raw_input)

        raw_input.set_shape([None, None, 3])

        # 是否转换到LAP颜色空间
        # 把数据转换到[-1,1]区间
        if a.lab_colorization:
            # load color and brightness from image, no B image exists here
            lab = rgb_to_lab(raw_input)
            L_chan, a_chan, b_chan = preprocess_lab(lab)
            a_images = tf.expand_dims(L_chan, axis=2)
            b_images = tf.stack([a_chan, b_chan], axis=2)
        else:
            # break apart image pair and move to range [-1, 1]
            width = tf.shape(raw_input)[1]  # [height, width, channels]
            a_images = preprocess(raw_input[:, :width // 2, :])
            b_images = preprocess(raw_input[:, width // 2:, :])

    # 样例图中,左侧为有雾图像,右侧为无雾图像
    if a.which_direction == "AtoB":
        inputs, targets = [a_images, b_images]
    elif a.which_direction == "BtoA":
        inputs, targets = [b_images, a_images]
    else:
        raise Exception("invalid direction")

    # synchronize seed for image operations so that we do the same operations to both
    # input and output images
    seed = random.randint(0, 2**31 - 1)

    # 变换图像
    # 水平翻转、裁切
    def transform(image):
        r = image
        if a.flip:
            r = tf.image.random_flip_left_right(r, seed=seed)

        # area produces a nice downscaling, but does nearest neighbor for upscaling
        # assume we're going to be doing downscaling here
        r = tf.image.resize_images(r, [a.scale_size, a.scale_size],
                                   method=tf.image.ResizeMethod.AREA)

        offset = tf.cast(tf.floor(
            tf.random_uniform([2], 0, a.scale_size - CROP_SIZE + 1,
                              seed=seed)),
                         dtype=tf.int32)
        if a.scale_size > CROP_SIZE:
            r = tf.image.crop_to_bounding_box(r, offset[0], offset[1],
                                              CROP_SIZE, CROP_SIZE)
        elif a.scale_size < CROP_SIZE:
            raise Exception("scale size cannot be less than crop size")
        return r

    with tf.name_scope("input_images"):
        input_images = transform(inputs)

    with tf.name_scope("target_images"):
        target_images = transform(targets)

    # 分割batch、epoch
    paths_batch, inputs_batch, targets_batch = tf.train.batch(
        [paths, input_images, target_images], batch_size=a.batch_size)
    steps_per_epoch = int(math.ceil(len(input_paths) / a.batch_size))

    return Examples(
        paths=paths_batch,
        inputs=inputs_batch,
        targets=targets_batch,
        count=len(input_paths),
        steps_per_epoch=steps_per_epoch,
    )
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)

    # Read list of images.
    tf.logging.info('Reading list of images...')
    image_paths = _ReadImageList(cmd_args.list_images_path)
    num_images = len(image_paths)
    tf.logging.info('done! Found %d images', num_images)

    # Parse DelfConfig proto.
    config = delf_config_pb2.DelfConfig()
    with tf.gfile.FastGFile(cmd_args.config_path, 'r') as f:
        text_format.Merge(f.read(), config)

    # Create output directory if necessary.
    if not os.path.exists(cmd_args.output_dir):
        os.makedirs(cmd_args.output_dir)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Reading list of images.
        filename_queue = tf.train.string_input_producer(image_paths,
                                                        shuffle=False)
        print("PRINTING THE fileNAME QUEUE", filename_queue)
        reader = tf.WholeFileReader()
        print("PRINTING THE reader", reader)
        _, value = reader.read(filename_queue)
        print("PRINTING the VALUE", value)
        image_tf = tf.image.decode_jpeg(value, channels=3)
        print("THE IMAGE", image_tf)
        """
    ADDED FOR CHECKING PURPOSE ONLY
    """
        imagetest = image_tf
        imagetest = tf.expand_dims(imagetest, 0)
        print("size of tensor === ", imagetest)
        print("THE IMAGE FORMED", imagetest)
        print("MAKING FIRST FUNCTION")
        modelsssss = feature_extractor.BuildModel('resnet_v1_50/block3',
                                                  'softplus',
                                                  'use_l2_normalized_feature',
                                                  1)
        print("CALLING THE MODEL")
        buildedModel, _ = modelsssss(imagetest, False, False)
        print("CAME BACK TO ORIGINAL")

        with tf.Session() as sess:
            # Initialize variables.
            init_op = tf.global_variables_initializer()
            print("SHOWING OPERATION", init_op)
            tf.logging.info('Starting session...')
            sess.run(init_op)
            tf.logging.info('Starting to load the models to be used...')
            # Loading model that will be used.
            tf.saved_model.loader.load(sess,
                                       [tf.saved_model.tag_constants.SERVING],
                                       config.model_path)
            graph = tf.get_default_graph()
            input_image = graph.get_tensor_by_name('input_image:0')
            input_score_threshold = graph.get_tensor_by_name(
                'input_abs_thres:0')
            input_image_scales = graph.get_tensor_by_name('input_scales:0')
            input_max_feature_num = graph.get_tensor_by_name(
                'input_max_feature_num:0')
            boxes = graph.get_tensor_by_name('boxes:0')
            raw_descriptors = graph.get_tensor_by_name('features:0')
            feature_scales = graph.get_tensor_by_name('scales:0')
            attention_with_extra_dim = graph.get_tensor_by_name('scores:0')
            attention = tf.reshape(attention_with_extra_dim,
                                   [tf.shape(attention_with_extra_dim)[0]])

            locations, descriptors = feature_extractor.DelfFeaturePostProcessing(
                boxes, raw_descriptors, config)
            tf.logging.info('Finished loading the models ...')
            print('Hi')
            print('------------------------locations--------------- = ',
                  locations)

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            start = time.clock()
            for i in range(num_images):
                # Write to log-info once in a while.
                if i == 0:
                    tf.logging.info(
                        'Starting to extract DELF features from images...')
                elif i % _STATUS_CHECK_ITERATIONS == 0:
                    elapsed = (time.clock() - start)
                    tf.logging.info(
                        'Processing image %d out of %d, last %d '
                        'images took %f seconds', i, num_images,
                        _STATUS_CHECK_ITERATIONS, elapsed)
                    start = time.clock()

                # # Get next image.
                im = sess.run(image_tf)

                # If descriptor already exists, skip its computation.
                out_desc_filename = os.path.splitext(
                    os.path.basename(image_paths[i]))[0] + _DELF_EXT
                out_desc_fullpath = os.path.join(cmd_args.output_dir,
                                                 out_desc_filename)
                if tf.gfile.Exists(out_desc_fullpath):
                    tf.logging.info('Skipping %s', image_paths[i])
                    continue

                # Extract and save features.
                (locations_out, descriptors_out, feature_scales_out,
                 attention_out) = sess.run(
                     [locations, descriptors, feature_scales, attention],
                     feed_dict={
                         input_image:
                         im,
                         input_score_threshold:
                         config.delf_local_config.score_threshold,
                         input_image_scales:
                         list(config.image_scales),
                         input_max_feature_num:
                         config.delf_local_config.max_feature_num
                     })

                feature_io.WriteToFile(out_desc_fullpath, locations_out,
                                       feature_scales_out, descriptors_out,
                                       attention_out)

            # Finalize enqueue threads.
            coord.request_stop()
            coord.join(threads)
示例#19
0
seed = 5555
image_names = tf.convert_to_tensor(
    sorted(glob.glob(os.path.join(feat_dir, '*.jpg'))))
mask_names = tf.convert_to_tensor(
    sorted(glob.glob(os.path.join(mask_dir, '*.png'))))

feature_queue = tf.train.string_input_producer(image_names,
                                               shuffle=True,
                                               seed=seed)

mask_queue = tf.train.string_input_producer(mask_names,
                                            shuffle=True,
                                            seed=seed)

image_reader = tf.WholeFileReader()
image_key, image_file = image_reader.read(feature_queue)
image = tf.image.decode_image(image_file)

mask_reader = tf.WholeFileReader()
mask_key, mask_file = image_reader.read(mask_queue)
mask = tf.image.decode_image(mask_file)

image = tf.divide(image, 255)
mask = tf.divide(mask, 255)

## Do a random crop
## Important? Do the crop before batching.
image_mask = tf.concat([image, mask], 2)
image_mask = tf.random_crop(image_mask, [crop_size, crop_size, 4])
image, mask = tf.split(image_mask, [3, 1], axis=2)
示例#20
0
def dataloader(opt):
    if opt.input_dir is None or not os.path.exists(opt.input_dir):
        raise Exception("input_dir does not exist")

    # return list of paht of matched files
    input_path = glob.glob(os.path.join(opt.input_dir, "*.jpg"))
    decode = tf.image.decode_jpeg
    if len(input_path) == 0:
        input_path = glob.glob(os.path.join(opt.input_dir, "*.png"))
        decode = tf.image.decode_png

    if len(input_path) == 0:
        raise Exception("input_dir continput_dirains no image files")

    # if the image names are numbers, sort by the value rather than asciibetically
    # having sorted inputs means that the outputs are sorted in test mode
    if all(get_filename(path).isdigit() for path in input_path):
        input_path = sorted(input_path, key=lambda path: int(get_filename(path)))
    else:
        input_path = sorted(input_path)

    with tf.name_scope("load_images"):
        path_queue = tf.train.string_input_producer(input_path, shuffle=opt.mode == "train")
        reader = tf.WholeFileReader()
        paths, contents = reader.read(path_queue)
        raw_input = decode(contents)
        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
        with tf.control_dependencies([assertion]):
            raw_input = tf.identity(raw_input)

        raw_input.set_shape([None, None, 3])

        if opt.lab_colorization:
            # load color and brightness from image, no B image exists here
            lab = rgb_to_lab(raw_input)
            L_chan, a_chan, b_chan = preprocess_lab(lab)
            a_images = tf.expand_dims(L_chan, axis=2)
            b_images = tf.stack([a_chan, b_chan], axis=2)
        else:
            # break apart image pair and move to range [-1, 1]
            width = tf.shape(raw_input)[1]  # [height, width, channels]
            a_images = preprocess(raw_input[:, :width//2, :])
            b_images = preprocess(raw_input[:, width//2:, :])

    if opt.which_direction == "AtoB":
        inputs, targets = [a_images, b_images]
    elif opt.which_direction == "BtoA":
        inputs, targets = [b_images, a_images]
    else:
        raise Exception("invalid direction")

    # synchronize seed for image operations so that we do the same operations to both
    # input and output images
    seed = random.randint(0, 2**31 - 1)

    with tf.name_scope("input_images"):
        input_images = transform(inputs, opt)

    with tf.name_scope("target_images"):
        target_images = transform(targets, opt)

    paths_batch, inputs_batch, targets_batch = tf.train.batch([paths, input_images, target_images], batch_size=opt.batch_size)
    steps_per_epoch = int(math.ceil(len(input_path) / opt.batch_size))

    return Examples(
        paths=paths_batch,
        inputs=inputs_batch,
        targets=targets_batch,
        count=len(input_path),
        steps_per_epoch=steps_per_epoch,
    )
示例#21
0
    def load_train_batch(self):
        """Load a batch of training instances.
        """
        opt = self.opt

        # Load the list of training files into queues
        #TODO
        if opt.train_lite:
            file_list = self.format_file_list(opt.dataset_dir,
                                              opt.filelist_dir, 'train_lite')
        else:
            file_list = self.format_file_list(opt.dataset_dir,
                                              opt.filelist_dir, 'train')
        image_paths_queue = tf.train.string_input_producer(
            file_list['image_file_list'], shuffle=False)
        cam_paths_queue = tf.train.string_input_producer(
            file_list['cam_file_list'], shuffle=False)

        # Load camera intrinsics
        cam_reader = tf.TextLineReader()
        _, raw_cam_contents = cam_reader.read(cam_paths_queue)
        rec_def = []
        for i in range(9):
            rec_def.append([1.])
        raw_cam_vec = tf.decode_csv(raw_cam_contents, record_defaults=rec_def)
        raw_cam_vec = tf.stack(raw_cam_vec)
        intrinsics = tf.reshape(raw_cam_vec, [3, 3])

        # Load images
        img_reader = tf.WholeFileReader()
        _, image_contents = img_reader.read(image_paths_queue)
        image_seq = tf.image.decode_jpeg(image_contents)
        tgt_image, src_image_stack = \
            self.unpack_image_sequence(
                image_seq, opt.img_height, opt.img_width, opt.num_source)

        #TODO Load Semantics
        #     See cityscape label defs in https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/helpers/labels.py#L62
        #     Also notice that deeplabv3+ uses `train_id` https://github.com/tensorflow/models/blob/69b016449ffc797421bf003d8b7fd8545db866d7/research/deeplab/datasets/build_cityscapes_data.py#L46
        #     Color maps are in https://github.com/tensorflow/models/blob/69b016449ffc797421bf003d8b7fd8545db866d7/research/deeplab/utils/get_dataset_colormap.py#L207
        if opt.sem_assist:
            sem_paths_queue = tf.train.string_input_producer(
                file_list['sem_image_file_list'], shuffle=False)
            sem_reader = tf.WholeFileReader()
            sem_keys, sem_contents = sem_reader.read(sem_paths_queue)

            if opt.load_from_raw:
                sem_seq = tf.reshape(
                    tf.decode_raw(sem_contents, tf.uint8),
                    [1, opt.img_height, (opt.num_source + 1) * opt.img_width])
            else:
                sem_seq = tf.py_func(read_npy_file, [sem_keys], [
                    tf.uint8,
                ])

        #TODO Load Instances: we use COCO
        #     Two channels: class and id level. For id level we only use the edge
        if opt.ins_assist:
            ins_paths_queue = tf.train.string_input_producer(
                file_list['ins_image_file_list'], shuffle=False)
            ins_reader = tf.WholeFileReader()
            ins_keys, ins_contents = ins_reader.read(ins_paths_queue)

            if opt.load_from_raw:
                ins_seq = tf.reshape(tf.decode_raw(ins_contents, tf.uint8), [
                    1, opt.img_height, (opt.num_source + 1) * opt.img_width, 2
                ])
            else:
                ins_seq = tf.py_func(read_npy_file, [ins_keys], [
                    tf.uint8,
                ])

        #TODO 1. SHUFFLE BATCH
        # Form training batches
        seed = random.randint(0, 2**31 - 1)
        min_after_dequeue = 2048
        capacity = min_after_dequeue + opt.num_threads * opt.batch_size

        if opt.sem_assist and opt.ins_assist:
            src_image_stack, tgt_image, intrinsics, sem_seq, ins_seq = tf.train.shuffle_batch(
                [
                    src_image_stack, tgt_image, intrinsics, sem_seq[0],
                    ins_seq[0]
                ], opt.batch_size, capacity, min_after_dequeue,
                opt.num_threads, seed)

        elif opt.sem_assist:
            src_image_stack, tgt_image, intrinsics, sem_seq = tf.train.shuffle_batch(
                [src_image_stack, tgt_image, intrinsics, sem_seq[0]],
                opt.batch_size, capacity, min_after_dequeue, opt.num_threads,
                seed)

        elif opt.ins_assist:
            src_image_stack, tgt_image, intrinsics, ins_seq = tf.train.shuffle_batch(
                [src_image_stack, tgt_image, intrinsics, ins_seq[0]],
                opt.batch_size, capacity, min_after_dequeue, opt.num_threads,
                seed)

        else:
            src_image_stack, tgt_image, intrinsics = tf.train.shuffle_batch(
                [src_image_stack, tgt_image, intrinsics], opt.batch_size,
                capacity, min_after_dequeue, opt.num_threads, seed)

        # semantic segmentation
        tgt_sem = None
        tgt_sem_map = None
        tgt_sem_mask = None
        tgt_sem_edge = None
        src_sem_stack = None
        src_sem_map_stack = None
        src_sem_mask_stack = None
        src_sem_edge_stack = None

        # ins0 ~ instance level, but still class segmentation
        tgt_ins0 = None
        tgt_ins0_map = None
        tgt_ins0_edge = None
        src_ins0_stack = None
        src_ins0_map_stack = None
        src_ins0_edge_stack = None

        # ins1 ~ instance level, but this is id segmentation
        tgt_ins1_edge = None
        src_ins1_edge_stack = None

        #TODO 2. TRAMSFORMATION AND UNPACKING
        if opt.sem_assist:
            #TODO get one-hot encoded         sem_oh_seq (4,128,1248,19)X{0,1}
            sem_oh_seq = tf.cast(
                tf.one_hot(sem_seq, on_value=1, depth=opt.sem_num_class),
                tf.uint8)
            #TODO decouple   tgt_sem (4,128,1248,19)X{0,1}   src_sem_stack (4,128,1248,2*19)X{0,1}
            tgt_sem, src_sem_stack = self.unpack_sem_sequence_batch_atom(
                sem_oh_seq, opt.sem_num_class)

            #TODO get densemap     sem_map_seq (4,128,1248,1)X{0,1,...,18}
            sem_map_seq = tf.expand_dims(sem_seq, -1)
            #TODO decouple   tgt_sem_map (4,128,1248,1)X{0,1,...,18}   src_sem_map_stack (4,128,1248,2*1)X{0,1,...,18}
            tgt_sem_map, src_sem_map_stack = self.unpack_sem_sequence_batch_atom(
                sem_map_seq, 1)

            if opt.sem_mask_explore:
                #TODO get sem mask   sem_mask_seq (4,128,1248,c) here we assume c=1
                sem_mask_seq = self.get_sem_mask_batch(sem_seq)
                #TODO decouple   tgt_sem_mask (4,128,1248,c)   src_sem_mask_stack (4,128,1248,2*c)
                tgt_sem_mask, src_sem_mask_stack = self.unpack_sem_sequence_batch_atom(
                    sem_mask_seq, 1)

            if opt.sem_edge_explore:
                #TODO get sem edge   sem_edge_seq (4,128,1248,c) here we assume c=1
                sem_edge_seq = self.get_sem_edge_batch(sem_seq)
                #TODO decouple   tgt_sem_edge (4,128,1248,c)   src_sem_edge_stack (4,128,1248,2*c)
                tgt_sem_edge, src_sem_edge_stack = self.unpack_sem_sequence_batch_atom(
                    sem_edge_seq, 1)

        if opt.ins_assist:
            ins0_seq = ins_seq[:, :, :, 0]
            ins1_seq = ins_seq[:, :, :, 1]

            #TODO get one-hot  ins0_oh_seq (4,128,1248,81)X{0,1}
            ins0_oh_seq = tf.cast(
                tf.one_hot(ins0_seq, on_value=1, depth=opt.ins_num_class),
                tf.uint8)
            #ins1_oh_seq = tf.cast(tf.one_hot(ins1_seq, on_value=1, depth = 255), tf.uint8)

            #TODO decouple   tgt_ins0 (4,128,1248,81)X{0,1}   src_ins0_stack (4,128,1248,2*81)X{0,1}
            tgt_ins0, src_ins0_stack = self.unpack_sem_sequence_batch_atom(
                ins0_oh_seq, opt.ins_num_class)
            #tgt_ins1, src_ins1_stack = self.unpack_sem_sequence_batch_atom(ins1_oh_seq, opt.ins_num_class)

            #TODO get densemap  sem_ins0_seq (4,128,1248,1)X{0,1,...,80}
            ins0_map_seq = ins_seq[:, :, :, :1]
            ins1_map_seq = ins_seq[:, :, :, 1:]

            #TODO decouple  tgt_ins0_map (4,128,1248,1)X{0,1,...,80}  src_ins0_map_stack (4,128,1248,2*1)X{0,1,...,80}
            tgt_ins0_map, src_ins0_map_stack = self.unpack_sem_sequence_batch_atom(
                ins0_map_seq, 1)
            tgt_ins1_map, src_ins1_map_stack = self.unpack_sem_sequence_batch_atom(
                ins1_map_seq, 1)

            if opt.ins0_edge_explore:
                #TODO get edge   ins0_edge_seq (4,128,1248,c)  here we assume c=1
                ins0_edge_seq = self.get_sem_edge_batch(ins0_seq)
                #TODO decouple   tgt_ins0_edge (4,128,1248,c)  src_ins0_edge_stack (4,128,1248,2*c)
                tgt_ins0_edge, src_ins0_edge_stack = self.unpack_sem_sequence_batch_atom(
                    ins0_edge_seq, 1)

            if opt.ins1_edge_explore:
                #TODO get edge   ins1_edge_seq (4,128,1248,c) here we assume c=1
                ins1_edge_seq = self.get_sem_edge_batch(ins1_seq)
                #TODO decouple   tgt_ins1_edge (4,128,1248,c)   src_ins1_edge_stack (4,128,1248,2*c)
                tgt_ins1_edge, src_ins1_edge_stack = self.unpack_sem_sequence_batch_atom(
                    ins1_edge_seq, 1)

        #TODO 3. DATA AUGMENTATION
        image_all = tf.concat([tgt_image, src_image_stack], axis=3)
        image_all, intrinsics, aug_params = self.data_augmentation(
            image_all, intrinsics, opt.img_height,
            opt.img_width)  #TODO changed API

        if opt.sem_assist:
            ##TODO Do the same data augmentation for semantic segmentations
            tgt_sem, src_sem_stack = self.data_aug(tgt_sem, src_sem_stack,
                                                   aug_params, "bilinear")
            tgt_sem_map, src_sem_map_stack = self.data_aug(
                tgt_sem_map, src_sem_map_stack, aug_params, "neighbor")

            if self.opt.sem_mask_explore:
                tgt_sem_mask, src_sem_mask_stack = \
                    self.data_aug(tgt_sem_mask, src_sem_mask_stack, aug_params, "bilinear")

            if self.opt.sem_edge_explore:
                tgt_sem_edge, src_sem_edge_stack = \
                    self.data_aug(tgt_sem_edge, src_sem_edge_stack, aug_params, "bilinear")
                #TODO maybe transfer needs this settings self.data_aug(tgt_sem_edge, src_sem_edge_stack, aug_params, "neighbor")

        if opt.ins_assist:
            ##TODO Do the same data augmentation for instance segmentations
            tgt_ins0, src_ins0_stack = self.data_aug(tgt_ins0, src_ins0_stack,
                                                     aug_params, "bilinear")
            #tgt_ins1, src_ins1_stack = self.data_aug(tgt_ins1, src_ins1_stack, aug_params, "bilinear")

            tgt_ins0_map, src_ins0_map_stack = self.data_aug(
                tgt_ins0_map, src_ins0_map_stack, aug_params, "neighbor")
            #tgt_ins1_map, src_ins1_map_stack = self.data_aug(tgt_ins1_map, src_ins1_map_stack, aug_params, "neighbor")

            if self.opt.ins0_edge_explore:
                tgt_ins0_edge, src_ins0_edge_stack = \
                    self.data_aug(tgt_ins0_edge, src_ins0_edge_stack, aug_params, "bilinear")
                #TODO maybe transfer needs this settings self.data_aug(tgt_ins0_edge, src_ins0_edge_stack, aug_params, "neighbor")

            if self.opt.ins1_edge_explore:
                tgt_ins1_edge, src_ins1_edge_stack = \
                    self.data_aug(tgt_ins1_edge, src_ins1_edge_stack, aug_params, "bilinear")
                #TODO maybe transfer needs this settings self.data_aug(tgt_ins1_edge, src_ins1_edge_stack, aug_params, "neighbor")

        # 4. RETURN
        # image_channels=3*opt.seq_length
        tgt_image = image_all[:, :, :, :3]
        src_image_stack = image_all[:, :, :, 3:]  #3:image_channels]
        intrinsics = self.get_multi_scale_intrinsics(intrinsics,
                                                     opt.num_scales)

        # if opt.sem_assist and opt.ins_assist:
        return tgt_image, src_image_stack, intrinsics, \
                [tgt_sem, tgt_sem_map, tgt_sem_mask, tgt_sem_edge], \
                [src_sem_stack, src_sem_map_stack, src_sem_mask_stack, src_sem_edge_stack], \
                [tgt_ins0, tgt_ins0_map, tgt_ins0_edge, tgt_ins1_edge], \
                [src_ins0_stack, src_ins0_map_stack, src_ins0_edge_stack, src_ins1_edge_stack]
示例#22
0
    def make_data_tensor(self, training=True):
        """

		:param training:
		:return:
		"""
        if training:
            folders = self.metatrain_folders
            num_total_batches = self.total_batch_num
        else:
            folders = self.metaval_folders
            num_total_batches = 600

        if training and os.path.exists('filelist.pkl'):

            labels = np.arange(self.nway).repeat(self.nimg).tolist()
            with open('filelist.pkl', 'rb') as f:
                all_filenames = pickle.load(f)
                print('load episodes from file, len:', len(all_filenames))

        else:  # test or not existed.

            # 16 in one class, 16*5 in one task
            # [task1_0_img0, task1_0_img15, task1_1_img0,]
            all_filenames = []
            for _ in tqdm.tqdm(range(num_total_batches),
                               'generating episodes'):  # 200000
                # from image folder sample 5 class randomly
                sampled_folders = random.sample(folders, self.nway)
                random.shuffle(sampled_folders)
                # sample 16 images from selected folders, and each with label 0-4, (0/1..., path), orderly, no shuffle!
                # len: 5 * 16
                labels_and_images = get_images(sampled_folders,
                                               range(self.nway),
                                               nb_samples=self.nimg,
                                               shuffle=False)

                # make sure the above isn't randomized order
                labels = [li[0] for li in labels_and_images]
                filenames = [li[1] for li in labels_and_images]
                all_filenames.extend(filenames)

            if training:  # only save for training.
                with open('filelist.pkl', 'wb') as f:
                    pickle.dump(all_filenames, f)
                    print('save all file list to filelist.pkl')

        # make queue for tensorflow to read from
        print('creating pipeline ops')
        filename_queue = tf.train.string_input_producer(
            tf.convert_to_tensor(all_filenames), shuffle=False)
        image_reader = tf.WholeFileReader()
        _, image_file = image_reader.read(filename_queue)

        image = tf.image.decode_jpeg(image_file, channels=3)
        # tensorflow format: N*H*W*C
        image.set_shape((self.imgsz[0], self.imgsz[1], 3))
        # reshape(image, [84*84*3])
        image = tf.reshape(image, [self.dim_input])
        # convert to range(0,1)
        image = tf.cast(image, tf.float32) / 255.0

        examples_per_batch = self.nway * self.nimg  # 5*16
        # batch here means batch of meta-learning, including 4 tasks = 4*80
        batch_image_size = self.meta_batchsz * examples_per_batch  # 4* 80

        print('batching images')
        images = tf.train.batch(
            [image],
            batch_size=batch_image_size,  # 4*80
            num_threads=self.meta_batchsz,
            capacity=256 + 3 * batch_image_size,  # 256 + 3* 4*80
        )

        all_image_batches, all_label_batches = [], []
        print('manipulating images to be right order')
        # images contains current batch, namely 4 task, 4* 80
        for i in range(self.meta_batchsz):  # 4
            # current task, 80 images
            image_batch = images[i * examples_per_batch:(i + 1) *
                                 examples_per_batch]

            # as all labels of all task are the same, which is 0,0,..1,1,..2,2,..3,3,..4,4...
            label_batch = tf.convert_to_tensor(labels)
            new_list, new_label_list = [], []
            # for each image from 0 to 15 in all 5 class
            for k in range(self.nimg):  # 16
                class_idxs = tf.range(0, self.nway)  # 0-4
                class_idxs = tf.random_shuffle(class_idxs)
                # it will cope with 5 images parallelly
                #    [0, 16, 32, 48, 64] or [1, 17, 33, 49, 65]
                true_idxs = class_idxs * self.nimg + k
                new_list.append(tf.gather(image_batch, true_idxs))

                new_label_list.append(tf.gather(label_batch, true_idxs))

            # [80, 84*84*3]
            new_list = tf.concat(
                new_list, 0
            )  # has shape [self.num_classes*self.num_samples_per_class, self.dim_input]
            # [80]
            new_label_list = tf.concat(new_label_list, 0)
            all_image_batches.append(new_list)
            all_label_batches.append(new_label_list)

        # [4, 80, 84*84*3]
        all_image_batches = tf.stack(all_image_batches)
        # [4, 80]
        all_label_batches = tf.stack(all_label_batches)
        # [4, 80, 5]
        all_label_batches = tf.one_hot(all_label_batches, self.nway)

        print('image_b:', all_image_batches)
        print('label_onehot_b:', all_label_batches)

        return all_image_batches, all_label_batches
示例#23
0
import tensorflow as tf

image_filename = "./images/chapter-05-object-recognition-and-classification/working-with-images/test-input-image.jpg"
# 获得文件名列表
filename_queue = tf.train.string_input_producer(
    tf.train.match_filenames_once(image_filename))
filename_queue1 = tf.train.string_input_producer(
    tf.train.match_filenames_once(image_filename))
# 生成文件名队列
image_reader = tf.WholeFileReader()
image_reader1 = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
_, image_file1 = image_reader1.read(filename_queue1)
# 通过阅读器返回一个键值对,其中value表示图像
image = tf.image.decode_jpeg(image_file)
image1 = tf.image.decode_jpeg(image_file1)
# 通过tf.image.decode_jpeg解码函数对图片进行解码,得到图像.
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(),
                   tf.local_variables_initializer())
init_op1 = tf.group(tf.global_variables_initializer(),
                    tf.local_variables_initializer())

sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
coord1 = tf.train.Coordinator()
print('the image is:', sess.run(image))
filename_queue.close(cancel_pending_enqueues=True)
# filename_queue1.close(cancel_pending_enqueue=True)
coord.request_stop()
示例#24
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Lenovo
# @Date:   2019-05-25 11:18:55
# @Last Modified by:   zy19950209
# @Last Modified time: 2019-05-25 13:08:22
import tensorflow as tf
with tf.Session() as sess:
    filename = [
        './mnist_data/A.jpg', './mnist_data/B.jpg', './mnist_data/C.jpg'
    ]
    filename_queue = tf.train.string_input_producer(filename,
                                                    shuffle=False,
                                                    num_epochs=5)
    reader = tf.WholeFileReader()  # 读取所有数据
    key, value = reader.read(filename)
    tf.local_variables_initializer().run()
    threads = tf.train.start_queue_runners(sess=sess)
    i = 0
    while True:
        i = i + 1
        image_data = sess.run(value)
        with open('read/test_%d.jpg' % i, 'wb') as f:
            f.write(image_data)
示例#25
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)

  # Read list of images.
  tf.logging.info('Reading list of images...')
  image_paths = _ReadImageList(cmd_args.list_images_path, cmd_args.prefix)
  num_images = len(image_paths)
  tf.logging.info('done! Found %d images', num_images)

  # Parse DelfConfig proto.
  config = delf_config_pb2.DelfConfig()
  with tf.gfile.FastGFile(cmd_args.config_path, 'r') as f:
    text_format.Merge(f.read(), config)

  # Tell TensorFlow that the model will be built into the default Graph.
  with tf.Graph().as_default():
    # Reading list of images.
    filename_queue = tf.train.string_input_producer(image_paths, shuffle=False)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    image_tf = tf.image.decode_jpeg(value, channels=3)
    image_tf = tf.image.resize(image_tf, (448, 448))

    with tf.Session() as sess:
      init_op = tf.global_variables_initializer()
      sess.run(init_op)

      extractor_fn = extractor.MakeExtractor(sess, config)

      # Start input enqueue threads.
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(sess=sess, coord=coord)
      start = time.clock()
      for i in range(num_images):
        # Write to log-info once in a while.
        if i == 0:
          tf.logging.info('Starting to extract DELF features from images...')
        elif i % _STATUS_CHECK_ITERATIONS == 0:
          elapsed = (time.clock() - start)
          tf.logging.info(
              'Processing image %d out of %d, last %d '
              'images took %f seconds', i, num_images, _STATUS_CHECK_ITERATIONS,
              elapsed)
          start = time.clock()

        # # Get next image.
        im = sess.run(image_tf)

        # If descriptor already exists, skip its computation.
        save_path = image_paths[i]
        save_path = save_path.replace('undist_images', 'reg_feat')
        save_path = save_path.replace('.jpg', '.bin')
        if cmd_args.skip_extracted and tf.gfile.Exists(save_path):
          tf.logging.info('Skipping %s', image_paths[i])
          continue

        # Extract and save features.
        reg_feat_out = extractor_fn(im)
        reg_feat_out = np.squeeze(reg_feat_out, axis=0)

        dir_name = os.path.dirname(save_path)
        if not os.path.exists(dir_name):
            os.mkdir(dir_name)
        reg_feat_out.astype(np.float32).tofile(save_path)

      # Finalize enqueue threads.
      coord.request_stop()
      coord.join(threads)
示例#26
0
def data_loader(FLAGS):
    with tf.device('/cpu:0'):
        # Define the returned data batches
        Data = collections.namedtuple(
            'Data',
            'paths_LR, paths_HR, inputs, targets, image_count, steps_per_epoch'
        )

        #Check the input directory
        if (FLAGS.input_dir_LR == 'None') or (FLAGS.input_dir_HR == 'None'):
            raise ValueError('Input directory is not provided')

        if (not os.path.exists(FLAGS.input_dir_LR)) or (not os.path.exists(
                FLAGS.input_dir_HR)):
            raise ValueError('Input directory not found')

        image_list_LR = os.listdir(FLAGS.input_dir_LR)
        image_list_LR = [_ for _ in image_list_LR if _.endswith('.png')]
        if len(image_list_LR) == 0:
            raise Exception('No png files in the input directory')

        image_list_LR_temp = sorted(image_list_LR)
        image_list_LR = [
            os.path.join(FLAGS.input_dir_LR, _) for _ in image_list_LR_temp
        ]
        image_list_HR = [
            os.path.join(FLAGS.input_dir_HR, _) for _ in image_list_LR_temp
        ]

        image_list_LR_tensor = tf.convert_to_tensor(image_list_LR,
                                                    dtype=tf.string)
        image_list_HR_tensor = tf.convert_to_tensor(image_list_HR,
                                                    dtype=tf.string)

        with tf.variable_scope('load_image'):
            # define the image list queue
            # image_list_LR_queue = tf.train.string_input_producer(image_list_LR, shuffle=False, capacity=FLAGS.name_queue_capacity)
            # image_list_HR_queue = tf.train.string_input_producer(image_list_HR, shuffle=False, capacity=FLAGS.name_queue_capacity)
            #print('[Queue] image list queue use shuffle: %s'%(FLAGS.mode == 'Train'))
            output = tf.train.slice_input_producer(
                [image_list_LR_tensor, image_list_HR_tensor],
                shuffle=False,
                capacity=FLAGS.name_queue_capacity)

            # Reading and decode the images
            reader = tf.WholeFileReader(name='image_reader')
            image_LR = tf.read_file(output[0])
            image_HR = tf.read_file(output[1])
            input_image_LR = tf.image.decode_png(image_LR, channels=3)
            input_image_HR = tf.image.decode_png(image_HR, channels=3)
            input_image_LR = tf.image.convert_image_dtype(input_image_LR,
                                                          dtype=tf.float32)
            input_image_HR = tf.image.convert_image_dtype(input_image_HR,
                                                          dtype=tf.float32)

            assertion = tf.assert_equal(
                tf.shape(input_image_LR)[2],
                3,
                message="image does not have 3 channels")
            with tf.control_dependencies([assertion]):
                input_image_LR = tf.identity(input_image_LR)
                input_image_HR = tf.identity(input_image_HR)

            # Normalize the low resolution image to [0, 1], high resolution to [-1, 1]
            a_image = preprocessLR(input_image_LR)
            b_image = preprocess(input_image_HR)

            inputs, targets = [a_image, b_image]

        # The data augmentation part
        with tf.name_scope('data_preprocessing'):
            with tf.name_scope('random_crop'):
                # Check whether perform crop
                if (FLAGS.random_crop is True) and FLAGS.mode == 'train':
                    print('[Config] Use random crop')
                    # Set the shape of the input image. the target will have 4X size
                    input_size = tf.shape(inputs)
                    target_size = tf.shape(targets)
                    offset_w = tf.cast(tf.floor(
                        tf.random_uniform([], 0,
                                          tf.cast(input_size[1], tf.float32) -
                                          FLAGS.crop_size)),
                                       dtype=tf.int32)
                    offset_h = tf.cast(tf.floor(
                        tf.random_uniform([], 0,
                                          tf.cast(input_size[0], tf.float32) -
                                          FLAGS.crop_size)),
                                       dtype=tf.int32)

                    if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
                        inputs = tf.image.crop_to_bounding_box(
                            inputs, offset_h, offset_w, FLAGS.crop_size,
                            FLAGS.crop_size)
                        targets = tf.image.crop_to_bounding_box(
                            targets, offset_h * 4, offset_w * 4,
                            FLAGS.crop_size * 4, FLAGS.crop_size * 4)
                    elif FLAGS.task == 'denoise':
                        inputs = tf.image.crop_to_bounding_box(
                            inputs, offset_h, offset_w, FLAGS.crop_size,
                            FLAGS.crop_size)
                        targets = tf.image.crop_to_bounding_box(
                            targets, offset_h, offset_w, FLAGS.crop_size,
                            FLAGS.crop_size)
                # Do not perform crop
                else:
                    inputs = tf.identity(inputs)
                    targets = tf.identity(targets)

            with tf.variable_scope('random_flip'):
                # Check for random flip:
                if (FLAGS.flip is True) and (FLAGS.mode == 'train'):
                    print('[Config] Use random flip')
                    # Produce the decision of random flip
                    decision = tf.random_uniform([], 0, 1, dtype=tf.float32)

                    input_images = random_flip(inputs, decision)
                    target_images = random_flip(targets, decision)
                else:
                    input_images = tf.identity(inputs)
                    target_images = tf.identity(targets)

            if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
                input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])
                target_images.set_shape(
                    [FLAGS.crop_size * 4, FLAGS.crop_size * 4, 3])
            elif FLAGS.task == 'denoise':
                input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])
                target_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])

        if FLAGS.mode == 'train':
            paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.shuffle_batch(
                [output[0], output[1], input_images, target_images],
                batch_size=FLAGS.batch_size,
                capacity=FLAGS.image_queue_capacity + 4 * FLAGS.batch_size,
                min_after_dequeue=FLAGS.image_queue_capacity,
                num_threads=FLAGS.queue_thread)
        else:
            paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.batch(
                [output[0], output[1], input_images, target_images],
                batch_size=FLAGS.batch_size,
                num_threads=FLAGS.queue_thread,
                allow_smaller_final_batch=True)

        steps_per_epoch = int(math.ceil(len(image_list_LR) / FLAGS.batch_size))
        if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
            inputs_batch.set_shape(
                [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
            targets_batch.set_shape([
                FLAGS.batch_size, FLAGS.crop_size * 4, FLAGS.crop_size * 4, 3
            ])
        elif FLAGS.task == 'denoise':
            inputs_batch.set_shape(
                [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
            targets_batch.set_shape(
                [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
    return Data(paths_LR=paths_LR_batch,
                paths_HR=paths_HR_batch,
                inputs=inputs_batch,
                targets=targets_batch,
                image_count=len(image_list_LR),
                steps_per_epoch=steps_per_epoch)
示例#27
0
def load_examples():
    if a.input_dir is None or not os.path.exists(a.input_dir):
        raise Exception("input_dir does not exist")

    input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
    decode = tf.image.decode_jpeg
    if len(input_paths) == 0:
        input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
        decode = tf.image.decode_png

    if len(input_paths) == 0:
        raise Exception("input_dir contains no image files")

    def get_name(path):
        name, _ = os.path.splitext(os.path.basename(path))
        return name

    # if the image names are numbers, sort by the value rather than asciibetically
    # having sorted inputs means that the outputs are sorted in test mode
    if all(get_name(path).isdigit() for path in input_paths):
        input_paths = sorted(input_paths, key=lambda path: int(get_name(path)))
    else:
        input_paths = sorted(input_paths)

    with tf.name_scope("load_images"):
        path_queue = tf.train.string_input_producer(input_paths, shuffle=a.mode == "train")
        reader = tf.WholeFileReader()
        paths, contents = reader.read(path_queue)

        raw_input = decode(contents)
        raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)

        assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels")
        with tf.control_dependencies([assertion]):
            raw_input = tf.identity(raw_input)

        raw_input.set_shape([None, None, 3])

        if a.lab_colorization:
            # load color and brightness from image, no B image exists here
            lab = rgb_to_lab(raw_input)
            L_chan, a_chan, b_chan = preprocess_lab(lab)
            a_images = tf.expand_dims(L_chan, axis=2)
            b_images = tf.stack([a_chan, b_chan], axis=2)
        else:
            # break apart image pair and move to range [-1, 1]
            width = tf.shape(raw_input)[1] # [height, width, channels]
            a_images = preprocess(raw_input[:,:width//2,:])
            b_images = preprocess(raw_input[:,width//2:,:])

    if a.which_direction == "AtoB":
        inputs, targets = [a_images, b_images]
    elif a.which_direction == "BtoA":
        inputs, targets = [b_images, a_images]
    else:
        raise Exception("invalid direction")

    # synchronize seed for image operations so that we do the same operations to both
    # input and output images
    seed = random.randint(0, 2**31 - 1)
    def transform(image):
        r = image
        if a.flip:
            r = tf.image.random_flip_left_right(r, seed=seed)

        # area produces a nice downscaling, but does nearest neighbor for upscaling
        # assume we're going to be doing downscaling here
        r = tf.image.resize_images(r, [a.scale_size, a.scale_size], method=tf.image.ResizeMethod.AREA)

        offset = tf.cast(tf.floor(tf.random_uniform([2], 0, a.scale_size - CROP_SIZE + 1, seed=seed)), dtype=tf.int32)
        if a.scale_size > CROP_SIZE:
            r = tf.image.crop_to_bounding_box(r, offset[0], offset[1], CROP_SIZE, CROP_SIZE)
        elif a.scale_size < CROP_SIZE:
            raise Exception("scale size cannot be less than crop size")
        return r

    with tf.name_scope("input_images"):
        input_images = transform(inputs)

    with tf.name_scope("target_images"):
        target_images = transform(targets)

    classes = []
    for xy in range(0, len(input_paths)):
        classes.append(input_paths[xy][len(input_paths[xy]) - 6:len(input_paths[xy]) - 5])

    to_categorical = tf.keras.utils.to_categorical

    classes_hot = to_categorical(classes, num_classes=2)

    classes_queue = tf.train.input_producer(classes_hot, shuffle=a.mode == "train")

    paths_batch, classes_batch, inputs_batch, targets_batch = tf.train.batch([paths, classes_queue.dequeue(), input_images, target_images], batch_size=a.batch_size)
    steps_per_epoch = int(math.ceil(len(input_paths) / a.batch_size))

    return Examples(
        paths=paths_batch,
        classes=classes_batch,
        inputs=inputs_batch,
        targets=targets_batch,
        count=len(input_paths),
        steps_per_epoch=steps_per_epoch,
    )
def image_input_fn(image_files):
    filename_queue = tf.train.string_input_producer(image_files, shuffle=False)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    image_tf = tf.image.decode_jpeg(value, channels=3)
    return tf.image.convert_image_dtype(image_tf, tf.float32)
示例#29
0
    def make_batch_tensor(self, network_config, restore_iter=0, train=True):
        TEST_INTERVAL = 500
        batch_image_size = (self.update_batch_size +
                            self.test_batch_size) * self.meta_batch_size
        if train:
            all_filenames = self.all_training_filenames
            if restore_iter > 0:
                all_filenames = all_filenames[batch_image_size *
                                              (restore_iter + 1):]
        else:
            all_filenames = self.all_val_filenames
            if restore_iter > 0:
                all_filenames = all_filenames[batch_image_size * (
                    int(restore_iter / TEST_INTERVAL) + 1):]

        im_height = network_config['image_height']
        im_width = network_config['image_width']
        num_channels = network_config['image_channels']
        # make queue for tensorflow to read from
        filename_queue = tf.train.string_input_producer(
            tf.convert_to_tensor(all_filenames), shuffle=False)
        print 'Generating image processing ops'
        image_reader = tf.WholeFileReader()
        _, image_file = image_reader.read(filename_queue)
        image = tf.image.decode_gif(image_file)
        # should be T x C x W x H
        image.set_shape((self.T, im_height, im_width, num_channels))
        image = tf.cast(image, tf.float32)
        image /= 255.0
        if FLAGS.hsv:
            eps_min, eps_max = 0.5, 1.5
            assert eps_max >= eps_min >= 0
            # convert to HSV only fine if input images in [0, 1]
            img_hsv = tf.image.rgb_to_hsv(image)
            img_h = img_hsv[..., 0]
            img_s = img_hsv[..., 1]
            img_v = img_hsv[..., 2]
            eps = tf.random_uniform([self.T, 1, 1], eps_min, eps_max)
            img_v = tf.clip_by_value(eps * img_v, 0., 1.)
            img_hsv = tf.stack([img_h, img_s, img_v], 3)
            image_rgb = tf.image.hsv_to_rgb(img_hsv)
            image = image_rgb

        print('image.shape', image.shape)
        image = tf.transpose(image,
                             perm=[0, 3, 2, 1
                                   ])  # transpose to mujoco setting for images
        print('image.reshape', image.shape)
        image = tf.reshape(image, [self.T, -1])
        num_preprocess_threads = 1  # TODO - enable this to be set to >1
        min_queue_examples = 64  #128 #256
        print 'Batching images'
        images = tf.train.batch(
            [image],
            batch_size=batch_image_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_image_size,
        )
        all_images = []
        for i in xrange(self.meta_batch_size):
            image = images[i * (self.update_batch_size +
                                self.test_batch_size):(i + 1) *
                           (self.update_batch_size + self.test_batch_size)]
            # print('i',i,'image.shape',image.shape)
            image = tf.reshape(
                image,
                [(self.update_batch_size + self.test_batch_size) * self.T, -1])
            # print('append', 'image.shape', image.shape)
            all_images.append(image)
        print('make_batch_tensor', tf.stack(all_images).shape)
        return tf.stack(all_images)
示例#30
0
def ExampleGen(data_path, num_epochs=None):
    """Generates tf.Examples from path of data files.
  ExampleGen
      Binary data format: <length><blob>. <length> represents the byte size
      of <blob>. <blob> is serialized tf.Example proto. The tf.Example contains
      the tokenized article text and summary.

    Args:
      data_path: path to tf.Example data files.
      num_epochs: Number of times to go through the data. None means infinite.

    Yields:
      Deserialized tf.Example.

    If there are multiple files specified, they accessed in a random order.
    """
    # epoch = 0
    # while True:
    #     fileselection_path = data_path + "/*.json"
    #     filenames = tf.train.match_filenames_once(fileselection_path)
    #     count_num_files = tf.size(filenames)
    #     filename_queue = tf.train.string_input_producer(filenames)
    #     reader = tf.WholeFileReader()
    #     filename, _ = reader.read(filename_queue)
    #     counter = 0
    #     with tf.Session() as sess:
    #         sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
    #         num_files = sess.run(count_num_files)
    #         if counter >= num_files:
    #          break
    #         coord = tf.train.Coordinator()
    #         threads = tf.train.start_queue_runners(coord=coord)
    #
    #         for i in range(num_files):
    #             csv_file = sess.run(filename)
    #             tf_example = example_pb2.Example()
    #             filename_queue_sub = tf.train.string_input_producer([csv_file])
    #             reader_sub = tf.WholeFileReader()
    #             _, file_contents_sub = reader_sub.read(filename_queue_sub)
    #
    #             threads_sub = tf.train.start_queue_runners(coord=coord)
    #             json_content = sess.run(file_contents_sub)
    #             json_text = json.loads(json_content)
    #             article = str(json_text['article'])
    #             summary = str(json_text["summary"])
    #             tf_example.features.feature['article'].bytes_list.value.extend([article])
    #             tf_example.features.feature['abstract'].bytes_list.value.extend([summary])
    #             counter += 1
    #             yield tf_example
    #
    #         coord.request_stop()
    #         coord.join(threads)
    #         coord.join(threads_sub)
    #     epoch += 1

    epoch = 0
    filenames = tf.train.match_filenames_once(data_path +"/*.json")
    count_num_files = tf.size(filenames)
    filename_queue = tf.train.string_input_producer(filenames)
    reader = tf.WholeFileReader()
    filename, _ = reader.read(filename_queue)

    with tf.Session() as sess:
        sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
        num_files = sess.run(count_num_files)
        counter =0
        while True:
            if counter >= num_files:
                break
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)


            for i in range(num_files):
                csv_file = sess.run(filename)
                tf_example = example_pb2.Example()
                filename_queue_sub = tf.train.string_input_producer([csv_file])
                reader_sub = tf.WholeFileReader()
                _, file_contents_sub = reader_sub.read(filename_queue_sub)
                threads_sub = tf.train.start_queue_runners(coord=coord)
                json_content = sess.run(file_contents_sub)
                json_text = json.loads(json.dumps( ast.literal_eval(json_content)))
                article = str(json_text.get('article').encode('ascii','ignore'))
                summary = str(json_text.get("summary").encode('ascii','ignore'))
                tf_example.features.feature['article'].bytes_list.value.extend([article])
                tf_example.features.feature['abstract'].bytes_list.value.extend([summary])
                counter = counter +1
                yield(tf_example)


            coord.request_stop()
            coord.join(threads)
            coord.join(threads_sub)
    epoch += 1