How to get images filenames from minibatch?

2019-08-06 04:51发布

I'm working on this tutorial:

https://github.com/Microsoft/CNTK/blob/master/Tutorials/CNTK_201B_CIFAR-10_ImageHandsOn.ipynb

The test / train data files are simple tab separated text files containing image filenames and correct labels like this:

...\data\CIFAR-10\test\00000.png    3
...\data\CIFAR-10\test\00001.png    8
...\data\CIFAR-10\test\00002.png    8

Assume I create a minibatch like this:

test_minibatch = reader_test.next_minibatch(10)

How can I get to the filenames for the images, which was in the first column of the test data file?

I tried with this code:

orig_features = np.asarray(test_minibatch[features_stream_info].m_data)
print(orig_features)

But, that results in printing the bytes of the images itself.

标签: cntk
1条回答
来,给爷笑一个
2楼-- · 2019-08-06 05:33

The file name is lost when loading the images through image reader.

One possible solution is to use a composite reader to load the map file in text format simultaneously. We have composite reader example in here with BrainScript: https://github.com/Microsoft/CNTK/tree/master/Examples/Image/Regression

With Python, you could do something like:

# read images
image_source = ImageDeserializer(map_file)
image_source.ignore_labels()
image_source.map_features(features_stream_name,
    [ImageDeserializer.scale(width=image_width, height=image_height, channels=num_channels,
                             scale_mode="pad", pad_value=114, interpolations='linear')])

# read rois and labels
roi_source = CTFDeserializer(roi_file)
roi_source.map_input(rois_stream_name, dim=rois_dim, format="dense")
label_source = CTFDeserializer(label_file)
label_source.map_input(labels_stream_name, dim=label_dim, format="dense")

# define a composite reader
rc = ReaderConfig([image_source, roi_source, label_source], epoch_size=sys.maxsize)
return rc.minibatch_source()
查看更多
登录 后发表回答