将数据转化成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()