Caffe - 通过网络并行转发多个图像

Caffe - Forward multiple images through a net in parallel

具体怎么做。现在,我必须遍历每个图像并转发它。我想知道我是否可以一次设置多张图片并通过

转发
for f in fnames:
    i+=1
    print i,"/",len(fnames), f
    img = Image.open(f)
    # scale all images to 256x256
    img = img.resize((256,256), PIL.Image.ANTIALIAS)
    img = numpy.array(img).astype(numpy.float32)

    transformed_image = transformer.preprocess('data', img)
    #print transformed_image.shape

    # use CNN to predict (but don't use predicted class)
    net.blobs['data'].data[...] = transformed_image

    output = net.forward()

您可以将所有图像放入一个批次,然后 运行 net.forward() 对整个批次一次。

bs = len(fnames)  # batch size
in_shape = net.blobs['data'].data.shape
in_shape[0] = bs # set new batch size
net.blobs['data'].reshape(*in_shape)
net.reshape()
for i, f in enumerate(fnames):
    img = Image.open(f)
    # scale all images to 256x256
    img = img.resize((256,256), PIL.Image.ANTIALIAS)
    img = numpy.array(img).astype(numpy.float32)

    transformed_image = transformer.preprocess('data', img)
    #print transformed_image.shape

    # put the image into i-th place in batch
    net.blobs['data'].data[i,:,:,:] = transformed_image   

# after reading all images into batch, forward once:
net.forward()