#include #include #include using namespace std; using namespace dlib; // ---------------------------------------------------------------------------------------- template using res = relu>>>>>>>; std::tuple res_ ( unsigned long outputs, unsigned long stride = 1 ) { return std::make_tuple(relu_(), add_prev1_(), bn_(CONV_MODE), con_(outputs,3,3,stride,stride), relu_(), bn_(CONV_MODE), con_(outputs,3,3,stride,stride)); } template using ares = relu>>>>>>>; // ---------------------------------------------------------------------------------------- int main(int argc, char** argv) try { if (argc != 2) { cout << "give MNIST data folder!" << endl; return 1; } std::vector> training_images; std::vector training_labels; std::vector> testing_images; std::vector testing_labels; load_mnist_dataset(argv[1], training_images, training_labels, testing_images, testing_labels); typedef loss_multiclass_log >>>>>>>>>>> net_type; const unsigned long number_of_classes = 10; net_type net(fc_(number_of_classes), avg_pool_(10,10,10,10), res_(8),res_(8),res_(8),res_(8,2), res_(8), // repeated 10 times res_(8,2), res_(8) ); dnn_trainer trainer(net,adam(0.001)); trainer.be_verbose(); trainer.set_synchronization_file("mnist_resnet_sync", std::chrono::seconds(100)); std::vector> mini_batch_samples; std::vector mini_batch_labels; dlib::rand rnd; //trainer.train(training_images, training_labels); while(trainer.get_step_size() >= 1e-3) { mini_batch_samples.clear(); mini_batch_labels.clear(); // make a 128 image mini-batch while(mini_batch_samples.size() < 128) { auto idx = rnd.get_random_32bit_number()%training_images.size(); mini_batch_samples.push_back(training_images[idx]); mini_batch_labels.push_back(training_labels[idx]); } trainer.train_one_step(mini_batch_samples, mini_batch_labels); } // wait for threaded processing to stop. trainer.get_net(); // You can access sub layers of the network like this: net.subnet().subnet().get_output(); layer<2>(net).get_output(); layer(net).get_output(); net.clean(); serialize("mnist_res_network.dat") << net; typedef loss_multiclass_log >>>>>>>>>>> test_net_type; test_net_type tnet = net; // or you could deserialize the saved network deserialize("mnist_res_network.dat") >> tnet; // Run the net on all the data to get predictions std::vector predicted_labels = tnet(training_images); int num_right = 0; int num_wrong = 0; for (size_t i = 0; i < training_images.size(); ++i) { if (predicted_labels[i] == training_labels[i]) ++num_right; else ++num_wrong; } cout << "training num_right: " << num_right << endl; cout << "training num_wrong: " << num_wrong << endl; cout << "training accuracy: " << num_right/(double)(num_right+num_wrong) << endl; predicted_labels = tnet(testing_images); num_right = 0; num_wrong = 0; for (size_t i = 0; i < testing_images.size(); ++i) { if (predicted_labels[i] == testing_labels[i]) ++num_right; else ++num_wrong; } cout << "testing num_right: " << num_right << endl; cout << "testing num_wrong: " << num_wrong << endl; cout << "testing accuracy: " << num_right/(double)(num_right+num_wrong) << endl; } catch(std::exception& e) { cout << e.what() << endl; }