Skip to content

Instantly share code, notes, and snippets.

@alecGraves
Created November 1, 2017 15:51
Show Gist options
  • Save alecGraves/67a037f0ac71c9dd95885b3eb11b8ab5 to your computer and use it in GitHub Desktop.
Save alecGraves/67a037f0ac71c9dd95885b3eb11b8ab5 to your computer and use it in GitHub Desktop.
matlab function for building resnet with nn toolbox
function net = resnet(input_shape)
%RESNET Creates a resnet model with imput_size
n = imageInputLayer(input_shape,'Name','input');
n = layerGraph(n);
n = bottleneck(256, ['bottleneck_' char(string(1))], n)
end
function graph = bottleneck(depth, name, graph)
head = graph.Layers(end).Name;
layers = [
convlayer(1, depth/2, [name '_s1'])
convlayer(3, depth/2, [name '_s2'])
noactconvlayer(1, depth, [name '_exp'])
additionLayer(2, 'Name', [name '_add'])
reluLayer('Name', [name, '_relu'])
];
graph = addLayers(graph, layers);
graph = connectLayers(graph, head, layers(1).Name);
graph = connectLayers(graph, head, [name '_add' '/in2']);
% figure;
% plot(graph);
% title('Bottleneck Block');
end
function layers = convlayer(kernel, filters, name)
layers = [
convolution2dLayer(kernel, filters, 'Padding', 'same', 'Name', [name, '_conv'])
batchNormalizationLayer('Name', [name, '_BN'])
reluLayer('Name', [name, '_relu'])
];
end
function layers = noactconvlayer(kernel, filters, name)
layers = [
convolution2dLayer(kernel, filters, 'Padding', 'same', 'Name', [name, '_conv'])
batchNormalizationLayer('Name', [name, '_BN'])
];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment