`
haoningabc
  • 浏览: 1466084 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

tensorflow的helloworld

阅读更多
安装
easy_install pip

virtualenv tensorenv
source bin/activate
#pip install tensorflow
#pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
#easy_install --upgrade six
#pip install --upgrade pip
#pip install six
#pip install matplotlib


pip install --upgrade tensorflow



helloworld
(tensorflow)$ cd tensorflow/models/image/mnist
(tensorflow)$ python convolutional.py

>>> import tensorflow as tf
>>> hello  = tf.constant('hello TensorFlow!')
>>> sess =  tf.Session()
>>> print sess.run(hello)
hello TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42



cd tensorenv/lib/python2.7/site-packages/tensorflow/models/image/mnist

python convolutional.py


基本概念
https://blog.csdn.net/ls617386/article/details/60572770

基本用法
http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/basic_usage.html




deepspeaker
gmm
Kaldi
Caffe

测试集的使用

下载MNIST训练集
http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html


gzip -d * 解压四个文件
安装生成图片的必要的库

yum install libpng-devel freetype-devel   -y
pip install matplotlib



xxd t10k-images-idx3-ubyte |less

hexdump t10k-images-idx3-ubyte |less

yum install libjpeg
yum install libjpeg-devel -y


pip install pillow

把数据生成图片
参考
https://blog.csdn.net/u010194274/article/details/50817999
# 
encoding: utf-8
import struct
import numpy as np
import  matplotlib.pyplot as plt
#import Image
from PIL import Image
#二进制的形式读入
filename='./MNIST_data_back/train-images-idx3-ubyte'
binfile=open(filename,'rb')
buf=binfile.read()
#大端法读入4个unsigned int32
#struct用法参见网站 http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html

index=0
magic,numImages,numRows,numColumns=struct.unpack_from('>IIII',buf,index)
index+=struct.calcsize('>IIII')
#将每张图片按照格式存储到对应位置
for image in range(0,numImages):
    im=struct.unpack_from('>784B',buf,index)
    index+=struct.calcsize('>784B')
    #这里注意 Image对象的dtype是uint8,需要转换
    im=np.array(im,dtype='uint8')
    im=im.reshape(28,28)
    # fig=plt.figure()
    # plotwindow=fig.add_subplot(111)
    # plt.imshow(im,cmap='gray')
    # plt.show()
    im=Image.fromarray(im)
    im.save('train/train_%s.bmp'%image,'bmp')








如果是解析
参考https://www.jianshu.com/p/84f72791806f
# encoding: utf-8
"""
@author: monitor1379
@contact: yy4f5da2@hotmail.com
@site: www.monitor1379.com

@version: 1.0
@license: Apache Licence
@file: mnist_decoder.py
@time: 2016/8/16 20:03

对MNIST手写数字数据文件转换为bmp图片文件格式。
数据集下载地址为http://yann.lecun.com/exdb/mnist。
相关格式转换见官网以及代码注释。

========================
关于IDX文件格式的解析规则:
========================
THE IDX FILE FORMAT

the IDX file format is a simple format for vectors and multidimensional matrices of various numerical types.
The basic format is

magic number
size in dimension 0
size in dimension 1
size in dimension 2
.....
size in dimension N
data

The magic number is an integer (MSB first). The first 2 bytes are always 0.

The third byte codes the type of the data:
0x08: unsigned byte
0x09: signed byte
0x0B: short (2 bytes)
0x0C: int (4 bytes)
0x0D: float (4 bytes)
0x0E: double (8 bytes)

The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....

The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).

The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.
"""

import numpy as np
import struct
import matplotlib.pyplot as plt

# 训练集文件
train_images_idx3_ubyte_file = './MNIST_data_back/train-images-idx3-ubyte'
# 训练集标签文件
train_labels_idx1_ubyte_file = './MNIST_data_back/train-labels-idx1-ubyte'

# 测试集文件
test_images_idx3_ubyte_file = './MNIST_data_back/t10k-images-idx3-ubyte'
# 测试集标签文件
test_labels_idx1_ubyte_file = './MNIST_data_back/t10k-labels-idx1-ubyte'


def decode_idx3_ubyte(idx3_ubyte_file):
    """
    解析idx3文件的通用函数
    :param idx3_ubyte_file: idx3文件路径
    :return: 数据集
    """
    # 读取二进制数据
    bin_data = open(idx3_ubyte_file, 'rb').read()

    # 解析文件头信息,依次为魔数、图片数量、每张图片高、每张图片宽
    offset = 0
    fmt_header = '>iiii'
    magic_number, num_images, num_rows, num_cols = struct.unpack_from(fmt_header, bin_data, offset)
    print '魔数:%d, 图片数量: %d张, 图片大小: %d*%d' % (magic_number, num_images, num_rows, num_cols)

    # 解析数据集
    image_size = num_rows * num_cols
    offset += struct.calcsize(fmt_header)
    fmt_image = '>' + str(image_size) + 'B'
    images = np.empty((num_images, num_rows, num_cols))
    for i in range(num_images):
        if (i + 1) % 10000 == 0:
            print '已解析 %d' % (i + 1) + '张'
        images[i] = np.array(struct.unpack_from(fmt_image, bin_data, offset)).reshape((num_rows, num_cols))
        offset += struct.calcsize(fmt_image)
    return images


def decode_idx1_ubyte(idx1_ubyte_file):
    """
    解析idx1文件的通用函数
    :param idx1_ubyte_file: idx1文件路径
    :return: 数据集
    """
    # 读取二进制数据
    bin_data = open(idx1_ubyte_file, 'rb').read()

    # 解析文件头信息,依次为魔数和标签数
    offset = 0
    fmt_header = '>ii'
    magic_number, num_images = struct.unpack_from(fmt_header, bin_data, offset)
    print '魔数:%d, 图片数量: %d张' % (magic_number, num_images)

    # 解析数据集
    offset += struct.calcsize(fmt_header)
    fmt_image = '>B'
    labels = np.empty(num_images)
    for i in range(num_images):
        if (i + 1) % 10000 == 0:
            print '已解析 %d' % (i + 1) + '张'
        labels[i] = struct.unpack_from(fmt_image, bin_data, offset)[0]
        offset += struct.calcsize(fmt_image)
    return labels


def load_train_images(idx_ubyte_file=train_images_idx3_ubyte_file):
    """
    TRAINING SET IMAGE FILE (train-images-idx3-ubyte):
    [offset] [type]          [value]          [description]
    0000     32 bit integer  0x00000803(2051) magic number
    0004     32 bit integer  60000            number of images
    0008     32 bit integer  28               number of rows
    0012     32 bit integer  28               number of columns
    0016     unsigned byte   ??               pixel
    0017     unsigned byte   ??               pixel
    ........
    xxxx     unsigned byte   ??               pixel
    Pixels are organized row-wise. Pixel values are 0 to 255. 0 means background (white), 255 means foreground (black).

    :param idx_ubyte_file: idx文件路径
    :return: n*row*col维np.array对象,n为图片数量
    """
    return decode_idx3_ubyte(idx_ubyte_file)


def load_train_labels(idx_ubyte_file=train_labels_idx1_ubyte_file):
    """
    TRAINING SET LABEL FILE (train-labels-idx1-ubyte):
    [offset] [type]          [value]          [description]
    0000     32 bit integer  0x00000801(2049) magic number (MSB first)
    0004     32 bit integer  60000            number of items
    0008     unsigned byte   ??               label
    0009     unsigned byte   ??               label
    ........
    xxxx     unsigned byte   ??               label
    The labels values are 0 to 9.

    :param idx_ubyte_file: idx文件路径
    :return: n*1维np.array对象,n为图片数量
    """
    return decode_idx1_ubyte(idx_ubyte_file)


def load_test_images(idx_ubyte_file=test_images_idx3_ubyte_file):
    """
    TEST SET IMAGE FILE (t10k-images-idx3-ubyte):
    [offset] [type]          [value]          [description]
    0000     32 bit integer  0x00000803(2051) magic number
    0004     32 bit integer  10000            number of images
    0008     32 bit integer  28               number of rows
    0012     32 bit integer  28               number of columns
    0016     unsigned byte   ??               pixel
    0017     unsigned byte   ??               pixel
    ........
    xxxx     unsigned byte   ??               pixel
    Pixels are organized row-wise. Pixel values are 0 to 255. 0 means background (white), 255 means foreground (black).

    :param idx_ubyte_file: idx文件路径
    :return: n*row*col维np.array对象,n为图片数量
    """
    return decode_idx3_ubyte(idx_ubyte_file)


def load_test_labels(idx_ubyte_file=test_labels_idx1_ubyte_file):
    """
    TEST SET LABEL FILE (t10k-labels-idx1-ubyte):
    [offset] [type]          [value]          [description]
    0000     32 bit integer  0x00000801(2049) magic number (MSB first)
    0004     32 bit integer  10000            number of items
    0008     unsigned byte   ??               label
    0009     unsigned byte   ??               label
    ........
    xxxx     unsigned byte   ??               label
    The labels values are 0 to 9.

    :param idx_ubyte_file: idx文件路径
    :return: n*1维np.array对象,n为图片数量
    """
    return decode_idx1_ubyte(idx_ubyte_file)




def run():
    train_images = load_train_images()
    train_labels = load_train_labels()
    # test_images = load_test_images()
    # test_labels = load_test_labels()

    # 查看前十个数据及其标签以读取是否正确
    for i in range(10):
        print train_labels[i]
        plt.imshow(train_images[i], cmap='gray')
        plt.show()
    print 'done'

if __name__ == '__main__':
    run()















  • 大小: 223.9 KB
分享到:
评论

相关推荐

    [AI教程]TensorFlow入门:Hello World与基本操作-代码

    [AI教程]TensorFlow入门:Hello World与基本操作-代码

    tensorflow_helloworld_demo.py例子

    官方例子,深度学习专用,机器学习专用,代码简单,一看就会(tensorflow helloworld demo)

    xuwening#blog#tensorflow之hello world1

    查看你安装的tensorflow版本:import tensorflow as tf我这里是tensorflow1.4,所以切换分之并运行:因为demo需要先下

    tensorflow_world_anaconda_anacondapython_

    本主题将详细探讨如何利用Anaconda这一流行的Python发行版来创建和管理TensorFlow环境,以及如何通过Jupyter Notebook运行“Hello World”程序。 首先,让我们了解Anaconda。Anaconda是一个开源的数据科学平台,它...

    appengine-tensorflow-python3:使用python3和tensorflow的Hello World应用程序用于Google App引擎

    这是一个使用python3和tensorflow的Hello World应用程序,用于Google App Engine灵活的环境本地运行克隆此仓库。 git clone ...

    keras入门——HelloWorld所需的训练和测试数据

    在本文中,我们将深入探讨如何使用Keras库进行深度学习,并以"HelloWorld"项目为例,详细介绍所需的训练和测试数据。Keras是一个高级神经网络API,它构建在TensorFlow、Theano和CNTK等后端之上,为快速实验和开发...

    helloworld_python:helloworld_python

    标题中的"helloworld_python:helloworld_python"暗示了我们将探讨的是关于Python编程的"Hello, World!"程序。在Python中,创建这个简单的程序只需要一行代码: ```python print("Hello, World!") ``` 这行代码的...

    matconvnet_helloworld

    通过"matconvnet_helloworld"这样的简单示例,我们可以逐步掌握CNN的原理和实践,为进一步深入学习和研究打下坚实基础。无论你是MATLAB的老手还是新手,MatConvNet都能让你在深度学习的道路上走得更远。

    Windows Anaconda 搭建 Tensorflow环境

    hello = tf.constant("Hello World") sess = tf.Session() print(sess.run(hello)) a = tf.constant("Hello") b = tf.constant("") c = tf.constant("World") print(sess.run(a + b + c)) ``` 运行上述...

    tensorflow1.8.0+python35 绿色版

    `test.py`是Python源代码文件,很可能包含了TensorFlow的“Hello, World!”程序。在TensorFlow中,这样的程序通常会展示基本的张量操作,例如创建、初始化和操作张量。通过运行这个程序,用户可以直观地了解如何在...

    tensorflow

    张量流参考/学分-https: tensorflow hello_world监督张量流中间监督tensorflow专家监督张量流生产监督tensorflow hello_world无监督tensorflow中间无监督tensorflow专家不受监督张量流生产不受监督

    HelloWorld:10 队的 HelloWorld

    【标题】"HelloWorld:10 队的 HelloWorld" 提到的是一个项目或程序,可能是一个编程竞赛或团队合作的成果,其中“10 队”可能指的是参与的团队数量,而“HelloWorld”是编程界的一个传统示例,通常用于教学和测试...

    Tensorflow 实战Google深度学习框架

    书中详细讲解了Tensorflow的基本使用,包括安装配置、基本API介绍以及如何创建第一个“Hello, World”程序。读者将学习如何定义变量、常量、操作符,以及如何构建和执行计算图。此外,还会有对会话(Session)和占位...

    史上最全的Tensorflow学习资源汇总.rar

    1)适合初学者的Tensorflow教程和代码示例:(https://github.com/aymericdamien/TensorFlow-Examples)该教程不光提供了一些经典的数据集,更是从实现最简单的“Hello World”开始,到机器学习的经典算法,再到神经...

    TensorFlow的基本用法入门版

    1、hello.py是第一个程序,日常hello world; 2、matmul.py是第二个程序,简单的矩阵乘法; 3、InteractiveSession是第三个程序,交互式使用; 由于ipython安装有点问题,目前不能用,代码没问题,用.py版验证过...

    Tensorflow中文文档

    TensorFlow的中文文档可以初步的了解TensorFlow的工作流程

    tensorflow简单原理介绍

    hello = tf.constant('Hello, World!') sess = tf.Session() print(sess.run(hello)) ``` #### 四、TensorFlow基础运算 在深入研究更复杂的模型之前,了解TensorFlow的基本运算非常重要。 - **图(Graph)**...

Global site tag (gtag.js) - Google Analytics