darknet/src/darknet.c

401 lines
13 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"
2015-08-17 19:00:12 +03:00
#include "blas.h"
2016-05-07 02:25:16 +03:00
#include "connected_layer.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
2016-08-06 01:27:07 +03:00
extern void run_voxel(int argc, char **argv);
extern void run_yolo(int argc, char **argv);
2016-07-20 00:50:01 +03:00
extern void run_detector(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);
2015-09-05 03:52:44 +03:00
extern void run_compare(int argc, char **argv);
2015-10-09 22:50:43 +03:00
extern void run_classifier(int argc, char **argv);
2016-01-28 23:30:38 +03:00
extern void run_char_rnn(int argc, char **argv);
2016-03-01 00:54:12 +03:00
extern void run_vid_rnn(int argc, char **argv);
extern void run_tag(int argc, char **argv);
extern void run_cifar(int argc, char **argv);
2016-03-14 09:18:42 +03:00
extern void run_go(int argc, char **argv);
2016-05-13 21:59:43 +03:00
extern void run_art(int argc, char **argv);
2016-08-06 01:27:07 +03:00
extern void run_super(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-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);
}
2016-08-06 01:27:07 +03:00
void speed(char *cfgfile, int tics)
{
if (tics == 0) tics = 1000;
network net = parse_network_cfg(cfgfile);
set_batch_network(&net, 1);
int i;
time_t start = time(0);
image im = make_image(net.w, net.h, net.c);
for(i = 0; i < tics; ++i){
network_predict(net, im.data);
}
double t = difftime(time(0), start);
printf("\n%d evals, %f Seconds\n", tics, t);
printf("Speed: %f sec/eval\n", t/tics);
printf("Speed: %f Hz\n", tics/t);
}
2016-06-20 00:28:15 +03:00
void operations(char *cfgfile)
{
gpu_index = -1;
network net = parse_network_cfg(cfgfile);
int i;
long ops = 0;
for(i = 0; i < net.n; ++i){
layer l = net.layers[i];
if(l.type == CONVOLUTIONAL){
2016-07-20 00:50:01 +03:00
ops += 2l * l.n * l.size*l.size*l.c * l.out_h*l.out_w;
2016-06-20 00:28:15 +03:00
} else if(l.type == CONNECTED){
2016-07-20 00:50:01 +03:00
ops += 2l * l.inputs * l.outputs;
2016-06-20 00:28:15 +03:00
}
}
printf("Floating Point Operations: %ld\n", ops);
2016-07-20 00:50:01 +03:00
printf("Floating Point Operations: %.2f Bn\n", (float)ops/1000000000.);
2016-06-20 00:28:15 +03:00
}
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-09-05 03:52:44 +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-09-01 21:21:01 +03:00
void stacked(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -1;
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
net.seen = 0;
save_weights_double(net, outfile);
}
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);
}
2016-07-20 00:50:01 +03:00
void reset_normalize_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 && l.batch_normalize) {
denormalize_convolutional_layer(l);
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
}
}
save_weights(net, outfile);
}
layer normalize_layer(layer l, int n)
{
int j;
l.batch_normalize=1;
l.scales = calloc(n, sizeof(float));
for(j = 0; j < n; ++j){
l.scales[j] = 1;
}
l.rolling_mean = calloc(n, sizeof(float));
l.rolling_variance = calloc(n, sizeof(float));
return l;
}
2015-11-04 06:23:17 +03:00
void normalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -1;
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
2016-07-20 00:50:01 +03:00
int i;
2015-11-04 06:23:17 +03:00
for(i = 0; i < net.n; ++i){
layer l = net.layers[i];
2016-07-20 00:50:01 +03:00
if(l.type == CONVOLUTIONAL && !l.batch_normalize){
net.layers[i] = normalize_layer(l, l.n);
}
if (l.type == CONNECTED && !l.batch_normalize) {
net.layers[i] = normalize_layer(l, l.outputs);
}
if (l.type == GRU && l.batch_normalize) {
*l.input_z_layer = normalize_layer(*l.input_z_layer, l.input_z_layer->outputs);
*l.input_r_layer = normalize_layer(*l.input_r_layer, l.input_r_layer->outputs);
*l.input_h_layer = normalize_layer(*l.input_h_layer, l.input_h_layer->outputs);
*l.state_z_layer = normalize_layer(*l.state_z_layer, l.state_z_layer->outputs);
*l.state_r_layer = normalize_layer(*l.state_r_layer, l.state_r_layer->outputs);
*l.state_h_layer = normalize_layer(*l.state_h_layer, l.state_h_layer->outputs);
2015-11-04 06:23:17 +03:00
net.layers[i].batch_normalize=1;
}
}
save_weights(net, outfile);
}
void denormalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -1;
network net = parse_network_cfg(cfgfile);
2016-03-14 09:18:42 +03:00
if (weightfile) {
2015-11-04 06:23:17 +03:00
load_weights(&net, weightfile);
}
int i;
2016-03-14 09:18:42 +03:00
for (i = 0; i < net.n; ++i) {
2015-11-04 06:23:17 +03:00
layer l = net.layers[i];
2016-03-14 09:18:42 +03:00
if (l.type == CONVOLUTIONAL && l.batch_normalize) {
2015-11-04 06:23:17 +03:00
denormalize_convolutional_layer(l);
net.layers[i].batch_normalize=0;
2016-05-07 02:25:16 +03:00
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
net.layers[i].batch_normalize=0;
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
l.input_z_layer->batch_normalize = 0;
l.input_r_layer->batch_normalize = 0;
l.input_h_layer->batch_normalize = 0;
l.state_z_layer->batch_normalize = 0;
l.state_r_layer->batch_normalize = 0;
l.state_h_layer->batch_normalize = 0;
net.layers[i].batch_normalize=0;
2015-11-04 06:23:17 +03:00
}
}
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)
{
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);
2016-01-28 23:30:38 +03:00
if(find_arg(argc, argv, "-nogpu")) {
gpu_index = -1;
}
2014-12-17 02:34:10 +03:00
#ifndef GPU
gpu_index = -1;
#else
if(gpu_index >= 0){
2015-09-01 21:21:01 +03:00
cudaError_t status = cudaSetDevice(gpu_index);
check_error(status);
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
2016-09-02 02:48:41 +03:00
if (0 == strcmp(argv[1], "average")){
2015-08-17 19:00:12 +03:00
average(argc, argv);
} else if (0 == strcmp(argv[1], "yolo")){
run_yolo(argc, argv);
2016-08-06 01:27:07 +03:00
} else if (0 == strcmp(argv[1], "voxel")){
run_voxel(argc, argv);
} else if (0 == strcmp(argv[1], "super")){
run_super(argc, argv);
2016-07-20 00:50:01 +03:00
} else if (0 == strcmp(argv[1], "detector")){
run_detector(argc, argv);
2016-03-01 00:54:12 +03:00
} else if (0 == strcmp(argv[1], "cifar")){
run_cifar(argc, argv);
2016-03-14 09:18:42 +03:00
} else if (0 == strcmp(argv[1], "go")){
run_go(argc, argv);
2016-01-28 23:30:38 +03:00
} else if (0 == strcmp(argv[1], "rnn")){
run_char_rnn(argc, argv);
2016-03-01 00:54:12 +03:00
} else if (0 == strcmp(argv[1], "vid")){
run_vid_rnn(argc, argv);
2015-07-31 02:19:14 +03:00
} else if (0 == strcmp(argv[1], "coco")){
run_coco(argc, argv);
2015-10-09 22:50:43 +03:00
} else if (0 == strcmp(argv[1], "classifier")){
run_classifier(argc, argv);
2016-05-13 21:59:43 +03:00
} else if (0 == strcmp(argv[1], "art")){
run_art(argc, argv);
2016-03-01 00:54:12 +03:00
} else if (0 == strcmp(argv[1], "tag")){
run_tag(argc, argv);
2015-09-05 03:52:44 +03:00
} else if (0 == strcmp(argv[1], "compare")){
run_compare(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);
2016-06-03 01:25:24 +03:00
} else if (0 == strcmp(argv[1], "3d")){
2016-08-06 01:27:07 +03:00
composite_3d(argv[2], argv[3], argv[4], (argc > 5) ? atof(argv[5]) : 0);
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]);
2016-07-20 00:50:01 +03:00
} else if (0 == strcmp(argv[1], "reset")){
reset_normalize_net(argv[2], argv[3], argv[4]);
2015-11-04 06:23:17 +03:00
} else if (0 == strcmp(argv[1], "denormalize")){
denormalize_net(argv[2], argv[3], argv[4]);
} else if (0 == strcmp(argv[1], "normalize")){
normalize_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]);
2016-06-20 00:28:15 +03:00
} else if (0 == strcmp(argv[1], "ops")){
operations(argv[2]);
2016-08-06 01:27:07 +03:00
} else if (0 == strcmp(argv[1], "speed")){
speed(argv[2], (argc > 3) ? atoi(argv[3]) : 0);
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]));
2016-06-20 00:28:15 +03:00
} else if (0 == strcmp(argv[1], "average")){
average(argc, argv);
2015-09-01 21:21:01 +03:00
} else if (0 == strcmp(argv[1], "stacked")){
stacked(argv[2], argv[3], argv[4]);
2015-03-08 21:25:28 +03:00
} 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;
}