使用TensorFlow编写常用模块的Python代码示例
将数据转化成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代码示例的更多相关文章
- 学习TensorFlow,浅析MNIST的python代码
在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...
- 编写高质量的Python代码系列(八)之部署
Python提供了一些工具,使我们可以把软件部署到不同的环境中.它也提供了一些模块,令开发者可以把程序编写的更加健壮.本章讲解如何使用Python调试.优化并测试程序,以提升其质量与性能. 第五十四条 ...
- 编写高质量的Python代码系列(一)之用Pythonic方式来思考
Python开发者用Pythonic这个形容词来描述具有特定风格的代码.这种风格是大家在使用Python语言进行编程并相互协作的过程中逐渐形成的习惯.那么,如何以改风格完成常见的Python编程工作呢 ...
- 编写高质量的Python代码系列(七)之协作开发
如果多个人要开发同一个Python程序,那就得仔细商量代码的写法了.即使你是一个人开发,也需要理解其他人所写的模块.本节讲解多人协作开发Python程序时所用的标准工具及最佳做法. 第四十九条:为每个 ...
- 编写高质量的Python代码系列(六)之内置模块
Python预装了许多写程序时会用到的重要模块.这些标准软件包与通常意义上的Python语言联系得非常精密,我们可以将其当成语言规范的一部分.本节将会讲解基本的内置模块. 第四十二条:用functoo ...
- 编写高质量的Python代码系列(五)之并发与并行
用Python可以很容易就能写出并发程序,这种程序可以在同一时间做许多间不同的事情.我们也可以通过系统调用.子进程(subprocess)及C语言扩展来实现并行处理. 第三十六条: 用subproce ...
- 编写高质量的Python代码系列(四)之元类及属性
元类(metaclass)及动态属性(dynamic attribute)都是很强大的Python特性,然后他们也可能导致及其古怪.及其突然的行为.本节讲解这些机制的常见用法,以确保各位程序员写出来的 ...
- 编写高质量的Python代码系列(三)之类与继承
用Python进行编程时,通常需要编写心累,并定义这些类应该如何通过其接口及继承体系与外界交互.本节讲解如何使用类和继承来表达对象所以更具备的行为. 第二十二条:尽量用辅助类来维护程序的状态,而不要用 ...
- 编写高质量的Python代码系列(二)之函数
Python中的函数具备多种特性,这可以简化编程工作.Python函数的某些性质与其他编程语言中的函数相似,但也有性质是Python独有的.本节将介绍如何用函数来表达亿图.提升可复用程度,并减少Bug ...
随机推荐
- 微信小程序遇坑笔记
最近做了一个简单的微信小程序,遇到了一些坑: 1.appid固定,但是appsecret是可以变的,而且没有地方查看,后台以查看就是变更了,所以这个地方在开发的时候需要保存好: 2.打开网页,这个网页 ...
- 全球NTP服务器列表
全球- pool.ntp.org 欧洲- europe.pool.ntp.org 亚洲- asia.pool.ntp.org 大洋洲- oceania.pool.ntp.org 北美- north-a ...
- 多个 WindowsFormsHost 叠加顺序调整
原文:多个 WindowsFormsHost 叠加顺序调整 工作中遇到多个 WindowsFormsHost 包装的控件叠加顺序的调整问题,用了 BingToFront 和 BringToBack,不 ...
- tcpdump的使用以及参数详解
平时分析客户端和服务器网络交互的问题时,很多情况下需要在客户端和服务器抓包分析报文.一般win下抓包使用WireShark即可,但是linux下就需要用到tcpdump了,下面是一些对于tcpdump ...
- HashMap导致死循环问题
虽然我推测是链表形成闭环,但 没有去证明过.从网上找了一下: http://blog.csdn.net/autoinspired/archive/2008/07/16/2662290.aspx 里面也 ...
- 兴趣爱好-QQ的本地共享
QQ这个本地共享简直了,不就实现了公网FTP的功能么?好方便啊,有啥文件共享给好友就直接放在本地就可以了,真好用
- ASP.NET-使用json
数据格式 vat strJson =' {"name":"jingya","Age":88} '; // 数字不用写双引号 JSON.par ...
- POJ 3696
这里面的一个转换的小技巧很重要,把888...8转换成(10^x-1)/9*8.神来之笔,佩服. 这样有(10^x-1)/9*8=L*p得10^x-1=L*p*9/8,设m=9*L/gcd(L,8). ...
- Android ToolBar 的简单封装
使用过 ToolBar 的朋友肯定对其使用方法不陌生,由于其使用方法非常easy.假设对 ActionBar 使用比較熟练的人来说.ToolBar 就更easy了!只是,相信大家在使用的过程中都遇到过 ...
- GCC 优化选项 -O1 -O2 -O3 -OS 优先级,-FOMIT-FRAME-POINTER(O3的优化很小,只增加了几条优化而已)
四种编译优化类型的解释: `-O ' `-O1 ' Optimize. Optimizing compilation takes somewhat ...