2013-11-04 23:11:01 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-03-06 21:49:03 +03:00
|
|
|
#include "parser.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "cuda.h"
|
2015-08-17 19:00:12 +03:00
|
|
|
#include "blas.h"
|
2015-03-06 21:49:03 +03:00
|
|
|
|
2015-08-14 18:45:32 +03:00
|
|
|
#ifdef OPENCV
|
|
|
|
#include "opencv2/highgui/highgui_c.h"
|
|
|
|
#endif
|
2014-01-29 04:28:42 +04:00
|
|
|
|
2015-03-06 21:49:03 +03:00
|
|
|
extern void run_imagenet(int argc, char **argv);
|
2015-08-14 21:45:11 +03:00
|
|
|
extern void run_yolo(int argc, char **argv);
|
2015-07-31 02:19:14 +03:00
|
|
|
extern void run_coco(int argc, char **argv);
|
2015-05-25 21:53:10 +03:00
|
|
|
extern void run_writing(int argc, char **argv);
|
2015-03-06 21:49:03 +03:00
|
|
|
extern void run_captcha(int argc, char **argv);
|
2015-07-08 10:36:43 +03:00
|
|
|
extern void run_nightmare(int argc, char **argv);
|
2015-08-14 02:02:22 +03:00
|
|
|
extern void run_dice(int argc, char **argv);
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2015-03-08 21:25:28 +03:00
|
|
|
void change_rate(char *filename, float scale, float add)
|
2015-02-07 05:53:53 +03:00
|
|
|
{
|
|
|
|
// Ready for some weird shit??
|
|
|
|
FILE *fp = fopen(filename, "r+b");
|
|
|
|
if(!fp) file_error(filename);
|
|
|
|
float rate = 0;
|
|
|
|
fread(&rate, sizeof(float), 1, fp);
|
2015-03-08 21:25:28 +03:00
|
|
|
printf("Scaling learning rate from %f to %f\n", rate, rate*scale+add);
|
|
|
|
rate = rate*scale + add;
|
2015-02-07 05:53:53 +03:00
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
fwrite(&rate, sizeof(float), 1, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-17 19:00:12 +03:00
|
|
|
void average(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *cfgfile = argv[2];
|
|
|
|
char *outfile = argv[3];
|
|
|
|
gpu_index = -1;
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
network sum = parse_network_cfg(cfgfile);
|
|
|
|
|
|
|
|
char *weightfile = argv[4];
|
|
|
|
load_weights(&sum, weightfile);
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
int n = argc - 5;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
weightfile = argv[i+5];
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
for(j = 0; j < net.n; ++j){
|
|
|
|
layer l = net.layers[j];
|
|
|
|
layer out = sum.layers[j];
|
|
|
|
if(l.type == CONVOLUTIONAL){
|
|
|
|
int num = l.n*l.c*l.size*l.size;
|
|
|
|
axpy_cpu(l.n, 1, l.biases, 1, out.biases, 1);
|
|
|
|
axpy_cpu(num, 1, l.filters, 1, out.filters, 1);
|
|
|
|
}
|
|
|
|
if(l.type == CONNECTED){
|
|
|
|
axpy_cpu(l.outputs, 1, l.biases, 1, out.biases, 1);
|
|
|
|
axpy_cpu(l.outputs*l.inputs, 1, l.weights, 1, out.weights, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = n+1;
|
|
|
|
for(j = 0; j < net.n; ++j){
|
|
|
|
layer l = sum.layers[j];
|
|
|
|
if(l.type == CONVOLUTIONAL){
|
|
|
|
int num = l.n*l.c*l.size*l.size;
|
|
|
|
scal_cpu(l.n, 1./n, l.biases, 1);
|
|
|
|
scal_cpu(num, 1./n, l.filters, 1);
|
|
|
|
}
|
|
|
|
if(l.type == CONNECTED){
|
|
|
|
scal_cpu(l.outputs, 1./n, l.biases, 1);
|
|
|
|
scal_cpu(l.outputs*l.inputs, 1./n, l.weights, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save_weights(sum, outfile);
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:25:28 +03:00
|
|
|
void partial(char *cfgfile, char *weightfile, char *outfile, int max)
|
|
|
|
{
|
2015-08-17 19:00:12 +03:00
|
|
|
gpu_index = -1;
|
2015-03-08 21:25:28 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights_upto(&net, weightfile, max);
|
|
|
|
}
|
2015-04-17 22:32:54 +03:00
|
|
|
net.seen = 0;
|
2015-07-21 02:16:26 +03:00
|
|
|
save_weights_upto(net, outfile, max);
|
2015-03-08 21:25:28 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 10:11:41 +03:00
|
|
|
#include "convolutional_layer.h"
|
2015-07-31 02:19:14 +03:00
|
|
|
void rescale_net(char *cfgfile, char *weightfile, char *outfile)
|
|
|
|
{
|
|
|
|
gpu_index = -1;
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < net.n; ++i){
|
|
|
|
layer l = net.layers[i];
|
|
|
|
if(l.type == CONVOLUTIONAL){
|
|
|
|
rescale_filters(l, 2, -.5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save_weights(net, outfile);
|
|
|
|
}
|
|
|
|
|
2015-06-10 10:11:41 +03:00
|
|
|
void rgbgr_net(char *cfgfile, char *weightfile, char *outfile)
|
|
|
|
{
|
2015-06-12 01:38:58 +03:00
|
|
|
gpu_index = -1;
|
2015-06-10 10:11:41 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < net.n; ++i){
|
|
|
|
layer l = net.layers[i];
|
|
|
|
if(l.type == CONVOLUTIONAL){
|
|
|
|
rgbgr_filters(l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save_weights(net, outfile);
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:25:28 +03:00
|
|
|
void visualize(char *cfgfile, char *weightfile)
|
|
|
|
{
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
visualize_network(net);
|
2015-08-17 19:00:12 +03:00
|
|
|
#ifdef OPENCV
|
2015-03-08 21:25:28 +03:00
|
|
|
cvWaitKey(0);
|
2015-08-17 19:00:12 +03:00
|
|
|
#endif
|
2015-03-08 21:25:28 +03:00
|
|
|
}
|
|
|
|
|
2014-12-17 02:34:10 +03:00
|
|
|
int main(int argc, char **argv)
|
2014-11-22 02:35:19 +03:00
|
|
|
{
|
2015-06-11 00:44:10 +03:00
|
|
|
//test_resize("data/bad.jpg");
|
2015-04-24 20:27:50 +03:00
|
|
|
//test_box();
|
2015-01-23 03:38:24 +03:00
|
|
|
//test_convolutional_layer();
|
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){
|
2015-01-23 03:38:24 +03:00
|
|
|
cudaSetDevice(gpu_index);
|
2014-12-17 02:34:10 +03:00
|
|
|
}
|
2014-12-12 00:15:26 +03:00
|
|
|
#endif
|
2014-12-17 02:34:10 +03:00
|
|
|
|
2015-03-06 21:49:03 +03:00
|
|
|
if(0==strcmp(argv[1], "imagenet")){
|
2015-03-08 21:25:28 +03:00
|
|
|
run_imagenet(argc, argv);
|
2015-08-17 19:00:12 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "average")){
|
|
|
|
average(argc, argv);
|
2015-08-14 21:45:11 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "yolo")){
|
|
|
|
run_yolo(argc, argv);
|
2015-07-31 02:19:14 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "coco")){
|
|
|
|
run_coco(argc, argv);
|
2015-08-14 02:02:22 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "dice")){
|
|
|
|
run_dice(argc, argv);
|
2015-05-25 21:53:10 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "writing")){
|
|
|
|
run_writing(argc, argv);
|
2015-04-14 00:09:55 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "test")){
|
|
|
|
test_resize(argv[2]);
|
2015-03-06 21:49:03 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "captcha")){
|
2015-03-08 21:25:28 +03:00
|
|
|
run_captcha(argc, argv);
|
2015-07-08 10:36:43 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "nightmare")){
|
|
|
|
run_nightmare(argc, argv);
|
2015-03-08 21:25:28 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "change")){
|
|
|
|
change_rate(argv[2], atof(argv[3]), (argc > 4) ? atof(argv[4]) : 0);
|
2015-06-10 10:11:41 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "rgbgr")){
|
|
|
|
rgbgr_net(argv[2], argv[3], argv[4]);
|
2015-07-31 02:19:14 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "rescale")){
|
|
|
|
rescale_net(argv[2], argv[3], argv[4]);
|
2015-03-08 21:25:28 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "partial")){
|
|
|
|
partial(argv[2], argv[3], argv[4], atoi(argv[5]));
|
|
|
|
} else if (0 == strcmp(argv[1], "visualize")){
|
|
|
|
visualize(argv[2], (argc > 3) ? argv[3] : 0);
|
2015-06-11 00:44:10 +03:00
|
|
|
} else if (0 == strcmp(argv[1], "imtest")){
|
|
|
|
test_resize(argv[2]);
|
2015-03-08 21:25:28 +03:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Not an option: %s\n", argv[1]);
|
2014-12-18 22:28:42 +03:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
return 0;
|
2014-02-15 04:09:07 +04:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
|