Skip to content

Instantly share code, notes, and snippets.

View joelouismarino's full-sized avatar

Joe Marino joelouismarino

View GitHub Profile
import caffe
MODEL_DEF = 'path/to/train_val.prototxt'
MODEL_WEIGHTS = 'path/to/bvlc_googlenet.caffemodel'
net = caffe.Net(MODEL_DEF, MODEL_WEIGHTS, caffe.TEST)
for layer_name in net.params.keys():
weights = np.copy(net.params[layer_name][0].data)
biases = np.copy(net.params[layer_name][1].data)
model_layer = googlenet.get_layer(name=layer_name)
model_layer.set_weights([weights, biases])
if 'fc' in layer_name or 'classifier' in layer_name:
weights = np.transpose(weights)
if 'conv' in layer_name or 'proj' in layer_name or (('1x1' in layer_name or '3x3' in layer_name or '5x5' in layer_name) and 'inception' in layer_name):
for i in range(weights.shape[0]): # go through each filter
for j in range(weights.shape[1]): # go through each channel
weights[i, j] = np.rot90(weights[i, j], 2) # rotate it (twice)
img = imread('cat.jpg', mode='RGB')
height,width = img.shape[:2]
img = img.astype('float32')
# subtract means
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
img[:,:,[0,1,2]] = img[:, :, [2, 1, 0]] # swap channels
img = img.transpose((2, 0, 1)) # re-order dimensions
img = img[:,(height-224)//2:(height+224)//2, (width-224)//2:(width+224)//2] #crop
net.blobs['data'].reshape(1, 3, 224, 224)
net.blobs['data'].data = img
output = net.forward()
import theano
def get_activations(model, layer, X_batch):
get_activations = theano.function([model.layers[0].input,K.learning_phase()], layer.output, allow_input_downcast=True)
activations = get_activations(X_batch,0)
return activations
caffe_act = net.blobs[layer_name].data
layer = googlenet.get_layer(name=layer_name)
keras_act = get_activations(googlenet, layer, img)
labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(labels_file, str, delimiter='\t')
caffe_top_inds = caffe_act[0].argsort()[::-1][:5]
zip(caffe_act[0][caffe_top_inds], labels[caffe_top_inds])
keras_top_inds = keras_act[0].argsort()[::-1][:5]
zip(keras_act[0][keras_top_inds], labels[keras_top_inds])