MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/dnn_introduction3_ex.cpp:62–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60
61
62int main() try
63{
64 // Let's instantiate our network in train mode.
65 model::train net;
66
67 // We create a new scope so that resources from the loaded network are freed
68 // automatically when leaving the scope.
69 {
70 // Now, let's define the classic ResNet50 network and load the pretrained model on
71 // ImageNet.
72 resnet::train_50 resnet50;
73 std::vector<string> labels;
74 deserialize("resnet50_1000_imagenet_classifier.dnn") >> resnet50 >> labels;
75
76 // For transfer learning, we are only interested in the ResNet50's backbone, which
77 // lays below the loss and the fc layers, so we can extract it as:
78 auto backbone = std::move(resnet50.subnet().subnet());
79
80 // We can now assign ResNet50's backbone to our network skipping the different
81 // layers, in our case, the loss layer and the fc layer:
82 net.subnet().subnet() = backbone;
83
84 // An alternative way to use the pretrained network on a different
85 // network is to extract the relevant part of the network (we remove
86 // loss and fc layers), stack the new layers on top of it and assign
87 // the network.
88 using net_type = loss_metric<fc_no_bias<128, decltype(backbone)>>;
89 net_type net2;
90 net2.subnet().subnet() = backbone;
91 }
92
93 // We can use the visit_layers function to modify the weight decay of the entire
94 // network:
95 visit_computational_layers(net, visitor_weight_decay_multiplier(0.001));
96
97 // We can also use predefined visitors to affect the learning rate of the whole
98 // network.
99 set_all_learning_rate_multipliers(net, 0.5);
100
101 // Modifying the learning rates of a network is a common practice for fine tuning, for
102 // this reason it is already provided. However, it is implemented internally using a
103 // visitor that is very similar to the one defined in this example.
104
105 // Usually, we want to freeze the network, except for the top layers:
106 visit_computational_layers(net.subnet().subnet(), visitor_weight_decay_multiplier(0));
107 set_all_learning_rate_multipliers(net.subnet().subnet(), 0);
108
109 // Alternatively, we can use the visit_layers_range to modify only a specific set of
110 // layers:
111 visit_computational_layers_range<0, 2>(net, visitor_weight_decay_multiplier(1));
112
113 // Sometimes we might want to set the learning rate differently throughout the network.
114 // Here we show how to use adjust the learning rate at the different ResNet50's
115 // convolutional blocks:
116 set_learning_rate_multipliers_range< 0, 2>(net, 1);
117 set_learning_rate_multipliers_range< 2, 38>(net, 0.1);
118 set_learning_rate_multipliers_range< 38, 107>(net, 0.01);
119 set_learning_rate_multipliers_range<107, 154>(net, 0.001);

Callers

nothing calls this directly

Calls 15

moveFunction · 0.85
assign_all_pixelsFunction · 0.85
rgb_pixelClass · 0.85
count_parametersFunction · 0.85
deserializeFunction · 0.70
to_tensorMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
forwardMethod · 0.45

Tested by

no test coverage detected