我的环境
- Python 3.6.8
- Tensorflow 1.12.0
我的问题
我创建了一个包含图像信息(原始图像、宽度、高度、通道)和标签(0或1)的tfrecords文件。我尝试从这个文件中获取数据,但图像和标签不匹配。
代码
我通过以下代码创建了tfrecords文件。
# make lists of paths to the image file and labelcat_dir = './training_set/cats/'dog_dir = './training_set/dogs/'image_paths = []labels = []for fname in os.listdir(cat_dir): if '.jpg' in fname: image_paths.append(cat_dir + fname) labels.append(1)for fname in os.listdir(dog_dir): if '.jpg' in fname: image_paths.append(dog_dir + fname) labels.append(0)# shuffled to separate training and test datashuffle_ind = np.random.permutation(len(labels))image_paths = np.array(image_paths)[shuffle_ind]labels = np.array(labels)[shuffle_ind]# store the datadef _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))def _float_feature(value): return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))from PIL import Imagewith tf.python_io.TFRecordWriter('training_data.tfrecords') as writer: for fname, label in zip(image_paths[:-1000], labels[:-1000]): image = Image.open(fname) image_np = np.array(image) image_shape = image_np.shape image = open(fname, 'rb').read() feature = { 'height' : _int64_feature(image_shape[0]), 'width' : _int64_feature(image_shape[1]), 'channel' : _int64_feature(image_shape[2]), 'image_raw' : _bytes_feature(image), 'label' : _int64_feature(label) } tf_example = tf.train.Example(features=tf.train.Features(feature=feature)) writer.write(tf_example.SerializeToString())
接下来我从这个文件中获取数据。
image_feature_description = { 'height' : tf.FixedLenFeature([], tf.int64), 'width' : tf.FixedLenFeature([], tf.int64), 'channel' : tf.FixedLenFeature([], tf.int64), 'image_raw' : tf.FixedLenFeature([], tf.string), 'label' : tf.FixedLenFeature([], tf.int64),}def _parse_fun(example_proto): feature = tf.parse_single_example(example_proto, image_feature_description) feature['image_raw'] = tf.image.decode_jpeg(feature['image_raw']) feature['image_raw'] = tf.cast(feature['image_raw'], tf.float32) / 255.0 feature['image_raw'] = tf.image.resize_images(feature['image_raw'], (150, 150)) feature['label'] = tf.cast(feature['label'], tf.int32) return featurewith tf.Session() as sess: raw_image_dataset = tf.data.TFRecordDataset('training_data.tfrecords') parsed_image_dataset = raw_image_dataset.map(_parse_fun) batched_dataset = parsed_image_dataset#.batch(1) init = tf.global_variables_initializer() init.run() iterator = batched_dataset.make_one_shot_iterator() for i in range(10): data = iterator.get_next() X_batch = data['image_raw'].eval() y_batch = data['label'].eval() plt.imshow(X_batch) plt.show() print(y_batch)
在这个代码中,我逐一比较图像和标签,但它们不匹配。为什么?
回答:
我是TensorFlow的新手,据我所知,问题似乎是你运行了两次,所以结果不匹配。你可以尝试:
data = iterator.get_next() data_dict = data.eval() X_batch = data_dict ['image_raw'] y_batch = data_dict ['label']