def extract_all(self, img): """ Evaluates an image with caffe and extracts features for each layer. """ # if the image in in grayscale, we make it to 3-channels if img.ndim == 2: img = np.tile(img[:, :, np.newaxis], (1, 1, 3)) elif img.shape[2] == 4: img = img[:, :, :3] # first, extract the 227x227 center, and convert it to BGR dim = self.get_input_dim() image = util.crop_image_center(img) image_reshape = skimage.transform.resize(image, (dim, dim)) image_reshape = (image_reshape * 255)[:, :, ::-1] # subtract the mean, cropping the 256x256 mean image xoff = (IMAGENET_MEAN.shape[1] - dim) / 2 yoff = (IMAGENET_MEAN.shape[0] - dim) / 2 image_reshape -= IMAGENET_MEAN[yoff + yoff + dim, xoff:xoff + dim] # oversample code image = image_reshape.swapaxes(1, 2).swapaxes(0, 1) input_blob = [ np.ascontiguousarray(image[np.newaxis], dtype=np.float32) ] # forward pass to the network num = 1 try: last_layer = self.net_.caffenet.blobs.items()[-1] num_output = len(last_layer[1].data.flatten()) except: # it means you have the old version of caffe num_output = 1000 output_blobs = [np.empty((num, num_output, 1, 1), dtype=np.float32)] self.net_.caffenet.Forward(input_blob, output_blobs) net_representation = {k: output for k, output in \ self.net_.caffenet.blobs.items()} return net_representation
def extract_all(self, img): """ Evaluates an image with caffe and extracts features for each layer. """ # if the image in in grayscale, we make it to 3-channels if img.ndim == 2: img = np.tile(img[:, :, np.newaxis], (1, 1, 3)) elif img.shape[2] == 4: img = img[:, :, :3] # first, extract the 227x227 center, and convert it to BGR dim = self.get_input_dim() image = util.crop_image_center(img) image_reshape = skimage.transform.resize(image, (dim, dim)) image_reshape = (image_reshape * 255)[:, :, ::-1] # subtract the mean, cropping the 256x256 mean image xoff = (IMAGENET_MEAN.shape[1] - dim)/2 yoff = (IMAGENET_MEAN.shape[0] - dim)/2 image_reshape -= IMAGENET_MEAN[yoff+yoff+dim, xoff:xoff+dim] # oversample code image = image_reshape.swapaxes(1, 2).swapaxes(0, 1) input_blob = [np.ascontiguousarray(image[np.newaxis], dtype=np.float32)] # forward pass to the network num = 1 try: last_layer = self.net_.caffenet.blobs.items()[-1] num_output = len(last_layer[1].data.flatten()) except: # it means you have the old version of caffe num_output = 1000 output_blobs = [np.empty((num, num_output, 1, 1), dtype=np.float32)] self.net_.caffenet.Forward(input_blob, output_blobs) net_representation = {k: output for k, output in \ self.net_.caffenet.blobs.items()} return net_representation
def evaluate(self, img, layer_name='softmax'): # for now only center_only is supported assert self.center_only_ == True # first, extract the 227x227 center dim = decaf.scripts.imagenet.INPUT_DIM image = util.crop_image_center(decaf.util.transform.as_rgb(img)) image = skimage.transform.resize(image, (dim, dim)) # convert to [0,255] float32 image = image.astype(np.float32) * 255. assert np.max(image) <= 255 # Flip the image if necessary, maintaining the c_contiguous order if decaf.scripts.imagenet._JEFFNET_FLIP: image = image[::-1, :].copy() # subtract the mean, cropping the 256x256 mean image xoff = (self.net_._data_mean.shape[1] - dim) / 2 yoff = (self.net_._data_mean.shape[0] - dim) / 2 image -= self.net_._data_mean[yoff + yoff + dim, xoff:xoff + dim] # make sure the data in contiguous in memory images = np.ascontiguousarray(image[np.newaxis], dtype=np.float32) # classify predictions = self.net_.classify_direct(images) scores = predictions.mean(0) # look at the particular layer if layer_name == 'softmax': return scores elif layer_name == 'fc7_relu': layer_name = 'fc7_neuron_cudanet_out' elif layer_name == 'fc7': layer_name = 'fc7_cudanet_out' elif layer_name == 'fc6_relu': layer_name = 'fc6_neuron_cudanet_out' elif layer_name == 'fc6': layer_name = 'fc6_cudanet_out' elif layer_name == 'pool5': layer_name = 'pool5_cudanet_out' else: raise ValueError('layer_name not supported') return self.net_.feature(layer_name)
def evaluate(self, img, layer_name = 'softmax'): # for now only center_only is supported assert self.center_only_ == True # first, extract the 227x227 center dim = decaf.scripts.imagenet.INPUT_DIM image = util.crop_image_center(decaf.util.transform.as_rgb(img)) image = skimage.transform.resize(image, (dim, dim)) # convert to [0,255] float32 image = image.astype(np.float32) * 255. assert np.max(image) <= 255 # Flip the image if necessary, maintaining the c_contiguous order if decaf.scripts.imagenet._JEFFNET_FLIP: image = image[::-1, :].copy() # subtract the mean, cropping the 256x256 mean image xoff = (self.net_._data_mean.shape[1] - dim)/2 yoff = (self.net_._data_mean.shape[0] - dim)/2 image -= self.net_._data_mean[yoff+yoff+dim, xoff:xoff+dim] # make sure the data in contiguous in memory images = np.ascontiguousarray(image[np.newaxis], dtype=np.float32) # classify predictions = self.net_.classify_direct(images) scores = predictions.mean(0) # look at the particular layer if layer_name == 'softmax': return scores elif layer_name == 'fc7_relu': layer_name = 'fc7_neuron_cudanet_out' elif layer_name == 'fc7': layer_name = 'fc7_cudanet_out' elif layer_name == 'fc6_relu': layer_name = 'fc6_neuron_cudanet_out' elif layer_name == 'fc6': layer_name = 'fc6_cudanet_out' elif layer_name == 'pool5': layer_name = 'pool5_cudanet_out' else: raise ValueError('layer_name not supported') return self.net_.feature(layer_name)
def evaluate(self, img, layer_name='softmax'): """ Evaluates an image with caffe and extracts features at the layer_name. layer_name can assume different values dependengly on the network architecture that you are using. Most common names are: - 'prob' or 'softmax' (default): for the last layer representation usually used for classitication - 'fc<N>', <N> is the level number: the fully connected layers - 'conv<N>': the convolutional layers - 'pool<N>': the pooling layers - 'norm<N>': the fully connected layers """ # if the image in in grayscale, we make it to 3-channels if img.ndim == 2: img = np.tile(img[:, :, np.newaxis], (1, 1, 3)) elif img.shape[2] == 4: img = img[:, :, :3] # first, extract the 227x227 center, and convert it to BGR dim = self.get_input_dim() image = util.crop_image_center(img) image_reshape = skimage.transform.resize(image, (dim, dim)) image_reshape = (image_reshape * 255)[:, :, ::-1] # subtract the mean, cropping the 256x256 mean image xoff = (IMAGENET_MEAN.shape[1] - dim) / 2 yoff = (IMAGENET_MEAN.shape[0] - dim) / 2 image_reshape -= IMAGENET_MEAN[yoff + yoff + dim, xoff:xoff + dim] # oversample code image = image_reshape.swapaxes(1, 2).swapaxes(0, 1) input_blob = [ np.ascontiguousarray(image[np.newaxis], dtype=np.float32) ] # forward pass to the network num = 1 try: last_layer = self.net_.caffenet.blobs.items()[-1] num_output = len(last_layer[1].data.flatten()) except: # it means you have the old version of caffe num_output = 1000 output_blobs = [np.empty((num, num_output, 1, 1), dtype=np.float32)] self.net_.caffenet.Forward(input_blob, output_blobs) #assert layer_name == 'softmax', 'layer_name not supported' # NOTE: decaf and caffe have different name conventions (keep softmax # for back-compatibility) if layer_name == 'softmax': layer_name = 'prob' try: net_representation = {k: output for k, output in \ self.net_.caffenet.blobs.items()} if net_representation.has_key(layer_name): scores = net_representation[layer_name].data[0] assert np.shape(net_representation[layer_name].data)[0] == 1 # Done for back-compatibility (remove single dimentions) if np.shape(scores)[1] == 1 and np.shape(scores)[2] == 1: scores = scores.flatten() else: raise ValueError('layer_name not supported') except: scores = output_blobs[0].mean(0).flatten() print 'Warning: Old version of Caffe, please install a more ' \ 'recent version (23 April 2014). The softmax' \ 'layer is now output of this function.' # if a subset is provided, we zero out the entry not used if self.wnid_subset != [] and layer_name == 'prob': for wnid in self.labels_: if wnid not in self.wnid_subset: scores[self.get_label_id(wnid)] = 0.0 return scores
def evaluate(self, img, layer_name = 'softmax'): """ Evaluates an image with caffe and extracts features at the layer_name. layer_name can assume different values dependengly on the network architecture that you are using. Most common names are: - 'prob' or 'softmax' (default): for the last layer representation usually used for classitication - 'fc<N>', <N> is the level number: the fully connected layers - 'conv<N>': the convolutional layers - 'pool<N>': the pooling layers - 'norm<N>': the fully connected layers """ # if the image in in grayscale, we make it to 3-channels if img.ndim == 2: img = np.tile(img[:, :, np.newaxis], (1, 1, 3)) elif img.shape[2] == 4: img = img[:, :, :3] # first, extract the 227x227 center, and convert it to BGR dim = self.get_input_dim() image = util.crop_image_center(img) image_reshape = skimage.transform.resize(image, (dim, dim)) image_reshape = (image_reshape * 255)[:, :, ::-1] # subtract the mean, cropping the 256x256 mean image xoff = (IMAGENET_MEAN.shape[1] - dim)/2 yoff = (IMAGENET_MEAN.shape[0] - dim)/2 image_reshape -= IMAGENET_MEAN[yoff+yoff+dim, xoff:xoff+dim] # oversample code image = image_reshape.swapaxes(1, 2).swapaxes(0, 1) input_blob = [np.ascontiguousarray(image[np.newaxis], dtype=np.float32)] # forward pass to the network num = 1 try: last_layer = self.net_.caffenet.blobs.items()[-1] num_output = len(last_layer[1].data.flatten()) except: # it means you have the old version of caffe num_output = 1000 output_blobs = [np.empty((num, num_output, 1, 1), dtype=np.float32)] self.net_.caffenet.Forward(input_blob, output_blobs) #assert layer_name == 'softmax', 'layer_name not supported' # NOTE: decaf and caffe have different name conventions (keep softmax # for back-compatibility) if layer_name == 'softmax': layer_name = 'prob' try: net_representation = {k: output for k, output in \ self.net_.caffenet.blobs.items()} if net_representation.has_key(layer_name): scores = net_representation[layer_name].data[0] assert np.shape(net_representation[layer_name].data)[0] == 1 # Done for back-compatibility (remove single dimentions) if np.shape(scores)[1]==1 and np.shape(scores)[2]==1: scores = scores.flatten() else: raise ValueError('layer_name not supported') except: scores = output_blobs[0].mean(0).flatten() print 'Warning: Old version of Caffe, please install a more ' \ 'recent version (23 April 2014). The softmax' \ 'layer is now output of this function.' # if a subset is provided, we zero out the entry not used if self.wnid_subset != [] and layer_name == 'prob': for wnid in self.labels_: if wnid not in self.wnid_subset: scores[self.get_label_id(wnid)] = 0.0 return scores