只有几串代码,快速实现,但是原理没有详细说明。
使用tensorflow-gpu 2以上版本:
BATCH_SIZE = 2
train_dir = "C:\\Users\Desktop\泸州老窖精品头曲组合装\\"
train_tfrecord = "C:\\Users\Desktop\\train.tfrecords"
dataset_to_tfrecord(dataset_dir=train_dir, tfrecord_name=train_tfrecord)
自定义函数如下:
def dataset_to_tfrecord(dataset_dir, tfrecord_name):
image_paths, image_labels = get_images_and_labels(dataset_dir)
image_paths_and_labels_dict = {}
for i in range(len(image_paths)):
image_paths_and_labels_dict[image_paths[i]] = image_labels[i]
# shuffle the dict
image_paths_and_labels_dict = shuffle_dict(image_paths_and_labels_dict) # 打乱数据
with tf.io.TFRecordWriter(path=tfrecord_name) as writer:
for image_path, label in image_paths_and_labels_dict.items():
print("Writing to tfrecord: {}".format(image_path))
image_string = open(