Exemplo n.º 1
0
def decode_image(image_bytes_tensor, channels=3):
    """We need this function as the tf function does not return a shape"""
    with tf.name_scope('decode_image') as scope:
        substr_jpg = tf.substr(image_bytes_tensor, 0, 3)
        substr_png = tf.substr(image_bytes_tensor, 0, 4)

        def _png():
            is_png = tf.equal(substr_png, b'\211PNG', name='is_png')
            assert_decode = tf.Assert(
                is_png, ['Unable to decode bytes as JPEG or PNG'])
            with tf.control_dependencies([assert_decode]):
                image_png = tf.image.decode_png(image_bytes_tensor,
                                                channels=channels)
                image_png = tf.image.convert_image_dtype(image_png,
                                                         dtype=tf.float32)
                return image_png

        def _jpeg():
            image_jpg = tf.image.decode_jpeg(image_bytes_tensor,
                                             channels=channels)
            image_jpg = tf.image.convert_image_dtype(image_jpg,
                                                     dtype=tf.float32)
            return image_jpg

        is_jpeg = tf.equal(substr_jpg, b'\xff\xd8\xff', name='is_jpeg')
        return tf.cond(is_jpeg, _jpeg, _png, name='cond_jpeg')
Exemplo n.º 2
0
def read_a3daps(filename, label):
    # .a3daps
    nx = 512
    ny = 660
    nt = 64

    scaling_bytes = 4  # float32
    scaling_pos = 292  # after 292 bytes of header crap
    data_offset = 512
    data_word_size = 2
    record_bytes = data_offset + nx * ny * nt * data_word_size
    image_bytes = nx * ny * nt * data_word_size

    datadir = CONFIG.datadir
    if not CONFIG.datadir.endswith('/'):
        datadir = CONFIG.datadir + '/'
    value = tf.read_file(datadir + filename + '.a3daps')
    data_scale_factor = tf.decode_raw(
        tf.substr(value, scaling_pos, scaling_bytes), tf.float32)
    data16 = tf.decode_raw(tf.substr(value, data_offset, image_bytes),
                           tf.uint16)  #tf.int16)
    # data16 = tf.bitcast(data16, tf.uint16)
    data16 = tf.reshape(data16, [nt, ny, nx])
    data16 = tf.transpose(data16)
    dataf = tf.to_float(data16)
    data_n = dataf / 65535. * (data_scale_factor * 1e5)  # roughly 0 to 1 range
    return data_n, label
Exemplo n.º 3
0
 def _load_corpus(self, file):
     
     dataset = tf.data.TextLineDataset(file)
     
     with tf.name_scope("load_corpus"):
         src_dataset = dataset.filter(lambda line: tf.logical_and(tf.size(line) > 0, tf.equal(tf.substr(line, 0, 2), tf.constant('Q:'))))
         src_dataset = src_dataset.map(lambda line: tf.substr(line, 2, MAX_LEN)).prefetch(4096)
         tgt_dataset = dataset.filter(lambda line: tf.logical_and(tf.size(line) > 0, tf.equal(tf.substr(line, 0, 2), tf.constant('A:'))))
         tgt_dataset = tgt_dataset.map(lambda line: tf.substr(line, 2, MAX_LEN)).prefetch(4096)
         src_tgt_dataset = tf.data.Dataset.zip((src_dataset, tgt_dataset))
         
     return src_tgt_dataset
Exemplo n.º 4
0
  def _testOutOfRangeError(self, dtype):
    # Scalar/Scalar
    test_string = b"Hello"
    position = np.array(7, dtype)
    length = np.array(3, dtype)
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      with self.assertRaises(tf.errors.InvalidArgumentError):
        substr = substr_op.eval()

    # Vector/Scalar
    test_string = [b"good", b"good", b"bad", b"good"]
    position = np.array(3, dtype)
    length = np.array(1, dtype)
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      with self.assertRaises(tf.errors.InvalidArgumentError):
        substr = substr_op.eval()

    # Negative pos
    test_string = b"Hello"
    position = np.array(-1, dtype)
    length = np.array(3, dtype)
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      with self.assertRaises(tf.errors.InvalidArgumentError):
        substr = substr_op.eval()

    # Matrix/Matrix
    test_string = [[b"good", b"good", b"good"],
                   [b"good", b"good", b"bad"],
                   [b"good", b"good", b"good"]]
    position = np.array([[1, 2, 3],
                         [1, 2, 3],
                         [1, 2, 3]], dtype)
    length = np.array([[3, 2, 1],
                       [1, 2, 3],
                       [2, 2, 2]], dtype)
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      with self.assertRaises(tf.errors.InvalidArgumentError):
        substr = substr_op.eval()

    # Broadcast
    test_string = [[b"good", b"good", b"good"],
                   [b"good", b"good", b"bad"]]
    position = np.array([1, 2, 3], dtype)
    length = np.array([1, 2, 3], dtype)
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      with self.assertRaises(tf.errors.InvalidArgumentError):
        substr = substr_op.eval()
Exemplo n.º 5
0
def read_shapenet(filename_queue):
    """Reads and parses examples from ShapeCat data files.

    Recommendation: if you want N-way read parallelism, call this function
    N times.  This will give you N independent Readers reading different
    files & positions within those files, which will give better mixing of
    examples.

    Args:
      filename_queue: A queue of strings with the filenames to read from.

    Returns:
      An object representing a single example, with the following fields:
        height: number of rows in the result (32)
        width: number of columns in the result (32)
        depth: number of color channels in the result (3)
        key: a scalar string Tensor describing the filename & record number
          for this example.

        depth_images: a [width, height, number_of_depth_images] float32 Tensor with the image data
    """

    class ShapenetCatRecord(object):
      pass
    result = ShapenetCatRecord()

    # check glx_main.cc in depth_renderer-egl for input format.
    label_bytes = 4  # int32_t for shapenet
    result.n_images = 12 + 12 + 4 + 1 + 1 # See the ctor of RLDriver
    result.height = config.DEFAULT_RES
    result.width = config.DEFAULT_RES
    result.depth_bytes = 4 # FP32 = 4 bytes
    image_bytes = result.height * result.width * result.depth_bytes
    # Every record consists of a label followed by the image, with a
    # fixed number of bytes for each.
    record_bytes = label_bytes + image_bytes * result.n_images

    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    result.key, record_string = reader.read(filename_queue)

    # The first bytes represent the label, which we convert from uint8->int32.
    result.label = tf.decode_raw(tf.substr(record_string, 0, label_bytes), tf.int32)

    record_depth_image_string = tf.substr(record_string, label_bytes, image_bytes * result.n_images)
    depth = tf.reshape(
            tf.decode_raw(record_depth_image_string, tf.float32),
            [result.n_images, result.height, result.width, 1])

    result.image = depth

    return result
Exemplo n.º 6
0
def read_letter(pairs_and_overlap_queue):
    # Read the letters, cyrillics, and numbers.
    letter = pairs_and_overlap_queue[0]
    cyrillic = pairs_and_overlap_queue[1]
    number = pairs_and_overlap_queue[2]

    # Do something with them
    # (doesn't matter what)
    letter = tf.substr(letter, 0, 1)
    cyrillic = tf.substr(cyrillic, 0, 1)
    number = tf.add(number, tf.constant(0))

    # Return them
    return letter, cyrillic, number
    def read_image(self, image_path, out_size, channels=3):
        print("---image_path---")
        print(image_path)
        # print(image_path[0])
        # print(image_path[1])
        # print(image_path[2])
        path_length = string_length_tf(image_path)[0]
        # path_length = tf.convert_to_tensor(10,dtype=tf.int64)
        # path_length = tf.constant(52, dtype=tf.int64)
        print("debug:)")
        file_extension = tf.substr(image_path, path_length - 3, 3)
        print("debug:):)")
        file_cond = tf.equal(file_extension, 'jpg')
        print("-------------")
        print(file_cond)
        print("-------------")

        image = tf.cond(
            file_cond, lambda: tf.image.decode_jpeg(tf.read_file(image_path),
                                                    channels=channels),
            lambda: tf.image.decode_png(tf.read_file(image_path),
                                        channels=channels))
        image = tf.cast(image, tf.float32)
        #image = tf.image.convert_image_dtype(image, tf.float32)
        image = tf.image.resize_images(image, out_size,
                                       tf.image.ResizeMethod.AREA)
        return image
Exemplo n.º 8
0
 def read_depth(self, image_path):
     path_length = self.string_length_tf(image_path)[0]
     file_extension = tf.substr(image_path, path_length - 3, 3)
     file_cond = tf.equal(file_extension, 'exr')
     image = tf.cond(file_cond, lambda: self.decode_exr(image_path),
                     lambda: self.read_png(image_path))
     return image
Exemplo n.º 9
0
    def next_example(height, width):
        # Ops for getting training images, from retrieving the filenames to reading the data
        all_files = [tf.train.match_filenames_once(
                                'training_data/1/TrainningSample%d/*.jpg' % x) for x in range(1,5)]
        

        filenames = tf.concat(all_files, 0)
        filename_queue = tf.train.string_input_producer(filenames)
        reader = tf.WholeFileReader()
        filename, file = reader.read(filename_queue)

        img = tf.image.decode_jpeg(file, channels=3)
        img = tf.image.rgb_to_grayscale(img)
        img = tf.image.resize_images(img, [height, width])

        filename_split = tf.string_split([filename], delimiter='/')
        
        label_id = tf.string_to_number(tf.substr(filename_split.values[1], 
                                            0, 1), out_type=tf.int32)
        label = tf.one_hot(
                    label_id-1, 
                    2, 
                    on_value=1.0, 
                    off_value=0.0, 
                    dtype=tf.float32)
        
        return img, label
Exemplo n.º 10
0
def img_and_lbl_queue_setup(filenames, labels):
    labels_tensor = tf.constant(labels, dtype=tf.float32)
    
    filenames_tensor = tf.constant(filenames)
    fnq = tf.RandomShuffleQueue(capacity=200, min_after_dequeue=100, dtypes=tf.string)
    fnq_enq_op = fnq.enqueue_many(filenames_tensor)
    filename = fnq.dequeue()
    
    reader = tf.WholeFileReader()
    flnm, data = reader.read(fnq)
    image = tf_decode(data, channels=3)
    image.set_shape([178, 218, image_channels])
    image = tf.image.crop_to_bounding_box(image, 50, 25, 128, 128)
    image = tf.to_float(image)
    
    image_index = [tf.cast(tf.string_to_number(tf.substr(flnm, len(data_dir), 6)) - 1, tf.int32)]
    image_labels = tf.reshape(tf.gather(labels_tensor, indices=image_index, axis=0), [n_labels])
    imq = tf.RandomShuffleQueue(capacity=60, min_after_dequeue=30, dtypes=[tf.float32, tf.float32],
                                shapes=[[128, 128, image_channels], [n_labels]])
    imq_enq_op = imq.enqueue([image, image_labels])
    imgs, img_lbls = imq.dequeue_many(batch_size_x)
    imgs = tf.image.resize_nearest_neighbor(imgs, size=[image_size, image_size])
    imgs = tf.subtract(1., tf.divide(imgs, 255.5))
    qr_f = tf.train.QueueRunner(fnq, [fnq_enq_op] * 3)
    qr_i = tf.train.QueueRunner(imq, [imq_enq_op] * 3)
    return imgs, img_lbls, qr_f, qr_i
Exemplo n.º 11
0
    def read_image(self, image_path):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = string_length_tf(image_path)[0]
        file_extension = tf.substr(image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'jpg')
        if self.dataset == 'hobot':
            image = tf.cond(
                file_cond,
                lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                lambda: tf.image.decode_png(tf.read_file(image_path),
                                            channels=3))
        else:
            image = tf.cond(
                file_cond,
                lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                lambda: tf.image.decode_png(tf.read_file(image_path)))

        # if the dataset is cityscapes, we crop the last fifth to remove the car hood
        if self.dataset == 'cityscapes':
            o_height = tf.shape(image)[0]
            crop_height = (o_height * 4) / 5
            image = image[:crop_height, :, :]

        image = tf.image.convert_image_dtype(image, tf.float32)
        image = tf.image.resize_images(image,
                                       [self.params.height, self.params.width],
                                       tf.image.ResizeMethod.AREA)

        return image
Exemplo n.º 12
0
    def read_image(self, image_path):
        """Read an image from the file system
            :params image_path: string, path to image
        """
        with tf.variable_scope("read_image"):
            path_length = string_length_tf(image_path)[0]
            file_extension = tf.substr(image_path, path_length - 3, 3)
            file_cond = tf.equal(file_extension, "jpg")

            image = tf.cond(
                file_cond,
                lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                lambda: tf.image.decode_png(tf.read_file(image_path)),
            )

            self.image_w = tf.shape(image)[1]
            self.image_h = tf.shape(image)[0]

            image = tf.image.convert_image_dtype(image, tf.float32)
            image = tf.image.resize_images(
                image,
                [self.params.height, self.params.width],
                tf.image.ResizeMethod.AREA,
            )
            return image
Exemplo n.º 13
0
    def read_image(self, image_path):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        # Q: [0] 不知道什么意思
        # 获取了图片路径的长度, 通过 python 的 len 函数
        path_length = string_length_tf(image_path)[0]
        # 获取图片路径的后缀名,str
        # tf.substr: 从字符串的 Tensor 中返回子字符串
        file_extension = tf.substr(image_path, path_length - 3, 3)
        # 判断后缀名是否为"jpg",bool
        file_cond = tf.equal(file_extension, 'jpg')

        # 读取 ==> 解码 ==> 转换格式 ==> resize
        # 根据 file_cond 的值,判断用 jpeg 解码还是 png 解码,显示需要 tf.image.convert_image_dtype() 转换一下格式
        # tf.cond:类似于三元组函数, ?:fn1:fn2
        image = tf.cond(file_cond,
                        lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                        lambda: tf.image.decode_png(tf.read_file(image_path)))

        # if the dataset is cityscapes, we crop the last fifth to remove the car hood
        # 如果数据集是 cityscapes,裁剪最后的1/5 以移除汽车引擎盖
        if self.dataset == 'cityscapes':
            o_height = tf.shape(image)[0]
            crop_height = (o_height * 4) // 5
            image = image[:crop_height, :, :]

        # 转换之前解码得到的 image 的格式, tf.float32
        image = tf.image.convert_image_dtype(image, tf.float32)
        # 将 image resize 为设定好的长宽
        # ResizeMethod.AREA --> 基于区域的图像插值算法,首先将原始低分辨率图像分割成不同区域,然后将插值点映射到低分辨率图像,
        # 判断其所属区域, 最后根据插值点的邻域像素设计不同的插值公式, 计算插值点的值。
        image = tf.image.resize_images(image,
                                       [self.params.height, self.params.width],
                                       tf.image.ResizeMethod.AREA)

        return image
Exemplo n.º 14
0
    def read_image(self, image_path, mask):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = self.string_length_tf(image_path)[0]
        file_extension = tf.substr(image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'jpg')

        if (mask == 1):
            image = tf.cond(
                file_cond, lambda: tf.image.decode_jpeg(
                    tf.read_file(image_path), channels=1),
                lambda: tf.image.decode_png(tf.read_file(image_path),
                                            channels=1))

            image = tf.image.resize_nearest_neighbor(tf.expand_dims(image, 0),
                                                     self.maskShape)
            image = tf.squeeze(image, squeeze_dims=[0])
        else:
            image = tf.cond(
                file_cond, lambda: tf.image.decode_jpeg(
                    tf.read_file(image_path), channels=3),
                lambda: tf.image.decode_png(tf.read_file(image_path),
                                            channels=3))
            image = tf.image.resize_images(
                image, [self.args.imageHeight, self.args.imageWidth])

        return image
Exemplo n.º 15
0
def encode_token(token, char_to_id_lookup_table, hparams):
    """
    Encode a word to a padded vector of character IDs

    :param token: tensor of strings representing single word / tokens
    :param char_to_id_lookup_table: Lookup table mapping characters to ids. See utils.vocab for reference
    :param hparams:
    :return: tensor of shape [len(token), max_word_length], representing each word as a vector of character IDs
    """
    max_word_length = hparams.max_word_length
    chars_padding_id = hparams.chars_padding_id
    tokens_bos = hparams.tokens_bos  # begining of sentence
    tokens_eos = hparams.tokens_eos  # end of sentence
    chars_bow_id = hparams.chars_bow_id  # begining of word
    chars_eow_id = hparams.chars_eow_id  # end of word

    s = token
    # Special handling of sentence start and end tokens, per existing model
    s = tf.where(tf.equal(s, tokens_bos), tf.fill(tf.shape(s), chr(0)), s)
    s = tf.where(tf.equal(s, tokens_eos), tf.fill(tf.shape(s), chr(1)), s)
    # Add start and end of word symbols
    s = tf.map_fn(
        lambda x: tf.constant(chr(chars_bow_id)) + x + tf.constant(
            chr(chars_eow_id)), s)
    # Add padding
    padding_token = chr(chars_padding_id) * max_word_length
    s = tf.map_fn(
        lambda x: tf.substr(x + tf.constant(padding_token), 0, max_word_length
                            ), s)
    # Split tokens into characters
    s = tf.sparse_tensor_to_dense(tf.string_split(s, delimiter=""),
                                  default_value=chr(chars_padding_id))
    # Convert characters to char IDs
    s = char_to_id_lookup_table.lookup(s)
    return s
Exemplo n.º 16
0
    def read_image_label(self, image_path):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = string_length_tf(image_path)[0]
        file_extension = tf.substr(image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'jpg')

        image = tf.cond(file_cond,
                        lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                        lambda: tf.image.decode_png(tf.read_file(image_path)))
        print("---->", image)
        # if the dataset is cityscapes, we crop the last fifth to remove the car hood
        if self.dataset == 'cityscapes':
            o_height = tf.shape(image)[0]
            crop_height = (o_height * 4) // 5
            image = image[:crop_height, :, :]

        #image  = tf.image.rgb_to_grayscale(image)
        print("--1-->", image)
        image = tf.image.resize_images(image,
                                       [self.params.height, self.params.width],
                                       tf.image.ResizeMethod.NEAREST_NEIGHBOR)
        #image = scipy.misc.imresize(image,  [self.params.height, self.params.width])
        print("--2-->", image)
        #image  = tf.image.convert_image_dtype(image,  tf.int32)
        print("--3-->", image)
        return image
Exemplo n.º 17
0
  def _testMismatchPosLenShapes(self, dtype):
    test_string = [[b"ten", b"eleven", b"twelve"],
                   [b"thirteen", b"fourteen", b"fifteen"],
                   [b"sixteen", b"seventeen", b"eighteen"]]
    position = np.array([[1, 2, 3]], dtype)
    length = np.array([2, 3, 4], dtype)
    # Should fail: position/length have different rank
    with self.assertRaises(ValueError):
      substr_op = tf.substr(test_string, position, length)

    position = np.array([[1, 2, 3],
                         [1, 2, 3],
                         [1, 2, 3]], dtype)
    length = np.array([[2, 3, 4]], dtype)
    # Should fail: postion/length have different dimensionality
    with self.assertRaises(ValueError):
      substr_op = tf.substr(test_string, position, length)
Exemplo n.º 18
0
    def _load_corpus(self, corpus_dir):
        for fd in range(2, -1, -1):
            file_list = []
            if fd == 0:
                file_dir = os.path.join(corpus_dir, AUG0_FOLDER)
            elif fd == 1:
                file_dir = os.path.join(corpus_dir, AUG1_FOLDER)
            else:
                file_dir = os.path.join(corpus_dir, AUG2_FOLDER)

            for data_file in sorted(os.listdir(file_dir)):
                full_path_name = os.path.join(file_dir, data_file)
                if os.path.isfile(
                        full_path_name) and data_file.lower().endswith('.txt'):
                    file_list.append(full_path_name)

            assert len(file_list) > 0
            dataset = tf.contrib.data.TextLineDataset(file_list)

            src_dataset = dataset.filter(lambda line: tf.logical_and(
                tf.size(line) > 0,
                tf.equal(tf.substr(line, 0, 2), tf.constant('Q:'))))
            src_dataset = src_dataset.map(
                lambda line: tf.substr(line, 2, MAX_LEN),
                output_buffer_size=4096)
            tgt_dataset = dataset.filter(lambda line: tf.logical_and(
                tf.size(line) > 0,
                tf.equal(tf.substr(line, 0, 2), tf.constant('A:'))))
            tgt_dataset = tgt_dataset.map(
                lambda line: tf.substr(line, 2, MAX_LEN),
                output_buffer_size=4096)

            src_tgt_dataset = tf.contrib.data.Dataset.zip(
                (src_dataset, tgt_dataset))
            if fd == 1:
                src_tgt_dataset = src_tgt_dataset.repeat(
                    self.hparams.aug1_repeat_times)
            elif fd == 2:
                src_tgt_dataset = src_tgt_dataset.repeat(
                    self.hparams.aug2_repeat_times)

            if self.text_set is None:
                self.text_set = src_tgt_dataset
            else:
                self.text_set = self.text_set.concatenate(src_tgt_dataset)
Exemplo n.º 19
0
def _read_flow(filenames, num_epochs=None):
    """Given a list of filenames, constructs a reader op for ground truth flow files."""
    filename_queue = tf.train.string_input_producer(filenames,
                                                    shuffle=False,
                                                    capacity=len(filenames),
                                                    num_epochs=num_epochs)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    value = tf.reshape(value, [1])
    value_width = tf.substr(value, 4, 4)
    value_height = tf.substr(value, 8, 4)
    width = tf.reshape(tf.decode_raw(value_width, out_type=tf.int32), [])
    height = tf.reshape(tf.decode_raw(value_height, out_type=tf.int32), [])

    value_flow = tf.substr(value, 12, 8 * 436 * 1024)
    flow = tf.decode_raw(value_flow, out_type=tf.float32)

    return tf.reshape(flow, [436, 1024, 2])
Exemplo n.º 20
0
 def create_char_vectors_from_post(self, raw_post):
     char2index = self.index
     if self.do_lowercase:
         raw_post = self.lowercase(raw_post)
     raw_post = tf.string_split(tf.reshape(raw_post, [-1]))
     culled_word_token_vals = tf.substr(raw_post.values, 0, self.mxwlen)
     char_tokens = tf.string_split(culled_word_token_vals, delimiter='')
     char_indices = char2index.lookup(char_tokens)
     return self.reshape_indices(char_indices, [self.mxlen, self.mxwlen])
Exemplo n.º 21
0
    def read_image_random_crop(self, left_image_path, right_image_path):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = string_length_tf(left_image_path)[0]
        file_extension = tf.substr(left_image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'jpg')

        l_image = tf.cond(
            file_cond,
            lambda: tf.image.decode_jpeg(tf.read_file(left_image_path)),
            lambda: tf.image.decode_png(tf.read_file(left_image_path)))
        r_image = tf.cond(
            file_cond,
            lambda: tf.image.decode_jpeg(tf.read_file(right_image_path)),
            lambda: tf.image.decode_png(tf.read_file(right_image_path)))

        # # if the dataset is cityscapes, we crop the last fifth to remove the car hood
        # if self.dataset == 'cityscapes':
        #     o_height    = tf.shape(image)[0]
        #     crop_height = (o_height * 4) // 5
        #     image  =  image[:crop_height,:,:]

        img_height = tf.shape(l_image)[0]
        img_width = tf.shape(l_image)[1]
        crop_cond = tf.less(img_width, img_height)
        max_offset = tf.cond(crop_cond, lambda: img_height - img_width,
                             lambda: img_width - img_height)
        img_y_offset = tf.cond(
            crop_cond,
            lambda: tf.random_uniform([], 0, max_offset, dtype=tf.int32),
            lambda: 0)
        img_x_offset = tf.cond(
            crop_cond, lambda: 0,
            lambda: tf.random_uniform([], 0, max_offset, dtype=tf.int32))

        l_image = tf.cond(
            crop_cond, lambda: tf.image.crop_to_bounding_box(
                l_image, img_y_offset, img_x_offset, img_width - 1, img_width -
                1), lambda: tf.image.crop_to_bounding_box(
                    l_image, img_y_offset, img_x_offset, img_height - 1,
                    img_height - 1))
        l_image = tf.image.convert_image_dtype(l_image, tf.float32)
        l_image = tf.image.resize_images(
            l_image, [self.params.height, self.params.width],
            tf.image.ResizeMethod.AREA)

        r_image = tf.cond(
            crop_cond, lambda: tf.image.crop_to_bounding_box(
                r_image, img_y_offset, img_x_offset, img_width - 1, img_width -
                1), lambda: tf.image.crop_to_bounding_box(
                    r_image, img_y_offset, img_x_offset, img_height - 1,
                    img_height - 1))
        r_image = tf.image.convert_image_dtype(r_image, tf.float32)
        r_image = tf.image.resize_images(
            r_image, [self.params.height, self.params.width],
            tf.image.ResizeMethod.AREA)

        return l_image, r_image
Exemplo n.º 22
0
 def create_char_vectors_from_post(self, raw_post, mxlen):
     char2index = self.index
     if self.do_lowercase:
         raw_post = self.lowercase(raw_post)
     raw_post = tf.string_split(tf.reshape(raw_post, [-1]))
     culled_word_token_vals = tf.substr(raw_post.values, 0, self.mxwlen)
     char_tokens = tf.string_split(culled_word_token_vals, delimiter='')
     char_indices = char2index.lookup(char_tokens)
     return self.reshape_indices(char_indices, [mxlen, self.mxwlen])
Exemplo n.º 23
0
def eval_data_reading_function(path):
    myset = tf.data.TextLineDataset(path)\
        .skip(1) \
        .filter(lambda line: tf.greater(tf.string_to_number(tf.substr(line, 0, 1), tf.int32), 6)) \
        .map(training_csv_parser)\
        .batch(10)
    return myset\
        .make_one_shot_iterator()\
        .get_next()
Exemplo n.º 24
0
    def word_to_subword(word):
        """generate subwords for word"""
        word_len = tf.size(tf.string_split([word], delimiter=''))
        subwords = tf.substr([word], 0, subword_size)
        for i in range(1, subword_max_length):
            subwords = tf.cond(
                i + subword_size - 1 < word_len, lambda: tf.concat(
                    [subwords, tf.substr([word], i, subword_size)], 0),
                lambda: subwords)

        subwords = tf.concat([
            subwords[:subword_max_length],
            tf.constant(subword_pad, shape=[subword_max_length])
        ],
                             axis=0)
        subwords = tf.reshape(subwords[:subword_max_length],
                              shape=[subword_max_length])

        return subwords
Exemplo n.º 25
0
  def _testScalarString(self, dtype):
    test_string = b"Hello"
    position = np.array(1, dtype)
    length = np.array(3, dtype)
    expected_value = b"ell"

    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      substr = substr_op.eval()
      self.assertAllEqual(substr, expected_value)
Exemplo n.º 26
0
  def _testVectorStrings(self, dtype):
    test_string = [b"Hello", b"World"]
    position = np.array(1, dtype)
    length = np.array(3, dtype)
    expected_value = [b"ell", b"orl"]

    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      substr = substr_op.eval()
      self.assertAllEqual(substr, expected_value)
Exemplo n.º 27
0
 def decode_csv(line):
     parsed_line = tf.decode_csv(line,
                                 [[0], [0], [0], [""], ["unknown"], [0.],
                                  [0], [0], [""], [0.], ["U"], ["U"]])
     label = parsed_line[1]
     del parsed_line[1]  # removes the label
     parsed_line[-2] = tf.substr(parsed_line[-2], 0, 1)
     features = parsed_line
     d = dict(zip(FEATURES, features)), label
     return d
Exemplo n.º 28
0
 def read_image(self, image_path):
     # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
     path_length = self.string_length_tf(image_path)[0]
     file_extension = tf.substr(image_path, path_length - 3, 3)
     file_cond = tf.equal(file_extension, 'jpg')
     image = tf.cond(file_cond,
                     lambda: tf.image.decode_jpeg(tf.read_file(image_path)),
                     lambda: tf.image.decode_png(tf.read_file(image_path)))
     image = tf.image.convert_image_dtype(image, tf.float32)
     return image
Exemplo n.º 29
0
def _read_flow(filenames, num_epochs=None):
    """Given a list of filenames, constructs a reader op for ground truth flow files."""
    filename_queue = tf.train.string_input_producer(filenames,
        shuffle=False, capacity=len(filenames), num_epochs=num_epochs)
    reader = tf.WholeFileReader()
    _, value = reader.read(filename_queue)
    value = tf.reshape(value, [1])
    value_width = tf.substr(value, 4, 4)
    value_height = tf.substr(value, 8, 4)
    width = tf.reshape(tf.decode_raw(value_width, out_type=tf.int32), [])
    height = tf.reshape(tf.decode_raw(value_height, out_type=tf.int32), [])

    value_flow = tf.substr(value, 12, 8 * width * height)
    flow = tf.decode_raw(value_flow, out_type=tf.float32)
    flow = tf.reshape(flow, [height, width, 2])
    mask = tf.to_float(tf.logical_and(flow[:, :, 0] < 1e9, flow[:, :, 1] < 1e9))
    mask = tf.reshape(mask, [height, width, 1])

    return flow, mask
Exemplo n.º 30
0
  def _testBroadcast(self, dtype):
    # Broadcast pos/len onto input string
    test_string = [[b"ten", b"eleven", b"twelve"],
                   [b"thirteen", b"fourteen", b"fifteen"],
                   [b"sixteen", b"seventeen", b"eighteen"],
                   [b"nineteen", b"twenty", b"twentyone"]]
    position = np.array([1, 2, 3], dtype)
    length = np.array([1, 2, 3], dtype)
    expected_value = [[b"e", b"ev", b"lve"],
                      [b"h", b"ur", b"tee"],
                      [b"i", b"ve", b"hte"],
                      [b"i", b"en", b"nty"]]
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      substr = substr_op.eval()
      self.assertAllEqual(substr, expected_value)

    # Broadcast input string onto pos/len
    test_string = [b"thirteen", b"fourteen", b"fifteen"]
    position = np.array([[1, 2, 3],
                         [3, 2, 1],
                         [5, 5, 5]], dtype)
    length = np.array([[3, 2, 1],
                       [1, 2, 3],
                       [2, 2, 2]], dtype)
    expected_value = [[b"hir", b"ur", b"t"],
                      [b"r", b"ur", b"ift"],
                      [b"ee", b"ee", b"en"]]
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      substr = substr_op.eval()
      self.assertAllEqual(substr, expected_value)

    # Test 1D broadcast
    test_string = b"thirteen"
    position = np.array([1, 5, 7], dtype)
    length = np.array([3, 2, 1], dtype)
    expected_value = [b"hir", b"ee", b"n"]
    substr_op = tf.substr(test_string, position, length)
    with self.test_session():
      substr = substr_op.eval()
      self.assertAllEqual(substr, expected_value)
Exemplo n.º 31
0
    def read_labels(self, image_path):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = string_length_tf(image_path)[0]
        file_extension = tf.substr(image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'png')

        image = tf.image.decode_png(tf.read_file(image_path), dtype=tf.uint16)
        image = tf.image.convert_image_dtype(image, tf.float32)
        #image  = tf.image.resize_images(image,  [self.params.height, self.params.width], tf.image.ResizeMethod.AREA)
        #image=image*(self.params.width/1240.0)
        return image
    def read_image(self, image_path, mask):
        # tf.decode_image does not return the image size, this is an ugly workaround to handle both jpeg and png
        path_length = self.string_length_tf(image_path)[0]
        file_extension = tf.substr(image_path, path_length - 3, 3)
        file_cond = tf.equal(file_extension, 'jpg')

        if (mask == 1):
            image = tf.cond(file_cond, lambda: tf.image.decode_jpeg(tf.read_file(image_path), channels=1),
                            lambda: tf.image.decode_png(tf.read_file(image_path), channels=1))

            image = tf.image.resize_nearest_neighbor(tf.expand_dims(image, 0), self.maskShape)
            image = tf.squeeze(image, squeeze_dims=[0])
        else:
            image = tf.cond(file_cond, lambda: tf.image.decode_jpeg(tf.read_file(image_path), channels=3),
                            lambda: tf.image.decode_png(tf.read_file(image_path), channels=3))
            image = tf.image.resize_images(image, [self.args.imageHeight, self.args.imageWidth])

        return image
Exemplo n.º 33
0
    def __init__(self, config, batch_size, one_hot=False):
        self.lookup = None
        reader = tf.TextLineReader()
        filename_queue = tf.train.string_input_producer(["chargan.txt"])
        key, x = reader.read(filename_queue)
        vocabulary = self.get_vocabulary()

        table = tf.contrib.lookup.string_to_index_table_from_tensor(
            mapping = vocabulary, default_value = 0)

        x = tf.string_join([x, tf.constant(" " * 64)]) 
        x = tf.substr(x, [0], [64])
        x = tf.string_split(x,delimiter='')
        x = tf.sparse_tensor_to_dense(x, default_value=' ')
        x = tf.reshape(x, [64])
        x = table.lookup(x)
        self.one_hot = one_hot
        if one_hot:
            x = tf.one_hot(x, len(vocabulary))
            x = tf.cast(x, dtype=tf.float32)
            x = tf.reshape(x, [1, int(x.get_shape()[0]), int(x.get_shape()[1]), 1])
        else:
            x = tf.cast(x, dtype=tf.float32)
            x -= len(vocabulary)/2.0
            x /= len(vocabulary)/2.0
            x = tf.reshape(x, [1,1, 64, 1])

        num_preprocess_threads = 8

        x = tf.train.shuffle_batch(
          [x],
          batch_size=batch_size,
          num_threads=num_preprocess_threads,
          capacity= 5000,
          min_after_dequeue=500,
          enqueue_many=True)

        self.x = x
        self.table = table