Skip to content

Instantly share code, notes, and snippets.

@StanislawAntol
Created January 25, 2018 20:22
Show Gist options
  • Save StanislawAntol/9a6d1e4065506c5a9c939580c1ce3980 to your computer and use it in GitHub Desktop.
Save StanislawAntol/9a6d1e4065506c5a9c939580c1ce3980 to your computer and use it in GitHub Desktop.
Baidu MDL SqueezeNet-related Bug Program
#include <iostream>
#include <dirent.h>
#include "net.h"
#include "base/matrix.h"
#include "loader/loader.h"
#include "math/gemm.h"
using namespace std;
mdl::Net *NET = nullptr;
int run(int run_idx, int num_images, int thread_num);
int main() {
int run_count = 3;
int num_images = 10;
int thread_num = 1;
for (int run_idx = 0; run_idx < run_count; run_idx++) {
cout << "main(): start running cycle : " << run_idx << endl;
run(run_idx, num_images, thread_num);
cout << "main(): end running cycle : " << run_idx << endl;
}
}
int initialize(int run_idx, int thread_num=1) {
cout << "initialize(" << run_idx << "): Start" << endl;
if (mdl::Gemmer::gemmers.size() == 0) {
for (int i = 0; i < min(thread_num, 3); i++) {
mdl::Gemmer::gemmers.push_back(new mdl::Gemmer());
}
}
mdl::Loader *loader = mdl::Loader::shared_instance();
string prefix("./model/squeezenet/");
auto t1 = mdl::time();
bool load_success = loader->load(prefix + "s_model.min.json", prefix + "s_data.min.bin");
auto t2 = mdl::time();
cout << "predict(" << run_idx << "): Load time : "
<< mdl::time_diff(t1, t2) << "ms" << endl;
if (!load_success) {
cout << "predict(" << run_idx << "): Load failure" << endl;
loader->clear();
return -1;
}
if (!loader->get_loaded()) {
throw_exception("loader is not loaded yet");
}
NET = new mdl::Net(loader->_model);
NET->set_thread_num(thread_num);
cout << "initialize(" << run_idx << "): End" << endl;
return 0;
}
void predict(int run_idx, int num_images=2) {
cout << "predict(" << run_idx << "): Start" << endl;
float input_ptr[3*227*227] = { 0.0f };
double total = 0;
vector<float> result;
for (int i = 0; i < num_images; i++) {
Time t1 = mdl::time();
result = NET->predict(input_ptr);
Time t2 = mdl::time();
double diff = mdl::time_diff(t1, t2);
total += diff;
}
cout << "predict(" << run_idx << "): Total time: "
<< total / num_images << "ms." << endl;
cout << "predict(" << run_idx << "): End" << endl;
}
void destruct(int run_idx) {
cout << "destruct(" << run_idx << "): Start" << endl;
if (NET) {
delete NET;
NET = nullptr;
}
mdl::Loader *loader = mdl::Loader::shared_instance();
loader->clear();
for (int i = 0; i < mdl::Gemmer::gemmers.size(); i++) {
cout << "destruct(" << run_idx << "): Deleting gemmer " << i << endl;
auto gemmer = mdl::Gemmer::gemmers.at(i);
delete gemmer;
}
mdl::Gemmer::gemmers.clear();
cout << "destruct(" << run_idx << "): End" << endl;
}
int run(int run_idx, int num_images=2, int thread_num=1) {
initialize(run_idx, thread_num);
predict(run_idx, num_images);
destruct(run_idx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment