2013-11-04 23:11:01 +04:00
|
|
|
#include "connected_layer.h"
|
|
|
|
#include "convolutional_layer.h"
|
|
|
|
#include "maxpool_layer.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "image.h"
|
2013-11-07 04:09:41 +04:00
|
|
|
#include "parser.h"
|
2013-11-13 22:50:38 +04:00
|
|
|
#include "data.h"
|
|
|
|
#include "matrix.h"
|
2013-12-03 04:41:40 +04:00
|
|
|
#include "utils.h"
|
2014-01-25 02:49:02 +04:00
|
|
|
#include "mini_blas.h"
|
2014-12-12 00:15:26 +03:00
|
|
|
#include "matrix.h"
|
2014-12-07 11:41:26 +03:00
|
|
|
#include "server.h"
|
2013-11-04 23:11:01 +04:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <fenv.h>
|
|
|
|
|
2013-11-04 23:11:01 +04:00
|
|
|
void test_load()
|
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
image dog = load_image("dog.jpg", 300, 400);
|
|
|
|
show_image(dog, "Test Load");
|
|
|
|
show_image_layers(dog, "Test Load");
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:50:38 +04:00
|
|
|
void test_parser()
|
2013-11-06 22:37:37 +04:00
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
network net = parse_network_cfg("cfg/trained_imagenet.cfg");
|
2014-11-19 00:51:04 +03:00
|
|
|
save_network(net, "cfg/trained_imagenet_smaller.cfg");
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
2013-11-06 22:37:37 +04:00
|
|
|
|
2014-12-23 01:35:37 +03:00
|
|
|
#define AMNT 3
|
2014-12-12 00:15:26 +03:00
|
|
|
void draw_detection(image im, float *box, int side)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int r, c;
|
2014-12-23 01:35:37 +03:00
|
|
|
float amount[AMNT] = {0};
|
2014-12-12 00:15:26 +03:00
|
|
|
for(r = 0; r < side*side; ++r){
|
2014-12-23 01:35:37 +03:00
|
|
|
float val = box[r*5];
|
|
|
|
for(j = 0; j < AMNT; ++j){
|
|
|
|
if(val > amount[j]) {
|
|
|
|
float swap = val;
|
|
|
|
val = amount[j];
|
|
|
|
amount[j] = swap;
|
2014-12-12 00:15:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 01:35:37 +03:00
|
|
|
float smallest = amount[AMNT-1];
|
2014-12-12 00:15:26 +03:00
|
|
|
|
|
|
|
for(r = 0; r < side; ++r){
|
|
|
|
for(c = 0; c < side; ++c){
|
|
|
|
j = (r*side + c) * 5;
|
|
|
|
printf("Prob: %f\n", box[j]);
|
|
|
|
if(box[j] >= smallest){
|
|
|
|
int d = im.w/side;
|
|
|
|
int y = r*d+box[j+1]*d;
|
|
|
|
int x = c*d+box[j+2]*d;
|
|
|
|
int h = box[j+3]*256;
|
|
|
|
int w = box[j+4]*256;
|
2014-12-23 01:35:37 +03:00
|
|
|
//printf("%f %f %f %f\n", box[j+1], box[j+2], box[j+3], box[j+4]);
|
|
|
|
//printf("%d %d %d %d\n", x, y, w, h);
|
|
|
|
//printf("%d %d %d %d\n", x-w/2, y-h/2, x+w/2, y+h/2);
|
2014-12-12 00:15:26 +03:00
|
|
|
draw_box(im, x-w/2, y-h/2, x+w/2, y+h/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
show_image(im, "box");
|
|
|
|
cvWaitKey(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
void train_detection_net()
|
|
|
|
{
|
|
|
|
float avg_loss = 1;
|
|
|
|
//network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg");
|
|
|
|
network net = parse_network_cfg("cfg/detnet.cfg");
|
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
2014-12-17 02:34:10 +03:00
|
|
|
int imgs = 1024;
|
2014-12-12 00:15:26 +03:00
|
|
|
srand(time(0));
|
|
|
|
//srand(23410);
|
2014-11-22 02:35:19 +03:00
|
|
|
int i = 0;
|
2014-11-28 21:38:26 +03:00
|
|
|
list *plist = get_paths("/home/pjreddie/data/imagenet/horse.txt");
|
2014-11-22 02:35:19 +03:00
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
clock_t time;
|
|
|
|
while(1){
|
|
|
|
i += 1;
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
data train = load_data_detection_jitter_random(imgs, paths, plist->size, 256, 256, 7, 7, 256);
|
2014-12-23 01:35:37 +03:00
|
|
|
//data train = load_data_detection_random(imgs, paths, plist->size, 224, 224, 7, 7, 256);
|
|
|
|
|
|
|
|
/*
|
|
|
|
image im = float_to_image(224, 224, 3, train.X.vals[923]);
|
|
|
|
draw_detection(im, train.y.vals[923], 7);
|
2014-12-03 19:48:07 +03:00
|
|
|
*/
|
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
normalize_data_rows(train);
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network(net, train);
|
2014-11-22 02:35:19 +03:00
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
|
|
|
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), i*imgs*net.batch);
|
|
|
|
if(i%10==0){
|
|
|
|
char buff[256];
|
2014-12-03 19:48:07 +03:00
|
|
|
sprintf(buff, "/home/pjreddie/imagenet_backup/detnet_%d.cfg", i);
|
2014-11-22 02:35:19 +03:00
|
|
|
save_network(net, buff);
|
|
|
|
}
|
2014-12-12 00:15:26 +03:00
|
|
|
free_data(train);
|
2014-11-22 02:35:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 11:41:26 +03:00
|
|
|
void train_imagenet_distributed(char *address)
|
|
|
|
{
|
|
|
|
float avg_loss = 1;
|
2014-12-08 07:16:21 +03:00
|
|
|
srand(time(0));
|
2014-12-12 00:15:26 +03:00
|
|
|
network net = parse_network_cfg("cfg/net.cfg");
|
|
|
|
set_learning_network(&net, 0, 1, 0);
|
2014-12-07 11:41:26 +03:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
2014-12-17 02:34:10 +03:00
|
|
|
int imgs = net.batch;
|
2014-12-07 11:41:26 +03:00
|
|
|
int i = 0;
|
|
|
|
char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list");
|
|
|
|
list *plist = get_paths("/data/imagenet/cls.train.list");
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
clock_t time;
|
2014-12-12 00:15:26 +03:00
|
|
|
data train, buffer;
|
2014-12-17 02:34:10 +03:00
|
|
|
pthread_t load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 224, 224, &buffer);
|
2014-12-07 11:41:26 +03:00
|
|
|
while(1){
|
|
|
|
i += 1;
|
2014-12-12 00:15:26 +03:00
|
|
|
|
2014-12-07 11:41:26 +03:00
|
|
|
time=clock();
|
2014-12-12 00:15:26 +03:00
|
|
|
client_update(net, address);
|
|
|
|
printf("Updated: %lf seconds\n", sec(clock()-time));
|
|
|
|
|
|
|
|
time=clock();
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
train = buffer;
|
2014-12-07 11:41:26 +03:00
|
|
|
normalize_data_rows(train);
|
2014-12-17 02:34:10 +03:00
|
|
|
load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 224, 224, &buffer);
|
2014-12-07 11:41:26 +03:00
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2014-12-12 00:15:26 +03:00
|
|
|
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network(net, train);
|
2014-12-07 11:41:26 +03:00
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
2014-12-17 02:34:10 +03:00
|
|
|
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), i*imgs);
|
2014-12-07 11:41:26 +03:00
|
|
|
free_data(train);
|
|
|
|
}
|
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
|
2014-12-16 22:40:05 +03:00
|
|
|
void train_imagenet(char *cfgfile)
|
2014-10-25 22:57:26 +04:00
|
|
|
{
|
2014-11-06 01:49:58 +03:00
|
|
|
float avg_loss = 1;
|
2014-11-22 02:35:19 +03:00
|
|
|
//network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg");
|
2014-12-08 07:16:21 +03:00
|
|
|
srand(time(0));
|
2014-12-16 22:40:05 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
2014-12-23 01:35:37 +03:00
|
|
|
set_learning_network(&net, net.learning_rate/10., .5, .0005);
|
2014-10-25 22:57:26 +04:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
2014-12-17 02:34:10 +03:00
|
|
|
int imgs = 1024;
|
2014-12-23 01:35:37 +03:00
|
|
|
int i = 44700;
|
2014-10-25 22:57:26 +04:00
|
|
|
char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list");
|
2014-10-28 05:45:06 +03:00
|
|
|
list *plist = get_paths("/data/imagenet/cls.train.list");
|
2014-10-25 22:57:26 +04:00
|
|
|
char **paths = (char **)list_to_array(plist);
|
2014-10-28 05:45:06 +03:00
|
|
|
printf("%d\n", plist->size);
|
2014-10-25 22:57:26 +04:00
|
|
|
clock_t time;
|
2014-12-12 00:15:26 +03:00
|
|
|
pthread_t load_thread;
|
|
|
|
data train;
|
|
|
|
data buffer;
|
2014-12-17 02:34:10 +03:00
|
|
|
load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer);
|
2014-11-22 02:35:19 +03:00
|
|
|
while(1){
|
|
|
|
i += 1;
|
2014-10-25 22:57:26 +04:00
|
|
|
time=clock();
|
2014-12-12 00:15:26 +03:00
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
train = buffer;
|
2014-11-06 01:49:58 +03:00
|
|
|
normalize_data_rows(train);
|
2014-12-17 02:34:10 +03:00
|
|
|
load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer);
|
2014-10-25 22:57:26 +04:00
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network(net, train);
|
2014-11-06 01:49:58 +03:00
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
2014-12-17 02:34:10 +03:00
|
|
|
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), i*imgs);
|
2014-11-22 02:35:19 +03:00
|
|
|
free_data(train);
|
2014-12-18 21:47:33 +03:00
|
|
|
if(i%100==0){
|
2014-11-22 02:35:19 +03:00
|
|
|
char buff[256];
|
2014-12-13 23:01:21 +03:00
|
|
|
sprintf(buff, "/home/pjreddie/imagenet_backup/net_%d.cfg", i);
|
2014-11-22 02:35:19 +03:00
|
|
|
save_network(net, buff);
|
|
|
|
}
|
|
|
|
}
|
2014-10-25 22:57:26 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 01:49:58 +03:00
|
|
|
void validate_imagenet(char *filename)
|
|
|
|
{
|
2014-12-13 23:01:21 +03:00
|
|
|
int i = 0;
|
2014-11-22 02:35:19 +03:00
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
srand(time(0));
|
2014-11-06 01:49:58 +03:00
|
|
|
|
|
|
|
char **labels = get_labels("/home/pjreddie/data/imagenet/cls.val.labels.list");
|
2014-11-22 02:35:19 +03:00
|
|
|
|
|
|
|
list *plist = get_paths("/home/pjreddie/data/imagenet/cls.val.list");
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
2014-11-06 01:49:58 +03:00
|
|
|
|
|
|
|
clock_t time;
|
|
|
|
float avg_acc = 0;
|
2014-12-12 00:15:26 +03:00
|
|
|
float avg_top5 = 0;
|
2014-11-06 01:49:58 +03:00
|
|
|
int splits = 50;
|
2014-12-13 23:01:21 +03:00
|
|
|
int num = (i+1)*m/splits - i*m/splits;
|
2014-11-22 02:35:19 +03:00
|
|
|
|
2014-12-13 23:01:21 +03:00
|
|
|
data val, buffer;
|
2014-12-16 22:40:05 +03:00
|
|
|
pthread_t load_thread = load_data_thread(paths, num, 0, labels, 1000, 256, 256, &buffer);
|
2014-12-13 23:01:21 +03:00
|
|
|
for(i = 1; i <= splits; ++i){
|
2014-11-06 01:49:58 +03:00
|
|
|
time=clock();
|
2014-12-03 19:48:07 +03:00
|
|
|
|
2014-12-13 23:01:21 +03:00
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
val = buffer;
|
2014-11-06 01:49:58 +03:00
|
|
|
normalize_data_rows(val);
|
2014-12-13 23:01:21 +03:00
|
|
|
|
|
|
|
num = (i+1)*m/splits - i*m/splits;
|
|
|
|
char **part = paths+(i*m/splits);
|
2014-12-16 22:40:05 +03:00
|
|
|
if(i != splits) load_thread = load_data_thread(part, num, 0, labels, 1000, 256, 256, &buffer);
|
2014-11-06 01:49:58 +03:00
|
|
|
printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
|
2014-12-13 23:01:21 +03:00
|
|
|
|
2014-11-06 01:49:58 +03:00
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
float *acc = network_accuracies(net, val);
|
2014-12-12 00:15:26 +03:00
|
|
|
avg_acc += acc[0];
|
|
|
|
avg_top5 += acc[1];
|
2014-12-13 23:01:21 +03:00
|
|
|
printf("%d: top1: %f, top5: %f, %lf seconds, %d images\n", i, avg_acc/i, avg_top5/i, sec(clock()-time), val.X.rows);
|
2014-11-22 02:35:19 +03:00
|
|
|
free_data(val);
|
|
|
|
}
|
2014-10-28 05:45:06 +03:00
|
|
|
}
|
|
|
|
|
2014-12-16 22:40:05 +03:00
|
|
|
void test_detection(char *cfgfile)
|
2014-11-28 21:38:26 +03:00
|
|
|
{
|
2014-12-16 22:40:05 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
set_batch_network(&net, 1);
|
2014-11-28 21:38:26 +03:00
|
|
|
srand(2222222);
|
|
|
|
clock_t time;
|
|
|
|
char filename[256];
|
|
|
|
while(1){
|
|
|
|
fgets(filename, 256, stdin);
|
2014-12-03 19:48:07 +03:00
|
|
|
strtok(filename, "\n");
|
2014-12-12 00:15:26 +03:00
|
|
|
image im = load_image_color(filename, 224, 224);
|
2014-11-28 21:38:26 +03:00
|
|
|
z_normalize_image(im);
|
|
|
|
printf("%d %d %d\n", im.h, im.w, im.c);
|
|
|
|
float *X = im.data;
|
|
|
|
time=clock();
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
|
2014-12-12 00:15:26 +03:00
|
|
|
draw_detection(im, predictions, 7);
|
2014-11-28 21:38:26 +03:00
|
|
|
free_image(im);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
void test_init(char *cfgfile)
|
|
|
|
{
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
srand(2222222);
|
|
|
|
int i = 0;
|
|
|
|
char *filename = "data/test.jpg";
|
|
|
|
|
|
|
|
image im = load_image_color(filename, 224, 224);
|
|
|
|
z_normalize_image(im);
|
|
|
|
float *X = im.data;
|
|
|
|
forward_network(net, X, 0, 1);
|
|
|
|
for(i = 0; i < net.n; ++i){
|
|
|
|
if(net.types[i] == CONVOLUTIONAL){
|
|
|
|
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
|
|
|
|
image output = get_convolutional_image(layer);
|
|
|
|
int size = output.h*output.w*output.c;
|
|
|
|
float v = variance_array(layer.output, size);
|
|
|
|
float m = mean_array(layer.output, size);
|
|
|
|
printf("%d: Convolutional, mean: %f, variance %f\n", i, m, v);
|
|
|
|
}
|
|
|
|
else if(net.types[i] == CONNECTED){
|
|
|
|
connected_layer layer = *(connected_layer *)net.layers[i];
|
|
|
|
int size = layer.outputs;
|
|
|
|
float v = variance_array(layer.output, size);
|
|
|
|
float m = mean_array(layer.output, size);
|
|
|
|
printf("%d: Connected, mean: %f, variance %f\n", i, m, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_image(im);
|
|
|
|
}
|
|
|
|
|
2014-10-25 22:57:26 +04:00
|
|
|
void test_imagenet()
|
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
network net = parse_network_cfg("cfg/imagenet_test.cfg");
|
2014-10-25 22:57:26 +04:00
|
|
|
//imgs=1;
|
2014-10-28 05:45:06 +03:00
|
|
|
srand(2222222);
|
|
|
|
int i = 0;
|
2014-10-25 22:57:26 +04:00
|
|
|
char **names = get_labels("cfg/shortnames.txt");
|
|
|
|
clock_t time;
|
|
|
|
char filename[256];
|
|
|
|
int indexes[10];
|
2014-10-28 05:45:06 +03:00
|
|
|
while(1){
|
2014-11-19 00:51:04 +03:00
|
|
|
fgets(filename, 256, stdin);
|
2014-12-03 19:48:07 +03:00
|
|
|
strtok(filename, "\n");
|
2014-10-25 22:57:26 +04:00
|
|
|
image im = load_image_color(filename, 256, 256);
|
2014-11-06 01:49:58 +03:00
|
|
|
z_normalize_image(im);
|
2014-10-25 22:57:26 +04:00
|
|
|
printf("%d %d %d\n", im.h, im.w, im.c);
|
|
|
|
float *X = im.data;
|
|
|
|
time=clock();
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
top_predictions(net, 10, indexes);
|
2014-10-28 05:45:06 +03:00
|
|
|
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
|
2014-10-25 22:57:26 +04:00
|
|
|
for(i = 0; i < 10; ++i){
|
|
|
|
int index = indexes[i];
|
|
|
|
printf("%s: %f\n", names[index], predictions[index]);
|
|
|
|
}
|
2014-10-28 05:45:06 +03:00
|
|
|
free_image(im);
|
|
|
|
}
|
2014-10-25 22:57:26 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 01:49:58 +03:00
|
|
|
void test_visualize(char *filename)
|
2014-04-11 12:00:27 +04:00
|
|
|
{
|
2014-11-06 01:49:58 +03:00
|
|
|
network net = parse_network_cfg(filename);
|
2014-10-28 05:45:06 +03:00
|
|
|
visualize_network(net);
|
|
|
|
cvWaitKey(0);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-13 08:57:34 +04:00
|
|
|
void test_cifar10()
|
|
|
|
{
|
2014-08-11 23:52:07 +04:00
|
|
|
network net = parse_network_cfg("cfg/cifar10_part5.cfg");
|
|
|
|
data test = load_cifar10_data("data/cifar10/test_batch.bin");
|
2014-10-28 05:45:06 +03:00
|
|
|
clock_t start = clock(), end;
|
2014-08-11 23:52:07 +04:00
|
|
|
float test_acc = network_accuracy(net, test);
|
2014-10-28 05:45:06 +03:00
|
|
|
end = clock();
|
2014-08-11 23:52:07 +04:00
|
|
|
printf("%f in %f Sec\n", test_acc, (float)(end-start)/CLOCKS_PER_SEC);
|
|
|
|
visualize_network(net);
|
|
|
|
cvWaitKey(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void train_cifar10()
|
|
|
|
{
|
|
|
|
srand(555555);
|
2014-12-17 02:34:10 +03:00
|
|
|
network net = parse_network_cfg("cfg/cifar10.cfg");
|
2014-08-11 23:52:07 +04:00
|
|
|
data test = load_cifar10_data("data/cifar10/test_batch.bin");
|
2014-08-08 23:04:15 +04:00
|
|
|
int count = 0;
|
|
|
|
int iters = 10000/net.batch;
|
|
|
|
data train = load_all_cifar10();
|
|
|
|
while(++count <= 10000){
|
2014-12-17 02:34:10 +03:00
|
|
|
clock_t time = clock();
|
|
|
|
float loss = train_network_sgd(net, train, iters);
|
2014-08-08 23:04:15 +04:00
|
|
|
|
2014-08-11 23:52:07 +04:00
|
|
|
if(count%10 == 0){
|
2014-12-17 02:34:10 +03:00
|
|
|
float test_acc = network_accuracy(net, test);
|
|
|
|
printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds\n", count, loss, test_acc,sec(clock()-time));
|
2014-08-11 23:52:07 +04:00
|
|
|
char buff[256];
|
2014-12-17 02:34:10 +03:00
|
|
|
sprintf(buff, "unikitty/cifar10_%d.cfg", count);
|
2014-08-11 23:52:07 +04:00
|
|
|
save_network(net, buff);
|
|
|
|
}else{
|
2014-12-17 02:34:10 +03:00
|
|
|
printf("%d: Loss: %f, Time: %lf seconds\n", count, loss, sec(clock()-time));
|
2014-08-11 23:52:07 +04:00
|
|
|
}
|
2014-08-08 23:04:15 +04:00
|
|
|
|
|
|
|
}
|
2014-12-17 02:34:10 +03:00
|
|
|
free_data(train);
|
2014-02-25 00:21:31 +04:00
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
|
2014-12-18 22:28:42 +03:00
|
|
|
void compare_nist(char *p1,char *p2)
|
|
|
|
{
|
|
|
|
srand(222222);
|
|
|
|
network n1 = parse_network_cfg(p1);
|
|
|
|
network n2 = parse_network_cfg(p2);
|
|
|
|
data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10);
|
|
|
|
normalize_data_rows(test);
|
|
|
|
compare_networks(n1, n2, test);
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
void test_nist(char *path)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2014-08-08 23:04:15 +04:00
|
|
|
srand(222222);
|
2014-12-12 00:15:26 +03:00
|
|
|
network net = parse_network_cfg(path);
|
2014-08-11 23:52:07 +04:00
|
|
|
data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10);
|
2014-12-12 00:15:26 +03:00
|
|
|
normalize_data_rows(test);
|
2014-08-11 23:52:07 +04:00
|
|
|
clock_t start = clock(), end;
|
2014-12-17 02:34:10 +03:00
|
|
|
float test_acc = network_accuracy(net, test);
|
2014-08-11 23:52:07 +04:00
|
|
|
end = clock();
|
|
|
|
printf("Accuracy: %f, Time: %lf seconds\n", test_acc,(float)(end-start)/CLOCKS_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2014-12-18 22:27:13 +03:00
|
|
|
void train_nist(char *cfgfile)
|
2014-08-11 23:52:07 +04:00
|
|
|
{
|
|
|
|
srand(222222);
|
2014-12-19 02:46:45 +03:00
|
|
|
// srand(time(0));
|
2014-08-08 23:04:15 +04:00
|
|
|
data train = load_categorical_data_csv("data/mnist/mnist_train.csv", 0, 10);
|
|
|
|
data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10);
|
2014-12-19 02:46:45 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
2014-08-08 23:04:15 +04:00
|
|
|
int count = 0;
|
2014-12-23 01:35:37 +03:00
|
|
|
int iters = 6000/net.batch + 1;
|
|
|
|
while(++count <= 100){
|
2014-08-08 23:04:15 +04:00
|
|
|
clock_t start = clock(), end;
|
2014-12-19 02:46:45 +03:00
|
|
|
normalize_data_rows(train);
|
|
|
|
normalize_data_rows(test);
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network_sgd(net, train, iters);
|
2014-12-12 00:15:26 +03:00
|
|
|
float test_acc = 0;
|
2014-12-19 02:46:45 +03:00
|
|
|
if(count%1 == 0) test_acc = network_accuracy(net, test);
|
|
|
|
end = clock();
|
2014-11-19 00:51:04 +03:00
|
|
|
printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC);
|
2014-08-08 23:04:15 +04:00
|
|
|
}
|
2014-12-19 02:46:45 +03:00
|
|
|
free_data(train);
|
|
|
|
free_data(test);
|
2014-12-18 22:27:13 +03:00
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s.trained", cfgfile);
|
|
|
|
save_network(net, buff);
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
|
|
|
|
2014-12-08 22:48:57 +03:00
|
|
|
void train_nist_distributed(char *address)
|
|
|
|
{
|
|
|
|
srand(time(0));
|
|
|
|
network net = parse_network_cfg("cfg/nist.client");
|
|
|
|
data train = load_categorical_data_csv("data/mnist/mnist_train.csv", 0, 10);
|
|
|
|
//data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10);
|
|
|
|
normalize_data_rows(train);
|
|
|
|
//normalize_data_rows(test);
|
|
|
|
int count = 0;
|
|
|
|
int iters = 50000/net.batch;
|
|
|
|
iters = 1000/net.batch + 1;
|
|
|
|
while(++count <= 2000){
|
|
|
|
clock_t start = clock(), end;
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network_sgd(net, train, iters);
|
2014-12-08 22:48:57 +03:00
|
|
|
client_update(net, address);
|
|
|
|
end = clock();
|
|
|
|
//float test_acc = network_accuracy_gpu(net, test);
|
|
|
|
//float test_acc = 0;
|
|
|
|
printf("%d: Loss: %f, Time: %lf seconds\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 21:38:50 +04:00
|
|
|
void test_ensemble()
|
|
|
|
{
|
2014-08-08 23:04:15 +04:00
|
|
|
int i;
|
|
|
|
srand(888888);
|
|
|
|
data d = load_categorical_data_csv("mnist/mnist_train.csv", 0, 10);
|
|
|
|
normalize_data_rows(d);
|
|
|
|
data test = load_categorical_data_csv("mnist/mnist_test.csv", 0,10);
|
|
|
|
normalize_data_rows(test);
|
|
|
|
data train = d;
|
|
|
|
// data *split = split_data(d, 1, 10);
|
|
|
|
// data train = split[0];
|
|
|
|
// data test = split[1];
|
|
|
|
matrix prediction = make_matrix(test.y.rows, test.y.cols);
|
|
|
|
int n = 30;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
int count = 0;
|
|
|
|
float lr = .0005;
|
|
|
|
float momentum = .9;
|
|
|
|
float decay = .01;
|
|
|
|
network net = parse_network_cfg("nist.cfg");
|
|
|
|
while(++count <= 15){
|
|
|
|
float acc = train_network_sgd(net, train, train.X.rows);
|
|
|
|
printf("Training Accuracy: %lf Learning Rate: %f Momentum: %f Decay: %f\n", acc, lr, momentum, decay );
|
|
|
|
lr /= 2;
|
|
|
|
}
|
|
|
|
matrix partial = network_predict_data(net, test);
|
2014-12-12 00:15:26 +03:00
|
|
|
float acc = matrix_topk_accuracy(test.y, partial,1);
|
2014-08-08 23:04:15 +04:00
|
|
|
printf("Model Accuracy: %lf\n", acc);
|
|
|
|
matrix_add_matrix(partial, prediction);
|
2014-12-12 00:15:26 +03:00
|
|
|
acc = matrix_topk_accuracy(test.y, prediction,1);
|
2014-08-08 23:04:15 +04:00
|
|
|
printf("Current Ensemble Accuracy: %lf\n", acc);
|
|
|
|
free_matrix(partial);
|
|
|
|
}
|
2014-12-12 00:15:26 +03:00
|
|
|
float acc = matrix_topk_accuracy(test.y, prediction,1);
|
2014-08-08 23:04:15 +04:00
|
|
|
printf("Full Ensemble Accuracy: %lf\n", acc);
|
2013-12-07 21:38:50 +04:00
|
|
|
}
|
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
void visualize_cat()
|
2014-02-18 11:32:18 +04:00
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
network net = parse_network_cfg("cfg/voc_imagenet.cfg");
|
|
|
|
image im = load_image("data/cat.png", 0, 0);
|
|
|
|
printf("Processing %dx%d image\n", im.h, im.w);
|
2014-08-08 23:04:15 +04:00
|
|
|
resize_network(net, im.h, im.w, im.c);
|
2014-10-13 11:29:01 +04:00
|
|
|
forward_network(net, im.data, 0, 0);
|
2014-11-22 02:35:19 +03:00
|
|
|
|
|
|
|
visualize_network(net);
|
|
|
|
cvWaitKey(0);
|
2014-02-18 11:32:18 +04:00
|
|
|
}
|
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
void test_gpu_net()
|
2014-02-18 11:32:18 +04:00
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
srand(222222);
|
|
|
|
network net = parse_network_cfg("cfg/nist.cfg");
|
|
|
|
data train = load_categorical_data_csv("data/mnist/mnist_train.csv", 0, 10);
|
|
|
|
data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10);
|
|
|
|
translate_data_rows(train, -144);
|
|
|
|
translate_data_rows(test, -144);
|
|
|
|
int count = 0;
|
|
|
|
int iters = 1000/net.batch;
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
while(++count <= 5){
|
|
|
|
clock_t start = clock(), end;
|
|
|
|
float loss = train_network_sgd(net, train, iters);
|
|
|
|
end = clock();
|
|
|
|
float test_acc = network_accuracy(net, test);
|
|
|
|
printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay);
|
|
|
|
}
|
2014-12-17 02:34:10 +03:00
|
|
|
|
|
|
|
gpu_index = -1;
|
2014-11-22 02:35:19 +03:00
|
|
|
count = 0;
|
|
|
|
srand(222222);
|
|
|
|
net = parse_network_cfg("cfg/nist.cfg");
|
|
|
|
while(++count <= 5){
|
|
|
|
clock_t start = clock(), end;
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network_sgd(net, train, iters);
|
2014-11-22 02:35:19 +03:00
|
|
|
end = clock();
|
|
|
|
float test_acc = network_accuracy(net, test);
|
|
|
|
printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay);
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 23:04:15 +04:00
|
|
|
|
2014-12-04 10:20:29 +03:00
|
|
|
void test_correct_alexnet()
|
|
|
|
{
|
|
|
|
char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list");
|
|
|
|
list *plist = get_paths("/data/imagenet/cls.train.list");
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
clock_t time;
|
|
|
|
int count = 0;
|
2014-12-16 22:40:05 +03:00
|
|
|
network net;
|
2014-12-17 02:34:10 +03:00
|
|
|
int imgs = net.batch;
|
|
|
|
|
2014-12-16 22:40:05 +03:00
|
|
|
count = 0;
|
|
|
|
srand(222222);
|
|
|
|
net = parse_network_cfg("cfg/net.cfg");
|
2014-12-04 10:20:29 +03:00
|
|
|
while(++count <= 5){
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
data train = load_data(paths, imgs, plist->size, labels, 1000, 256, 256);
|
2014-12-04 10:20:29 +03:00
|
|
|
normalize_data_rows(train);
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network(net, train);
|
2014-12-04 10:20:29 +03:00
|
|
|
printf("%d: %f, %lf seconds, %d images\n", count, loss, sec(clock()-time), imgs*net.batch);
|
|
|
|
free_data(train);
|
|
|
|
}
|
2014-12-17 02:34:10 +03:00
|
|
|
|
|
|
|
gpu_index = -1;
|
2014-12-04 10:20:29 +03:00
|
|
|
count = 0;
|
|
|
|
srand(222222);
|
2014-12-13 23:01:21 +03:00
|
|
|
net = parse_network_cfg("cfg/net.cfg");
|
2014-12-16 22:40:05 +03:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
2014-12-04 10:20:29 +03:00
|
|
|
while(++count <= 5){
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
data train = load_data(paths, imgs, plist->size, labels, 1000, 256,256);
|
2014-12-04 10:20:29 +03:00
|
|
|
normalize_data_rows(train);
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2014-12-17 02:34:10 +03:00
|
|
|
float loss = train_network(net, train);
|
2014-12-04 10:20:29 +03:00
|
|
|
printf("%d: %f, %lf seconds, %d images\n", count, loss, sec(clock()-time), imgs*net.batch);
|
|
|
|
free_data(train);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 11:41:26 +03:00
|
|
|
void run_server()
|
2014-12-03 19:48:07 +03:00
|
|
|
{
|
2014-12-08 07:16:21 +03:00
|
|
|
srand(time(0));
|
2014-12-12 00:15:26 +03:00
|
|
|
network net = parse_network_cfg("cfg/net.cfg");
|
|
|
|
set_batch_network(&net, 1);
|
2014-12-04 10:20:29 +03:00
|
|
|
server_update(net);
|
2014-12-03 19:48:07 +03:00
|
|
|
}
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2014-12-03 19:48:07 +03:00
|
|
|
void test_client()
|
|
|
|
{
|
2014-12-07 11:41:26 +03:00
|
|
|
network net = parse_network_cfg("cfg/alexnet.client");
|
|
|
|
clock_t time=clock();
|
|
|
|
client_update(net, "localhost");
|
|
|
|
printf("1\n");
|
|
|
|
client_update(net, "localhost");
|
|
|
|
printf("2\n");
|
|
|
|
client_update(net, "localhost");
|
|
|
|
printf("3\n");
|
|
|
|
printf("Transfered: %lf seconds\n", sec(clock()-time));
|
2014-12-03 19:48:07 +03:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
|
2014-12-17 02:34:10 +03:00
|
|
|
void del_arg(int argc, char **argv, int index)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = index; i < argc-1; ++i) argv[i] = argv[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
int find_arg(int argc, char* argv[], char *arg)
|
2014-12-08 22:48:57 +03:00
|
|
|
{
|
|
|
|
int i;
|
2014-12-17 02:41:36 +03:00
|
|
|
for(i = 0; i < argc; ++i) if(0==strcmp(argv[i], arg)) {
|
2014-12-17 02:34:10 +03:00
|
|
|
del_arg(argc, argv, i);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-08 22:48:57 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:34:10 +03:00
|
|
|
int find_int_arg(int argc, char **argv, char *arg, int def)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < argc-1; ++i){
|
|
|
|
if(0==strcmp(argv[i], arg)){
|
|
|
|
def = atoi(argv[i+1]);
|
|
|
|
del_arg(argc, argv, i);
|
|
|
|
del_arg(argc, argv, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2014-11-22 02:35:19 +03:00
|
|
|
{
|
|
|
|
if(argc < 2){
|
|
|
|
fprintf(stderr, "usage: %s <function>\n", argv[0]);
|
|
|
|
return 0;
|
2014-08-08 23:04:15 +04:00
|
|
|
}
|
2014-12-17 02:34:10 +03:00
|
|
|
gpu_index = find_int_arg(argc, argv, "-i", 0);
|
|
|
|
if(find_arg(argc, argv, "-nogpu")) gpu_index = -1;
|
|
|
|
|
|
|
|
#ifndef GPU
|
|
|
|
gpu_index = -1;
|
|
|
|
#else
|
|
|
|
if(gpu_index >= 0){
|
|
|
|
cl_setup();
|
|
|
|
}
|
2014-12-12 00:15:26 +03:00
|
|
|
#endif
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2014-12-16 22:40:05 +03:00
|
|
|
if(0==strcmp(argv[1], "detection")) train_detection_net();
|
|
|
|
else if(0==strcmp(argv[1], "cifar")) train_cifar10();
|
2014-12-04 10:20:29 +03:00
|
|
|
else if(0==strcmp(argv[1], "test_correct")) test_correct_alexnet();
|
2014-11-22 02:35:19 +03:00
|
|
|
else if(0==strcmp(argv[1], "test")) test_imagenet();
|
2014-12-07 11:41:26 +03:00
|
|
|
else if(0==strcmp(argv[1], "server")) run_server();
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
#ifdef GPU
|
|
|
|
else if(0==strcmp(argv[1], "test_gpu")) test_gpu_blas();
|
|
|
|
#endif
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2014-12-08 10:59:45 +03:00
|
|
|
else if(argc < 3){
|
2014-12-16 22:40:05 +03:00
|
|
|
fprintf(stderr, "usage: %s <function> <filename>\n", argv[0]);
|
2014-12-08 10:59:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-18 22:27:13 +03:00
|
|
|
else if(0==strcmp(argv[1], "nist")) train_nist(argv[2]);
|
2014-12-16 22:40:05 +03:00
|
|
|
else if(0==strcmp(argv[1], "train")) train_imagenet(argv[2]);
|
2014-12-12 00:15:26 +03:00
|
|
|
else if(0==strcmp(argv[1], "client")) train_imagenet_distributed(argv[2]);
|
2014-12-16 22:40:05 +03:00
|
|
|
else if(0==strcmp(argv[1], "detect")) test_detection(argv[2]);
|
2014-12-12 00:15:26 +03:00
|
|
|
else if(0==strcmp(argv[1], "init")) test_init(argv[2]);
|
2014-12-08 10:59:45 +03:00
|
|
|
else if(0==strcmp(argv[1], "visualize")) test_visualize(argv[2]);
|
|
|
|
else if(0==strcmp(argv[1], "valid")) validate_imagenet(argv[2]);
|
2014-12-12 00:15:26 +03:00
|
|
|
else if(0==strcmp(argv[1], "testnist")) test_nist(argv[2]);
|
2014-12-18 22:28:42 +03:00
|
|
|
else if(argc < 4){
|
|
|
|
fprintf(stderr, "usage: %s <function> <filename> <filename>\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(0==strcmp(argv[1], "compare")) compare_nist(argv[2], argv[3]);
|
2014-11-22 02:35:19 +03:00
|
|
|
fprintf(stderr, "Success!\n");
|
|
|
|
return 0;
|
2014-02-15 04:09:07 +04:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
|