darknet/src/darknet.c

147 lines
4.0 KiB
C
Raw Normal View History

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"
#define _GNU_SOURCE
#include <fenv.h>
2015-03-06 21:49:03 +03:00
extern void run_imagenet(int argc, char **argv);
extern void run_detection(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)
{
// 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;
fseek(fp, 0, SEEK_SET);
fwrite(&rate, sizeof(float), 1, fp);
fclose(fp);
}
2015-03-08 21:25:28 +03:00
void partial(char *cfgfile, char *weightfile, char *outfile, int max)
{
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-06-10 10:11:41 +03:00
#ifdef OPENCV
2015-03-08 21:25:28 +03:00
cvWaitKey(0);
2015-06-10 10:11:41 +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)
{
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();
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-03-06 21:49:03 +03:00
} else if (0 == strcmp(argv[1], "detection")){
2015-03-08 21:25:28 +03:00
run_detection(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
}
return 0;
}