将cifar10数据集保存为可见图片

下载cifar10数据集:http://www.cs.toronto.edu/~kriz/cifar.html

选择cifar-10-python.tar.gz进行下载。

1 建立 main.py

import tensorflow as tf
import os
import scipy.misc
import cifar10_input

def inputs_origin(data_dir):
    filenames = [os.path.join(data_dir, ‘data_batch_%d‘ % i) for i in range(1, 6)]
    for f in filenames:
        print(f)
        if not tf.gfile.Exists(f):
            raise ValueError(‘Failed to find file‘ + f)
    filenames_queue =tf.train.string_input_producer(filenames)
    read_input = cifar10_input.read_cifar10(filenames_queue)
    reshaped_image = tf.cast(read_input.uint8image,tf.float32)
    print(reshaped_image)
    return reshaped_image

if __name__ == ‘__main__‘:
    with tf.Session() as sess:
        reshaped_image = inputs_origin(‘cifar-10-batches-py‘)
        threads = tf.train.start_queue_runners(sess=sess)
        print(threads)
        sess.run(tf.global_variables_initializer())
        if not os.path.exists(‘cifar-10-batches-py/raw/‘):
            os.makedirs(‘cifar-10-batches-py/raw/‘)
        for i in range(30):
            image = sess.run(reshaped_image)
            scipy.misc.toimage(image).save(‘cifar-10-batches-py/raw/%d.jpg‘ %i)

2 建立 cifar10_input.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf

# Process images of this size. Note that this differs from the original CIFAR
# image size of 32 x 32. If one alters this number, then the entire model
# architecture will change and any model would need to be retrained.
IMAGE_SIZE = 24

# Global constants describing the CIFAR-10 data set.
NUM_CLASSES = 10
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 10000

def read_cifar10(filename_queue):
  """Reads and parses examples from CIFAR10 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.
      label: an int32 Tensor with the label in the range 0..9.
      uint8image: a [height, width, depth] uint8 Tensor with the image data
  """

  class CIFAR10Record(object):
    pass

  result = CIFAR10Record()

  # Dimensions of the images in the CIFAR-10 dataset.
  # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the
  # input format.
  label_bytes = 1  # 2 for CIFAR-100
  result.height = 50
  result.width = 50
  result.depth = 3
  image_bytes = result.height * result.width * result.depth
  # Every record consists of a label followed by the image, with a
  # fixed number of bytes for each.
  record_bytes = label_bytes + image_bytes

  # Read a record, getting filenames from the filename_queue.  No
  # header or footer in the CIFAR-10 format, so we leave header_bytes
  # and footer_bytes at their default of 0.
  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  result.key, value = reader.read(filename_queue)

  # Convert from a string to a vector of uint8 that is record_bytes long.
  record_bytes = tf.decode_raw(value, tf.uint8)

  # The first bytes represent the label, which we convert from uint8->int32.
  result.label = tf.cast(
      tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32)

  # The remaining bytes after the label represent the image, which we reshape
  # from [depth * height * width] to [depth, height, width].
  depth_major = tf.reshape(
      tf.strided_slice(record_bytes, [label_bytes],
                       [label_bytes + image_bytes]),
      [result.depth, result.height, result.width])
  # Convert from [depth, height, width] to [height, width, depth].
  result.uint8image = tf.transpose(depth_major, [1, 2, 0])

  return result

def _generate_image_and_label_batch(image, label, min_queue_examples,
                                    batch_size, shuffle):
  """Construct a queued batch of images and labels.
  Args:
    image: 3-D Tensor of [height, width, 3] of type.float32.
    label: 1-D Tensor of type.int32
    min_queue_examples: int32, minimum number of samples to retain
      in the queue that provides of batches of examples.
    batch_size: Number of images per batch.
    shuffle: boolean indicating whether to use a shuffling queue.
  Returns:
    images: Images. 4D tensor of [batch_size, height, width, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  # Create a queue that shuffles the examples, and then
  # read ‘batch_size‘ images + labels from the example queue.
  num_preprocess_threads = 16
  if shuffle:
    images, label_batch = tf.train.shuffle_batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size,
        min_after_dequeue=min_queue_examples)
  else:
    images, label_batch = tf.train.batch(
        [image, label],
        batch_size=batch_size,
        num_threads=num_preprocess_threads,
        capacity=min_queue_examples + 3 * batch_size)

  # Display the training images in the visualizer.
  tf.summary.image(‘images‘, images)

  return images, tf.reshape(label_batch, [batch_size])

def distorted_inputs(data_dir, batch_size):
  """Construct distorted input for CIFAR training using the Reader ops.
  Args:
    data_dir: Path to the CIFAR-10 data directory.
    batch_size: Number of images per batch.
  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  filenames = [
      os.path.join(data_dir, ‘data_batch_%d.bin‘ % i) for i in xrange(1, 6)
  ]
  for f in filenames:
    if not tf.gfile.Exists(f):
      raise ValueError(‘Failed to find file: ‘ + f)

  # Create a queue that produces the filenames to read.
  filename_queue = tf.train.string_input_producer(filenames)

  # Read examples from files in the filename queue.
  read_input = read_cifar10(filename_queue)
  reshaped_image = tf.cast(read_input.uint8image, tf.float32)

  height = IMAGE_SIZE
  width = IMAGE_SIZE

  # Image processing for training the network. Note the many random
  # distortions applied to the image.

  # Randomly crop a [height, width] section of the image.
  distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

  # Randomly flip the image horizontally.
  distorted_image = tf.image.random_flip_left_right(distorted_image)

  # Because these operations are not commutative, consider randomizing
  # the order their operation.
  distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
  distorted_image = tf.image.random_contrast(
      distorted_image, lower=0.2, upper=1.8)

  # Subtract off the mean and divide by the variance of the pixels.
  float_image = tf.image.per_image_standardization(distorted_image)

  # Set the shapes of tensors.
  float_image.set_shape([height, width, 3])
  read_input.label.set_shape([1])

  # Ensure that the random shuffling has good mixing properties.
  min_fraction_of_examples_in_queue = 0.4
  min_queue_examples = int(
      NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN * min_fraction_of_examples_in_queue)
  print(‘Filling queue with %d CIFAR images before starting to train. ‘
        ‘This will take a few minutes.‘ % min_queue_examples)

  # Generate a batch of images and labels by building up a queue of examples.
  return _generate_image_and_label_batch(
      float_image,
      read_input.label,
      min_queue_examples,
      batch_size,
      shuffle=True)

def inputs(eval_data, data_dir, batch_size):
  """Construct input for CIFAR evaluation using the Reader ops.
  Args:
    eval_data: bool, indicating if one should use the train or eval data set.
    data_dir: Path to the CIFAR-10 data directory.
    batch_size: Number of images per batch.
  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
  """
  if not eval_data:
    filenames = [
        os.path.join(data_dir, ‘data_batch_%d.bin‘ % i) for i in xrange(1, 6)
    ]
    num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
  else:
    filenames = [os.path.join(data_dir, ‘test_batch.bin‘)]
    num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_EVAL

  for f in filenames:
    if not tf.gfile.Exists(f):
      raise ValueError(‘Failed to find file: ‘ + f)

  # Create a queue that produces the filenames to read.
  filename_queue = tf.train.string_input_producer(filenames)

  # Read examples from files in the filename queue.
  read_input = read_cifar10(filename_queue)
  reshaped_image = tf.cast(read_input.uint8image, tf.float32)

  height = IMAGE_SIZE
  width = IMAGE_SIZE

  # Image processing for evaluation.
  # Crop the central [height, width] of the image.
  resized_image = tf.image.resize_image_with_crop_or_pad(
      reshaped_image, width, height)

  # Subtract off the mean and divide by the variance of the pixels.
  float_image = tf.image.per_image_standardization(resized_image)

  # Set the shapes of tensors.
  float_image.set_shape([height, width, 3])
  read_input.label.set_shape([1])

  # Ensure that the random shuffling has good mixing properties.
  min_fraction_of_examples_in_queue = 0.4
  min_queue_examples = int(
      num_examples_per_epoch * min_fraction_of_examples_in_queue)

  # Generate a batch of images and labels by building up a queue of examples.
  return _generate_image_and_label_batch(
      float_image,
      read_input.label,
      min_queue_examples,
      batch_size,
      shuffle=False)

 显示部分图片:

 

原文地址:https://www.cnblogs.com/dudu1992/p/8908081.html

时间: 2024-11-08 21:58:39

将cifar10数据集保存为可见图片的相关文章

Caffe2——cifar10数据集创建lmdb或leveldb类型的数据

Caffe2——cifar10数据集创建lmdb或leveldb类型的数据 cifar10数据集和mnist数据集存储方式不同,cifar10数据集把标签和图像数据以bin文件的方式存放在同一个文件内,这种存放方式使得每个子cifar数据bin文件的结构相同,所以cifar转换数据代码比mnist的代码更加的模块化,分为源数据读取模块(image_read函数),把lmdb(leveldb)数据转换的变量声明,句柄(函数)调用都放到定义的caffe::db子空间中,这样简化了代码,而且使得代码更

python实现cifar10数据集的可视化

在学习tensorflow的mnist和cifar实例的时候,官方文档给出的讲解都是一张张图片,直观清晰,当我们看到程序下载下来的数据的时候,宝宝都惊呆了,都是二进制文件,这些二进制文件还不小,用文本编辑器打开看也看不懂,要是将数据再现为图像,多好! (1)CIFAR-10数据集介绍 ① CIFAR-10数据集包含60000个32*32的彩色图像,共有10类.有50000个训练图像和10000个测试图像. 数据集分为5个训练块和1个测试块,每个块有10000个图像.测试块包含从每类随机选择的10

CIFAR-10数据集图像分类【PCA+基于最小错误率的贝叶斯决策】

CIFAR-10和CIFAR-100均是带有标签的数据集,都出自于规模更大的一个数据集,他有八千万张小图片.而本次实验采用CIFAR-10数据集,该数据集共有60000张彩色图像,这些图像是32*32,分为10个类,每类6000张图.这里面有50000张用于训练,构成了5个训练批,每一批10000张图:另外10000用于测试,单独构成一批.测试批的数据里,取自10类中的每一类,每一类随机取1000张.抽剩下的就随机排列组成了训练批.注意一个训练批中的各类图像并不一定数量相同,总的来看训练批,每一

【VC++技术杂谈006】截取电脑桌面并将其保存为bmp图片

本文主要介绍如何截取电脑桌面并将其保存为bmp图片. 1. Bmp图像文件组成 Bmp是Windows操作系统中的标准图像文件格式. Bmp图像文件由四部分组成: (1)位图头文件数据结构,包含Bmp图像文件的类型.文件大小等信息. (2)位图信息数据结构,包含Bmp图像的宽.高.压缩类型等信息. (3)颜色表,该部分可选,有些位图需要,有些位图(如24位真彩色位图)不需要. (4)位图数据. 1.1位图头文件数据结构 位图头文件数据结构包含Bmp图像文件的类型.文件大小等信息,占用14个字节.

PHP《将画布(canvas)图像保存成本地图片的方法》

用PHP将网页上的Canvas图像保存到服务器上的方法 2014年6月27日 歪脖骇客 发表回复 8 在几年前HTML5还没有流行的时候,我们的项目经理曾经向我提出这样一个需求:让项目评审专家们在评审结束时用笔在平板电脑上进行电子签名. 这需要我们评审软件里提供这样一个功能:打开浏览器,登录,进入评审意见页,页面最下部有个方块区域,用户在这里用触摸笔进行签名,然后这个签名将会保持 的服务器上. 这样的一个需求在当时是让我大费周折,但如今想起来,如果用html5的canvas实现,真是太简单了.在

Word中截取部分内容并保存为jpg图片的方法

private void button1_Click(object sender, EventArgs e) { var appWord = new Microsoft.Office.Interop.Word.Application(); var doc = new Microsoft.Office.Interop.Word.Document(); object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_- //打

用PHP和MySQL保存和输出图片

mysql可以直接保存二进制的数据,数据类型是blob.   通常在数据库中所使用的文本或整数类型的字段和需要用来保存图片的字段的不同之    处就在于两者所需要保存的数据量不同.MySQL数据库使用专门的字段来保存大容量的数据,数据    类型为BLOB.        MySQL数据库为BLOB做出的定义如下:BLOB数据类型是一种大型的二进制对象,可以保存可    变数量的数据.BLOB具有四种类型,分别是TINYBLOB,BLOB,  MEDIUMBLOB  和LONGBLOB,区别在于

[ActionScript 3.0] 通过BitmapData将对象保存成jpg图片

此方法需要用到JPGEncoder.as和BitString.as这两个类,是将BitmapData对象转换成ByteArray,然后通过FileStream把此ByteArray写入到文件保存成jpg图片,因为用到File相关类,故需要用air播放器发布flash,在此提供两个下载JPGEncoder.as和BitString.as类的地址, CSDN:http://download.csdn.net/source/3205224 Adobe官方的CoreLib下载: http://code.

C# 保存窗体为图片(保存纵断面图)

源码如下: #region 保存纵断面截图 private void button_save_Click(object sender , EventArgs e) { SaveFileDialog saveImageDialog = new SaveFileDialog(); saveImageDialog.Title = "保存纵断面图"; saveImageDialog.DefaultExt = ".png"; saveImageDialog.FileName