📄 comp_crop_or_zeropad.m
字号:
function x_new = comp_crop_or_zeropad (x, fsize, shift, type)% FUNCTION x_new = comp_crop_or_zeropad (x, fsize, shift, type)% % Depending on the operation type, x will be either cropped or% padded with zeros to ensure proper convolution.% For maximum (or soft-max) operations, zero-padding will be fine.% For weighted sum, zero-padding will also be fine.% For convolutions with oriented filters (such as S1 layer),% zero-padding may introduce some artifacts if the background is% not zeros; hence, cropping will be done.% Set border_case option.if strcmp(type,'gaussf') | strcmp(type,'normsp') | strcmp(type,'normdp') border_care = 'crop';else border_care = 'zero-pad';endif strcmp (border_care, 'crop') % check for the case when input image is smaller than the filter, % and "crop" was the border care option. if (size(x,1)<fsize(1)) | (size(x,2)<fsize(2)) x_new = []; else for i = 1:2 % loop thru x and y direction % number of possible shifts that fits within the input. num_possible_shift = floor((size(x,i)-fsize(i))/shift); num_possible_shift = max([0 num_possible_shift]); remainder = size(x,i)-fsize(i)-shift*num_possible_shift; size_range(i) = size(x,i)-remainder; end x_new = zeros(size_range(1),size_range(2),size(x,3)); x_new = x(1:size_range(1),1:size_range(2),:); endelseif strcmp (border_care, 'zero-pad') for i = 1:2 % loop thru x and y direction num_possible_shift = floor((size(x,i)-fsize(i))/shift); num_possible_shift = max([0 num_possible_shift]); remainder = size(x,i)-fsize(i)-shift*num_possible_shift; if remainder > 0 size_range(i) = size(x,i)+shift-remainder; elseif remainder == 0 size_range(i) = size(x,i); elseif remainder < 0 % filter is larger than x. size_range(i) = fsize(i); end end x_new = zeros(size_range(1), size_range(2), size(x,3)); x_new(1:size(x,1),1:size(x,2),:) = x;end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -