Commit 541ce716 authored by Davis King's avatar Davis King

Added the program that made the resnet model.

parent d0a4c681
......@@ -35,6 +35,7 @@ if (COMPILER_CAN_DO_CPP_11)
add_example(dnn_mnist_advanced_ex)
add_example(dnn_inception_ex)
add_example(dnn_imagenet_ex)
add_example(dnn_imagenet_train_ex)
endif()
#here we apply our macros
......
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This example shows how to classify an image into one of the 1000 imagenet clategories
using the deep learning tools from the dlib C++ Library. We will use the pretrained
ResNet34 model available on the dlib website.
This example shows how to classify an image into one of the 1000 imagenet
categories using the deep learning tools from the dlib C++ Library. We will
use the pretrained ResNet34 model available on the dlib website.
The ResNet34 model is from Deep Residual Learning for Image Recognition by He, Zhang,
Ren, and Sun.
The ResNet34 architecture is from the paper Deep Residual Learning for Image
Recognition by He, Zhang, Ren, and Sun. The model file that comes with dlib
was trained using the dnn_imagenet_train_ex.cpp program on a Titan X for
about 2 weeks. This pretrained model has a top5 error of 7.572% on the 2012
imagenet validation dataset.
For an introduction to dlib's DNN module read the dnn_mnist_ex.cpp and
dnn_mnist_advanced_ex.cpp example programs.
These tools will use CUDA and cuDNN to drastically accelerate network
training and testing. CMake should automatically find them if they are
installed and configure things appropriately. If not, the program will
Finally, these tools will use CUDA and cuDNN to drastically accelerate
network training and testing. CMake should automatically find them if they
are installed and configure things appropriately. If not, the program will
still run but will be much slower to execute.
*/
......@@ -27,6 +33,7 @@ using namespace dlib;
// ----------------------------------------------------------------------------------------
// This block of statements defines the resnet-34 network
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = add_prev1<block<N,BN,1,tag1<SUBNET>>>;
......@@ -41,14 +48,14 @@ template <int N, typename SUBNET> using ares = relu<residual<block,N,affine
template <int N, typename SUBNET> using ares_down = relu<residual_down<block,N,affine,SUBNET>>;
typedef loss_multiclass_log<fc<1000,avg_pool_everything<
using anet_type = loss_multiclass_log<fc<1000,avg_pool_everything<
ares<512,ares<512,ares_down<512,
ares<256,ares<256,ares<256,ares<256,ares<256,ares_down<256,
ares<128,ares<128,ares<128,ares_down<128,
ares<64,ares<64,ares<64,
max_pool<3,3,2,2,relu<affine<con<64,7,7,2,2,
input_rgb_image_sized<227>
>>>>>>>>>>>>>>>>>>>>>>> anet_type;
>>>>>>>>>>>>>>>>>>>>>>>;
// ----------------------------------------------------------------------------------------
......@@ -101,11 +108,17 @@ void randomly_crop_images (
int main(int argc, char** argv) try
{
if (argc == 1)
{
cout << "Give this program image files as command line arguments.\n" << endl;
cout << "You will also need a copy of the file resnet34_1000_imagenet_classifier.dnn which" << endl;
cout << "is available at http://dlib.net/files/resnet34_1000_imagenet_classifier.dnn.bz2" << endl;
cout << endl;
return 1;
}
std::vector<string> labels;
anet_type net;
// Get this file from http://dlib.net/files/resnet34_1000_imagenet_classifier.dnn.bz2
// This pretrained model has a top5 error of 7.572% on the 2012 imagenet validation
// dataset.
deserialize("resnet34_1000_imagenet_classifier.dnn") >> net >> labels;
......@@ -118,13 +131,15 @@ int main(int argc, char** argv) try
dlib::rand rnd;
image_window win;
// read images from the command prompt and print the top 5 best labels.
// read images from the command prompt and print the top 5 best labels for each.
for (int i = 1; i < argc; ++i)
{
load_image(img, argv[i]);
const int num_crops = 16;
// Grab 16 random crops from the image. We will run all of them through the
// network and average the results.
randomly_crop_images(img, images, rnd, num_crops);
// p(i) == the probability the image contains object of class i.
matrix<float,1,1000> p = sum_rows(mat(snet(images.begin(), images.end())))/num_crops;
win.set_image(img);
......@@ -135,6 +150,7 @@ int main(int argc, char** argv) try
p(predicted_label) = 0;
}
cout << "Hit enter to process the next image";
cin.get();
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment