最近在鲲鹏920上搭建了tensorflow环境,运行Model Zoo中的image_classification里的models时需要准备ILSVRC2012数据集,运行maskrcnn里的models时需要准备coco2017。所以本仓库记录从零准备数据集的过程。
注:由于目的是为了让模型跑通,所以可能数据集的处理有些不合适,但最终结果是模型顺利eval。
处理ILSVRC2012 coco2017需要用到tensorflow-models(master)中的文件,所以需要 git clone https://github.com/tensorflow/models.git
。
参考链接:https://blog.csdn.net/RayChiu757374816/article/details/126870264
官网 or 百度网盘(提取码rw1v):ILSVRC2012_bbox_val_v3.tgz和ILSVRC2012_img_val.tar
tar -xvf ILSVRC2012_img_val.tar -C val/
tar -xvf ILSVRC2012_bbox_val_v3.tgz -C bbox/
# tf.app.flags.DEFINE_string('train_directory', '/tmp/',
# 'Training data directory')
# tf.app.flags.DEFINE_string('validation_directory', '/tmp/',
# 'Validation data directory')
# tf.app.flags.DEFINE_string('output_directory', '/tmp/',
# 'Output data directory')
tf.app.flags.DEFINE_string('train_directory', '/home/tf-test/dataset/ILSVRC2012',
'Training data directory')
tf.app.flags.DEFINE_string('validation_directory', '/home/tf-test/dataset/ILSVRC2012',
'Validation data directory')
tf.app.flags.DEFINE_string('output_directory', '/home/tf-test/dataset/ILSVRC2012',
'Output data directory')
#shuffled_index = range(len(filenames))
shuffled_index = list(range(len(filenames)))
# colorspace = 'RGB'
# channels = 3
# image_format = 'JPEG'
colorspace = b'RGB'
channels = 3
image_format = b'JPEG'
#image_data = tf.gfile.GFile(filename, 'r').read()
image_data = tf.gfile.GFile(filename, 'rb').read()
# def _bytes_feature(value):
# """Wrapper for inserting bytes features into Example proto."""
# return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _bytes_feature(value):
"""Wrapper for inserting bytes features into Example proto."""
if(type(value) is not bytes):
value=value.encode("utf8")
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
#spacing = np.linspace(0, len(filenames), FLAGS.num_threads + 1).astype(np.int)
spacing = np.linspace(0, len(filenames), FLAGS.num_threads + 1).astype(int)
# def __init__(self):
# # Create a single Session to run all image coding calls.
# self._sess = tf.Session()
# # Initializes function that converts PNG to JPEG data.
# self._png_data = tf.placeholder(dtype=tf.string)
# image = tf.image.decode_png(self._png_data, channels=3)
# self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)
# # Initializes function that converts CMYK JPEG data to RGB JPEG data.
# self._cmyk_data = tf.placeholder(dtype=tf.string)
# image = tf.image.decode_jpeg(self._cmyk_data, channels=0)
# self._cmyk_to_rgb = tf.image.encode_jpeg(image, format='rgb', quality=100)
# # Initializes function that decodes RGB JPEG data.
# self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
# self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)
def __init__(self):
# Create a single Session to run all image coding calls.
self._sess = tf.Session()
tf.compat.v1.disable_eager_execution()
# Initializes function that converts PNG to JPEG data.
self._png_data = tf.placeholder(dtype=tf.string)
image = tf.image.decode_png(self._png_data, channels=3)
self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)
# Initializes function that converts CMYK JPEG data to RGB JPEG data.
self._cmyk_data = tf.placeholder(dtype=tf.string)
image = tf.image.decode_jpeg(self._cmyk_data, channels=0)
self._cmyk_to_rgb = tf.image.encode_jpeg(image, format='rgb', quality=100)
# Initializes function that decodes RGB JPEG data.
self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)
工作路径:/home/tf-test/dataset/models/research/slim/datasets
python preprocess_imagenet_validation_data.py /home/tf-test/dataset/ILSVRC2012/val/ imagenet_2012_validation_synset_labels.txt
python process_bounding_boxes.py /home/tf-test/dataset/ILSVRC2012/bbox/ imagenet_lsvrc_2015_synsets.txt | sort > imagenet_2012_bounding_boxes.csv
python build_imagenet_data.py --output_directory /home/tf-test/dataset/ILSVRC2012/imagenet_tf/ --validation_directory /home/tf-test/dataset/ILSVRC2012/val/
[tf-test@jiakai-openeuler-01 imagenet_tf]$ ls
train-00000-of-01024 train-00231-of-01024 train-00462-of-01024 train-00693-of-01024 train-00924-of-01024
train-00001-of-01024 train-00232-of-01024 train-00463-of-01024 train-00694-of-01024 train-00925-of-01024
train-00002-of-01024 train-00233-of-01024 train-00464-of-01024 train-00695-of-01024 train-00926-of-01024
...
annotations_trainval2017.zip val2017.zip instances_val2017.json
unzip val2017.zip
unzip annotations_trainval2017.zip
工作路径:/home/tf-test/dataset/models/official/vision/data
python create_coco_tf_record.py --logtostderr \
--image_dir=/home/tf-test/dataset/coco2017/val2017/ \
--caption_annotations_file=/home/tf-test/dataset/coco2017/annotations/captions_val2017.json \
--output_file_prefix=/home/tf-test/dataset/coco2017/output/val \
--num_shards=100
[tf-test@jiakai-openeuler-01 output]$ ls
val-00000-of-00100.tfrecord val-00025-of-00100.tfrecord val-00050-of-00100.tfrecord val-00075-of-00100.tfrecord
val-00001-of-00100.tfrecord val-00026-of-00100.tfrecord val-00051-of-00100.tfrecord val-00076-of-00100.tfrecord
val-00002-of-00100.tfrecord val-00027-of-00100.tfrecord val-00052-of-00100.tfrecord val-00077-of-00100.tfrecord
val-00003-of-00100.tfrecord val-00028-of-00100.tfrecord val-00053-of-00100.tfrecord val-00078-of-00100.tfrecord
...
注:instances_val2017.json要放在output下,与val*文件放在一起