资源简介

cyclegan图像转换压缩包,橘子苹果数据集及相关项目代码,可直接运行。

资源截图

代码片段和文件信息

import tensorflow as tf
import random
import os

try:
  from os import scandir
except ImportError:
  # Python 2 polyfill module
  from scandir import scandir
    

FLAGS = tf.flags.FLAGS

tf.flags.DEFINE_string(‘X_input_dir‘ ‘data/apple2orange/trainA‘
                       ‘X input directory default: data/apple2orange/trainA‘)
tf.flags.DEFINE_string(‘Y_input_dir‘ ‘data/apple2orange/trainB‘
                       ‘Y input directory default: data/apple2orange/trainB‘)
tf.flags.DEFINE_string(‘X_output_file‘ ‘data/tfrecords/apple.tfrecords‘
                       ‘X output tfrecords file default: data/tfrecords/apple.tfrecords‘)
tf.flags.DEFINE_string(‘Y_output_file‘ ‘data/tfrecords/orange.tfrecords‘
                       ‘Y output tfrecords file default: data/tfrecords/orange.tfrecords‘)


def data_reader(input_dir shuffle=True):
  “““Read images from input_dir then shuffle them
  Args:
    input_dir: string path of input dir e.g. /path/to/dir
  Returns:
    file_paths: list of strings
  “““
  file_paths = []

  for img_file in scandir(input_dir):
    if img_file.name.endswith(‘.jpg‘) and img_file.is_file():
      file_paths.append(img_file.path)

  if shuffle:
    # Shuffle the ordering of all image files in order to guarantee
    # random ordering of the images with respect to label in the
    # saved TFRecord files. Make the randomization repeatable.
    shuffled_index = list(range(len(file_paths)))
    random.seed(12345)
    random.shuffle(shuffled_index)

    file_paths = [file_paths[i] for i in shuffled_index]

  return file_paths


def _int64_feature(value):
  “““Wrapper for inserting int64 features into Example proto.“““
  if not isinstance(value list):
    value = [value]
  return tf.train.Feature(int64_list=tf.train.Int64List(value=value))


def _bytes_feature(value):
  “““Wrapper for inserting bytes features into Example proto.“““
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def _convert_to_example(file_path image_buffer):
  “““Build an Example proto for an example.
  Args:
    file_path: string path to an image file e.g. ‘/path/to/example.JPG‘
    image_buffer: string JPEG encoding of RGB image
  Returns:
    Example proto
  “““
  file_name = file_path.split(‘/‘)[-1]

  example = tf.train.Example(features=tf.train.Features(feature={
      ‘image/file_name‘: _bytes_feature(tf.compat.as_bytes(os.path.basename(file_name)))
      ‘image/encoded_image‘: _bytes_feature((image_buffer))
    }))
  return example

def data_writer(input_dir output_file):
  “““Write data to tfrecords
  “““
  file_paths = data_reader(input_dir)

  # create tfrecords dir if not exists
  output_dir = os.path.dirname(output_file)
  try:
    os.makedirs(output_dir)
  except os.error as e:
    pass

  images_num = len(file_paths)

  # dump to tfrecords file
  writer = tf.python_io.TFRecordWriter(output_file)

  for i in range(len(file_paths)):
    file_path = file_paths[i]

    with tf.gfile.FastGFile(file_path ‘rb‘)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-02-19 13:14  CycleGAN-TensorFlow-master\
     文件        1134  2018-02-19 13:14  CycleGAN-TensorFlow-master\.gitignore
     文件        1064  2018-02-19 13:14  CycleGAN-TensorFlow-master\LICENSE
     文件        1462  2018-02-19 13:14  CycleGAN-TensorFlow-master\Makefile
     文件        5249  2018-02-19 13:14  CycleGAN-TensorFlow-master\README.md
     文件        3508  2018-02-19 13:14  CycleGAN-TensorFlow-master\build_data.py
     文件        1575  2018-02-19 13:14  CycleGAN-TensorFlow-master\discriminator.py
     文件         891  2018-02-19 13:14  CycleGAN-TensorFlow-master\download_dataset.sh
     文件        2427  2018-02-19 13:14  CycleGAN-TensorFlow-master\export_graph.py
     文件        2346  2018-02-19 13:14  CycleGAN-TensorFlow-master\generator.py
     文件        1696  2018-02-19 13:14  CycleGAN-TensorFlow-master\inference.py
     文件        6802  2018-02-19 13:14  CycleGAN-TensorFlow-master\model.py
     文件        7553  2018-02-19 13:14  CycleGAN-TensorFlow-master\ops.py
     文件        3092  2018-02-19 13:14  CycleGAN-TensorFlow-master\reader.py
     目录           0  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\
     文件       31383  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_apple2orange_1.jpg
     文件       30087  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_apple2orange_2.jpg
     文件       30996  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_apple2orange_3.jpg
     文件       33407  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_apple2orange_4.jpg
     文件       29567  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_orange2apple_1.jpg
     文件       35564  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_orange2apple_2.jpg
     文件       35508  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_orange2apple_3.jpg
     文件       24023  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\fake_orange2apple_4.jpg
     文件       52036  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_apple2orange_1.jpg
     文件       37790  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_apple2orange_2.jpg
     文件       46289  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_apple2orange_3.jpg
     文件       55345  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_apple2orange_4.jpg
     文件       50803  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_orange2apple_1.jpg
     文件       57115  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_orange2apple_2.jpg
     文件       55258  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_orange2apple_3.jpg
     文件       38452  2018-02-19 13:14  CycleGAN-TensorFlow-master\samples\real_orange2apple_4.jpg
............此处省略3个文件信息

评论

共有 条评论