6

I want implement VGG Face Descriptor in python. But I keep getting an error:

TypeError: can only concatenate list (not "numpy.ndarray") to list

My code:

import numpy as np
import cv2 
import caffe
img = cv2.imread("ak.png")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
net = caffe.Net("VGG_FACE_deploy.prototxt","VGG_FACE.caffemodel",  caffe.TEST)
print net.forward(img)

Can you help me ?

UPDATE 1

This working code is example in matlab

%  Copyright (c) 2015, Omkar M. Parkhi
%  All rights reserved.
img = imread('ak.png');
img = single(img);

    Img = [129.1863,104.7624,93.5940] ;

img = cat(3,img(:,:,1)-averageImage(1),...
    img(:,:,2)-averageImage(2),...
    img(:,:,3)-averageImage(3));

img = img(:, :, [3, 2, 1]); % convert from RGB to BGR
img = permute(img, [2, 1, 3]); % permute width and height

model = 'VGG_FACE_16_deploy.prototxt';
weights = 'VGG_FACE.caffemodel';
caffe.set_mode_cpu();
net = caffe.Net(model, weights, 'test'); % create net and load weights

res = net.forward({img});
prob = res{1};

caffe_ft = net.blobs('fc7').get_data();
stop-cran
  • 4,229
  • 2
  • 30
  • 47
Iwn
  • 65
  • 2
  • 10
  • why aren't you using `caffe.io.load_image`? – Shai Nov 22 '15 at 12:07
  • If i try `caffe.io.load_image` s i get same error `TypeError: can only concatenate list (not "numpy.ndarray") to list`. If I try passing a single element list to method i get error `TypeError: unhashable type: 'numpy.ndarray'` – Iwn Nov 22 '15 at 12:54
  • try `net.forward_all` instead of `forward`. – Shai Nov 22 '15 at 13:00
  • same error `TypeError: unhashable type: 'numpy.ndarray'` – Iwn Nov 22 '15 at 13:05
  • can you get a stack trace to show what line of code exactly causes this error? – Shai Nov 22 '15 at 13:30
  • The error is caused on line 173 in this file - [link](http://pastebin.com/3kZ4hww6) and this is the image [link](http://pastebin.com/SQ8HnVTU) – Iwn Nov 22 '15 at 13:55
  • line 173 is a comment. can you please copy the line? – Shai Nov 22 '15 at 14:03
  • sorry. `all_outs = {out: [] for out in set(self.outputs + (blobs or []))}` . Line 174. – Iwn Nov 22 '15 at 14:05
  • can you post `'VGG_FACE_16_deploy.prototxt'` please? – Shai Nov 22 '15 at 14:08
  • There is `VGG_FACE_16_deploy.prototxt` - [link](http://pastebin.com/z6NE8CCm) – Iwn Nov 22 '15 at 14:26
  • can you please try `net.forward_all(data=[img])`? – Shai Nov 22 '15 at 14:29
  • Ok. This is looking better. When I try `net.forward_all(data = [img])` I get error `AttributeError: 'list' object has no attribute 'shape'` (caused by line 101). But when i try `net.forward_all(data = img)` i get error `ValueError: could not broadcast input array from shape (1,224,3) into shape (1,3,224,224)` (caused by line 176 and then 103). This maybe resolve UPDATE 1 in my post but i dont know synatx of matlab language. Do you know what happen with image from read image to call `res = net.forward({img});` – Iwn Nov 22 '15 at 17:39

3 Answers3

7

To use python interface you need to transform the input image before feeding it to the net

img = caffe.io.load_image( "ak.png" )
img = img[:,:,::-1]*255.0 # convert RGB->BGR
avg = np.array([93.5940, 104.7624, 129.1863])  # BGR mean values
img = img - avg # subtract mean (numpy takes care of dimensions :)

Now img is H-by-W-by-3 numpy array.
Caffe expects its inputs as 4D: batch_index x channel x width x height.
Therefore you need to transpose the input and add a singleton dimension to represent the "batch_index" leading dimension

img = img.transpose((2,0,1)) 
img = img[None,:] # add singleton dimension

Now you can run the forward pass

out = net.forward_all( data = img )
Shai
  • 111,146
  • 38
  • 238
  • 371
1

OpenCV reads in BGR and scaled to 255 format by default, so:

img = cv2.imread('ak.png')
avg = np.array([93.5940,104.7624,129.1863]) # BGR mean from VGG
img -= avg # subtract mean
img = img.transpose((2,0,1)) # to match image input dimension: 3x224x224
img = img[None,:] # add singleton dimension to match batch dimension
out = net.forward_all(data = img)
loknar
  • 539
  • 5
  • 12
0

Try passing a single element list to the method.

net.forward ([img])
ypx
  • 1,459
  • 11
  • 19