Search code examples
cntk

How to get images filenames from minibatch?


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.


Solution

  • 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()