This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
googlenet = Model(input=input, output=[loss1_classifier_act, loss2_classifier_act, loss3_classifier_act]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if 'fc' in layer_name or 'classifier' in layer_name: | |
weights = np.transpose(weights) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
net.blobs['data'].reshape(1, 3, 224, 224) | |
net.blobs['data'].data = img | |
output = net.forward() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
caffe_act = net.blobs[layer_name].data | |
layer = googlenet.get_layer(name=layer_name) | |
keras_act = get_activations(googlenet, layer, img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |