python中tensorflow的模块怎么加_使用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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值