Understanding deep features with computer-generated imagery

Mathieu Aubry and Bryan Russell, ICCV 2015

Disclaimer: This implementation of the approach described in the paper does not claim to be optimal in any way

This first release for the code only contains the code for the quantatative feature analysis. Code for generating the embedding images, perform the Nearest Neighboor retrieval and generate colored-coded view of the analysis will be released progressively.


In [1]:
## imports
from image_generator import ImageGenerator
import cnn_statistics as stat
import numpy as np
import os
import time
caffe_root = '/home/ma/caffe/'  # change to your caffe root
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import scipy
import math
import string

In [2]:
## initialize ALEXNET network
caffe.set_device(0)
caffe.set_mode_gpu()
net = caffe.Classifier(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
                       caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')
net.transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1))  # ImageNet mean
net.transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
net.transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB

Change the paths to your data and desired result folder


In [3]:
## Data and results folders
DATA_ROOT='/home/ma/DATA/modelNet/web'
DATA_ROOT_rotation=DATA_ROOT+'/render_rotation'
DATA_ROOT_light=DATA_ROOT+'/render_light'
DATA_ROOT_color_bg=DATA_ROOT+'/render_color_bg'
DATA_ROOT_color_fg=DATA_ROOT+'/render_color_fg'
RESULTS_ROOT='/home/ma/RESULTS/modelNet_web'
if not os.path.exists(RESULTS_ROOT):
        os.mkdir(RESULTS_ROOT)

Select wich eperiments you want to run (some may require a long time!)


In [4]:
categories_names=['car'] # ['car','chair','sofa' ,'toilet','bed']
test_layers=['pool5','fc6','fc7','fc8','prob'] # ['data','conv1','pool1','norm1','conv2','pool2','norm2','conv3','conv4', 'conv5','pool5','fc6','fc7','fc8','prob']
generator_kinds=['rotation']#['rotation','translationxy','scale','light','color_fg','color_bg',]
synth_generator_kinds=['rectangle','bicolor']

In [5]:
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#   COMPUTE ANALYSIS
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# for experiments using synthetic 2D examples
for generator_kind in synth_generator_kinds:
     print('%%%%%%%%%%  '+ generator_kind)
     params={'variation_name':generator_kind}
     results_folder=RESULTS_ROOT+'/'+generator_kind
     generator=ImageGenerator(params)
        
     # computing only the variance statistics
     stat.var_sep(generator,net,results_folder,test_layers)

    # computing the PCA (slower and limited to higher layers because it computes the covariance)
    # note: alternative PCA methods could be use to compute this for lower layers (see paper for details)
#         stat.pca(generator,net,results_folder,test_layers,N_projections=300)
    
# for experiments using CAD models
for category_name in categories_names:#['bicycle']:#

    print('%%%%%%%%%%%%%%%%%%%%  '+ category_name+' %%%%%%%%%%%%%%%%%%%%%%')
    
    # define the parameter for each transformation: some are generated on the fly (e.g. translation),
    # some have to be already available (rotations)
    params={}
    data_folder=DATA_ROOT_rotation+'/'+category_name
    params['rotation']={'variation_name':'subfolders','data_folder':data_folder}
    params['scale']={'variation_name':'scale','data_folder':data_folder}
    params['translationxy']={'variation_name':'translationxy','data_folder':data_folder}
    data_folder=DATA_ROOT_light+'/'+category_name
    params['light']={'variation_name':'subfolders','data_folder':data_folder}
    data_folder=DATA_ROOT_color_bg+'/'+category_name
    params['color_bg']={'variation_name':'subfolders','data_folder':data_folder}
    data_folder=DATA_ROOT_color_fg+'/'+category_name
    params['color_fg']={'variation_name':'subfolders','data_folder':data_folder}
    
   
    for generator_kind in generator_kinds:
         
         print('%%%%%%%%%%  '+ generator_kind)
         generator=ImageGenerator(params[generator_kind])
     
         results_folder=RESULTS_ROOT+'/'+category_name+'_'+generator_kind
            
         # computing only the variance statistics
         stat.var_sep(generator,net,results_folder,test_layers)
         
         print('')   
        # computing the PCA (slower and limited to higher layers because it computes the covariance)
        # note: alternative PCA methods could be use to compute this for lower layers (see paper for details)
#         stat.pca(generator,net,results_folder,test_layers,N_projections=300)


%%%%%%%%%%  rectangle
computed 13 instances in 6.50043606758 seconds 
Variance repartition layer pool5 :
Dimension 1 : 49.8%
Dimension 2 : 9.5%
Residual    : 40.8%
Variance repartition layer fc6 :
Dimension 1 : 45.1%
Dimension 2 : 22.3%
Residual    : 32.6%
Variance repartition layer fc7 :
Dimension 1 : 33.9%
Dimension 2 : 37.0%
Residual    : 29.1%
Variance repartition layer fc8 :
Dimension 1 : 26.1%
Dimension 2 : 46.8%
Residual    : 27.1%
Variance repartition layer prob :
Dimension 1 : 18.6%
Dimension 2 : 49.0%
Residual    : 32.4%
%%%%%%%%%%  bicolor
computed 125 instances in 125.190086126 seconds 
Variance repartition layer pool5 :
Dimension 1 : 20.4%
Dimension 2 : 44.8%
Residual    : 34.8%
Variance repartition layer fc6 :
Dimension 1 : 18.7%
Dimension 2 : 42.6%
Residual    : 38.7%
Variance repartition layer fc7 :
Dimension 1 : 19.2%
Dimension 2 : 39.9%
Residual    : 40.8%
Variance repartition layer fc8 :
Dimension 1 : 20.3%
Dimension 2 : 38.3%
Residual    : 41.4%
Variance repartition layer prob :
Dimension 1 : 15.3%
Dimension 2 : 30.6%
Residual    : 54.1%
%%%%%%%%%%%%%%%%%%%%  car %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%  rotation
computed 485 instances in 198.420681 seconds 
Variance repartition layer pool5 :
Dimension 1 : 36.0%
Dimension 2 : 19.7%
Residual    : 44.3%
Variance repartition layer fc6 :
Dimension 1 : 27.2%
Dimension 2 : 29.2%
Residual    : 43.6%
Variance repartition layer fc7 :
Dimension 1 : 26.8%
Dimension 2 : 32.3%
Residual    : 40.9%
Variance repartition layer fc8 :
Dimension 1 : 30.5%
Dimension 2 : 37.4%
Residual    : 32.1%
Variance repartition layer prob :
Dimension 1 : 16.0%
Dimension 2 : 34.9%
Residual    : 49.1%