Commit b0684a0e authored by Federico Rossi's avatar Federico Rossi

Pretrained models updated

parent 9955202b
......@@ -80,9 +80,10 @@ add_library(${project_library_target_name} INTERFACE)
##### INCLUDE POSIT FILES ######
set(CPPPOSIT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../../../../cppposit_private/include)
set(CPPPOSIT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../../../../cppposit_private/include/tabulated)
set(CPPOSIT_SRC_DIR ${PROJECT_SOURCE_DIR}/../../../../cppposit_private/src)
include_directories(${PROJECT_SOURCE_DIR}/../../../../cppposit_private/include)
include_directories(${PROJECT_SOURCE_DIR}/../../../../cppposit_private/include/tabulated)
################################
......
......@@ -3,9 +3,7 @@
//#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 ;
......@@ -17,19 +15,13 @@ double test_time(tiny_dnn::network<tiny_dnn::sequential>& net,size_t width,size_
auto start = get_time::now();
net.predict(tmp);
auto end = get_time::now();
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);
tiny_dnn::models::alexnet<224,224,1000> net;
tiny_dnn::models::vgg19<1000> net2;
tiny_dnn::models::resnet152<224,224,1000> net3;
std::cout << "Elapsed (ms): " << test_time(net,224,224);
}
......@@ -25,9 +25,11 @@ void recognize(const std::string &net, const std::string &src_filename,int numte
std::vector<tiny_dnn::vec_t> test_images;
std::vector<unsigned int> times;
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);
//parse_cifar10(src_filename + "/data_batch_" + std::to_string(i) + ".bin",
// &test_images, &test_labels, -2.0, 2.0, 0, 0);
}
parse_cifar100(src_filename + "/test.bin", &test_images, &test_labels,
-1.0, 1.0, 0, 0);
int successes = 0;
auto start = get_time::now();
for(unsigned int i = 0; i < numtest; ++i) {
......@@ -35,11 +37,15 @@ void recognize(const std::string &net, const std::string &src_filename,int numte
auto res = nn.predict(test_images[i]);
auto inner_e = get_time::now();
std::vector<std::pair<double, int>> scores;
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
scores.emplace_back(res[i], i);
}
sort(scores.begin(), scores.end(), std::greater<std::pair<double, int>>());
if(test_labels[i] == scores[0].second) successes++;
if(test_labels[i] == scores[0].second || //top-5
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++;
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();
unsigned int tttt = std::chrono::duration_cast<ms>(inner_e - inner_b).count();
......@@ -60,3 +66,18 @@ int main(int argc, char **argv) {
}
recognize(std::string(argv[1]), argv[2],atoi(argv[3]));
}
/*
CIFAR100 top-5:
% ms
Softfloat32 90.2% ~CIFAR-10
Posit16 90.2% ~CIFAR-10
Posit14 90.2% ~CIFAR-10
Posit12 90.2% ~CIFAR-10
Posit10 90.2% ~CIFAR-10
Posit8 80% ~CIFAR-10
*/
\ No newline at end of file
......@@ -37,7 +37,7 @@ void construct_net(N &nn, tiny_dnn::core::backend_t backend_type) {
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();
nn << fc(4096,100) << elu() << softmax();
}
void train_cifar10(std::string data_dir_path,
......@@ -59,14 +59,15 @@ void train_cifar10(std::string data_dir_path,
std::vector<tiny_dnn::vec_t> train_images_o, test_images_o;
for (int i = 1; i <= 5; i++) {
parse_cifar10(data_dir_path + "/data_batch_" + std::to_string(i) + ".bin",
&train_images_o, &train_labels_o, -1.0, 1.0, 0, 0);
//parse_cifar10(data_dir_path + "/data_batch_" + std::to_string(i) + ".bin",
// &train_images_o, &train_labels_o, -1.0, 1.0, 0, 0);
}
//parse_cifar100(data_dir_path+"/train.bin",&train_images_o, &train_labels_o, -1.0, 1.0, 0, 0);
parse_cifar10(data_dir_path + "/test_batch.bin", &test_images_o, &test_labels_o,
parse_cifar100(data_dir_path + "/test.bin", &train_images_o, &train_labels_o,
-1.0, 1.0, 0, 0);
std::cout << "start learning" << std::endl;
std::cout << "start learning: " << train_labels_o.size() << std::endl;
/*SUBSAMPLING*/
std::vector<tiny_dnn::label_t> train_labels(train_labels_o.begin(),train_labels_o.begin()+num_train);
......
......@@ -18,21 +18,23 @@ int main(int argc, char **argv) {
}
std::string model_path(argv[1]);
std::string data_path(argv[2]);
float lowb = atof(argv[3]);
float highb = atof(argv[4]);
std::cout << "Loading model " << model_path << std::endl;
net.load(model_path); //load pre-trained model
std::cout << "Done..." << std::endl;
std::vector<tiny_dnn::label_t> test_labels;
std::vector<tiny_dnn::vec_t> test_images;
std::vector<unsigned int> times;
tiny_dnn::parse_mnist_labels(data_path+"/train-labels.idx1-ubyte",
tiny_dnn::parse_mnist_labels(data_path + "/t10k-labels.idx1-ubyte",
&test_labels);
tiny_dnn::parse_mnist_images(data_path+"/train-images.idx3-ubyte",
&test_images, -1.0, 1.0, 2, 2);
tiny_dnn::parse_mnist_images(data_path + "/t10k-images.idx3-ubyte",
&test_images, lowb, highb, 2, 2);
int successes = 0;
auto start = get_time::now();
double stime = 0;
int num = (argc > 3)? atoi(argv[3]):test_images.size();
int num = (argc > 5)? atoi(argv[5]):test_images.size();
for(unsigned int i = 0; i < num; ++i) {
auto inner_b = get_time::now();
auto res = net.predict(test_images[i]);
......
......@@ -38,6 +38,7 @@ class elu_layer : public activation_layer {
std::string layer_type() const override { return "elu-activation"; }
void forward_activation(const vec_t &x, vec_t &y) override {
std::cout << "Elu\n";
for (size_t j = 0; j < x.size(); j++) {
y[j] = tiny_dnn::elu(x[j],alpha_);
//x[j] < float_t(0) ? (alpha_ * (tiny_dnn::exp(x[j]) - float_t(1))) : x[j];
......
......@@ -22,6 +22,7 @@ class pseudotanh_layer : public activation_layer {
std::string layer_type() const override { return "pseudotanh-activation"; }
// y[j] = 2*sigmoid(2*x) - 1;
void forward_activation(const vec_t &x, vec_t &y) override {
std::cout << "Pseudotanh\n";
for (size_t j = 0; j < x.size(); j++) {
y[j] = tiny_dnn::pseudotanh(x[j]);
}
......
......@@ -22,8 +22,9 @@ class tanh_layer : public activation_layer {
std::string layer_type() const override { return "tanh-activation"; }
void forward_activation(const vec_t &x, vec_t &y) override {
std::cout << "Tanh\n";
for (size_t j = 0; j < x.size(); j++) {
y[j] = tiny_dnn::tanh(x[j]);
y[j] = std::max(float_t(0), x[j]);
}
}
......
......@@ -9,7 +9,7 @@
#include <cstddef>
#include <cstdint>
//#include "floats/softfloat.hpp"
#include "floats/softfloat.hpp"
#ifdef CNN_USE_POSIT
//#define POSIT_CEREAL
#undef CNN_USE_AVX
......@@ -47,9 +47,9 @@ typedef X float_t;
#elif defined CNN_USE_DOUBLE
typedef softfloat32 float_t;
#else
typedef float float_t;
typedef softfloat32 float_t;
#endif
typedef float float_accum_t;
typedef softfloat32 float_accum_t;
} // namespace tiny_dnn
/*
Copyright (c) 2013, Taiga Nomi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file.
*/
#pragma once
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#include "tiny_dnn/util/util.h"
#define CIFAR100_IMAGE_DEPTH (3)
#define CIFAR100_IMAGE_WIDTH (32)
#define CIFAR100_IMAGE_HEIGHT (32)
#define CIFAR100_IMAGE_AREA (CIFAR100_IMAGE_WIDTH * CIFAR100_IMAGE_HEIGHT)
#define CIFAR100_IMAGE_SIZE (CIFAR100_IMAGE_AREA * CIFAR100_IMAGE_DEPTH)
namespace tiny_dnn {
/**
* parse CIFAR-100 database format images
*
* @param filename [in] filename of database(binary version)
* @param train_images [out] parsed images
* @param train_labels [out] parsed labels
* @param scale_min [in] min-value of output
* @param scale_max [in] max-value of output
* @param x_padding [in] adding border width (left,right)
* @param y_padding [in] adding border width (top,bottom)
**/
inline void parse_cifar100(const std::string &filename,
std::vector<vec_t> *train_images,
std::vector<label_t> *train_labels,
float_t scale_min,
float_t scale_max,
int x_padding,
int y_padding) {
if (x_padding < 0 || y_padding < 0)
throw nn_error("padding size must not be negative");
if (scale_min >= scale_max)
throw nn_error("scale_max must be greater than scale_min");
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
if (ifs.fail() || ifs.bad())
throw nn_error("failed to open file:" + filename);
uint8_t label,garbage;
std::vector<unsigned char> buf(CIFAR100_IMAGE_SIZE);
while (ifs.read(reinterpret_cast<char *>(&label), 1)) {
// first read of label is the coarse one, not interested
// read again
ifs.read(reinterpret_cast<char *>(&label), 1);
vec_t img;
if (!ifs.read(reinterpret_cast<char *>(&buf[0]), CIFAR100_IMAGE_SIZE)) break;
if (x_padding || y_padding) {
int w = CIFAR100_IMAGE_WIDTH + 2 * x_padding;
int h = CIFAR100_IMAGE_HEIGHT + 2 * y_padding;
img.resize(w * h * CIFAR100_IMAGE_DEPTH, scale_min);
for (int c = 0; c < CIFAR100_IMAGE_DEPTH; c++) {
for (int y = 0; y < CIFAR100_IMAGE_HEIGHT; y++) {
for (int x = 0; x < CIFAR100_IMAGE_WIDTH; x++) {
img[c * w * h + (y + y_padding) * w + x + x_padding] =
scale_min +
(scale_max - scale_min) *
float_t(double(buf[c * CIFAR100_IMAGE_AREA + y * CIFAR100_IMAGE_WIDTH + x] / 255));
}
}
}
} else {
std::transform(buf.begin(), buf.end(), std::back_inserter(img),
[=](unsigned char c) {
return scale_min + (scale_max - scale_min) * float_t(double(c) / 255);
});
}
train_images->push_back(img);
train_labels->push_back(label);
}
}
} // namespace tiny_dnn
......@@ -56,19 +56,6 @@ namespace tiny_dnn {
return residualBlocks;
}
void experimentalBlock(tiny_dnn::network<tiny_dnn::sequential>* nn,size_t w,size_t h,size_t c) {
using add = tiny_dnn::elementwise_add_layer;
using conv = tiny_dnn::convolutional_layer;
using relu = tiny_dnn::relu_layer;
layer* last = (*nn)[nn->layer_size()-1];
*nn << conv(w,h,3,c,c,padding::same,true, 1, 1, 1, 1);
*nn << relu();
*nn << conv(w,h,3,c,c,padding::same,true, 1, 1, 1, 1);
layer* joint = new add(2,w*h*c);
*nn << *joint;
*last << *joint;
}
}
}
/*
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file.
*/
#pragma once
#include "tiny_dnn/tiny_dnn.h"
......
/*
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file.
*/
#pragma once
#include "tiny_dnn/tiny_dnn.h"
......
/*
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
in the LICENSE file.
*/
#pragma once
#include "tiny_dnn/tiny_dnn.h"
......
......@@ -29,8 +29,8 @@ class alexnet : public tiny_dnn::network<tiny_dnn::sequential> {
return s;
}
public:
explicit alexnet(const std::string &name = "")
: tiny_dnn::network<tiny_dnn::sequential>(name) {
explicit alexnet(bool include_classifier=false)
: tiny_dnn::network<tiny_dnn::sequential>() {
// todo: (karandesai) shift this to tiny_dnn::activation
using relu = tiny_dnn::activation::relu;
using conv = tiny_dnn::layers::conv;
......@@ -57,6 +57,8 @@ class alexnet : public tiny_dnn::network<tiny_dnn::sequential> {
*this << max_pool(s, s, 256, 1);
*this << conv(s,s,s,256,2048,padding::valid, true, 1, 1);
*this << relu();
if(!include_classifier) return;
*this << fc(2048,2048,true);
*this << relu();
*this << fc(2048,n_classes,true);
......
#pragma once
namespace models {
#include "tiny_dnn/models/alexnet.h"
#include "tiny_dnn/models/vgg16.h"
#include "tiny_dnn/models/vgg19.h"
#include "tiny_dnn/models/resnet34.h"
#include "tiny_dnn/models/resnet152.h"
}
/*
Copyright (c) 2013, Taiga Nomi and the respective contributors
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
......@@ -16,8 +16,8 @@
template <size_t width,size_t height,size_t n_classes>
class resnet152 : public tiny_dnn::network<tiny_dnn::sequential> {
public:
explicit resnet152(const std::string &name = "")
: tiny_dnn::network<tiny_dnn::sequential>(name) {
explicit resnet152(bool include_classifier=false)
: tiny_dnn::network<tiny_dnn::sequential>() {
// todo: (karandesai) shift this to tiny_dnn::activation
using relu = tiny_dnn::activation::relu;
using conv = tiny_dnn::layers::conv;
......@@ -47,6 +47,7 @@ class resnet152 : public tiny_dnn::network<tiny_dnn::sequential> {
*this << tiny_dnn::residual::make_block3(img_width,img_height,512,false);
*this << tiny_dnn::residual::make_block3(img_width,img_height,512,false);
if(!include_classifier) return;
*this << ave_pool(img_width,img_height,4*512) << fc(4*512,1000) << sotfmax();
......
/*
Copyright (c) 2013, Taiga Nomi and the respective contributors
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
......@@ -16,8 +16,8 @@
template <size_t width,size_t height,size_t n_classes>
class resnet34 : public tiny_dnn::network<tiny_dnn::sequential> {
public:
explicit resnet34(const std::string &name = "")
: tiny_dnn::network<tiny_dnn::sequential>(name) {
explicit resnet34(bool include_classifier=false)
: tiny_dnn::network<tiny_dnn::sequential>() {
// todo: (karandesai) shift this to tiny_dnn::activation
using relu = tiny_dnn::activation::relu;
using conv = tiny_dnn::layers::conv;
......@@ -49,6 +49,7 @@ class resnet34 : public tiny_dnn::network<tiny_dnn::sequential> {
*this << tiny_dnn::residual::make_block2(img_width,img_height,512,false);
*this << tiny_dnn::residual::make_block2(img_width,img_height,512,false);
if(!include_classifier) return;
*this << ave_pool(img_width,img_height,512) << fc(512,1000) << sotfmax();
......
/*
Copyright (c) 2013, Taiga Nomi and the respective contributors
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
......@@ -11,13 +11,11 @@
// Based on:
// https://github.com/DeepMark/deepmark/blob/master/torch/image%2Bvideo/alexnet.lua
template <size_t n_classes>
class vgg16 : public tiny_dnn::network<tiny_dnn::sequential> {
public:
explicit vgg16(const std::string &name = "")
: tiny_dnn::network<tiny_dnn::sequential>(name) {
explicit vgg16(bool include_classifier=true)
: tiny_dnn::network<tiny_dnn::sequential>() {
// todo: (karandesai) shift this to tiny_dnn::activation
using relu = tiny_dnn::activation::relu;
using conv = tiny_dnn::layers::conv;
......@@ -68,6 +66,7 @@ class vgg16 : public tiny_dnn::network<tiny_dnn::sequential> {
*this << max_pool(14, 14, 512, 2);
// FULLY CONNECTED
if(!include_classifier) return;
*this << fc(7*7*512,4096) << relu();
*this << fc(4096,4096) << relu();
*this << fc(4096,n_classes) << sotfmax();
......
/*
Copyright (c) 2013, Taiga Nomi and the respective contributors
Copyright (c) 2019, Federico Rossi and the respective contributors
All rights reserved.
Use of this source code is governed by a BSD-style license that can be found
......@@ -16,8 +16,8 @@
template <size_t n_classes>
class vgg19 : public tiny_dnn::network<tiny_dnn::sequential> {
public:
explicit vgg19(const std::string &name = "")
: tiny_dnn::network<tiny_dnn::sequential>(name) {
explicit vgg19(bool include_classifier=false)
: tiny_dnn::network<tiny_dnn::sequential>() {
// todo: (karandesai) shift this to tiny_dnn::activation
using relu = tiny_dnn::activation::relu;
using conv = tiny_dnn::layers::conv;
......@@ -74,6 +74,7 @@ class vgg19 : public tiny_dnn::network<tiny_dnn::sequential> {
*this << max_pool(14, 14, 512, 2);
// FULLY CONNECTED
if(!include_classifier) return;
*this << fc(7*7*512,4096) << relu();
*this << fc(4096,4096) << relu();
*this << fc(4096,n_classes) << sotfmax();
......
......@@ -322,7 +322,7 @@ class sequential : public nodes {
auto in = nodes_[i + 1]->inputs();
if (out[0] != in[0]) {
throw nn_error("");
throw nn_error("Connectivity check failed");
}
}
}
......
......@@ -73,6 +73,7 @@
#include "tiny_dnn/util/report.h"
#include "tiny_dnn/util/vector_utils.h"
#include "tiny_dnn/io/cifar10_parser.h"
#include "tiny_dnn/io/cifar100_parser.h"
#include "tiny_dnn/io/imageNet_parser.h"
#include "tiny_dnn/io/display.h"
#include "tiny_dnn/io/layer_factory.h"
......@@ -88,7 +89,6 @@
#include "tiny_dnn/layers/residual.h"
#ifdef DNN_USE_IMAGE_API
#include "tiny_dnn/util/image.h"
#endif // DNN_USE_IMAGE_API
......@@ -192,7 +192,6 @@ namespace residual {
using sc = tiny_dnn::shortcut_layer;
}
#include "tiny_dnn/models/alexnet.h"
using batch_norm = tiny_dnn::batch_normalization_layer;
......@@ -202,6 +201,8 @@ using slice = tiny_dnn::slice_layer;
using power = tiny_dnn::power_layer;
#include "tiny_dnn/models/models.h"
} // namespace tiny_dnn
#ifdef CNN_USE_CAFFE_CONVERTER
......
......@@ -108,6 +108,9 @@ namespace tiny_dnn {
// 2*sigm(2x)-1
// derivative 2*d(sigm(2x))*2
float_t pseudotanh(float_t x) {
#ifdef CNN_TAB_TYPE
return x.tanh();
#else
#ifdef CNN_USE_POSIT
#if(CNN_EXP_BITS == 0)
return x.fastTanh();
......@@ -117,6 +120,7 @@ namespace tiny_dnn {
#else
return float_t(2)*sigmoid(float_t(2)*x)-float_t(1);
#endif
#endif
}
float_t elu(float_t x,float_t a=float_t(1.0)) {
......
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