-
Notifications
You must be signed in to change notification settings - Fork 18.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matcaffe2 #501
Closed
Closed
Matcaffe2 #501
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
c876a49
Extended matcaffe, splited init in init_net and load_modl, also to ge…
sguada d9725e8
Convert int to mxCreateDoubleScalar
sguada 345fe4d
Fixed do_get_blobs_info
sguada c9eae73
Rename load_model to load_net
sguada 3f9b2d9
Added count to blobs, changed name of blobs to weights for layers
sguada 4a10c70
Changed name of blobs to weights for layers
sguada adedeba
Added get_layer_weights
sguada 5a8f7d3
Added get_layer_weights to interface
sguada 677668b
Added set_layer_weights
sguada 7d41603
# This is a combination of 4 commits.
sguada 30f23fc
Added save_net to be able to save a net to a proto binary file
sguada b3c3a40
Added check that layer_blobs[j]->count() == numel(elem)
sguada 664947c
Added set_weights to be able to set multiple layers weights
sguada 1eba9d8
Added get_blob_data
sguada dc05410
Added get_blob_diff to get the diff in blob
sguada f93c146
Added get_all_data and get_all_diff
sguada 9d66670
Return [output,loss] in the forward pass
sguada 28eb111
Re-organize mx_loss argument for do_foward
sguada 7595530
Added forward_prefilled
sguada c9a6301
Added logs to track the loss
sguada d150a56
Added backward_prefilled
sguada 26344fa
Fix check number of arguments for backwarda and backward_prefilled
sguada ac199b4
Remove set_mode_test from matcaffe_init.m
sguada 83156ec
Added tools to display weights as filters
sguada 86ee662
Now load_net return a new init_key
sguada 7467d58
Added Class CaffeNet to wrap all the calls to caffe
sguada e996763
Added get_mode get_phase and get_device
sguada 481016c
Added setters and getters to handle interactions with caffe
sguada 34b7f48
Added GetDevice to commom Caffe and fixed get mode and phase in matcaffe
sguada db0d259
added set_input_blobs and set_output_blobs
sguada 55120f7
Fixed device_id in MatCaffe
sguada 18e0004
Fixed reference to weights_changed and initialize mode, phase and dev…
sguada 9899661
Added set_input_blobs and set_output_blobs, avoided set when values d…
sguada 5809489
Removed AbortSet when values don't change
sguada 89c5294
Only load model and not init again
sguada 69c60aa
Don't get_weights during initialization
sguada cf6468f
Make weights a dependent property
sguada 343d085
Reorganize Constractor and Instance
sguada ec6cae8
Now initialize without parameters loads the default imagenet_deploy a…
sguada 9507c91
Fixed if and conditions
sguada 90e79f0
Added init(model_def_file,model_file) and delete to free up memory)
sguada 42f1630
Check that the net is initialized before doing operations
sguada ce5d3e6
Make reset to remove self
sguada bdd2b8a
Restore reset to just call caffe('reset') and empyt info
sguada c6ca902
Clear weights_store when reset or delete
sguada a560523
Adapted matcaffe_demo to use CaffeNet
sguada 139c42d
Added forgotten self
sguada d2562a5
Now matcaffe_demo([],1) runs in gpu mode with peppers.png
sguada 7cd79a8
Added demo of how to extract weights from the default network
sguada 907fbd4
Simplify matcaffe_demo_backward.m
sguada 536df00
Adjusted demos to use CaffeNet
sguada 87c19c5
Replace condition checking by assert(is_initialized)
sguada 52f09ce
Add caffe_safe to catch caffe exceptions
sguada ab12475
Redefine CHECK_EQ to avoid core_dump in Matlab
sguada 41f3f1c
Fixed varargin in caffe_safe.m
sguada d508d75
Reduce redundancy of code by creating auxiliary methods
sguada 0ebb0be
Added mxarray_to_blob_data and mxarray_to_blob_diff
sguada 8cf6dc1
Added comments to demo_backward
sguada a52e701
Create CHECK_EQ_MEX
sguada 364f1a8
Move mxArray<->Blobs to the begining
sguada 21b70bd
Added share_ptr
sguada f06fcac
Added share_ptr
sguada 790abca
Make lint happy
sguada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
classdef CaffeNet < handle | ||
properties (SetAccess = private) | ||
model_def_file = '../../examples/imagenet/imagenet_deploy.prototxt'; | ||
model_file = '../../examples/imagenet/caffe_reference_imagenet_model'; | ||
layers_info | ||
blobs_info | ||
end | ||
properties | ||
mode | ||
phase | ||
device_id | ||
input_blobs | ||
output_blobs | ||
end | ||
properties (Dependent) | ||
weights | ||
end | ||
properties (Access = private) | ||
init_key | ||
weights_changed = true; | ||
weights_store | ||
end | ||
methods (Access=private) | ||
function self = CaffeNet(model_def_file, model_file) | ||
if nargin == 0 | ||
init(self, self.model_def_file, self.model_file); | ||
end | ||
if nargin > 0 | ||
init_net(self, model_def_file); | ||
end | ||
if nargin > 1 | ||
load_net(self, model_file); | ||
end | ||
assert(is_initialized(self)) | ||
self.mode = caffe_safe('get_mode'); | ||
self.phase = caffe_safe('get_phase'); | ||
self.device_id = caffe_safe('get_device'); | ||
end | ||
end | ||
methods (Static) | ||
function obj = instance(model_def_file, model_file) | ||
persistent self | ||
if isempty(self) | ||
switch nargin | ||
case 2 | ||
self = CaffeNet(model_def_file, model_file); | ||
case 1 | ||
self = CaffeNet(model_def_file); | ||
case 0 | ||
self = CaffeNet(); | ||
end | ||
else | ||
if nargin > 0 && ~isempty(model_def_file) | ||
init_net(self,model_def_file); | ||
end | ||
if nargin > 1 && ~isempty(model_file) | ||
load_net(self,model_file); | ||
end | ||
end | ||
obj = self; | ||
end | ||
end | ||
methods | ||
function weights = get.weights(self) | ||
assert(is_initialized(self)) | ||
if (self.weights_changed) | ||
self.weights_store = caffe_safe('get_weights'); | ||
self.weights_changed = false; | ||
end | ||
weights = self.weights_store; | ||
end | ||
function set.weights(self,weights) | ||
assert(is_initialized(self)) | ||
caffe_safe('set_weights', weights); | ||
self.weights_store = weights; | ||
self.weights_changed = false; | ||
end | ||
function set.mode(self,mode) | ||
% mode = {'CPU' 'GPU'} | ||
switch mode | ||
case 'CPU' | ||
caffe_safe('set_mode_cpu'); | ||
self.mode = mode; | ||
case 'GPU' | ||
caffe_safe('set_mode_gpu'); | ||
self.mode = mode; | ||
otherwise | ||
fprintf('Mode unknown choose between CPU and GPU\n'); | ||
error('Mode unknown'); | ||
end | ||
end | ||
function set.phase(self, phase) | ||
% phase = {'TRAIN' 'TEST'} | ||
switch phase | ||
case 'TRAIN' | ||
caffe_safe('set_phase_train'); | ||
self.phase = phase; | ||
case {'test','TEST'} | ||
caffe_safe('set_phase_test'); | ||
self.phase = phase; | ||
otherwise | ||
fprintf('Phase unknown choose between TRAIN and TEST') | ||
error('Phase unknown'); | ||
end | ||
end | ||
function set.device_id(self, device_id) | ||
caffe_safe('set_device', device_id); | ||
self.device_id = device_id; | ||
end | ||
function set.input_blobs(self, input_blobs) | ||
assert(is_initialized(self)) | ||
caffe_safe('set_input_blobs', input_blobs); | ||
self.input_blobs = input_blobs; | ||
end | ||
function set.output_blobs(self, output_blobs) | ||
assert(is_initialized(self)) | ||
caffe_safe('set_output_blobs', output_blobs); | ||
self.output_blobs = output_blobs; | ||
end | ||
end | ||
methods | ||
function res = forward(self,input) | ||
assert(is_initialized(self)) | ||
if nargin < 2 | ||
res = caffe_safe('forward'); | ||
else | ||
res = caffe_safe('forward',input); | ||
end | ||
end | ||
function res = backward(self,diff) | ||
assert(is_initialized(self)) | ||
if nargin < 2 | ||
res = caffe_safe('backward'); | ||
else | ||
res = caffe_safe('backward',diff); | ||
end | ||
end | ||
function res = forward_prefilled(self) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('forward_prefilled'); | ||
end | ||
function res = backward_prefilled(self) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('backward_prefilled'); | ||
end | ||
function res = init(self, model_def_file, model_file) | ||
self.init_key = caffe_safe('init',model_def_file, model_file); | ||
assert(is_initialized(self)) | ||
self.model_def_file = model_def_file; | ||
self.model_file = model_file; | ||
self.layers_info = caffe_safe('get_layers_info'); | ||
self.blobs_info = caffe_safe('get_blobs_info'); | ||
self.weights_changed = true; | ||
res = self.init_key; | ||
end | ||
function res = init_net(self, model_def_file) | ||
self.init_key = caffe_safe('init_net',model_def_file); | ||
assert(is_initialized(self)) | ||
self.model_def_file = model_def_file; | ||
self.model_file = []; | ||
self.layers_info = caffe_safe('get_layers_info'); | ||
self.blobs_info = caffe_safe('get_blobs_info'); | ||
self.weights_changed = true; | ||
res = self.init_key; | ||
end | ||
function res = load_net(self, model_file) | ||
assert(is_initialized(self)) | ||
self.init_key = caffe_safe('load_net',model_file); | ||
self.model_file = model_file; | ||
self.weights_changed = true; | ||
res = self.init_key; | ||
end | ||
function res = save_net(self, model_file) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('save_net', model_file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think caffe_safe('save_net',...) shouldn't return anything, since the corresponding matcaffe function doesn't return anything. |
||
end | ||
function res = is_initialized(~) | ||
res = caffe_safe('is_initialized')>0; | ||
end | ||
function set_mode_cpu(self) | ||
self.mode = 'CPU'; | ||
end | ||
function set_mode_gpu(self) | ||
self.mode = 'GPU'; | ||
end | ||
function set_phase_train(self) | ||
self.phase = 'TRAIN'; | ||
end | ||
function set_phase_test(self) | ||
self.phase = 'TEST'; | ||
end | ||
function set_device(self, device_id) | ||
self.device_id = device_id; | ||
end | ||
function res = get_weights(self) | ||
assert(is_initialized(self)) | ||
res = self.weights; | ||
end | ||
function set_weights(self, weights) | ||
self.weights = weights; | ||
end | ||
function res = get_layer_weights(self, layer_name) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('get_layer_weights', layer_name); | ||
end | ||
function res = set_layer_weights(self, layer_name, weights) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('set_layer_weights', layer_name, weights); | ||
self.weights = caffe_safe('get_weights'); | ||
end | ||
function res = get_layers_info(self) | ||
assert(is_initialized(self)) | ||
res = self.layers_info; | ||
end | ||
function res = get_blobs_info(self) | ||
assert(is_initialized(self)) | ||
res = self.blobs_info; | ||
end | ||
function res = get_blob_data(self, blob_name) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('get_blob_data', blob_name); | ||
end | ||
function res = get_blob_diff(self, blob_name) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('get_blob_diff', blob_name); | ||
end | ||
function res = get_all_data(self) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('get_all_data'); | ||
end | ||
function res = get_all_diff(self) | ||
assert(is_initialized(self)) | ||
res = caffe_safe('get_all_diff'); | ||
end | ||
function res = get_init_key(self) | ||
self.init_key = caffe_safe('get_init_key'); | ||
res = self.init_key; | ||
end | ||
function reset(self) | ||
caffe_safe('reset'); | ||
self.init_key = caffe_safe('get_init_key'); | ||
self.layers_info = []; | ||
self.blobs_info = []; | ||
self.weights_store = []; | ||
end | ||
function delete(self) | ||
self.weights_store = []; | ||
caffe_safe('reset'); | ||
clear caffe; | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function varargout = caffe_safe(varargin) | ||
try | ||
switch nargout | ||
case 0 | ||
caffe(varargin{:}); | ||
varargout={}; | ||
case 1 | ||
varargout{1} = caffe(varargin{:}); | ||
case 2 | ||
[varargout{1} varargout{2}] = caffe(varargin{:}); | ||
end | ||
catch | ||
error('Exception in caffe'); | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
function [h, display_array] = displayData(X, example_width, display_cols) | ||
%DISPLAYDATA Display 2D data in a nice grid | ||
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data | ||
% stored in X in a nice grid. It returns the figure handle h and the | ||
% displayed array if requested. | ||
|
||
% Set example_width automatically if not passed in | ||
if ~exist('example_width', 'var') || isempty(example_width) | ||
example_width = round(sqrt(size(X, 2))); | ||
end | ||
|
||
% Gray Image | ||
colormap(gray); | ||
|
||
% Compute rows, cols | ||
[m n] = size(X); | ||
example_height = (n / example_width); | ||
|
||
% Compute number of items to display | ||
if ~exist('display_cols', 'var') | ||
display_cols = floor(sqrt(m)); | ||
end | ||
display_rows = ceil(m / display_cols); | ||
|
||
% Between images padding | ||
pad = 1; | ||
|
||
% Setup blank display | ||
display_array = - ones(pad + display_rows * (example_height + pad), ... | ||
pad + display_cols * (example_width + pad)); | ||
|
||
% Copy each example into a patch on the display array | ||
curr_ex = 1; | ||
for j = 1:display_rows | ||
for i = 1:display_cols | ||
if curr_ex > m, | ||
break; | ||
end | ||
% Copy the patch | ||
|
||
% Get the max value of the patch | ||
max_val = max(abs(X(curr_ex, :))); | ||
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ... | ||
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ... | ||
reshape(X(curr_ex, :), example_height, example_width) / max_val; | ||
curr_ex = curr_ex + 1; | ||
end | ||
if curr_ex > m, | ||
break; | ||
end | ||
end | ||
|
||
% Display Image | ||
h = imagesc(display_array, [-1 1]); | ||
|
||
% Do not show axis | ||
axis image off | ||
|
||
drawnow; | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function displayFilters(X, display_cols) | ||
%displayFilters Display [height * width * num_channels * num_filters] filters | ||
% in a nice grid using display_cols, at most it displays 32 filters | ||
% [h, display_array] = displayFilters(X, display_cols) | ||
|
||
% Gray Image | ||
colormap(gray); | ||
|
||
% Compute rows, cols | ||
[example_height example_width num_channels num_filters] = size(X); | ||
|
||
|
||
% Compute number of items to display | ||
if ~exist('display_cols', 'var') | ||
display_cols = floor(sqrt(num_filters)); | ||
end | ||
display_rows = ceil(num_filters / display_cols); | ||
colimage = []; | ||
for n = 1:min(32,num_filters) | ||
if mod(n,8)== 1 | ||
figure(ceil(n/8)) | ||
colimage = []; | ||
end | ||
filter = reshape(X(:,:,:,n),[],num_channels)'; | ||
[~,dsp] = displayData(filter,example_width,num_channels/4); | ||
colimage = cat(1,colimage,dsp,ones(1,size(dsp,2))); | ||
if mod(n,8)== 0 | ||
imagesc(colimage, [-1 1]), axis off; drawnow; | ||
end | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why upper and lower here and just upper above? Maybe just switch on lower(phase) here and in other places.