将数据转化成tfrecords的形式

import os

import tensorflow as tf

import numpy as np

import matplotlib.image as mpimg

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

from scipy import misc

#import driving_data

imageFold = 'X'

imageLabelFile = 'X'

recordsPath = 'X'

recordsName = 'X'

timeSteps = X

maxClipNumInRecord = X

croppedImageHeight = X

resizedImageHeight = X

resizedImageWidth = X

imageChannel = X

recordIndex = 0

clipNumInRecord = 0

writer = tf.python_io.TFRecordWriter(recordsPath + recordsName + str(recordIndex))

clip = np.zeros((resizedImageHeight, resizedImageWidth, imageChannel), dtype = np.float32)

imgNumInClip = 0

imgNumProcessed = 0

imagesNum = len(open(imageLabelFile).readlines())

trainingImageNum = int(imagesNum * 0.8)

mu = 1

sigma = 2

np.random.seed(0)

cnt = -1

with open(imageLabelFile) as f:

for line in f:

if line.split(',')[0][0] == 'f':

continue

cnt = cnt + 1

path = line.split(',')[0]

imagePath = imageFold + path + '.jpg'

img = mpimg.imread(imagePath)

img = misc.imresize(img[-croppedImageHeight:, :, :], [resizedImageHeight, resizedImageWidth]) / 255.0

#print(img.shape)

steeringAngle = np.float32(line.split(',')[1])

clip = np.concatenate((np.array(img, dtype = np.float32), clip), axis = 2)

imgNumInClip += 1

imgNumProcessed += 1

if imgNumProcessed == trainingImageNum + 1:

clip = np.zeros((resizedImageHeight, resizedImageWidth, imageChannel), dtype = np.float32)

clip = np.concatenate((np.array(img, dtype = np.float32), clip), axis = 2)

imgNumInClip = 1

writer.close()

recordIndex = 0

recordsName = 'clipTestingData.tfrecords'

writer = tf.python_io.TFRecordWriter(recordsPath + recordsName + str(recordIndex))

clipNumInRecord = 0

print('Current record is ' + recordsPath + recordsName + str(recordIndex))

continue

if imgNumInClip == timeSteps:

if clipNumInRecord >= maxClipNumInRecord:

writer.close()

recordIndex += 1

writer = tf.python_io.TFRecordWriter(recordsPath + recordsName + str(recordIndex))

clipNumInRecord = 0

print('Current record is ' + recordsPath + recordsName + str(recordIndex))

clipNumInRecord += 1

clip = np.delete(clip, np.s_[-imageChannel :], axis = 2)

if clipNumInRecord % 100 == 0:

print(str(clipNumInRecord))

imgNumInClip -= 1

clipString = clip.reshape(-1)

example = tf.train.Example(features = tf.train.Features(feature = {'steeringAngle': tf.train.Feature(float_list=tf.train.FloatList(value=[steeringAngle])),

'clippedImageArray': tf.train.Feature(float_list=tf.train.FloatList(value=clipString)),

'imagePath': tf.train.Feature(bytes_list=tf.train.BytesList(value=[path]))

}))

writer.write(example.SerializeToString())

writer.close()

使用TensorFlow编写常用模块的Python代码示例的更多相关文章

  1. 学习TensorFlow,浅析MNIST的python代码

    在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...

  2. 编写高质量的Python代码系列(八)之部署

    Python提供了一些工具,使我们可以把软件部署到不同的环境中.它也提供了一些模块,令开发者可以把程序编写的更加健壮.本章讲解如何使用Python调试.优化并测试程序,以提升其质量与性能. 第五十四条 ...

  3. 编写高质量的Python代码系列(一)之用Pythonic方式来思考

    Python开发者用Pythonic这个形容词来描述具有特定风格的代码.这种风格是大家在使用Python语言进行编程并相互协作的过程中逐渐形成的习惯.那么,如何以改风格完成常见的Python编程工作呢 ...

  4. 编写高质量的Python代码系列(七)之协作开发

    如果多个人要开发同一个Python程序,那就得仔细商量代码的写法了.即使你是一个人开发,也需要理解其他人所写的模块.本节讲解多人协作开发Python程序时所用的标准工具及最佳做法. 第四十九条:为每个 ...

  5. 编写高质量的Python代码系列(六)之内置模块

    Python预装了许多写程序时会用到的重要模块.这些标准软件包与通常意义上的Python语言联系得非常精密,我们可以将其当成语言规范的一部分.本节将会讲解基本的内置模块. 第四十二条:用functoo ...

  6. 编写高质量的Python代码系列(五)之并发与并行

    用Python可以很容易就能写出并发程序,这种程序可以在同一时间做许多间不同的事情.我们也可以通过系统调用.子进程(subprocess)及C语言扩展来实现并行处理. 第三十六条: 用subproce ...

  7. 编写高质量的Python代码系列(四)之元类及属性

    元类(metaclass)及动态属性(dynamic attribute)都是很强大的Python特性,然后他们也可能导致及其古怪.及其突然的行为.本节讲解这些机制的常见用法,以确保各位程序员写出来的 ...

  8. 编写高质量的Python代码系列(三)之类与继承

    用Python进行编程时,通常需要编写心累,并定义这些类应该如何通过其接口及继承体系与外界交互.本节讲解如何使用类和继承来表达对象所以更具备的行为. 第二十二条:尽量用辅助类来维护程序的状态,而不要用 ...

  9. 编写高质量的Python代码系列(二)之函数

    Python中的函数具备多种特性,这可以简化编程工作.Python函数的某些性质与其他编程语言中的函数相似,但也有性质是Python独有的.本节将介绍如何用函数来表达亿图.提升可复用程度,并减少Bug ...

随机推荐

  1. python包管理(distutils、easy_install、pip、setup.py/requirements.txt、wheel)

    distutils.distutils2 distutils是 python 标准库的一部分,2000年发布.使用它能够进行 python 模块的 安装 和 发布. distutils2 被设计为 d ...

  2. HDU 3763 CD【二分查找】

    解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...

  3. 如何使用阿里云的yum源

    这里需要有网.因为我们需要下载会用到wget [root@localhost ~]# iptables -F[root@localhost ~]# systemctl stop firewalld[r ...

  4. webpack安装,npm WARN optional SKIPPING OPTIONAL DEPENDENCY,npm WARN notsup SKIPPING OPTIONAL DEPENDENCY警告

    npm install webpack -g//全局安装webpack 电脑上安装完后: 其中有两个警告: npm WARN optional SKIPPING OPTIONAL DEPENDENCY ...

  5. sklearn学习3----模型选择和评估(1)训练集和测试集的切分

    来自链接:https://blog.csdn.net/zahuopuboss/article/details/54948181 1.sklearn.model_selection.train_test ...

  6. codevs 3945 完美拓印 (KMP)

    题目大意:给你一个神奇的印章,他左右下三个面都是直的,上面是凸凹不平的面(凸凹都平行于别的面).然后给你一个轮廓线,如果一个面能与轮廓线完全重合,可以把印章的这个沿着轮廓线拓印,求所有的拓印方案. 把 ...

  7. Eigen下载安装

    首先提供Eigen的两个重要网站 官方网站 下载地址 1.下载 wget http://bitbucket.org/eigen/eigen/get/3.3.5.tar.gz 2.解压缩 tar -zx ...

  8. v4l2程序实例

    #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> ...

  9. Mybatis动态代理实现函数调用

    如果我们要使用MyBatis进行数据库操作的话,大致要做两件事情: 1. 定义DAO接口 在DAO接口中定义需要进行的数据库操作. 2. 创建映射文件 当有了DAO接口后,还需要为该接口创建映射文件. ...

  10. PatentTips - Increasing turbo mode residency of a processor

    BACKGROUND Many modern operating systems (OS's) use the Advanced Configuration and Power Interface ( ...