From a53df9892c49758bfee1de3455054dec9fc6dbdb Mon Sep 17 00:00:00 2001 From: Ronghang Hu Date: Wed, 27 May 2015 12:37:20 +0800 Subject: [PATCH] Fix matlab tailing dimension 1 issue for shape match Matlab cannot have tailing dimension 1 for ndim > 2, so you cannot create 20 x 10 x 1 x 1 array in matlab as it becomes 20 x 10. Extend matlab arrays to have tailing dimension 1 during shape match. --- matlab/+caffe/+test/test_net.m | 2 +- matlab/+caffe/+test/test_solver.m | 2 +- matlab/+caffe/Blob.m | 15 ++++++++++----- matlab/+caffe/get_net.m | 1 - matlab/+caffe/get_solver.m | 2 +- matlab/+caffe/io.m | 2 +- matlab/+caffe/private/CHECK.m | 2 +- matlab/+caffe/private/CHECK_FILE_EXIST.m | 2 +- matlab/+caffe/private/is_valid_handle.m | 2 +- matlab/+caffe/reset.m | 2 +- matlab/+caffe/run_tests.m | 2 +- matlab/+caffe/set_device.m | 2 +- matlab/+caffe/set_mode_cpu.m | 2 +- matlab/+caffe/set_mode_gpu.m | 2 +- 14 files changed, 22 insertions(+), 18 deletions(-) diff --git a/matlab/+caffe/+test/test_net.m b/matlab/+caffe/+test/test_net.m index 4958d50..5d9ba00 100644 --- a/matlab/+caffe/+test/test_net.m +++ b/matlab/+caffe/+test/test_net.m @@ -71,4 +71,4 @@ classdef test_net < matlab.unittest.TestCase end end end -end \ No newline at end of file +end diff --git a/matlab/+caffe/+test/test_solver.m b/matlab/+caffe/+test/test_solver.m index 9774ec1..682dad4 100644 --- a/matlab/+caffe/+test/test_solver.m +++ b/matlab/+caffe/+test/test_solver.m @@ -40,4 +40,4 @@ classdef test_solver < matlab.unittest.TestCase self.verifyEqual(self.solver.iter(), 100) end end -end \ No newline at end of file +end diff --git a/matlab/+caffe/Blob.m b/matlab/+caffe/Blob.m index a6326d1..f9b6409 100644 --- a/matlab/+caffe/Blob.m +++ b/matlab/+caffe/Blob.m @@ -51,7 +51,6 @@ classdef Blob < handle % note: matlab arrays always have at least 2 dimensions. To compare % shape between size of data and shape of this blob, extend shape of % this blob to have at least 2 dimensions - data_size = size(data); self_shape_extended = self.shape; if isempty(self_shape_extended) % target blob is a scalar (0 dim) @@ -60,12 +59,18 @@ classdef Blob < handle % target blob is a vector (1 dim) self_shape_extended = [self_shape_extended, 1]; end - is_matched = (length(self_shape_extended) == length(data_size)) ... - && all(self_shape_extended == data_size); + % also, matlab cannot have tailing dimension 1 for ndim > 2, so you + % cannot create 20 x 10 x 1 x 1 array in matlab as it becomes 20 x 10 + % extend matlab arrays to have tailing dimension 1 during shape match + data_size_extended = ... + [size(data), ones(1, length(self_shape_extended) - ndims(data))]; + is_matched = ... + (length(self_shape_extended) == length(data_size_extended)) ... + && all(self_shape_extended == data_size_extended); CHECK(is_matched, ... sprintf('%s, data size: [ %s], blob shape: [ %s]', ... 'data size does not match blob shape', ... - sprintf('%d ', data_size), sprintf('%d ', self_shape_extended))); + sprintf('%d ', data_size_extended), sprintf('%d ', self_shape_extended))); end end -end \ No newline at end of file +end diff --git a/matlab/+caffe/get_net.m b/matlab/+caffe/get_net.m index 04872b2..d60979d 100644 --- a/matlab/+caffe/get_net.m +++ b/matlab/+caffe/get_net.m @@ -34,4 +34,3 @@ if nargin == 3 end end - diff --git a/matlab/+caffe/get_solver.m b/matlab/+caffe/get_solver.m index 855709c..30366d8 100644 --- a/matlab/+caffe/get_solver.m +++ b/matlab/+caffe/get_solver.m @@ -8,4 +8,4 @@ CHECK_FILE_EXIST(solver_file); pSolver = caffe_('get_solver', solver_file); solver = caffe.Solver(pSolver); -end \ No newline at end of file +end diff --git a/matlab/+caffe/io.m b/matlab/+caffe/io.m index 95d5890..7fad968 100644 --- a/matlab/+caffe/io.m +++ b/matlab/+caffe/io.m @@ -19,4 +19,4 @@ classdef io mean_data = caffe_('read_mean', mean_proto_file); end end -end \ No newline at end of file +end diff --git a/matlab/+caffe/private/CHECK.m b/matlab/+caffe/private/CHECK.m index 1cf2103..2170654 100644 --- a/matlab/+caffe/private/CHECK.m +++ b/matlab/+caffe/private/CHECK.m @@ -4,4 +4,4 @@ if ~expr error(error_msg); end -end \ No newline at end of file +end diff --git a/matlab/+caffe/private/CHECK_FILE_EXIST.m b/matlab/+caffe/private/CHECK_FILE_EXIST.m index 1db464c..8c80fb8 100644 --- a/matlab/+caffe/private/CHECK_FILE_EXIST.m +++ b/matlab/+caffe/private/CHECK_FILE_EXIST.m @@ -4,4 +4,4 @@ if exist(filename, 'file') == 0 error('%s does not exist', filename); end -end \ No newline at end of file +end diff --git a/matlab/+caffe/private/is_valid_handle.m b/matlab/+caffe/private/is_valid_handle.m index f03469c..77abf21 100644 --- a/matlab/+caffe/private/is_valid_handle.m +++ b/matlab/+caffe/private/is_valid_handle.m @@ -25,4 +25,4 @@ else && hObj.init_key == init_key; end -end \ No newline at end of file +end diff --git a/matlab/+caffe/reset.m b/matlab/+caffe/reset.m index 2df812b..c1cfea4 100644 --- a/matlab/+caffe/reset.m +++ b/matlab/+caffe/reset.m @@ -5,4 +5,4 @@ function reset() caffe_('reset'); is_valid_handle('get_new_init_key'); -end \ No newline at end of file +end diff --git a/matlab/+caffe/run_tests.m b/matlab/+caffe/run_tests.m index 20834d4..fb1089c 100644 --- a/matlab/+caffe/run_tests.m +++ b/matlab/+caffe/run_tests.m @@ -9,4 +9,4 @@ results = [... ]; caffe.reset(); -end \ No newline at end of file +end diff --git a/matlab/+caffe/set_device.m b/matlab/+caffe/set_device.m index 43f00bd..f94068c 100644 --- a/matlab/+caffe/set_device.m +++ b/matlab/+caffe/set_device.m @@ -8,4 +8,4 @@ device_id = double(device_id); caffe_('set_device', device_id); -end \ No newline at end of file +end diff --git a/matlab/+caffe/set_mode_cpu.m b/matlab/+caffe/set_mode_cpu.m index 8c0576c..a87e0e2 100644 --- a/matlab/+caffe/set_mode_cpu.m +++ b/matlab/+caffe/set_mode_cpu.m @@ -4,4 +4,4 @@ function set_mode_cpu() caffe_('set_mode_cpu'); -end \ No newline at end of file +end diff --git a/matlab/+caffe/set_mode_gpu.m b/matlab/+caffe/set_mode_gpu.m index ecd13b5..78e5f67 100644 --- a/matlab/+caffe/set_mode_gpu.m +++ b/matlab/+caffe/set_mode_gpu.m @@ -4,4 +4,4 @@ function set_mode_gpu() caffe_('set_mode_gpu'); -end \ No newline at end of file +end -- 2.7.4