当前位置: 首页>>代码示例>>Python>>正文


Python io_ops.read_file函数代码示例

本文整理汇总了Python中tensorflow.python.ops.io_ops.read_file函数的典型用法代码示例。如果您正苦于以下问题:Python read_file函数的具体用法?Python read_file怎么用?Python read_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了read_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _evalDecodeJpeg

  def _evalDecodeJpeg(self, image_name, parallelism, num_iters, tile=None):
    """Evaluate DecodeJpegOp for the given image.

    TODO(tanmingxing): add decoding+cropping as well.

    Args:
      image_name: a string of image file name (without suffix).
      parallelism: the number of concurrent decode_jpeg ops to be run.
      num_iters: number of iterations for evaluation.
      tile: if not None, tile the image to composite a larger fake image.

    Returns:
      The duration of the run in seconds.
    """
    ops.reset_default_graph()

    image_file_path = os.path.join(prefix_path, image_name)

    if tile is None:
      image_content = variable_scope.get_variable(
          'image_%s' % image_name,
          initializer=io_ops.read_file(image_file_path))
    else:
      single_image = image_ops.decode_jpeg(
          io_ops.read_file(image_file_path), channels=3, name='single_image')
      # Tile the image to composite a new larger image.
      tiled_image = array_ops.tile(single_image, tile)
      image_content = variable_scope.get_variable(
          'tiled_image_%s' % image_name,
          initializer=image_ops.encode_jpeg(tiled_image))

    with session.Session() as sess:
      sess.run(variables.global_variables_initializer())
      images = []
      for i in xrange(parallelism):
        images.append(
            image_ops.decode_jpeg(
                image_content, channels=3, name='image_%d' % (i)))

      r = control_flow_ops.group(*images)

      for _ in xrange(3):
        # Skip warm up time.
        sess.run(r)

      start_time = time.time()
      for _ in xrange(num_iters):
        sess.run(r)
    return time.time() - start_time
开发者ID:chdinh,项目名称:tensorflow,代码行数:49,代码来源:decode_jpeg_op_test.py

示例2: testCmyk

 def testCmyk(self):
     # Confirm that CMYK reads in as RGB
     base = "tensorflow/core/lib/jpeg/testdata"
     rgb_path = os.path.join(base, "jpeg_merge_test1.jpg")
     cmyk_path = os.path.join(base, "jpeg_merge_test1_cmyk.jpg")
     shape = 256, 128, 3
     for channels in 3, 0:
         with self.test_session() as sess:
             rgb = image_ops.decode_jpeg(io_ops.read_file(rgb_path), channels=channels)
             cmyk = image_ops.decode_jpeg(io_ops.read_file(cmyk_path), channels=channels)
             rgb, cmyk = sess.run([rgb, cmyk])
             self.assertEqual(rgb.shape, shape)
             self.assertEqual(cmyk.shape, shape)
             error = self.averageError(rgb, cmyk)
             self.assertLess(error, 4)
开发者ID:texttheater,项目名称:tensorflow,代码行数:15,代码来源:image_ops_test.py

示例3: prepare_background_data

  def prepare_background_data(self):
    """Searches a folder for background noise audio, and loads it into memory.

    It's expected that the background audio samples will be in a subdirectory
    named '_background_noise_' inside the 'data_dir' folder, as .wavs that match
    the sample rate of the training data, but can be much longer in duration.

    If the '_background_noise_' folder doesn't exist at all, this isn't an
    error, it's just taken to mean that no background noise augmentation should
    be used. If the folder does exist, but it's empty, that's treated as an
    error.

    Returns:
      List of raw PCM-encoded audio samples of background noise.

    Raises:
      Exception: If files aren't found in the folder.
    """
    self.background_data = []
    background_dir = os.path.join(self.data_dir, BACKGROUND_NOISE_DIR_NAME)
    if not os.path.exists(background_dir):
      return self.background_data
    with tf.Session(graph=tf.Graph()) as sess:
      wav_filename_placeholder = tf.placeholder(tf.string, [])
      wav_loader = io_ops.read_file(wav_filename_placeholder)
      wav_decoder = contrib_audio.decode_wav(wav_loader, desired_channels=1)
      search_path = os.path.join(self.data_dir, BACKGROUND_NOISE_DIR_NAME,
                                 '*.wav')
      for wav_path in gfile.Glob(search_path):
        wav_data = sess.run(
            wav_decoder,
            feed_dict={wav_filename_placeholder: wav_path}).audio.flatten()
        self.background_data.append(wav_data)
      if not self.background_data:
        raise Exception('No background wav files were found in ' + search_path)
开发者ID:AnishShah,项目名称:tensorflow,代码行数:35,代码来源:input_data.py

示例4: testGif

  def testGif(self):
    # Read some real GIFs
    path = os.path.join(prefix_path, "gif", "testdata", "scan.gif")
    width = 20
    height = 40
    stride = 5
    shape = (12, height, width, 3)

    with self.session(use_gpu=True) as sess:
      gif0 = io_ops.read_file(path)
      image0 = image_ops.decode_image(gif0)
      image1 = image_ops.decode_gif(gif0)
      gif0, image0, image1 = self.evaluate([gif0, image0, image1])

      self.assertEqual(image0.shape, shape)
      self.assertAllEqual(image0, image1)

      for frame_idx, frame in enumerate(image0):
        gt = np.zeros(shape[1:], dtype=np.uint8)
        start = frame_idx * stride
        end = (frame_idx + 1) * stride
        if end <= width:
          gt[:, start:end, :] = 255
        else:
          start -= width
          end -= width
          gt[start:end, :, :] = 255

        self.assertAllClose(frame, gt)

        bad_channels = image_ops.decode_image(gif0, channels=1)
        with self.assertRaises(errors_impl.InvalidArgumentError):
          self.evaluate(bad_channels)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:33,代码来源:decode_image_op_test.py

示例5: _restore_op

 def _restore_op(self, iterator_resource):
   iterator_state_variant = parsing_ops.parse_tensor(
       io_ops.read_file(self._iterator_checkpoint_prefix_local()),
       dtypes.variant)
   restore_op = gen_dataset_ops.deserialize_iterator(iterator_resource,
                                                     iterator_state_variant)
   return restore_op
开发者ID:ChengYuXiang,项目名称:tensorflow,代码行数:7,代码来源:range_dataset_op_test.py

示例6: get_unprocessed_data

 def get_unprocessed_data(self, how_many, model_settings, mode):
   """Gets sample data without transformations."""
   candidates = self.data_index[mode]
   if how_many == -1:
     sample_count = len(candidates)
   else:
     sample_count = how_many
   desired_samples = model_settings['desired_samples']
   words_list = self.words_list
   data = np.zeros((sample_count, desired_samples))
   labels = []
   with tf.Session(graph=tf.Graph()) as sess:
     wav_filename_placeholder = tf.placeholder(tf.string, [], name='filename')
     wav_loader = io_ops.read_file(wav_filename_placeholder)
     wav_decoder = contrib_audio.decode_wav(
         wav_loader, desired_channels=1, desired_samples=desired_samples)
     foreground_volume_placeholder = tf.placeholder(
         tf.float32, [], name='foreground_volume')
     scaled_foreground = tf.multiply(wav_decoder.audio,
                                     foreground_volume_placeholder)
     for i in range(sample_count):
       if how_many == -1:
         sample_index = i
       else:
         sample_index = np.random.randint(len(candidates))
       sample = candidates[sample_index]
       input_dict = {wav_filename_placeholder: sample['file']}
       if sample['label'] == SILENCE_LABEL:
         input_dict[foreground_volume_placeholder] = 0
       else:
         input_dict[foreground_volume_placeholder] = 1
       data[i, :] = sess.run(scaled_foreground, feed_dict=input_dict).flatten()
       label_index = self.word_to_index[sample['label']]
       labels.append(words_list[label_index])
   return data, labels
开发者ID:indranig,项目名称:examples,代码行数:35,代码来源:generator.py

示例7: testGif

  def testGif(self):
    # Read some real GIFs
    path = os.path.join(prefix_path, "gif", "testdata", "scan.gif")
    WIDTH = 20
    HEIGHT = 40
    STRIDE = 5
    shape = (12, HEIGHT, WIDTH, 3)

    with self.test_session(use_gpu=True) as sess:
      gif0 = io_ops.read_file(path)
      image0 = image_ops.decode_image(gif0)
      image1 = image_ops.decode_gif(gif0)
      gif0, image0, image1 = sess.run([gif0, image0, image1])

      self.assertEqual(image0.shape, shape)
      self.assertAllEqual(image0, image1)

      for frame_idx, frame in enumerate(image0):
        gt = np.zeros(shape[1:], dtype=np.uint8)
        start = frame_idx * STRIDE
        end = (frame_idx + 1) * STRIDE
        if end <= WIDTH:
          gt[:, start:end, :] = 255
        else:
          start -= WIDTH
          end -= WIDTH
          gt[start:end, :, :] = 255

        self.assertAllClose(frame, gt)

        bad_channels = image_ops.decode_image(gif0, channels=1)
        with self.assertRaises(errors_impl.InvalidArgumentError):
          bad_channels.eval()
开发者ID:AliMiraftab,项目名称:tensorflow,代码行数:33,代码来源:decode_image_op_test.py

示例8: prepare_processing_graph

  def prepare_processing_graph(self, model_settings):
    """Builds a TensorFlow graph to apply the input distortions.

    Creates a graph that loads a WAVE file, decodes it, scales the volume,
    shifts it in time, adds in background noise, calculates a spectrogram, and
    then builds an MFCC fingerprint from that.

    This must be called with an active TensorFlow session running, and it
    creates multiple placeholder inputs, and one output:

      - wav_filename_placeholder_: Filename of the WAV to load.
      - foreground_volume_placeholder_: How loud the main clip should be.
      - time_shift_padding_placeholder_: Where to pad the clip.
      - time_shift_offset_placeholder_: How much to move the clip in time.
      - background_data_placeholder_: PCM sample data for background noise.
      - background_volume_placeholder_: Loudness of mixed-in background.
      - mfcc_: Output 2D fingerprint of processed audio.

    Args:
      model_settings: Information about the current model being trained.
    """
    desired_samples = model_settings['desired_samples']
    self.wav_filename_placeholder_ = tf.placeholder(tf.string, [])
    wav_loader = io_ops.read_file(self.wav_filename_placeholder_)
    wav_decoder = contrib_audio.decode_wav(
        wav_loader, desired_channels=1, desired_samples=desired_samples)
    # Allow the audio sample's volume to be adjusted.
    self.foreground_volume_placeholder_ = tf.placeholder(tf.float32, [])
    scaled_foreground = tf.multiply(wav_decoder.audio,
                                    self.foreground_volume_placeholder_)
    # Shift the sample's start position, and pad any gaps with zeros.
    self.time_shift_padding_placeholder_ = tf.placeholder(tf.int32, [2, 2])
    self.time_shift_offset_placeholder_ = tf.placeholder(tf.int32, [2])
    padded_foreground = tf.pad(
        scaled_foreground,
        self.time_shift_padding_placeholder_,
        mode='CONSTANT')
    sliced_foreground = tf.slice(padded_foreground,
                                 self.time_shift_offset_placeholder_,
                                 [desired_samples, -1])
    # Mix in background noise.
    self.background_data_placeholder_ = tf.placeholder(tf.float32,
                                                       [desired_samples, 1])
    self.background_volume_placeholder_ = tf.placeholder(tf.float32, [])
    background_mul = tf.multiply(self.background_data_placeholder_,
                                 self.background_volume_placeholder_)
    background_add = tf.add(background_mul, sliced_foreground)
    background_clamp = tf.clip_by_value(background_add, -1.0, 1.0)
    # Run the spectrogram and MFCC ops to get a 2D 'fingerprint' of the audio.
    spectrogram = contrib_audio.audio_spectrogram(
        background_clamp,
        window_size=model_settings['window_size_samples'],
        stride=model_settings['window_stride_samples'],
        magnitude_squared=True)
    self.mfcc_ = contrib_audio.mfcc(
        spectrogram,
        wav_decoder.sample_rate,
        dct_coefficient_count=model_settings['dct_coefficient_count'])
开发者ID:TianyouLi,项目名称:tensorflow,代码行数:58,代码来源:input_data.py

示例9: load_wav_file

def load_wav_file(filename):
  """Loads an audio file and returns a float PCM-encoded array of samples."""
  with tf.Session(graph=tf.Graph()) as sess:
    wav_filename_placeholder = tf.placeholder(tf.string, [])
    wav_loader = io_ops.read_file(wav_filename_placeholder)
    wav_decoder = contrib_audio.decode_wav(wav_loader, desired_channels=1)
    return sess.run(
        wav_decoder, feed_dict={
            wav_filename_placeholder: filename
        }).audio.flatten()
开发者ID:indranig,项目名称:examples,代码行数:10,代码来源:generator.py

示例10: testBmp

 def testBmp(self):
   # Read a real bmp and verify shape
   path = os.path.join(prefix_path, "bmp", "testdata", "lena.bmp")
   with self.session(use_gpu=True) as sess:
     bmp0 = io_ops.read_file(path)
     image0 = image_ops.decode_image(bmp0)
     image1 = image_ops.decode_bmp(bmp0)
     bmp0, image0, image1 = self.evaluate([bmp0, image0, image1])
     self.assertEqual(len(bmp0), 4194)
     self.assertAllEqual(image0, image1)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:10,代码来源:decode_image_op_test.py

示例11: testExisting

 def testExisting(self):
     # Read a real jpeg and verify shape
     path = "tensorflow/core/lib/jpeg/testdata/" "jpeg_merge_test1.jpg"
     with self.test_session() as sess:
         jpeg0 = io_ops.read_file(path)
         image0 = image_ops.decode_jpeg(jpeg0)
         image1 = image_ops.decode_jpeg(image_ops.encode_jpeg(image0))
         jpeg0, image0, image1 = sess.run([jpeg0, image0, image1])
         self.assertEqual(len(jpeg0), 3771)
         self.assertEqual(image0.shape, (256, 128, 3))
         self.assertLess(self.averageError(image0, image1), 0.8)
开发者ID:sarvex,项目名称:tensorflow,代码行数:11,代码来源:image_ops_test.py

示例12: testJpeg

 def testJpeg(self):
   # Read a real jpeg and verify shape
   path = os.path.join(prefix_path, "jpeg", "testdata", "jpeg_merge_test1.jpg")
   with self.test_session(use_gpu=True) as sess:
     jpeg0 = io_ops.read_file(path)
     image0 = image_ops.decode_image(jpeg0)
     image1 = image_ops.decode_jpeg(jpeg0)
     jpeg0, image0, image1 = sess.run([jpeg0, image0, image1])
     self.assertEqual(len(jpeg0), 3771)
     self.assertEqual(image0.shape, (256, 128, 3))
     self.assertAllEqual(image0, image1)
开发者ID:AliMiraftab,项目名称:tensorflow,代码行数:11,代码来源:decode_image_op_test.py

示例13: testReadFile

 def testReadFile(self):
   cases = ['', 'Some contents', 'Неки садржаји на српском']
   for contents in cases:
     contents = compat.as_bytes(contents)
     with tempfile.NamedTemporaryFile(
         prefix='ReadFileTest', dir=self.get_temp_dir(), delete=False) as temp:
       temp.write(contents)
     with self.cached_session():
       read = io_ops.read_file(temp.name)
       self.assertEqual([], read.get_shape())
       self.assertEqual(read.eval(), contents)
     os.remove(temp.name)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:12,代码来源:io_ops_test.py

示例14: testPng

 def testPng(self):
   # Read some real PNGs, converting to different channel numbers
   inputs = [(1, "lena_gray.png")]
   for channels_in, filename in inputs:
     for channels in 0, 1, 3, 4:
       with self.cached_session(use_gpu=True) as sess:
         path = os.path.join(prefix_path, "png", "testdata", filename)
         png0 = io_ops.read_file(path)
         image0 = image_ops.decode_image(png0, channels=channels)
         image1 = image_ops.decode_png(png0, channels=channels)
         png0, image0, image1 = self.evaluate([png0, image0, image1])
         self.assertEqual(image0.shape, (26, 51, channels or channels_in))
         self.assertAllEqual(image0, image1)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:13,代码来源:decode_image_op_test.py

示例15: prepare_processing_graph

 def prepare_processing_graph(self, model_settings):
   """Builds a TensorFlow graph to apply the input distortions"""
   desired_samples = model_settings['desired_samples']
   self.wav_filename_placeholder_ = tf.placeholder(
       tf.string, [], name='filename')
   wav_loader = io_ops.read_file(self.wav_filename_placeholder_)
   wav_decoder = contrib_audio.decode_wav(
       wav_loader, desired_channels=1, desired_samples=desired_samples)
   # Allow the audio sample's volume to be adjusted.
   self.foreground_volume_placeholder_ = tf.placeholder(
       tf.float32, [], name='foreground_volme')
   scaled_foreground = tf.multiply(wav_decoder.audio,
                                   self.foreground_volume_placeholder_)
   # Shift the sample's start position, and pad any gaps with zeros.
   self.time_shift_placeholder_ = tf.placeholder(tf.int32, name='timeshift')
   shifted_foreground = tf_roll(scaled_foreground,
                                self.time_shift_placeholder_)
   # Mix in background noise.
   self.background_data_placeholder_ = tf.placeholder(
       tf.float32, [desired_samples, 1], name='background_data')
   self.background_volume_placeholder_ = tf.placeholder(
       tf.float32, [], name='background_volume')
   background_mul = tf.multiply(self.background_data_placeholder_,
                                self.background_volume_placeholder_)
   background_add = tf.add(background_mul, shifted_foreground)
   # removed clipping: tf.clip_by_value(background_add, -1.0, 1.0)
   self.background_clamp_ = background_add
   self.background_clamp_ = tf.reshape(self.background_clamp_,
                                       (1, model_settings['desired_samples']))
   # Run the spectrogram and MFCC ops to get a 2D 'fingerprint' of the audio.
   stfts = tf.contrib.signal.stft(
       self.background_clamp_,
       frame_length=model_settings['window_size_samples'],
       frame_step=model_settings['window_stride_samples'],
       fft_length=None)
   self.spectrogram_ = tf.abs(stfts)
   num_spectrogram_bins = self.spectrogram_.shape[-1].value
   lower_edge_hertz, upper_edge_hertz = 80.0, 7600.0
   linear_to_mel_weight_matrix = \
       tf.contrib.signal.linear_to_mel_weight_matrix(
           model_settings['dct_coefficient_count'],
           num_spectrogram_bins, model_settings['sample_rate'],
           lower_edge_hertz, upper_edge_hertz)
   mel_spectrograms = tf.tensordot(self.spectrogram_,
                                   linear_to_mel_weight_matrix, 1)
   mel_spectrograms.set_shape(self.spectrogram_.shape[:-1].concatenate(
       linear_to_mel_weight_matrix.shape[-1:]))
   log_mel_spectrograms = tf.log(mel_spectrograms + 1e-6)
   self.mfcc_ = tf.contrib.signal.mfccs_from_log_mel_spectrograms(
       log_mel_spectrograms)[:, :, :
                             model_settings['num_log_mel_features']]  # :13
开发者ID:indranig,项目名称:examples,代码行数:51,代码来源:generator.py


注:本文中的tensorflow.python.ops.io_ops.read_file函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。