2017-02-12 33 views
1

這樣做的確切程度如何。現在,我必須遍歷每個圖像並轉發它。我想知道我是否可以一次設定多個圖像,並通過將所有圖像設置爲一個批處理,然後運行net.forward()一次整批轉發它們通過咖啡 - 通過網絡並行傳送多個圖像

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

回答

2

你可以做到這一點。

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() 
+0

我只是試圖做that..but遺憾的是,似乎我網的尺寸固定爲10(10,3,224,224) – Raaj

+0

@Raaj整形不能解決呢? – Shai

+0

重塑在這裏不起作用。 3,244,244部分是指RGB圖像。 10是指N個圖像。你的意思是我以某種方式追加/修改網絡對象? – Raaj