void train_mnist_distill(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); char *backup_directory = "backup"; int classes = 10; int N = 50000; int epoch = (*net.seen)/N; data train;// = load_all_mnist10(); matrix soft = csv_to_matrix("results/ensemble.csv"); float weight = .9; scale_matrix(soft, weight); scale_matrix(train.y, 1. - weight); matrix_add_matrix(soft, train.y); while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ clock_t time=clock(); float loss = train_network_sgd(net, train, 1); if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.95 + loss*.05; if(get_current_batch(net)%100 == 0) { printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); } if(*net.seen/N > epoch){ epoch = *net.seen/N; char buff[256]; sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); save_weights(net, buff); } if(get_current_batch(net)%100 == 0){ char buff[256]; sprintf(buff, "%s/%s.backup",backup_directory,base); save_weights(net, buff); } } char buff[256]; sprintf(buff, "%s/%s.weights", backup_directory, base); save_weights(net, buff); free_network(net); free(base); free_data(train); }
void train_char_rnn(char *cfgfile, char *weightfile, char *filename) { FILE *fp = fopen(filename, "r"); //FILE *fp = fopen("data/ab.txt", "r"); //FILE *fp = fopen("data/grrm/asoiaf.txt", "r"); fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fseek(fp, 0, SEEK_SET); char *text = calloc(size, sizeof(char)); fread(text, 1, size, fp); fclose(fp); char *backup_directory = "/home/pjreddie/backup/"; srand(time(0)); data_seed = time(0); char *base = basecfg(cfgfile); printf("%s\n", base); float avg_loss = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int batch = net.batch; int steps = net.time_steps; int i = (*net.seen)/net.batch; clock_t time; while(get_current_batch(net) < net.max_batches){ i += 1; time=clock(); float_pair p = get_rnn_data(text, size, batch/steps, steps); float loss = train_network_datum(net, p.x, p.y) / (batch); free(p.x); free(p.y); if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %f rate, %lf seconds\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time)); if(i%100==0){ char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); save_weights(net, buff); } if(i%10==0){ char buff[256]; sprintf(buff, "%s/%s.backup", backup_directory, base); save_weights(net, buff); } } char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); }
void train_go(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); char *backup_directory = "/home/pjreddie/backup/"; char buff[256]; float *board = calloc(19*19*net.batch, sizeof(float)); float *move = calloc(19*19*net.batch, sizeof(float)); moves m = load_go_moves("/home/pjreddie/go.train"); //moves m = load_go_moves("games.txt"); int N = m.n; int epoch = (*net.seen)/N; while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ clock_t time=clock(); random_go_moves(m, board, move, net.batch); float loss = train_network_datum(net, board, move) / net.batch; if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.95 + loss*.05; printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); if(*net.seen/N > epoch){ epoch = *net.seen/N; char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory,base, epoch); save_weights(net, buff); } if(get_current_batch(net)%100 == 0){ char buff[256]; sprintf(buff, "%s/%s.backup",backup_directory,base); save_weights(net, buff); } if(get_current_batch(net)%10000 == 0){ char buff[256]; sprintf(buff, "%s/%s_%d.backup",backup_directory,base,get_current_batch(net)); save_weights(net, buff); } } sprintf(buff, "%s/%s.weights", backup_directory, base); save_weights(net, buff); free_network(net); free(base); }
void normalize_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network *net = load_network(cfgfile, weightfile, 0); int i; for(i = 0; i < net->n; ++i){ layer l = net->layers[i]; 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); net->layers[i].batch_normalize=1; } } save_weights(net, outfile); free_network(net); }
void oneoff(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network *net = parse_network_cfg(cfgfile); int oldn = net->layers[net->n - 2].n; int c = net->layers[net->n - 2].c; scal_cpu(oldn*c, .1, net->layers[net->n - 2].weights, 1); scal_cpu(oldn, 0, net->layers[net->n - 2].biases, 1); net->layers[net->n - 2].n = 11921; net->layers[net->n - 2].biases += 5; net->layers[net->n - 2].weights += 5*c; if(weightfile){ load_weights(net, weightfile); } net->layers[net->n - 2].biases -= 5; net->layers[net->n - 2].weights -= 5*c; net->layers[net->n - 2].n = oldn; printf("%d\n", oldn); layer l = net->layers[net->n - 2]; copy_cpu(l.n/3, l.biases, 1, l.biases + l.n/3, 1); copy_cpu(l.n/3, l.biases, 1, l.biases + 2*l.n/3, 1); copy_cpu(l.n/3*l.c, l.weights, 1, l.weights + l.n/3*l.c, 1); copy_cpu(l.n/3*l.c, l.weights, 1, l.weights + 2*l.n/3*l.c, 1); *net->seen = 0; save_weights(net, outfile); free_network(net); }
void denormalize_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network *net = load_network(cfgfile, weightfile, 0); int i; for (i = 0; i < net->n; ++i) { layer l = net->layers[i]; if ((l.type == DECONVOLUTIONAL || l.type == CONVOLUTIONAL) && l.batch_normalize) { denormalize_convolutional_layer(l); net->layers[i].batch_normalize=0; } 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; } } save_weights(net, outfile); free_network(net); }
int end(int winner, char *reason) { /* char *foo[] = {"1/2-1/2", "1-0", "0-1" }; */ if (weight_mode) { update_weights(winner); save_weights(weightfile); } /* if ((winner>=-1)&&(winner<=1)) */ /* { */ /* output("%s {%s}\n", foo[winner+1],reason); */ /* } */ /* else */ /* { */ /* output("%s {%s}\n", "ended", reason); */ /* } */ /* if (((computer[WHITE]+computer[BLACK])>0)&&book_mode) */ /* { */ /* hardupdatebook(WHITE, bookfile); */ /* } */ return winner; }
void train_cifar(char *cfgfile, char *weightfile) { srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); char *backup_directory = "/home/pjreddie/backup/"; int classes = 10; int N = 50000; char **labels = get_labels("data/cifar/labels.txt"); int epoch = (*net.seen)/N; data train = load_all_cifar10(); while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ clock_t time=clock(); float loss = train_network_sgd(net, train, 1); if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.95 + loss*.05; printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); if(*net.seen/N > epoch){ epoch = *net.seen/N; char buff[256]; sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); save_weights(net, buff); } if(get_current_batch(net)%100 == 0){ char buff[256]; sprintf(buff, "%s/%s.backup",backup_directory,base); save_weights(net, buff); } } char buff[256]; sprintf(buff, "%s/%s.weights", backup_directory, base); save_weights(net, buff); free_network(net); free_ptrs((void**)labels, classes); free(base); free_data(train); }
void partial(char *cfgfile, char *weightfile, char *outfile, int max) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights_upto(&net, weightfile, max); } net.seen = 0; save_weights(net, outfile); }
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.weights, 1, out.weights, 1); if(l.batch_normalize){ axpy_cpu(l.n, 1, l.scales, 1, out.scales, 1); axpy_cpu(l.n, 1, l.rolling_mean, 1, out.rolling_mean, 1); axpy_cpu(l.n, 1, l.rolling_variance, 1, out.rolling_variance, 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.weights, 1); if(l.batch_normalize){ scal_cpu(l.n, 1./n, l.scales, 1); scal_cpu(l.n, 1./n, l.rolling_mean, 1); scal_cpu(l.n, 1./n, l.rolling_variance, 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); }
void train_imagenet(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); //net.seen=0; int imgs = 1024; int i = net.seen/imgs; char **labels = get_labels("data/inet.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; pthread_t load_thread; data train; data buffer; load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer); while(1){ ++i; time=clock(); pthread_join(load_thread, 0); train = buffer; /* image im = float_to_image(256, 256, 3, train.X.vals[114]); show_image(im, "training"); cvWaitKey(0); */ load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); net.seen += imgs; if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), net.seen); free_data(train); if((i % 20000) == 0) net.learning_rate *= .1; //if(i%100 == 0 && net.learning_rate > .00001) net.learning_rate *= .97; if(i%1000==0){ char buff[256]; sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i); save_weights(net, buff); } } }
void exit_engine(void) { if (weight_mode) { save_weights(weightfile); } if (board) { free(board); } credits(); }
void rgbgr_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network *net = load_network(cfgfile, weightfile, 0); int i; for(i = 0; i < net->n; ++i){ layer l = net->layers[i]; if(l.type == CONVOLUTIONAL){ rgbgr_weights(l); break; } } save_weights(net, outfile); free_network(net); }
void rgbgr_net(char *cfgfile, char *weightfile, char *outfile) { 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); }
static void denormalize_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_t l = net.layers[i]; if (l.type == CONVOLUTIONAL && l.batch_normalize) { denormalize_convolutional_layer(l); net.layers[i].batch_normalize=0; } } save_weights(net, outfile); }
static 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_t l = net.layers[i]; if(l.type == CONVOLUTIONAL){ rescale_filters(l, 2, -.5); break; } } save_weights(net, outfile); }
static 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_t l = net.layers[j]; layer_t out = sum.layers[j]; if(l.type == CONVOLUTIONAL){ int num = l.n*l.c*l.size*l.size; fltadd(out.biases, l.biases, l.n); fltadd(out.filters, l.filters, num); } if(l.type == CONNECTED){ fltadd(out.biases, l.biases, l.outputs); fltadd(out.weights, l.weights, l.outputs * l.inputs); } } } n = n+1; for(j = 0; j < net.n; ++j){ layer_t 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); }
void train_writing(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = 256; int i = net.seen/imgs; list *plist = get_paths("data/train.list"); char **paths = (char **)list_to_array(plist); printf("%d\n", plist->size); clock_t time; while(1){ ++i; time=clock(); data train = load_data_writing(paths, imgs, plist->size, 256, 256, 4); float loss = train_network(net, train); #ifdef GPU float *out = get_network_output_gpu(net); #else float *out = get_network_output(net); #endif // image pred = float_to_image(32, 32, 1, out); // print_image(pred); net.seen += imgs; if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), net.seen); free_data(train); if((i % 20000) == 0) net.learning_rate *= .1; //if(i%100 == 0 && net.learning_rate > .00001) net.learning_rate *= .97; if(i%250==0){ char buff[256]; sprintf(buff, "/home/pjreddie/writing_backup/%s_%d.weights", base, i); save_weights(net, buff); } } }
void train_dice(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); char *backup_directory = "/home/pjreddie/backup/"; printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = 1024; int i = *net.seen/imgs; char **labels = dice_labels; list *plist = get_paths("data/dice/dice.train.list"); char **paths = (char **)list_to_array(plist); printf("%d\n", plist->size); clock_t time; while(1){ ++i; time=clock(); data train = load_data(paths, imgs, plist->size, labels, 6, net.w, net.h); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), *net.seen); free_data(train); if((i % 100) == 0) net.learning_rate *= .1; if(i%100==0){ char buff[256]; sprintf(buff, "%s/%s_%d.weights",backup_directory,base, i); save_weights(net, buff); } } }
static void 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, j; for(i = 0; i < net.n; ++i){ layer_t l = net.layers[i]; if(l.type == CONVOLUTIONAL){ net.layers[i].batch_normalize=1; net.layers[i].scales = calloc(l.n, sizeof(float)); for(j = 0; j < l.n; ++j){ net.layers[i].scales[i] = 1; } net.layers[i].rolling_mean = calloc(l.n, sizeof(float)); net.layers[i].rolling_variance = calloc(l.n, sizeof(float)); } } save_weights(net, outfile); }
void train_captcha(char *cfgfile, char *weightfile) { float avg_loss = -1; srand(time(0)); char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = 1024; int i = net.seen/imgs; list *plist = get_paths("/data/captcha/train.auto5"); char **paths = (char **)list_to_array(plist); printf("%d\n", plist->size); clock_t time; while(1){ ++i; time=clock(); data train = load_data_captcha(paths, imgs, plist->size, 10, 200, 60); translate_data_rows(train, -128); scale_data_rows(train, 1./128); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); net.seen += imgs; if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), net.seen); free_data(train); if(i%10==0){ char buff[256]; sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i); save_weights(net, buff); } } }
void denormalize_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); net.layers[i].batch_normalize=0; } 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; } } save_weights(net, outfile); }
void train_yolo(char *datacfg, char *cfgfile, char *weightfile) { list *options = read_data_cfg(datacfg); char *train_list = option_find_str(options, "train", "data/train_list.txt"); //char *test_list = option_find_str(options, "test", "data/test_list.txt"); //char *valid_list = option_find_str(options, "valid", "data/valid_list.txt"); char *backup_directory = option_find_str(options, "backup", "/backup/"); //char *label_list = option_find_str(options, "labels", "data/labels_list.txt"); //int classes = option_find_int(options, "classes", 2); srand(time(0)); data_seed = time(0); char *base = basecfg(cfgfile); printf("%s\n", base); float avg_loss = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = net.batch*net.subdivisions; int i = *net.seen/imgs; data train, buffer; layer l = net.layers[net.n - 1]; int side = l.side; int classes = l.classes; float jitter = l.jitter; list *plist = get_paths(train_list); //int N = plist->size; char **paths = (char **)list_to_array(plist); load_args args = {0}; args.w = net.w; args.h = net.h; args.paths = paths; args.n = imgs; args.m = plist->size; args.classes = classes; args.jitter = jitter; args.num_boxes = side; args.d = &buffer; args.type = REGION_DATA; pthread_t load_thread = load_data_in_thread(args); clock_t time; //while(i*imgs < N*120){ while(get_current_batch(net) < net.max_batches){ i += 1; time=clock(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data_in_thread(args); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); if(i%1000==0 || (i < 1000 && i%100 == 0)){ char buff[256]; sprintf(buff, "%s/%s_%06d.weights", backup_directory, base, i); save_weights(net, buff); } free_data(train); } char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); }
void train_captcha(char *cfgfile, char *weightfile) { srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = 1024; int i = *net.seen/imgs; int solved = 1; list *plist; char **labels = get_labels("/data/captcha/reimgs.labels.list"); if (solved){ plist = get_paths("/data/captcha/reimgs.solved.list"); }else{ plist = get_paths("/data/captcha/reimgs.raw.list"); } char **paths = (char **)list_to_array(plist); printf("%d\n", plist->size); clock_t time; #if defined __linux__ || defined __APPLE__ || defined PTHREAD_WINDOWS pthread_t load_thread; #else #endif data train; data buffer; load_args args = {0}; args.w = net.w; args.h = net.h; args.paths = paths; args.classes = 26; args.n = imgs; args.m = plist->size; args.labels = labels; args.d = &buffer; args.type = CLASSIFICATION_DATA; #if defined __linux__ || defined __APPLE__ || defined PTHREAD_WINDOWS load_thread = load_data_in_thread(args); #endif while(1){ ++i; time=clock(); #if defined __linux__ || defined __APPLE__ || defined PTHREAD_WINDOWS pthread_join(load_thread, 0); #endif train = buffer; fix_data_captcha(train, solved); /* image im = float_to_image(256, 256, 3, train.X.vals[114]); show_image(im, "training"); cvWaitKey(0); */ #if defined __linux__ || defined __APPLE__ || defined PTHREAD_WINDOWS load_thread = load_data_in_thread(args); #endif printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), *net.seen); free_data(train); if(i%100==0){ char buff[256]; sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i); save_weights(net, buff); } } }
void train_writing(char *cfgfile, char *weightfile) { char *backup_directory = "/home/kunle12/backup/"; srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); printf("%s\n", base); network * net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay); int imgs = net->batch*net->subdivisions; list *plist = get_paths("figures.list"); char **paths = (char **)list_to_array(plist); clock_t time; int N = plist->size; printf("N: %d\n", N); image out = get_network_image(net); data train, buffer; load_args args = {0}; args.w = net->w; args.h = net->h; args.out_w = out.w; args.out_h = out.h; args.paths = paths; args.n = imgs; args.m = N; args.d = &buffer; args.type = WRITING_DATA; pthread_t load_thread = load_data_in_thread(args); int epoch = (*net->seen)/N; while(get_current_batch(net) < net->max_batches || net->max_batches == 0){ time=clock(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data_in_thread(args); printf("Loaded %lf seconds\n",sec(clock()-time)); time=clock(); float loss = train_network(net, train); /* image pred = float_to_image(64, 64, 1, out); print_image(pred); */ /* image im = float_to_image(256, 256, 3, train.X.vals[0]); image lab = float_to_image(64, 64, 1, train.y.vals[0]); image pred = float_to_image(64, 64, 1, out); show_image(im, "image"); show_image(lab, "label"); print_image(lab); show_image(pred, "pred"); cvWaitKey(0); */ if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net->seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net->seen); free_data(train); if(get_current_batch(net)%100 == 0){ char buff[256]; sprintf(buff, "%s/%s_batch_%ld.weights", backup_directory, base, get_current_batch(net)); save_weights(net, buff); } if(*net->seen/N > epoch){ epoch = *net->seen/N; char buff[256]; sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); save_weights(net, buff); } } }
void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, int tokenized) { srand(time(0)); data_seed = time(0); unsigned char *text = 0; int *tokens = 0; size_t size; if(tokenized){ tokens = read_tokenized_data(filename, &size); } else { FILE *fp = fopen(filename, "rb"); fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); text = calloc(size+1, sizeof(char)); fread(text, 1, size, fp); fclose(fp); } char *backup_directory = "/home/pjreddie/backup/"; char *base = basecfg(cfgfile); fprintf(stderr, "%s\n", base); float avg_loss = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } int inputs = get_network_input_size(net); fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int batch = net.batch; int steps = net.time_steps; if(clear) *net.seen = 0; int i = (*net.seen)/net.batch; int streams = batch/steps; size_t *offsets = calloc(streams, sizeof(size_t)); int j; for(j = 0; j < streams; ++j){ offsets[j] = rand_size_t()%size; } clock_t time; while(get_current_batch(net) < net.max_batches){ i += 1; time=clock(); float_pair p; if(tokenized){ p = get_rnn_token_data(tokens, offsets, inputs, size, streams, steps); }else{ p = get_rnn_data(text, offsets, inputs, size, streams, steps); } float loss = train_network_datum(net, p.x, p.y) / (batch); free(p.x); free(p.y); if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; int chars = get_current_batch(net)*batch; fprintf(stderr, "%d: %f, %f avg, %f rate, %lf seconds, %f epochs\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), (float) chars/size); for(j = 0; j < streams; ++j){ //printf("%d\n", j); if(rand()%10 == 0){ //fprintf(stderr, "Reset\n"); offsets[j] = rand_size_t()%size; reset_rnn_state(net, j); } } if(i%1000==0){ char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); save_weights(net, buff); } if(i%10==0){ char buff[256]; sprintf(buff, "%s/%s.backup", backup_directory, base); save_weights(net, buff); } } char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); }
void train_coco(char *cfgfile, char *weightfile) { //char *train_images = "/home/pjreddie/data/voc/test/train.txt"; //char *train_images = "/home/pjreddie/data/coco/train.txt"; char *train_images = "data/coco.trainval.txt"; char *backup_directory = "/home/pjreddie/backup/"; srand(time(0)); data_seed = time(0); char *base = basecfg(cfgfile); printf("%s\n", base); float avg_loss = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = net.batch*net.subdivisions; int i = *net.seen/imgs; data train, buffer; layer l = net.layers[net.n - 1]; int side = l.side; int classes = l.classes; float jitter = l.jitter; list *plist = get_paths(train_images); //int N = plist->size; char **paths = (char **)list_to_array(plist); load_args args = {0}; args.w = net.w; args.h = net.h; args.paths = paths; args.n = imgs; args.m = plist->size; args.classes = classes; args.jitter = jitter; args.num_boxes = side; args.d = &buffer; args.type = REGION_DATA; pthread_t load_thread = load_data_in_thread(args); clock_t time; //while(i*imgs < N*120){ while(get_current_batch(net) < net.max_batches){ i += 1; time=clock(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data_in_thread(args); printf("Loaded: %lf seconds\n", sec(clock()-time)); /* image im = float_to_image(net.w, net.h, 3, train.X.vals[113]); image copy = copy_image(im); draw_coco(copy, train.y.vals[113], 7, "truth"); cvWaitKey(0); free_image(copy); */ time=clock(); float loss = train_network(net, train); if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); if(i%1000==0 || (i < 1000 && i%100 == 0)){ char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); save_weights(net, buff); } free_data(train); } char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); }
void train_compare(char *cfgfile, char *weightfile) { data_seed = time(0); srand(time(0)); float avg_loss = -1; char *base = basecfg(cfgfile); char *backup_directory = "/home/pjreddie/backup/"; printf("%s\n", base); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); int imgs = 1024; list *plist = get_paths("data/compare.train.list"); char **paths = (char **)list_to_array(plist); int N = plist->size; printf("%d\n", N); clock_t time; #ifndef _MSC_VER pthread_t load_thread; #endif data train; data buffer; load_args args = {0}; args.w = net.w; args.h = net.h; args.paths = paths; args.classes = 20; args.n = imgs; args.m = N; args.d = &buffer; args.type = COMPARE_DATA; #ifndef _MSC_VER load_thread = load_data_in_thread(args); #endif int epoch = *net.seen/N; int i = 0; while(1){ ++i; time=clock(); #ifndef _MSC_VER pthread_join(load_thread, 0); #else load_data_in_thread(args); #endif train = buffer; #ifndef _MSC_VER load_thread = load_data_in_thread(args); #endif printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%.3f: %f, %f avg, %lf seconds, %d images\n", (float)*net.seen/N, loss, avg_loss, sec(clock()-time), *net.seen); free_data(train); if(i%100 == 0){ char buff[256]; sprintf(buff, "%s/%s_%d_minor_%d.weights",backup_directory,base, epoch, i); save_weights(net, buff); } if(*net.seen/N > epoch){ epoch = *net.seen/N; i = 0; char buff[256]; sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); save_weights(net, buff); if(epoch%22 == 0) net.learning_rate *= .1; } } #ifndef _MSC_VER pthread_join(load_thread, 0); #endif free_data(buffer); free_network(net); free_ptrs((void**)paths, plist->size); free_list(plist); free(base); }
void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear) { list *options = read_data_cfg(datacfg); char *train_images = option_find_str(options, "train", "data/train.list"); char *backup_directory = option_find_str(options, "backup", "/backup/"); srand(time(0)); char *base = basecfg(cfgfile); printf("%s\n", base); float avg_loss = -1; network *nets = calloc(ngpus, sizeof(network)); srand(time(0)); int seed = rand(); int i; for(i = 0; i < ngpus; ++i){ srand(seed); #ifdef GPU cuda_set_device(gpus[i]); #endif nets[i] = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&nets[i], weightfile); } if(clear) *nets[i].seen = 0; nets[i].learning_rate *= ngpus; } srand(time(0)); network net = nets[0]; int imgs = net.batch * net.subdivisions * ngpus; printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); data train, buffer; layer l = net.layers[net.n - 1]; int classes = l.classes; float jitter = l.jitter; list *plist = get_paths(train_images); //int N = plist->size; char **paths = (char **)list_to_array(plist); load_args args = {0}; args.w = net.w; args.h = net.h; args.paths = paths; args.n = imgs; args.m = plist->size; args.classes = classes; args.jitter = jitter; args.num_boxes = l.max_boxes; args.d = &buffer; args.type = DETECTION_DATA; args.threads = 8; args.angle = net.angle; args.exposure = net.exposure; args.saturation = net.saturation; args.hue = net.hue; pthread_t load_thread = load_data(args); clock_t time; int count = 0; //while(i*imgs < N*120){ while(get_current_batch(net) < net.max_batches){ if(l.random && count++%10 == 0){ printf("Resizing\n"); //int dim = (rand() % 10 + 10) * 32; //if (get_current_batch(net)+200 > net.max_batches) dim = 608; //int dim = (rand() % 4 + 16) * 32; int dim = (args.w <= args.h ? args.w : args.h); printf("%d\n", dim); args.w = dim; args.h = dim; pthread_join(load_thread, 0); train = buffer; free_data(train); load_thread = load_data(args); for(i = 0; i < ngpus; ++i){ resize_network(nets + i, dim, dim); } net = nets[0]; } time=clock(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data(args); /* int k; for(k = 0; k < l.max_boxes; ++k){ box b = float_to_box(train.y.vals[10] + 1 + k*5); if(!b.x) break; printf("loaded: %f %f %f %f\n", b.x, b.y, b.w, b.h); } image im = float_to_image(448, 448, 3, train.X.vals[10]); int k; for(k = 0; k < l.max_boxes; ++k){ box b = float_to_box(train.y.vals[10] + 1 + k*5); printf("%d %d %d %d\n", truth.x, truth.y, truth.w, truth.h); draw_bbox(im, b, 8, 1,0,0); } save_image(im, "truth11"); */ printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = 0; #ifdef GPU if(ngpus == 1){ loss = train_network(net, train); } else { loss = train_networks(nets, ngpus, train, 4); } #else loss = train_network(net, train); #endif if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; i = get_current_batch(net); printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); if(i%1000==0 || (i < 1000 && i%100 == 0)){ #ifdef GPU if(ngpus != 1) sync_nets(nets, ngpus, 0); #endif char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); save_weights(net, buff); } free_data(train); } #ifdef GPU if(ngpus != 1) sync_nets(nets, ngpus, 0); #endif char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); }
void train_yolo(char *cfgfile, char *weightfile) { char *train_images = "/data/voc/train.txt"; char *backup_directory = "/home/kunle12/backup/"; srand(time(0)); char *base = basecfg(cfgfile); printf("%s\n", base); float avg_loss = -1; network * net = load_network(cfgfile, weightfile, 0); printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay); int imgs = net->batch*net->subdivisions; int i = *net->seen/imgs; data train, buffer; layer l = net->layers[net->n - 1]; int side = l.side; int classes = l.classes; float jitter = l.jitter; list *plist = get_paths(train_images); //int N = plist->size; char **paths = (char **)list_to_array(plist); load_args args = {0}; args.w = net->w; args.h = net->h; args.paths = paths; args.n = imgs; args.m = plist->size; args.classes = classes; args.jitter = jitter; args.num_boxes = side; args.d = &buffer; args.type = REGION_DATA; args.angle = net->angle; args.exposure = net->exposure; args.saturation = net->saturation; args.hue = net->hue; pthread_t load_thread = load_data_in_thread(args); clock_t time; //while(i*imgs < N*120){ while(get_current_batch(net) < net->max_batches){ i += 1; time=clock(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data_in_thread(args); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); float loss = train_network(net, train); if (avg_loss < 0) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); if(i%1000==0 || (i < 1000 && i%100 == 0)){ char buff[256]; sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); save_weights(net, buff); } free_data(train); } char buff[256]; sprintf(buff, "%s/%s_final.weights", backup_directory, base); save_weights(net, buff); free_network( net ); }