Commit 8ae50096 authored by Federico Rossi's avatar Federico Rossi

..

parent 0f66b2c3
......@@ -15,60 +15,6 @@ if(USE_SERIALIZER)
${project_library_target_name} ${REQUIRED_LIBRARIES})
add_dependencies(inet_tests_type inet_test)
add_executable(inet_train_p16_2 train.cpp ${tiny_dnn_headers})
target_link_libraries(inet_train_p16_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_p16_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_trains_type inet_train_p16_2)
add_executable(inet_test_p16_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p16_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p16_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p16_2)
add_executable(inet_test_p16_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p16_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p16_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p16_0)
add_executable(inet_train_p14_2 train.cpp ${tiny_dnn_headers})
target_link_libraries(inet_train_p14_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_p14_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_trains_type inet_train_p14_2)
add_executable(inet_test_p14_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p14_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p14_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p14_2)
add_executable(inet_test_p14_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p14_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p14_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p14_0)
add_executable(inet_train_p12_2 train.cpp ${tiny_dnn_headers})
target_link_libraries(inet_train_p12_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_p12_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_trains_type inet_train_p12_2)
add_executable(inet_test_p12_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p12_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p12_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p12_2)
add_executable(inet_test_p12_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p12_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p12_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p12_0)
add_executable(inet_test_p10_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p10_0
......@@ -82,81 +28,5 @@ add_executable(inet_test_p12_0 test.cpp ${tiny_dnn_headers})
target_compile_definitions(inet_test_p8_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=8 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p8_0)
add_executable(inet_test_p7_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p7_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p7_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=7 CNN_EXP_BITS=1 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p7_0)
add_executable(inet_test_p6_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p6_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p6_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=6 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p6_0)
add_executable(inet_test_p5_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p5_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p5_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=5 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p5_0)
add_executable(inet_test_p4_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(inet_test_p4_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_p4_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=4 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(inet_tests_type inet_test_p4_0)
if(TAB8)
add_executable(inet_train_posittab8 train.cpp ${TAB_POSIT_LIB8} ${tiny_dnn_headers})
target_link_libraries(inet_train_posittab8
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_posittab8 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit8)
add_executable(inet_test_posittab8 test.cpp ${TAB_POSIT_LIB8} ${tiny_dnn_headers})
target_link_libraries(inet_test_posittab8
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_posittab8 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit8)
add_dependencies(inet_tests_type inet_test_posittab8)
add_dependencies(inet_trains_type inet_train_posittab8)
endif(TAB8)
if(TAB10)
add_executable(inet_train_posittab10 train.cpp ${TAB_POSIT_LIB10} ${tiny_dnn_headers})
target_link_libraries(inet_train_posittab10
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_posittab10 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit10)
add_executable(inet_test_posittab10 test.cpp ${TAB_POSIT_LIB10} ${tiny_dnn_headers})
target_link_libraries(inet_test_posittab10
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_posittab10 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit10)
add_dependencies(inet_tests_type inet_test_posittab10)
add_dependencies(inet_trains_type inet_train_posittab10)
endif(TAB10)
if(TAB12)
add_executable(inet_train_posittab12 train.cpp ${TAB_POSIT_LIB12} ${tiny_dnn_headers})
target_link_libraries(inet_train_posittab12
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_posittab12 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit12)
add_executable(inet_test_posittab12 test.cpp ${TAB_POSIT_LIB12} ${tiny_dnn_headers})
target_link_libraries(inet_test_posittab12
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_posittab12 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit12)
add_dependencies(inet_tests_type inet_test_posittab12)
add_dependencies(inet_trains_type inet_train_posittab12)
endif(TAB12)
if(TAB14)
add_executable(inet_train_posittab14 train.cpp ${TAB_POSIT_LIB14} ${tiny_dnn_headers})
target_link_libraries(inet_train_posittab14
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_train_posittab14 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit14)
add_executable(inet_test_posittab14 test.cpp ${TAB_POSIT_LIB14} ${tiny_dnn_headers})
target_link_libraries(inet_test_posittab14
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(inet_test_posittab14 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit14)
add_dependencies(inet_tests_type inet_test_posittab14)
add_dependencies(inet_trains_type inet_train_posittab14)
endif(TAB14)
endif()
\ No newline at end of file
endif()
......@@ -3,48 +3,33 @@
//#define CNN_USE_POSIT
#include "tiny_dnn/tiny_dnn.h"
#include "tiny_dnn/models/alexnet.h"
#include "tiny_dnn/models/vgg16.h"
#include "tiny_dnn/models/resnet34.h"
using ns = std::chrono::nanoseconds;
using ms = std::chrono::milliseconds;
using get_time = std::chrono::steady_clock ;
using namespace tiny_dnn;
int main(int argc, char **argv) {
tiny_dnn::models::alexnet<227,227,1000> net;
for(int i = 0; i < 1; ++i) {
std::cout << i << "/50" << std::endl;
net.predict(image<tiny_dnn::float_t>(shape3d(227,227,3),image_type::rgb));
}
/*tiny_dnn::network<tiny_dnn::sequential> net;
std::string model_path(argv[1]);
std::string data_path(argv[2]);
net.load(model_path); //load pre-trained model
std::vector<tiny_dnn::label_t> vlab;
std::vector<tiny_dnn::vec_t> vimg;
std::vector<unsigned int> times;
parse_binImagenet<32,32,3,uint8_t>(data_path,&vimg,&vlab,-1,1);
int successes = 0;
auto start = get_time::now();
double test_time(tiny_dnn::network<tiny_dnn::sequential>& net,size_t width,size_t height) {
image<> tmp(shape3d(width,height,3),image_type::rgb);
auto start = get_time::now();
net.predict(tmp);
auto end = get_time::now();
for(unsigned int i = 0; i < 1000; ++i) {
auto inner_b = get_time::now();
auto res = net.predict(vimg[i]);
auto inner_e = get_time::now();
std::vector<std::pair<double, unsigned int>> scores;
for (int j = 0; j < 42; j++) scores.emplace_back(res[j], j);
sort(scores.begin(), scores.end(), std::greater<std::pair<double, int>>());
if(vlab[i] == scores[0].second) successes++;
std::cout << "Inferencing " << i+1 << "/" << vimg.size() << "(Acc: " << (double)successes/double(i+1) << ")" << std::endl;
std::clog << "\r" << i+1 << "/" << vimg.size();
unsigned int tttt = std::chrono::duration_cast<ms>(inner_e - inner_b).count();
times.push_back(tttt);
}
auto end = get_time::now();
auto diff = end - start;
make_test_report(vimg,successes,times,std::chrono::duration_cast<ms>(diff).count(),"../../plot/test_perf_");
std::cout << "Accuracy: " << successes << "/" << vimg.size() << "\n";
std::cout << "Time elapsed: " << std::chrono::duration_cast<ms>(diff).count() << "ms\n";
return 0;*/
}
\ No newline at end of file
return double(std::chrono::duration_cast<ms>(end - start).count());
}
int main(int argc, char **argv) {
alexnet<224,224,1000> net;
vgg16<1000> net2;
resnet34<224,224,1000> net3;
std::cout << "Testing Alexnet...\n";
//std::cout << "Elapsed (ms): " << test_time(net,224,224);
std::cout << "\nTesting VGG16...\n";
//std::cout << "Elapsed (ms): " << test_time(net2,224,224);
std::cout << "\nTesting Resnet...\n";
std::cout << "Elapsed (ms): " << test_time(net3,224,224);
}
......@@ -73,9 +73,9 @@ int main(int argc, char **argv) {
std::vector<vec_t> timg,vimg;
std::string base_path(argv[1]);
for(int i = 1; i <= 1;++i)
parse_binImagenet<32,32,3,uint16_t>(base_path+"/train_data_batch_"+std::to_string(i)+".bin",&timg,&tlab,-1,1);
parse_binImagenet<32,32,3,uint16_t>(base_path+"/train_data_batch_"+std::to_string(i)+".bin",&timg,&tlab,-4,4);
std::cout << "Num train images: " << timg.size() << std::endl;
parse_binImagenet<32,32,3,uint16_t>(argv[2],&vimg,&vlab,-1,1);
parse_binImagenet<32,32,3,uint16_t>(argv[2],&vimg,&vlab,-4,4);
std::cout << "Num val images: " << vimg.size() << std::endl;
tiny_dnn::adagrad optimizer;
optimizer.alpha*=4;
......
......@@ -27,6 +27,7 @@ void test(const std::string &model_file,
const std::string &out_file,
const tiny_dnn::shape3d& input_shape) {
auto net = tiny_dnn::create_net_from_caffe_prototxt(model_file,input_shape);
std::cout << "Done..." << std::endl;
tiny_dnn::reload_weight_from_caffe_protobinary(trained_file, net.get());
std::cout << "============================================================" << std::endl;
auto nn = *net;
......
......@@ -14,100 +14,53 @@ if(USE_SERIALIZER)
${project_library_target_name} ${REQUIRED_LIBRARIES})
add_dependencies(cifar_tests_type cifar_test)
add_executable(cifar_train_p16_2 train_hard.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_train_p16_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_p16_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_trains_type cifar_train_p16_2)
add_executable(cifar_test_p16_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p16_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p16_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p16_2)
add_executable(cifar_test_p16_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p16_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p16_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
target_compile_definitions(cifar_test_p16_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p16_0)
add_executable(cifar_train_p14_2 train_hard.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_train_p14_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_p14_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_trains_type cifar_train_p14_2)
add_executable(cifar_test_p14_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p14_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p14_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p14_2)
add_executable(cifar_test_p14_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p14_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p14_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
target_compile_definitions(cifar_test_p14_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=14 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p14_0)
add_executable(cifar_train_p12_2 train_hard.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_train_p12_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_p12_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_trains_type cifar_train_p12_2)
add_executable(cifar_test_p12_2 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p12_2
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p12_2 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=2 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p12_2)
add_executable(cifar_test_p12_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p12_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p12_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
target_compile_definitions(cifar_test_p12_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=12 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p12_0)
add_executable(cifar_test_p10_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p10_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p10_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=10 CNN_EXP_BITS=0 CNN_POS_STORAGE=int16_t CNN_POS_BACKEND=uint32_t)
target_compile_definitions(cifar_test_p10_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=10 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p10_0)
add_executable(cifar_test_p8_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p8_0
add_executable(cifar_test_p9_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p9_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p8_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=8 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p8_0)
add_executable(cifar_test_p7_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p7_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p7_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=7 CNN_EXP_BITS=1 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p7_0)
add_executable(cifar_test_p6_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p6_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p6_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=6 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p6_0)
target_compile_definitions(cifar_test_p9_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=9 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p9_0)
add_executable(cifar_test_p5_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p5_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p5_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=5 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p5_0)
add_executable(cifar_test_p4_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p4_0
add_executable(cifar_test_p8_0 test.cpp ${tiny_dnn_headers})
target_link_libraries(cifar_test_p8_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_test_p4_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=4 CNN_EXP_BITS=0 CNN_POS_STORAGE=int8_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p4_0)
target_compile_definitions(cifar_test_p8_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=8 CNN_EXP_BITS=0 CNN_POS_STORAGE=int32_t CNN_POS_BACKEND=uint32_t)
add_dependencies(cifar_tests_type cifar_test_p8_0)
if(TAB8)
add_executable(cifar_train_posittab8 train_hard.cpp ${TAB_POSIT_LIB8} ${tiny_dnn_headers})
add_executable(cifar_train_posittab8 train.cpp ${TAB_POSIT_LIB8} ${tiny_dnn_headers})
target_link_libraries(cifar_train_posittab8
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_posittab8 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit8)
......@@ -120,7 +73,7 @@ add_executable(cifar_test_p12_0 test.cpp ${tiny_dnn_headers})
endif(TAB8)
if(TAB10)
add_executable(cifar_train_posittab10 train_hard.cpp ${TAB_POSIT_LIB10} ${tiny_dnn_headers})
add_executable(cifar_train_posittab10 train.cpp ${TAB_POSIT_LIB10} ${tiny_dnn_headers})
target_link_libraries(cifar_train_posittab10
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_posittab10 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit10)
......@@ -133,7 +86,7 @@ add_executable(cifar_test_p12_0 test.cpp ${tiny_dnn_headers})
endif(TAB10)
if(TAB12)
add_executable(cifar_train_posittab12 train_hard.cpp ${TAB_POSIT_LIB12} ${tiny_dnn_headers})
add_executable(cifar_train_posittab12 train.cpp ${TAB_POSIT_LIB12} ${tiny_dnn_headers})
target_link_libraries(cifar_train_posittab12
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_posittab12 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit12)
......@@ -147,7 +100,7 @@ add_executable(cifar_test_p12_0 test.cpp ${tiny_dnn_headers})
if(TAB14)
add_executable(cifar_train_posittab14 train_hard.cpp ${TAB_POSIT_LIB14} ${tiny_dnn_headers})
add_executable(cifar_train_posittab14 train.cpp ${TAB_POSIT_LIB14} ${tiny_dnn_headers})
target_link_libraries(cifar_train_posittab14
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(cifar_train_posittab14 PRIVATE CNN_USE_POSIT CNN_TAB_TYPE=posit14)
......@@ -159,4 +112,4 @@ add_executable(cifar_test_p12_0 test.cpp ${tiny_dnn_headers})
add_dependencies(cifar_trains_type cifar_train_posittab14)
endif(TAB14)
endif()
\ No newline at end of file
endif()
......@@ -15,16 +15,19 @@ using get_time = std::chrono::steady_clock ;
void recognize(const std::string &net, const std::string &src_filename,int numtest) {
startLog(false);
//startLog(false);
tiny_dnn::network<tiny_dnn::sequential> nn;
nn.load(net);
endLog("cifar_network_weights.txt");
//endLog("cifar_network_weights.txt");
/*====================================================*/
std::vector<tiny_dnn::label_t> test_labels;
std::vector<tiny_dnn::vec_t> test_images;
std::vector<unsigned int> times;
parse_cifar10(src_filename + "/test_batch.bin", &test_images, &test_labels,-1.0, 1.0, 0, 0);
for (int i = 1; i <= 5; i++) {
parse_cifar10(src_filename + "/data_batch_" + std::to_string(i) + ".bin",
&test_images, &test_labels, -2.0, 2.0, 0, 0);
}
int successes = 0;
auto start = get_time::now();
for(unsigned int i = 0; i < numtest; ++i) {
......@@ -36,14 +39,11 @@ void recognize(const std::string &net, const std::string &src_filename,int numte
scores.emplace_back(res[i], i);
}
sort(scores.begin(), scores.end(), std::greater<std::pair<double, int>>());
if(test_labels[i] == scores[0].second ||
test_labels[i] == scores[1].second ||
test_labels[i] == scores[2].second ||
test_labels[i] == scores[3].second ||
test_labels[i] == scores[4].second) successes++;
if(test_labels[i] == scores[0].second) successes++;
std::cout << "Inferencing (" << scores[0].second << "-"<< test_labels[i] << ")" << i+1 << "/" << test_images.size() << "(Acc: " << (double)successes/double(i+1) << ")" << std::endl;
std::clog << "\r" << i+1 << "/" << test_images.size();
//std::clog << "\r" << i+1 << "/" << test_images.size();
unsigned int tttt = std::chrono::duration_cast<ms>(inner_e - inner_b).count();
std::cout << tttt << " ms\n";
times.push_back(tttt);
}
auto end = get_time::now();
......
......@@ -29,26 +29,27 @@ void construct_net(N &nn, tiny_dnn::core::backend_t backend_type) {
using fc = tiny_dnn::layers::fc;
using conv = tiny_dnn::layers::conv;
using ave_pool = tiny_dnn::layers::ave_pool;
using tanh = tiny_dnn::activation::elu;
using elu = tiny_dnn::activation::elu;
using tiny_dnn::core::connection_table;
using padding = tiny_dnn::padding;
using softmax = tiny_dnn::activation::softmax;
using flatten = tiny_dnn::layers::flatten;
nn << conv(32, 32, 5, 3, 12, // C1, 3@32x32-in, 6@28x28-out
padding::valid, true, 1, 1, 1, 1, backend_type)
<< tanh()
<< elu()
<< ave_pool(28, 28, 12, 2) // S2, 6@28x28-in, 6@14x14-out
<< tanh()
<< elu()
<< conv(14, 14, 5, 12, 16, // C3, 6@14x14-in, 16@10x10-out
connection_table(tbl, 6, 16),
padding::valid, true, 1, 1, 1, 1, backend_type)
<< tanh()
<< elu()
<< ave_pool(10, 10, 16, 2) // S4, 16@10x10-in, 16@5x5-out
<< tanh()
<< conv(5, 5, 5, 16, 120, // C5, 16@5x5-in, 120@1x1-out
<< elu()
<< conv(5, 5, 5, 16, 120, // C5, 16@5x5-in, 120@3x3-out
padding::valid, true, 1, 1, 1, 1, backend_type)
<< tanh()
<< elu()
<< fc(120, 10, true, backend_type) // F6, 120-in, 10-out
<< tanh();
<< softmax();
}
void train_cifar10(std::string data_dir_path,
......@@ -59,9 +60,9 @@ void train_cifar10(std::string data_dir_path,
std::ostream &log,int num_train) {
// specify loss-function and learning strategy
tiny_dnn::network<tiny_dnn::sequential> nn;
tiny_dnn::adagrad optimizer;
tiny_dnn::adam optimizer;
construct_net(nn, backend_type);
nn.load("../../pretrained_models/cifar10.model");
std::cout << "load models..." << std::endl;
......@@ -82,10 +83,9 @@ void train_cifar10(std::string data_dir_path,
/*SUBSAMPLING*/
std::vector<tiny_dnn::label_t> train_labels(train_labels_o.begin(),train_labels_o.begin()+num_train);
std::vector<tiny_dnn::vec_t> train_images(train_images_o.begin(),train_images_o.begin()+num_train);
std::vector<tiny_dnn::label_t> test_labels(test_labels_o.begin(),test_labels_o.begin()+1000);
std::vector<tiny_dnn::vec_t> test_images(test_images_o.begin(),test_images_o.begin()+1000);
std::vector<tiny_dnn::label_t> test_labels(train_labels_o.begin(),train_labels_o.begin()+num_train);
std::vector<tiny_dnn::vec_t> test_images(train_images_o.begin(),train_images_o.begin()+num_train);
/****************/
vutil::randomShuffle(train_labels,train_images);
tiny_dnn::progress_display disp(train_images.size());
tiny_dnn::timer t;
......
......@@ -15,30 +15,29 @@ void construct_net(N &nn, tiny_dnn::core::backend_t backend_type) {
using fc = tiny_dnn::layers::fc;
using conv = tiny_dnn::layers::conv;
using max_pool = tiny_dnn::layers::max_pool;
using relu = tiny_dnn::activation::relu;
using elu = tiny_dnn::activation::elu;
using tiny_dnn::core::connection_table;
using padding = tiny_dnn::padding;
using softmax = tiny_dnn::activation::softmax;
// VGG-16 Like network
// Block 1
nn << conv(32, 32, 3, 3, 3, 64, padding::same) << relu();
nn << conv(32, 32, 3, 3, 64, 64, padding::same) << relu();
nn << max_pool(32, 32, 64, 2) << relu();
nn << conv(32, 32, 3, 3, 3, 64, padding::same) << elu();
nn << conv(32, 32, 3, 3, 64, 64, padding::same) << elu();
nn << max_pool(32, 32, 64, 2) << elu();
// Block 2
nn << conv(16, 16, 3, 3, 64, 128, padding::same) << relu();
nn << conv(16, 16, 3, 3, 128, 128, padding::same) << relu();
nn << max_pool(16, 16, 128, 2) << relu();
nn << conv(16, 16, 3, 3, 64, 128, padding::same) << elu();
nn << conv(16, 16, 3, 3, 128, 128, padding::same) << elu();
nn << max_pool(16, 16, 128, 2) << elu();
// Block 3
nn << conv(8, 8, 3, 3, 128, 256, padding::same) << relu();
nn << conv(8, 8, 3, 3, 256, 256, padding::same) << relu();
nn << conv(8, 8, 3, 3, 256, 256, padding::same) << relu();
nn << max_pool(8, 8, 256, 2) << relu();
nn << fc(4*4*256,4096) << relu();
nn << fc(4096,4096) << relu();
nn << fc(4096,10) << relu() << softmax();
nn << conv(8, 8, 3, 3, 128, 256, padding::same) << elu();
nn << conv(8, 8, 3, 3, 256, 256, padding::same) << elu();
nn << conv(8, 8, 3, 3, 256, 256, padding::same) << elu();
nn << max_pool(8, 8, 256, 2) << elu();
nn << fc(4096,10) << elu() << softmax();
}
void train_cifar10(std::string data_dir_path,
......@@ -72,21 +71,23 @@ void train_cifar10(std::string data_dir_path,
/*SUBSAMPLING*/
std::vector<tiny_dnn::label_t> train_labels(train_labels_o.begin(),train_labels_o.begin()+num_train);
std::vector<tiny_dnn::vec_t> train_images(train_images_o.begin(),train_images_o.begin()+num_train);
std::vector<tiny_dnn::label_t> test_labels(test_labels_o.begin(),test_labels_o.begin()+1000);
std::vector<tiny_dnn::vec_t> test_images(test_images_o.begin(),test_images_o.begin()+1000);
std::vector<tiny_dnn::label_t> test_labels(train_labels_o.begin(),train_labels_o.begin()+num_train);
std::vector<tiny_dnn::vec_t> test_images(train_images_o.begin(),train_images_o.begin()+num_train);
/****************/
tiny_dnn::progress_display disp(train_images.size());
tiny_dnn::timer t;
optimizer.alpha *=4;
int epoch = 1;
optimizer.alpha *=
std::min(tiny_dnn::float_t(4),
static_cast<tiny_dnn::float_t>(sqrt(n_minibatch) * learning_rate)); int epoch = 1;
// create callback
auto on_enumerate_epoch = [&]() {
std::cout << "Epoch " << epoch << "/" << n_train_epochs << " finished. "
<< t.elapsed() << "s elapsed." << std::endl;
++epoch;
tiny_dnn::result res = nn.test(test_images, test_labels);
log << res.accuracy() << std::endl;
tiny_dnn::result res = nn.test(train_images, train_labels);
std::cout << res.accuracy() << "%" << std::endl;
disp.restart(train_images.size());
t.restart();
......@@ -128,9 +129,7 @@ static void usage(const char *argv0) {
}
int main(int argc, char **argv) {
// srand(time(NULL));
unsigned int seed = rand();
std::cout << "Seed:\n" << seed << std::endl;
unsigned int seed = 132145;
tiny_dnn::set_random_seed(seed);
double learning_rate = 0.01;
int epochs = 30;
......
......@@ -18,7 +18,7 @@ if(USE_SERIALIZER)
add_executable(mnist_train_p16_0 train.cpp ${tiny_dnn_headers})
target_link_libraries(mnist_train_p16_0
${project_library_target_name} ${REQUIRED_LIBRARIES})
target_compile_definitions(mnist_train_p16_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=0 CNN_POS_STORAGE=int_fast16_t CNN_POS_BACKEND=uint_fast16_t)
target_compile_definitions(mnist_train_p16_0 PRIVATE CNN_USE_POSIT CNN_POS_BITS=16 CNN_EXP_BITS=0 CNN_POS_STORAGE=int_fast32_t CNN_POS_BACKEND=uint_fast32_t)
add_dependencies(mnist_trains_type mnist_train_p16_0)
......@@ -26,7 +26,7 @@ if(USE_SERIALIZER)