Loading batch of images on gpu for detection

I was trying to run a modified version of the Pre-trained TSN Models on UCF101 tutorial on GPU.

The idea is to collect frames from a video or a camera stream for a certain time interval (1 second in my case), run the activity recognition on this batch, and repeat the process. I’m having a hard time figuring out how to load the batch of frames in the same context as the model.

I’m using:

def main():

    ctx = [mx.gpu(0)]
    net = get_model('inceptionv3_ucf101', nclass=101, pretrained=True, ctx=ctx)
    net.collect_params().reset_ctx(ctx)

    cap = cv2.VideoCapture("files/test_01.mp4")

    while(True):
        ret, frame = cap.read()
        frame_nd_resized = image.imresize(nd.array(frame_np_orig), 299, 299)

        key = cv2.waitKey(1)
        if (key == ord('q')) or (ret == False):
            break

        ## accumulating frames in batch
        frames_to_analyze.append(frame_nd_resized.asnumpy())

        ## if frames have been collected for selected amount of time, transform them and send them to detection
        if (datetime.datetime.now()-start_time_batch).total_seconds() > time_interval_to_analyze:
            transform_fn = transforms.Compose([
                video.VideoToTensor(),
                video.VideoNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ])
            frames_to_analyze_transformed = transform_fn(frames_to_analyze)

            ## running predictions over batch of frames
            pred_batch = net(nd.array(frames_to_analyze_transformed, ctx=ctx))
            pred_batch.wait_to_read()
       
            ## flush frames buffer
            frames_to_analyze = []
            frames_to_analyze_transformed = []
            print(f"\tHistory so far: {past_batches_results}")
            print("\tFlushing memory\n")
            ## reset timer for batch collection
            start_time_batch = datetime.datetime.now()

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

I’m getting the error:

AttributeError: 'list' object has no attribute 'device_typeid'

so I guess I’m supposed to load the frames batch to the selected ctx in another way, but I cannot figure out how.

from gluoncv.data import batchify
## running predictions over batch of frames
pred_batch = net(batchify.Stack()(frames_to_analyze_transformed).as_in_context(ctx[0]))

Thank you very much, it’s working now!