0

I am using im2rec.py tool to first generate lst files and then to generate .rec and .idx files as following:

BASE_DIR = './'
IMAGES_DIR = os.path.join(BASE_DIR,'IMAGES') 
DATASET_DIR = os.path.join(BASE_DIR,'Dataset') 
TRAIN_RATIO = 0.8
TEST_DATA_RATIO = 0.1 
Dataset_lst_file = os.path.join(DATASET_DIR,"dataset") 

!python $BASE_DIR/tools/im2rec.py --list --recursive --test-ratio=$TEST_DATA_RATIO --train-ratio=$TRAIN_RATIO  $Dataset_lst_file $IMAGES_DIR 

!python $BASE_DIR/tools/im2rec.py --resize 224 --center-crop --num-thread 4 $Dataset_lst_file $IMAGES_DIR

I am successfully generating .lst, .rec and .idx files. However, my doubt is how can I read a specific image from the .rec file and plot it. For instance, to know if the images were recorded ok or just to explore my dataset.

------------Update----------

I was able to plot as following:

#https://mxnet.apache.org/versions/1.5.0/tutorials/basic/data.html
data_iter = mx.image.ImageIter(batch_size=4, data_shape=(3, 224, 224),
                              path_imgrec=Dataset_lst_file+'_train.rec',
                              path_imgidx=Dataset_lst_file+'_train.idx')
data_iter.reset()
for j in range(4):
    batch = data_iter.next()
    data = batch.data[0]
    #print(batch)
    label = batch.label[0].asnumpy()
    for i in range(4):
        ax = plt.subplot(1,4,i+1)
        plt.imshow(data[i].asnumpy().astype(np.uint8).transpose((1,2,0)))
        ax.set_title('class: ' + str(label[i]))
        plt.axis('off')
    plt.show()
jessica
  • 379
  • 8
  • 23

2 Answers2

0

This tutorial includes an example of image visualization from a .rec file: https://gluon-cv.mxnet.io/build/examples_detection/finetune_detection.html

dataset = gcv.data.RecordFileDetection('pikachu_train.rec')
classes = ['pikachu']  # only one foreground class here
image, label = dataset[0]
print('label:', label)
# display image and label
ax = viz.plot_bbox(image, bboxes=label[:, :4], labels=label[:, 4:5], class_names=classes)
plt.show()
Olivier Cruchant
  • 3,747
  • 15
  • 18
0

For completeness to previous answer. This will display images to screen using plot_bbox or render images to a folder.

Usage :

dumpRecordFileDetection('./data/val.rec', False, True, classes, ctx)
def dumpRecordFileDetection(record_filename, display_ui, output_to_directory, classes, ctx):    
    """Dump RecordFileDetection to screen or a directory"""
    if isinstance(ctx, mx.Context):
        ctx = [ctx]
    dataset = gcv.data.RecordFileDetection(record_filename)
    print('images:', len(dataset))
    image, label = dataset[0]
    bboxes=label[:, :4]
    labels=label[:, 4:5]

    print(image.shape, label.shape)
    print('labeldata:', label)
    print('bboxes:', bboxes)
    print('labels:', labels)
 
    image_dump_dir = os.path.join("./dump")
    if not os.path.exists(image_dump_dir):
        os.makedirs(image_dump_dir)

    for i, batch in enumerate(dataset):
        size = len(batch)
        image, label = batch
        print(image.shape, label.shape)
        bboxes = label[:, :4]
        labels = label[:, 4:5].astype(np.uint8) 
        
        if output_to_directory:
            file_path = os.path.join("./dump", "{0}_.png".format(i))
            # Format (c x H x W) 
            img = image.asnumpy().astype(np.uint8) 
            for box, lbl, in zip(bboxes, labels):
                cv2.rectangle(img,(box[0], box[1]),(box[2], box[3]),(0, 0, 255), 2)
                txt = "{0}".format(classes[lbl[0]])
                cv2.putText(img,txt,(box[0], box[1]), cv2.FONT_HERSHEY_PLAIN,1,(0,255,0),1,cv2.LINE_AA, False)
            cv2.imwrite(file_path, img)

        if display_ui:
            ax = viz.plot_bbox(image, bboxes=bboxes, labels=labels, class_names=classes)
            plt.show()
Greg
  • 1,671
  • 2
  • 15
  • 30