Example #1
0
void IniFile::setStringValue(const char * section, const char * key, const char * value)
{
	int sec_start, sec_end, key_start, key_end, value_start, value_end;
	std::string sec(section);
	std::string k(key);
	std::string val(value);

	loadIniFile();

	sec_start = 0;
	int leftbrace, rightbrace, lastnewline, nextnewline;
	do {
		sec_start = _FileContainer.find(section, sec_start + 1);
		while (isInComment(sec_start))
		{
			sec_start = _FileContainer.find(section, sec_start + 1);
		}
		if (sec_start == std::string::npos)
			break;
		leftbrace = _FileContainer.rfind('[', sec_start);
		rightbrace = _FileContainer.find(']', sec_start);
		lastnewline = _FileContainer.rfind('\n', sec_start);
		nextnewline = _FileContainer.find('\n', sec_start);
		if (rightbrace == std::string::npos)
		{
			sec_start = std::string::npos;
			break;
		}
		if (nextnewline == std::string::npos)
			nextnewline = _FileContainer.size();
	} while (leftbrace < lastnewline || rightbrace > nextnewline);
	
	/* not find the section */
	if (sec_start == std::string::npos)
	{
		_FileContainer += "\n[" + sec + "]\n" + k + "=" + val + "\n";

		_FileMap.insert(std::map<std::string, std::string>::value_type(sec + "|" + k, val));
	}
	else{
		sec_end = sec_start + strlen(section);

		int bound = _FileContainer.find('[', sec_start);
		key_start = _FileContainer.find(key, sec_end);
		while (isInComment(key_start))
		{
			key_start = _FileContainer.find(key, key_start + 1);
		}

		/* not find the key */
		if (key_start == std::string::npos || (bound > std::string::npos && key_start > bound))
		{
			int pos = _FileContainer.find('\n', sec_end) + 1;
			_FileContainer.insert(pos, key);
			pos += strlen(key);
			_FileContainer.insert(pos, "=");
			pos++;
			_FileContainer.insert(pos, value);
			pos += strlen(value);
			_FileContainer.insert(pos, "\n");

			_FileMap.insert(std::map<std::string, std::string>::value_type(sec + "|" + k, val));
		}

		else {
			key_end = key_start + strlen(key);
			value_start = _FileContainer.find('=', key_end) + 1;
			while (_FileContainer[value_start] == ' ')
				value_start++;
			value_end = _FileContainer.find('\n', value_start) - 1;
			if (value_end == std::string::npos)
				value_end = _FileContainer.size() - 1;
			while (_FileContainer[value_end] == ' ')
				value_end--;

			_FileContainer.replace(value_start, value_end - value_start + 1, value);

			auto iter = _FileMap.find(sec + "|" + k);
			iter->second = val;
		}
	}

	std::fstream file(_FileName.c_str(), std::ios::out);
	file << _FileContainer;
}
Example #2
0
void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear,
		int tokenized) {
	srand(time(0));
	unsigned char *text = 0;
	int *tokens = 0;
	size_t size;
	if (tokenized) {
		tokens = read_tokenized_data(filename, &size);
	} else {
		text = read_file(filename);
		size = strlen((const char*) text);
	}

	char *backup_directory = "/home/pjreddie/backup/";
	char *base = basecfg(cfgfile);
	fprintf(stderr, "%s\n", base);
	real_t avg_loss = -1;
	network *net = load_network(cfgfile, weightfile, clear);

	int inputs = net->inputs;
	fprintf(stderr,
			"Learning Rate: %g, Momentum: %g, Decay: %g, Inputs: %d %d %d\n",
			net->learning_rate, net->momentum, net->decay, inputs, net->batch,
			net->time_steps);
	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();
		real_t_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);
		}

		copy_cpu(net->inputs * net->batch, p.x, 1, net->input, 1);
		copy_cpu(net->truths * net->batch, p.y, 1, net->truth, 1);
		real_t loss = train_network_datum(net) / (batch);
		free(p.x);
		free(p.y);
		if (avg_loss < 0)
			avg_loss = loss;
		avg_loss = avg_loss * .9 + loss * .1;

		size_t 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),
				(real_t) chars / size);

		for (j = 0; j < streams; ++j) {
			//printf("%d\n", j);
			if (rand() % 64 == 0) {
				//fprintf(stderr, "Reset\n");
				offsets[j] = rand_size_t() % size;
				reset_network_state(net, j);
			}
		}

		if (i % 10000 == 0) {
			char buff[256];
			sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
			save_weights(net, buff);
		}
		if (i % 100 == 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);
}
Example #3
0
const timeUnit *timeUnit::minHelp() {
  int64_t ns_per_sec = sec().get_ns_per_unit().getNumer();
  return new timeUnit(fraction(I64_C(60) * ns_per_sec));
}
Example #4
0
void train_coco(char *cfgfile, char *weightfile)
{
    //char *train_images = "/home/pjreddie/data/coco/train.txt";
    char *train_images = "/home/pjreddie/data/voc/test/train.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 = 128;
    int i = *net.seen/imgs;
    data train, buffer;


    layer l = net.layers[net.n - 1];

    int side = l.side;
    int classes = l.classes;

    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.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) {
        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, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), i*imgs);
        if(i%1000==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_regressor(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
		int ngpus, int clear) {
	int i;

	real_t avg_loss = -1;
	char *base = basecfg(cfgfile);
	printf("%s\n", base);
	printf("%d\n", ngpus);
	network **nets = calloc(ngpus, sizeof(network*));

	srand(time(0));
	int seed = rand();
	for (i = 0; i < ngpus; ++i) {
		srand(seed);
#ifdef GPU
		cuda_set_device(gpus[i]);
#endif
		nets[i] = load_network(cfgfile, weightfile, clear);
		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);
	list *options = read_data_cfg(datacfg);

	char *backup_directory = option_find_str(options, "backup", "/backup/");
	char *train_list = option_find_str(options, "train", "data/train.list");
	int classes = option_find_int(options, "classes", 1);

	list *plist = get_paths(train_list);
	char **paths = (char **) list_to_array(plist);
	printf("%d\n", plist->size);
	int N = plist->size;
	clock_t time;

	load_args args = { 0 };
	args.w = net->w;
	args.h = net->h;
	args.threads = 32;
	args.classes = classes;

	args.min = net->min_ratio * net->w;
	args.max = net->max_ratio * net->w;
	args.angle = net->angle;
	args.aspect = net->aspect;
	args.exposure = net->exposure;
	args.saturation = net->saturation;
	args.hue = net->hue;
	args.size = net->w;

	args.paths = paths;
	args.n = imgs;
	args.m = N;
	args.type = REGRESSION_DATA;

	data train;
	data buffer;
	pthread_t load_thread;
	args.d = &buffer;
	load_thread = load_data(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(args);

		printf("Loaded: %lf seconds\n", sec(clock() - time));
		time = clock();

		real_t 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 == -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), (real_t)(*net->seen) / N, loss,
				avg_loss, get_current_rate(net), sec(clock() - time),
				*net->seen);
		free_data(train);
		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**) paths, plist->size);
	free_list(plist);
	free(base);
}
Example #6
0
void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num)
{
    network *net = load_network(cfgfile, weightfile, 0);
    set_batch_network(net, 1);
    srand(2222222);

    list *options = read_data_cfg(datacfg);

    char *name_list = option_find_str(options, "names", 0);
    if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
    int top = option_find_int(options, "top", 1);

    int i = 0;
    char **names = get_labels(name_list);
    clock_t time;
    int *indexes = calloc(top, sizeof(int));
    char buff[256];
    char *input = buff;
    while(1){
        if(filename){
            strncpy(input, filename, 256);
        }else{
            printf("Enter Image Path: ");
            fflush(stdout);
            input = fgets(input, 256, stdin);
            if(!input) return;
            strtok(input, "\n");
        }
        image orig = load_image_color(input, 0, 0);
        image r = resize_min(orig, 256);
        image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224);
        float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742};
        float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583};
        float var[3];
        var[0] = std[0]*std[0];
        var[1] = std[1]*std[1];
        var[2] = std[2]*std[2];

        normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h);

        float *X = im.data;
        time=clock();
        float *predictions = network_predict(net, X);

        layer l = net->layers[layer_num];
        for(i = 0; i < l.c; ++i){
            if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]);
        }
#ifdef GPU
        cuda_pull_array(l.output_gpu, l.output, l.outputs);
#endif
        for(i = 0; i < l.outputs; ++i){
            printf("%f\n", l.output[i]);
        }
        /*

           printf("\n\nWeights\n");
           for(i = 0; i < l.n*l.size*l.size*l.c; ++i){
           printf("%f\n", l.filters[i]);
           }

           printf("\n\nBiases\n");
           for(i = 0; i < l.n; ++i){
           printf("%f\n", l.biases[i]);
           }
         */

        top_predictions(net, top, indexes);
        printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
        for(i = 0; i < top; ++i){
            int index = indexes[i];
            printf("%s: %f\n", names[index], predictions[index]);
        }
        free_image(im);
        if (filename) break;
    }
}
Example #7
0
void run_jit_float( const float*                    i_a,
                    const float*                    i_b,
                    float*                          o_c,
                    const libxsmm_xgemm_descriptor* i_xgemm_desc,
                    const char*                     i_arch ) {
  struct timeval l_start, l_end;

  /* define function pointer */
  typedef void (*jitfun)(const float* a, const float* b, float* c);
  typedef void (*jitfun_pf)(const float* a, const float* b, float* c, const float* a_pf, const float* b_pf, const float* c_pf);
  jitfun l_test_jit;
  jitfun_pf l_test_jit_pf;

  double l_jittime = 0.0;
  gettimeofday(&l_start, NULL);
  
  /* allocate buffer for code */
  unsigned char* l_gen_code = (unsigned char*) malloc( 32768 * sizeof(unsigned char) );
  libxsmm_generated_code l_generated_code;
  l_generated_code.generated_code = (void*)l_gen_code;
  l_generated_code.buffer_size = 32768;
  l_generated_code.code_size = 0;
  l_generated_code.code_type = 2;
  l_generated_code.last_error = 0;

  /* generate kernel */
  libxsmm_generator_dense_kernel( &l_generated_code,
                                  i_xgemm_desc,
                                  i_arch );

  /* handle evetl. errors */
  if ( l_generated_code.last_error != 0 ) {
    fprintf(stderr, "%s\n", libxsmm_strerror( l_generated_code.last_error ) );
    exit(-1);
  }

  /* create executable buffer */
  int l_code_pages = (((l_generated_code.code_size-1)*sizeof(unsigned char))/LIBXSMM_BUILD_PAGESIZE)+1;
  int l_code_page_size = LIBXSMM_BUILD_PAGESIZE*l_code_pages;
  int l_fd = open("/dev/zero", O_RDWR);
  void* p = mmap(0, l_code_page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, l_fd, 0);
  close(l_fd);
  /* explicitly disable THP for this memory region, kernel 2.6.38 or higher! 
  madvise(p, l_code_page_size, MADV_NOHUGEPAGE); */
  if (p == MAP_FAILED) {
    fprintf(stderr, "LIBXSMM: something bad happend in mmap, couldn't allocate code buffer!\n");
    exit(-1);
  }
  unsigned char* l_code = (unsigned char*)p;
  memset( l_code, 0, l_code_page_size );
  memcpy( l_code, l_gen_code, l_generated_code.code_size );
  int error = mprotect( (void*)l_code, l_code_page_size, PROT_EXEC | PROT_READ );
  if (error == -1) {
    int errsv = errno;
    if (errsv == EINVAL) {
      fprintf(stderr, "mprotect failed: addr is not a valid pointer, or not a multiple of the system page size!\n");
    } else if (errsv == ENOMEM) {
      fprintf(stderr, "mprotect failed: Internal kernel structures could not be allocated!\n");
    } else if (errsv == EACCES) {
      fprintf(stderr, "mprotect failed: The memory cannot be given the specified access!\n");
    } else {
      fprintf(stderr, "mprotect failed: Unknown Error!\n");
    }
    exit(-1);
  }

  /* set function pointer and jitted code */
  if ( i_xgemm_desc->prefetch == LIBXSMM_PREFETCH_NONE ) {
    l_test_jit = (jitfun)l_code;
  } else {
    l_test_jit_pf = (jitfun_pf)l_code;
  }

  gettimeofday(&l_end, NULL);  
  l_jittime = sec(l_start, l_end);

  printf("size of generated code: %i\n", l_generated_code.code_size );

  /* write buffer for manual decode as binary to a file */
  char l_objdump_name[128];
  sprintf( l_objdump_name, "kernel_%i_%i_%i.bin", i_xgemm_desc->m, i_xgemm_desc->n, i_xgemm_desc->k ); 
  FILE *l_byte_code = fopen( l_objdump_name, "wb");

  if ( l_byte_code != NULL ){
    fwrite( (const void*)l_gen_code, 1, l_generated_code.code_size, l_byte_code);
    fclose( l_byte_code );
  } else {
    /* error */
  }

  unsigned int l_t;
  double l_runtime = 0.0;

  gettimeofday(&l_start, NULL);

  if ( i_xgemm_desc->prefetch == LIBXSMM_PREFETCH_NONE ) {
    for ( l_t = 0; l_t < REPS; l_t++ ) {
      l_test_jit(i_a, i_b, o_c);
    }
  } else {
    for ( l_t = 0; l_t < REPS; l_t++ ) {
      l_test_jit_pf(i_a, i_b, o_c, i_a, i_b, o_c);
    }
  }

  gettimeofday(&l_end, NULL);  
  l_runtime = sec(l_start, l_end);

  printf("%fs for creating jit\n", l_jittime);
  printf("%fs for executing jit\n", l_runtime);
  printf("%f GFLOPS for jit\n", ((double)((double)REPS * (double)i_xgemm_desc->m * (double)i_xgemm_desc->n * (double)i_xgemm_desc->k) * 2.0) / (l_runtime * 1.0e9));

  free(l_gen_code);
}
Example #8
0
void train_super(char *cfgfile, char *weightfile, int clear)
{
    char *train_images = "/data/imagenet/imagenet1k.train.list";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(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);
    }
    if(clear) *net.seen = 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;


    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.scale = 4;
    args.paths = paths;
    args.n = imgs;
    args.m = plist->size;
    args.d = &buffer;
    args.type = SUPER_DATA;

#ifdef __linux__
    pthread_t load_thread = load_data_in_thread(args);
#endif
    clock_t time;
    //while(i*imgs < N*120){
    while(get_current_batch(net) < net.max_batches){
        i += 1;
        time=clock();
#ifdef __linux__
        pthread_join(load_thread, 0);
#endif
        train = buffer;
#ifdef __linux__
        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 < 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){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
            save_weights(net, buff);
        }
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, base);
            save_weights(net, buff);
        }
        free_data(train);
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
}
Example #9
0
void train_tag(char *cfgfile, char *weightfile, int clear)
{
    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);
    }
    if(clear) *net.seen = 0;
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    int imgs = 1024;
    list *plist = get_paths("/home/pjreddie/tag/train.list");
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
    int N = plist->size;
    clock_t time;
    pthread_t load_thread;
    data train;
    data buffer;

    load_args args = {0};
    args.w = net.w;
    args.h = net.h;

    args.min = net.w;
    args.max = net.max_crop;
    args.size = net.w;

    args.paths = paths;
    args.classes = net.outputs;
    args.n = imgs;
    args.m = N;
    args.d = &buffer;
    args.type = TAG_DATA;

    fprintf(stderr, "%d classes\n", net.outputs);

    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);
        if(avg_loss == -1) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;
        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);
        free_data(train);
        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);

    pthread_join(load_thread, 0);
    free_data(buffer);
    free_network(net);
    free_ptrs((void**)paths, plist->size);
    free_list(plist);
    free(base);
}
Example #10
0
File: lsd.c Project: vaiv/OpenANPR
void train_lsd2(char *cfgfile, char *weightfile, char *acfgfile, char *aweightfile, int clear)
{
#ifdef GPU
    char *train_images = "/home/pjreddie/data/coco/trainvalno5k.txt";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(time(0));
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);
    }
    if(clear) *net.seen = 0;

    char *abase = basecfg(acfgfile);
    network anet = parse_network_cfg(acfgfile);
    if(aweightfile){
        load_weights(&anet, aweightfile);
    }
    if(clear) *anet.seen = 0;

    int i, j, k;
    layer imlayer = {};
    for (i = 0; i < net.n; ++i) {
        if (net.layers[i].out_c == 3) {
            imlayer = net.layers[i];
            break;
        }
    }

    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    int imgs = net.batch*net.subdivisions;
    i = *net.seen/imgs;
    data train, buffer;


    list *plist = get_paths(train_images);
    //int N = plist->size;
    char **paths = (char **)list_to_array(plist);

    load_args args = {};
    args.w = net.w;
    args.h = net.h;
    args.paths = paths;
    args.n = imgs;
    args.m = plist->size;
    args.d = &buffer;

    args.min = net.min_crop;
    args.max = net.max_crop;
    args.angle = net.angle;
    args.aspect = net.aspect;
    args.exposure = net.exposure;
    args.saturation = net.saturation;
    args.hue = net.hue;
    args.size = net.w;
    args.type = CLASSIFICATION_DATA;
    args.classes = 1;
    char *ls[1] = {"coco"};
    args.labels = ls;

    pthread_t load_thread = load_data_in_thread(args);
    clock_t time;

    network_state gstate = {};
    gstate.index = 0;
    gstate.net = net;
    int x_size = get_network_input_size(net)*net.batch;
    int y_size = 1*net.batch;
    gstate.input = cuda_make_array(0, x_size);
    gstate.truth = 0;
    gstate.delta = 0;
    gstate.train = 1;
    float *X = (float*)calloc(x_size, sizeof(float));
    float *y = (float*)calloc(y_size, sizeof(float));

    network_state astate = {};
    astate.index = 0;
    astate.net = anet;
    int ay_size = get_network_output_size(anet)*anet.batch;
    astate.input = 0;
    astate.truth = 0;
    astate.delta = 0;
    astate.train = 1;

    float *imerror = cuda_make_array(0, imlayer.outputs);
    float *ones_gpu = cuda_make_array(0, ay_size);
    fill_ongpu(ay_size, 1, ones_gpu, 1);

    float aloss_avg = -1;
    float gloss_avg = -1;

    //data generated = copy_data(train);

    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));

        data generated = copy_data(train);
        time=clock();
        float gloss = 0;

        for(j = 0; j < net.subdivisions; ++j){
            get_next_batch(train, net.batch, j*net.batch, X, y);
            cuda_push_array(gstate.input, X, x_size);
            *net.seen += net.batch;
            forward_network_gpu(net, gstate);

            fill_ongpu(imlayer.outputs, 0, imerror, 1);
            astate.input = imlayer.output_gpu;
            astate.delta = imerror;
            astate.truth = ones_gpu;
            forward_network_gpu(anet, astate);
            backward_network_gpu(anet, astate);

            scal_ongpu(imlayer.outputs, 1, imerror, 1);
            axpy_ongpu(imlayer.outputs, 1, imerror, 1, imlayer.delta_gpu, 1);

            backward_network_gpu(net, gstate);

            printf("features %f\n", cuda_mag_array(imlayer.delta_gpu, imlayer.outputs));
            printf("realness %f\n", cuda_mag_array(imerror, imlayer.outputs));

            gloss += get_network_cost(net) /(net.subdivisions*net.batch);

            cuda_pull_array(imlayer.output_gpu, imlayer.output, x_size);
            for(k = 0; k < net.batch; ++k){
                int index = j*net.batch + k;
                copy_cpu(imlayer.outputs, imlayer.output + k*imlayer.outputs, 1, generated.X.vals[index], 1);
                generated.y.vals[index][0] = 0;
            }
        }
        harmless_update_network_gpu(anet);

        data merge = concat_data(train, generated);
        randomize_data(merge);
        float aloss = train_network(anet, merge);

        update_network_gpu(net);
        update_network_gpu(anet);
        free_data(merge);
        free_data(train);
        free_data(generated);
        if (aloss_avg < 0) aloss_avg = aloss;
        aloss_avg = aloss_avg*.9 + aloss*.1;
        gloss_avg = gloss_avg*.9 + gloss*.1;

        printf("%d: gen: %f, adv: %f | gen_avg: %f, adv_avg: %f, %f rate, %lf seconds, %d images\n", i, gloss, aloss, gloss_avg, aloss_avg, get_current_rate(net), sec(clock()-time), i*imgs);
        if(i%1000==0){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
            save_weights(net, buff);
            sprintf(buff, "%s/%s_%d.weights", backup_directory, abase, i);
            save_weights(anet, buff);
        }
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, base);
            save_weights(net, buff);
            sprintf(buff, "%s/%s.backup", backup_directory, abase);
            save_weights(anet, buff);
        }
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
#endif
}
Example #11
0
File: lsd.c Project: vaiv/OpenANPR
void train_lsd(char *cfgfile, char *weightfile, int clear)
{
    char *train_images = "/home/pjreddie/data/coco/trainvalno5k.txt";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(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);
    }
    if(clear) *net.seen = 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;


    list *plist = get_paths(train_images);
    //int N = plist->size;
    char **paths = (char **)list_to_array(plist);

    load_args args = {};
    args.w = net.w;
    args.h = net.h;
    args.paths = paths;
    args.n = imgs;
    args.m = plist->size;
    args.d = &buffer;

    args.min = net.min_crop;
    args.max = net.max_crop;
    args.angle = net.angle;
    args.aspect = net.aspect;
    args.exposure = net.exposure;
    args.saturation = net.saturation;
    args.hue = net.hue;
    args.size = net.w;
    args.type = CLASSIFICATION_DATA;
    args.classes = 1;
    char *ls[1] = {"coco"};
    args.labels = ls;

    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){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
            save_weights(net, buff);
        }
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, base);
            save_weights(net, buff);
        }
        free_data(train);
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
}
Example #12
0
File: lsd.c Project: vaiv/OpenANPR
void train_lsd3(char *fcfg, char *fweight, char *gcfg, char *gweight, char *acfg, char *aweight, int clear)
{
#ifdef GPU
    //char *train_images = "/home/pjreddie/data/coco/trainvalno5k.txt";
    char *train_images = "/home/pjreddie/data/imagenet/imagenet1k.train.list";
    //char *style_images = "/home/pjreddie/data/coco/trainvalno5k.txt";
    char *style_images = "/home/pjreddie/zelda.txt";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(time(0));
    network fnet = load_network(fcfg, fweight, clear);
    network gnet = load_network(gcfg, gweight, clear);
    network anet = load_network(acfg, aweight, clear);
    char *gbase = basecfg(gcfg);
    char *abase = basecfg(acfg);

    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", gnet.learning_rate, gnet.momentum, gnet.decay);
    int imgs = gnet.batch*gnet.subdivisions;
    int i = *gnet.seen/imgs;
    data train, tbuffer;
    data style, sbuffer;


    list *slist = get_paths(style_images);
    char **spaths = (char **)list_to_array(slist);

    list *tlist = get_paths(train_images);
    char **tpaths = (char **)list_to_array(tlist);

    load_args targs= get_base_args(gnet);
    targs.paths = tpaths;
    targs.n = imgs;
    targs.m = tlist->size;
    targs.d = &tbuffer;
    targs.type = CLASSIFICATION_DATA;
    targs.classes = 1;
    char *ls[1] = {"zelda"};
    targs.labels = ls;

    load_args sargs = get_base_args(gnet);
    sargs.paths = spaths;
    sargs.n = imgs;
    sargs.m = slist->size;
    sargs.d = &sbuffer;
    sargs.type = CLASSIFICATION_DATA;
    sargs.classes = 1;
    sargs.labels = ls;

    pthread_t tload_thread = load_data_in_thread(targs);
    pthread_t sload_thread = load_data_in_thread(sargs);
    clock_t time;

    float aloss_avg = -1;
    float floss_avg = -1;

    network_state fstate = {};
    fstate.index = 0;
    fstate.net = fnet;
    int x_size = get_network_input_size(fnet)*fnet.batch;
    int y_size = get_network_output_size(fnet)*fnet.batch;
    fstate.input = cuda_make_array(0, x_size);
    fstate.truth = cuda_make_array(0, y_size);
    fstate.delta = cuda_make_array(0, x_size);
    fstate.train = 1;
    float *X = (float*)calloc(x_size, sizeof(float));
    float *y = (float*)calloc(y_size, sizeof(float));

    float *ones = cuda_make_array(0, anet.batch);
    float *zeros = cuda_make_array(0, anet.batch);
    fill_ongpu(anet.batch, .99, ones, 1);
    fill_ongpu(anet.batch, .01, zeros, 1);

    network_state astate = {};
    astate.index = 0;
    astate.net = anet;
    int ax_size = get_network_input_size(anet)*anet.batch;
    int ay_size = get_network_output_size(anet)*anet.batch;
    astate.input = 0;
    astate.truth = ones;
    astate.delta = cuda_make_array(0, ax_size);
    astate.train = 1;

    network_state gstate = {};
    gstate.index = 0;
    gstate.net = gnet;
    int gx_size = get_network_input_size(gnet)*gnet.batch;
    int gy_size = get_network_output_size(gnet)*gnet.batch;
    gstate.input = cuda_make_array(0, gx_size);
    gstate.truth = 0;
    gstate.delta = 0;
    gstate.train = 1;

    while (get_current_batch(gnet) < gnet.max_batches) {
        i += 1;
        time=clock();
        pthread_join(tload_thread, 0);
        pthread_join(sload_thread, 0);
        train = tbuffer;
        style = sbuffer;
        tload_thread = load_data_in_thread(targs);
        sload_thread = load_data_in_thread(sargs);

        printf("Loaded: %lf seconds\n", sec(clock()-time));

        data generated = copy_data(train);
        time=clock();

        int j, k;
        float floss = 0;
        for(j = 0; j < fnet.subdivisions; ++j){
            layer imlayer = gnet.layers[gnet.n - 1];
            get_next_batch(train, fnet.batch, j*fnet.batch, X, y);

            cuda_push_array(fstate.input, X, x_size);
            cuda_push_array(gstate.input, X, gx_size);
            *gnet.seen += gnet.batch;

            forward_network_gpu(fnet, fstate);
            float *feats = fnet.layers[fnet.n - 2].output_gpu;
            copy_ongpu(y_size, feats, 1, fstate.truth, 1);

            forward_network_gpu(gnet, gstate);
            float *gen = gnet.layers[gnet.n-1].output_gpu;
            copy_ongpu(x_size, gen, 1, fstate.input, 1);

            fill_ongpu(x_size, 0, fstate.delta, 1);
            forward_network_gpu(fnet, fstate);
            backward_network_gpu(fnet, fstate);
            //HERE

            astate.input = gen;
            fill_ongpu(ax_size, 0, astate.delta, 1);
            forward_network_gpu(anet, astate);
            backward_network_gpu(anet, astate);

            float *delta = imlayer.delta_gpu;
            fill_ongpu(x_size, 0, delta, 1);
            scal_ongpu(x_size, 100, astate.delta, 1);
            scal_ongpu(x_size, .00001, fstate.delta, 1);
            axpy_ongpu(x_size, 1, fstate.delta, 1, delta, 1);
            axpy_ongpu(x_size, 1, astate.delta, 1, delta, 1);

            //fill_ongpu(x_size, 0, delta, 1);
            //cuda_push_array(delta, X, x_size);
            //axpy_ongpu(x_size, -1, imlayer.output_gpu, 1, delta, 1);
            //printf("pix error: %f\n", cuda_mag_array(delta, x_size));
            printf("fea error: %f\n", cuda_mag_array(fstate.delta, x_size));
            printf("adv error: %f\n", cuda_mag_array(astate.delta, x_size));
            //axpy_ongpu(x_size, 1, astate.delta, 1, delta, 1);

            backward_network_gpu(gnet, gstate);

            floss += get_network_cost(fnet) /(fnet.subdivisions*fnet.batch);

            cuda_pull_array(imlayer.output_gpu, imlayer.output, x_size);
            for(k = 0; k < gnet.batch; ++k){
                int index = j*gnet.batch + k;
                copy_cpu(imlayer.outputs, imlayer.output + k*imlayer.outputs, 1, generated.X.vals[index], 1);
                generated.y.vals[index][0] = .01;
            }
        }

/*
        image sim = float_to_image(anet.w, anet.h, anet.c, style.X.vals[j]);
        show_image(sim, "style");
        cvWaitKey(0);
        */

        harmless_update_network_gpu(anet);

        data merge = concat_data(style, generated);
        randomize_data(merge);
        float aloss = train_network(anet, merge);

        update_network_gpu(gnet);

        free_data(merge);
        free_data(train);
        free_data(generated);
        free_data(style);
        if (aloss_avg < 0) aloss_avg = aloss;
        if (floss_avg < 0) floss_avg = floss;
        aloss_avg = aloss_avg*.9 + aloss*.1;
        floss_avg = floss_avg*.9 + floss*.1;

        printf("%d: gen: %f, adv: %f | gen_avg: %f, adv_avg: %f, %f rate, %lf seconds, %d images\n", i, floss, aloss, floss_avg, aloss_avg, get_current_rate(gnet), sec(clock()-time), i*imgs);
        if(i%1000==0){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, gbase, i);
            save_weights(gnet, buff);
            sprintf(buff, "%s/%s_%d.weights", backup_directory, abase, i);
            save_weights(anet, buff);
        }
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, gbase);
            save_weights(gnet, buff);
            sprintf(buff, "%s/%s.backup", backup_directory, abase);
            save_weights(anet, buff);
        }
    }
#endif
}
Example #13
0
/* IGameEventManager2::FireEvent post hook */
bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
{
	EventHook *pHook;
	EventInfo info;
	IChangeableForward *pForward;
	Handle_t hndl = 0;

	/* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
	if (!pEvent)
	{
		RETURN_META_VALUE(MRES_IGNORED, false);
	}

	pHook = m_EventStack.front();

	if (pHook != NULL)
	{
		pForward = pHook->pPostHook;

		if (pForward)
		{
			if (pHook->postCopy)
			{
				info.bDontBroadcast = bDontBroadcast;
				info.pEvent = m_EventCopies.front();
				info.pOwner = NULL;
				hndl = handlesys->CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);

				pForward->PushCell(hndl);
			} else {
				pForward->PushCell(BAD_HANDLE);
			}

			pForward->PushString(pHook->name.chars());
			pForward->PushCell(bDontBroadcast);
			pForward->Execute(NULL);

			if (pHook->postCopy)
			{
				/* Free handle */
				HandleSecurity sec(NULL, g_pCoreIdent);
				handlesys->FreeHandle(hndl, &sec);

				/* Free event structure */
				gameevents->FreeEvent(info.pEvent);
				m_EventCopies.pop();
			}
		}

		/* Decrement reference count, check if a delayed delete is needed */
		if (--pHook->refCount == 0)
		{
			assert(pHook->pPostHook == NULL);
			assert(pHook->pPreHook == NULL);
			m_EventHooks.remove(pHook->name.chars());
			delete pHook;
		}
	}

	m_EventStack.pop();

	RETURN_META_VALUE(MRES_IGNORED, true);
}
Example #14
0
/* IGameEventManager2::FireEvent hook */
bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
{
	EventHook *pHook;
	IChangeableForward *pForward;
	const char *name;
	cell_t res = Pl_Continue;
	bool broadcast = bDontBroadcast;

	/* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
	if (!pEvent)
	{
		RETURN_META_VALUE(MRES_IGNORED, false);
	}

	name = pEvent->GetName();
	
	if (m_EventHooks.retrieve(name, &pHook))
	{
		/* Push the event onto the event stack.  The reference count is increased to make sure 
		 * the structure is not garbage collected in between now and the post hook.
		 */
		pHook->refCount++;
		m_EventStack.push(pHook);

		pForward = pHook->pPreHook;

		if (pForward)
		{
			EventInfo info(pEvent, NULL);
			HandleSecurity sec(NULL, g_pCoreIdent);
			Handle_t hndl = handlesys->CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);

			info.bDontBroadcast = bDontBroadcast;

			EventForwardFilter filter(&info);

			pForward->PushCell(hndl);
			pForward->PushString(name);
			pForward->PushCell(bDontBroadcast);
			pForward->Execute(&res, &filter);

			broadcast = info.bDontBroadcast;

			handlesys->FreeHandle(hndl, &sec);
		}

		if (pHook->postCopy)
		{
			m_EventCopies.push(gameevents->DuplicateEvent(pEvent));
		}

		if (res)
		{
			gameevents->FreeEvent(pEvent);
			RETURN_META_VALUE(MRES_SUPERCEDE, false);
		}
	}
	else
	{
		m_EventStack.push(NULL);
	}

	if (broadcast != bDontBroadcast)
		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, true, &IGameEventManager2::FireEvent, (pEvent, broadcast));

	RETURN_META_VALUE(MRES_IGNORED, true);
}
Example #15
0
sec MyTimer::GetValue()
    {
    if(stop.u.HighPart!=start.u.HighPart) 
		return sec(-1.);
    return sec((stop.u.LowPart-start.u.LowPart)/freq);
    }
Example #16
0
VehicleObject *GameWorld::createVehicle(const uint16_t id, const glm::vec3& pos, const glm::quat& rot, GameObjectID gid)
{
	auto vti = data->findObjectType<VehicleData>(id);
	if( vti ) {
		logger->info("World", "Creating Vehicle ID " + std::to_string(id) + " (" + vti->gameName + ")");
		
		if(! vti->modelName.empty()) {
			data->loadDFF(vti->modelName + ".dff");
		}
		if(! vti->textureName.empty()) {
			data->loadTXD(vti->textureName + ".txd");
		}
		
		glm::u8vec3 prim(255), sec(128);
		auto palit = data->vehiclePalettes.find(vti->modelName); // modelname is conveniently lowercase (usually)
		if(palit != data->vehiclePalettes.end() && palit->second.size() > 0 ) {
			 std::uniform_int_distribution<int> uniform(0, palit->second.size()-1);
			 int set = uniform(randomEngine);
			 prim = data->vehicleColours[palit->second[set].first];
			 sec = data->vehicleColours[palit->second[set].second];
		}
		else {
			logger->warning("World", "No colour palette for vehicle " + vti->modelName);
		}
		
		auto wi = data->findObjectType<ObjectData>(vti->wheelModelID);
		if( wi )
		{
			if(! wi->textureName.empty()) {
				data->loadTXD(wi->textureName + ".txd");
			}
		}
		
		ModelRef& m = data->models[vti->modelName];
		auto model = m->resource;
		auto info = data->vehicleInfo.find(vti->handlingID);
		if(model && info != data->vehicleInfo.end()) {
			if( info->second->wheels.size() == 0 && info->second->seats.size() == 0 ) {
				for( const ModelFrame* f : model->frames ) {
					const std::string& name = f->getName();
					
					if( name.size() > 5 && name.substr(0, 5) == "wheel" ) {
						auto frameTrans = f->getMatrix();
						info->second->wheels.push_back({glm::vec3(frameTrans[3])});
					}
					if(name.size() > 3 && name.substr(0, 3) == "ped" && name.substr(name.size()-4) == "seat") {
						auto p = f->getDefaultTranslation();
						p.x = p.x * -1.f;
						info->second->seats.push_back({p});
						p.x = p.x * -1.f;
						info->second->seats.push_back({p});
					}
				}
			}
		}

		auto vehicle = new VehicleObject{ this, pos, rot, m, vti, info->second, prim, sec };
		vehicle->setGameObjectID(gid);

		vehiclePool.insert( vehicle );
        allObjects.push_back( vehicle );

		return vehicle;
	}
	return nullptr;
}
Example #17
0
void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
{
    int i = 0;
    network *net = load_network(filename, weightfile, 0);
    srand(time(0));

    list *options = read_data_cfg(datacfg);

    char *label_list = option_find_str(options, "labels", "data/labels.list");
    char *valid_list = option_find_str(options, "valid", "data/train.list");
    int classes = option_find_int(options, "classes", 2);
    int topk = option_find_int(options, "top", 1);

    char **labels = get_labels(label_list);
    list *plist = get_paths(valid_list);

    char **paths = (char **)list_to_array(plist);
    int m = plist->size;
    free_list(plist);

    clock_t time;
    float avg_acc = 0;
    float avg_topk = 0;
    int splits = m/1000;
    int num = (i+1)*m/splits - i*m/splits;

    data val, buffer;

    load_args args = {0};
    args.w = net->w;
    args.h = net->h;

    args.paths = paths;
    args.classes = classes;
    args.n = num;
    args.m = 0;
    args.labels = labels;
    args.d = &buffer;
    args.type = OLD_CLASSIFICATION_DATA;

    pthread_t load_thread = load_data_in_thread(args);
    for(i = 1; i <= splits; ++i){
        time=clock();

        pthread_join(load_thread, 0);
        val = buffer;

        num = (i+1)*m/splits - i*m/splits;
        char **part = paths+(i*m/splits);
        if(i != splits){
            args.paths = part;
            load_thread = load_data_in_thread(args);
        }
        printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));

        time=clock();
        float *acc = network_accuracies(net, val, topk);
        avg_acc += acc[0];
        avg_topk += acc[1];
        printf("%d: top 1: %f, top %d: %f, %lf seconds, %d images\n", i, avg_acc/i, topk, avg_topk/i, sec(clock()-time), val.X.rows);
        free_data(val);
    }
}
Example #18
0
int main(int argc, char *argv[])
{
  int n,m,k;
  int lda,ldb,ldc;
  double* a;
  double* b;
  double* c1;
  double* c2;
  struct timeval l_start, l_end;
  double l_total = 0.0;
  int reps, i, j;
  const int nblock = 16;
  double alpha = 1.0, beta = 1.0;
  char transa = 'N', transb = 'N';
  libxsmm_gemm_prefetch_type l_prefetch_op = LIBXSMM_PREFETCH_NONE;
  libxsmm_dmmfunction kernel = NULL;

  if (argc != 5) {
    fprintf(stderr, "Invalid ./a,out M N K reps\n");
    exit(-1);
  }

  m = atoi(argv[1]);
  n = atoi(argv[2]);
  k = atoi(argv[3]);
  reps = atoi(argv[4]);
  /* this is col-major what you want to use for the sizes in question */
  lda = m;
  ldb = k;
  ldc = m;

  if (n % nblock != 0) {
    fprintf(stderr, "N needs to be divisable by %i\n", nblock);
    exit(-1);
  }

  a  = (double*)_mm_malloc(lda*k*sizeof(double), 64);
  b  = (double*)_mm_malloc(ldb*n*sizeof(double), 64);
  c1 = (double*)_mm_malloc(ldc*n*sizeof(double), 64);
  c2 = (double*)_mm_malloc(ldc*n*sizeof(double), 64);

  #pragma omp parallel for
  for (i = 0; i < lda*k; i++) {
    a[i] = libxsmm_rand_f64();
  }

  #pragma omp parallel for
  for (i = 0; i < ldb*n; i++) {
    b[i] = libxsmm_rand_f64();
  }

  #pragma omp parallel for
  for (i = 0; i < ldc*n; i++) {
    c1[i] = 0;
    c2[i] = 0;
  }

  /* JIT Kernel */
  kernel = libxsmm_dmmdispatch(m, nblock, k, NULL, NULL, NULL, NULL, NULL, NULL, &l_prefetch_op );

  /* init MKL */
  dgemm(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c1, &ldc);

  #pragma omp parallel for
  for (i = 0; i < ldc*n; i++) {
    c1[i] = 0;
    c2[i] = 0;
  }

  gettimeofday(&l_start, NULL);
  for ( j = 0; j < reps; j++ ) {
    dgemm(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c1, &ldc);
  }
  gettimeofday(&l_end, NULL);
  l_total = sec(l_start, l_end);

  fprintf(stdout, "time[s] MKL     (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, l_total/(double)reps );
  fprintf(stdout, "GFLOPS  MKL     (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, (2.0 * (double)m * (double)n * (double)k * (double)reps * 1.0e-9) / l_total );
  fprintf(stdout, "GB/s    MKL     (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, ((double)sizeof(double) * (((double)m * (double)n) + ((double)k * (double)n)) * (double)reps * 1.0e-9) / l_total );

  gettimeofday(&l_start, NULL);
  for ( j = 0; j < reps; j++ ) {
    #pragma omp parallel for private(i)
    for ( i = 0; i < n; i+=nblock) {
      kernel( a, b+(ldb*i), c2+(ldc*i), NULL, NULL, NULL );
    }
    gettimeofday(&l_end, NULL);
  }
  l_total = sec(l_start, l_end);

  fprintf(stdout, "time[s] libxsmm (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, l_total/(double)reps );
  fprintf(stdout, "GFLOPS  libxsmm (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, (2.0 * (double)m * (double)n * (double)k * (double)reps * 1.0e-9) / l_total );
  fprintf(stdout, "GB/s    libxsmm (CM, M=%i, N=%i, K=%i): %f\n", m, n, k, ((double)sizeof(double) * (((double)m * (double)n) + ((double)k * (double)n)) * (double)reps * 1.0e-9) / l_total );

  /* test result */
  double max_error = 0.0;
  for ( i = 0; i < ldc*n; i++) {
    if (max_error < fabs(c1[i] - c2[i])) {
      max_error = fabs(c1[i] - c2[i]);
    }
  }
  printf("max error: %f\n\n", max_error);
}
Example #19
0
void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
{
    int curr = 0;
    network *net = load_network(cfgfile, weightfile, 0);
    srand(time(0));

    list *options = read_data_cfg(datacfg);

    char *test_list = option_find_str(options, "test", "data/test.list");
    int classes = option_find_int(options, "classes", 2);

    list *plist = get_paths(test_list);

    char **paths = (char **)list_to_array(plist);
    int m = plist->size;
    free_list(plist);

    clock_t time;

    data val, buffer;

    load_args args = {0};
    args.w = net->w;
    args.h = net->h;
    args.paths = paths;
    args.classes = classes;
    args.n = net->batch;
    args.m = 0;
    args.labels = 0;
    args.d = &buffer;
    args.type = OLD_CLASSIFICATION_DATA;

    pthread_t load_thread = load_data_in_thread(args);
    for(curr = net->batch; curr < m; curr += net->batch){
        time=clock();

        pthread_join(load_thread, 0);
        val = buffer;

        if(curr < m){
            args.paths = paths + curr;
            if (curr + net->batch > m) args.n = m - curr;
            load_thread = load_data_in_thread(args);
        }
        fprintf(stderr, "Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));

        time=clock();
        matrix pred = network_predict_data(net, val);

        int i, j;
        if (target_layer >= 0){
            //layer l = net->layers[target_layer];
        }

        for(i = 0; i < pred.rows; ++i){
            printf("%s", paths[curr-net->batch+i]);
            for(j = 0; j < pred.cols; ++j){
                printf("\t%g", pred.vals[i][j]);
            }
            printf("\n");
        }

        free_matrix(pred);

        fprintf(stderr, "%lf seconds, %d images, %d total\n", sec(clock()-time), val.X.rows, curr);
        free_data(val);
    }
}
Example #20
0
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];
    sprintf(buff, "/home/pjreddie/go.train.%02d", rand()%10);
    data train = load_go(buff);

    int N = train.X.rows;
    int epoch = (*net.seen)/N;
    while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
        clock_t time=clock();

        data batch = get_random_data(train, net.batch);
        int i;
        for(i = 0; i < batch.X.rows; ++i){
            int flip = rand()%2;
            int rotate = rand()%4;
            image in = float_to_image(19, 19, 1, batch.X.vals[i]);
            image out = float_to_image(19, 19, 1, batch.y.vals[i]);
            //show_image_normalized(in, "in");
            //show_image_normalized(out, "out");
            if(flip){
                flip_image(in);
                flip_image(out);
            }
            rotate_image_cw(in, rotate);
            rotate_image_cw(out, rotate);
            //show_image_normalized(in, "in2");
            //show_image_normalized(out, "out2");
            //cvWaitKey(0);
        }
        float loss = train_network(net, batch);
        free_data(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);

            free_data(train);
            sprintf(buff, "/home/pjreddie/go.train.%02d", epoch%10);
            train = load_go(buff);
        }
        if(get_current_batch(net)%100 == 0){
            char buff[256];
            sprintf(buff, "%s/%s.backup",backup_directory,base);
            save_weights(net, buff);
        }
    }
    sprintf(buff, "%s/%s.weights", backup_directory, base);
    save_weights(net, buff);

    free_network(net);
    free(base);
    free_data(train);
}
Example #21
0
/*
 * Main program
 */
int
main(
    int	argc,		/* command line options */
    char	**argv		/* poiniter to list of tokens */
)
{
    struct timeval tv;	/* system clock at startup */
    audio_info_t info;	/* Sun audio structure */
    struct tm *tm = NULL;	/* structure returned by gmtime */
    char	device[50];	/* audio device */
    char	code[100];	/* timecode */
    int	rval, temp, arg, sw, ptr;
    int	minute, hour, day, year;
    int	i;

    /*
     * Parse options
     */
    strcpy(device, DEVICE);
    year = 0;
    while ((temp = getopt(argc, argv, "a:dhilsu:v:y:")) != -1) {
        switch (temp) {

        case 'a':	/* specify audio device (/dev/audio) */
            strcpy(device, optarg);
            break;

        case 'd':	/* set DST for summer (WWV/H only) */
            dst++;
            break;

        case 'h':	/* select WWVH sync frequency */
            tone = 1200;
            break;

        case 'i':	/* select irig format */
            encode = IRIG;
            break;

        case 'l':	/* set leap warning bit (WWV/H only) */
            leap++;
            break;

        case 's':	/* enable speaker */
            port |= AUDIO_SPEAKER;
            break;

        case 'u':	/* set DUT1 offset (-7 to +7) */
            sscanf(optarg, "%d", &dut1);
            if (dut1 < 0)
                dut1 = abs(dut1);
            else
                dut1 |= 0x8;
            break;

        case 'v':	/* set output level (0-255) */
            sscanf(optarg, "%d", &level);
            break;

        case 'y':	/* set initial date and time */
            sscanf(optarg, "%2d%3d%2d%2d", &year, &day,
                   &hour, &minute);
            utc++;
            break;

defult:
            printf("invalid option %c\n", temp);
            break;
        }
    }

    /*
     * Open audio device and set options
     */
    fd = open("/dev/audio", O_WRONLY);
    if (fd <= 0) {
        printf("audio open %s\n", strerror(errno));
        exit(1);
    }
    rval = ioctl(fd, AUDIO_GETINFO, &info);
    if (rval < 0) {
        printf("audio control %s\n", strerror(errno));
        exit(0);
    }
    info.play.port = port;
    info.play.gain = level;
    info.play.sample_rate = SECOND;
    info.play.channels = 1;
    info.play.precision = 8;
    info.play.encoding = AUDIO_ENCODING_ULAW;
    printf("port %d gain %d rate %d chan %d prec %d encode %d\n",
           info.play.port, info.play.gain, info.play.sample_rate,
           info.play.channels, info.play.precision,
           info.play.encoding);
    ioctl(fd, AUDIO_SETINFO, &info);

    /*
     * Unless specified otherwise, read the system clock and
     * initialize the time.
     */
    if (!utc) {
        gettimeofday(&tv, NULL);
        tm = gmtime(&tv.tv_sec);
        minute = tm->tm_min;
        hour = tm->tm_hour;
        day = tm->tm_yday + 1;
        year = tm->tm_year % 100;
        second = tm->tm_sec;

        /*
         * Delay the first second so the generator is accurately
         * aligned with the system clock within one sample (125
         * microseconds ).
         */
        delay(SECOND - tv.tv_usec * 8 / 1000);
    }
    memset(code, 0, sizeof(code));
    switch (encode) {

    /*
     * For WWV/H and default time, carefully set the signal
     * generator seconds number to agree with the current time.
     */
    case WWV:
        printf("year %d day %d time %02d:%02d:%02d tone %d\n",
               year, day, hour, minute, second, tone);
        sprintf(code, "%01d%03d%02d%02d%01d", year / 10, day,
                hour, minute, year % 10);
        printf("%s\n", code);
        ptr = 8;
        for (i = 0; i <= second; i++) {
            if (progx[i].sw == DEC)
                ptr--;
        }
        break;

    /*
     * For IRIG the signal generator runs every second, so requires
     * no additional alignment.
     */
    case IRIG:
        printf("sbs %x year %d day %d time %02d:%02d:%02d\n",
               0, year, day, hour, minute, second);
        break;
    }

    /*
     * Run the signal generator to generate new timecode strings
     * once per minute for WWV/H and once per second for IRIG.
     */
    while(1) {

        /*
         * Crank the state machine to propagate carries to the
         * year of century. Note that we delayed up to one
         * second for alignment after reading the time, so this
         * is the next second.
         */
        second = (second + 1) % 60;
        if (second == 0) {
            minute++;
            if (minute >= 60) {
                minute = 0;
                hour++;
            }
            if (hour >= 24) {
                hour = 0;
                day++;
            }

            /*
             * At year rollover check for leap second.
             */
            if (day >= (year & 0x3 ? 366 : 367)) {
                if (leap) {
                    sec(DATA0);
                    printf("\nleap!");
                    leap = 0;
                }
                day = 1;
                year++;
            }
            if (encode == WWV) {
                sprintf(code, "%01d%03d%02d%02d%01d",
                        year / 10, day, hour, minute, year %
                        10);
                printf("\n%s\n", code);
                ptr = 8;
            }
        }
        if (encode == IRIG) {
            sprintf(code, "%04x%04d%06d%02d%02d%02d", 0,
                    year, day, hour, minute, second);
            printf("%s\n", code);
            ptr = 19;
        }

        /*
         * Generate data for the second
         */
        switch(encode) {

        /*
         * The IRIG second consists of 20 BCD digits of width-
         * modulateod pulses at 2, 5 and 8 ms and modulated 50
         * percent on the 1000-Hz carrier.
         */
        case IRIG:
            for (i = 0; i < 100; i++) {
                if (i < 10) {
                    sw = progz[i].sw;
                    arg = progz[i].arg;
                } else {
                    sw = progy[i % 10].sw;
                    arg = progy[i % 10].arg;
                }
                switch(sw) {

                case COEF:	/* send BCD bit */
                    if (code[ptr] & arg) {
                        peep(M5, 1000, HIGH);
                        peep(M5, 1000, LOW);
                        printf("1");
                    } else {
                        peep(M2, 1000, HIGH);
                        peep(M8, 1000, LOW);
                        printf("0");
                    }
                    break;

                case DEC:	/* send IM/PI bit */
                    ptr--;
                    printf(" ");
                    peep(arg, 1000, HIGH);
                    peep(10 - arg, 1000, LOW);
                    break;

                case MIN:	/* send data bit */
                    peep(arg, 1000, HIGH);
                    peep(10 - arg, 1000, LOW);
                    printf("M ");
                    break;
                }
                if (ptr < 0)
                    break;
            }
            printf("\n");
            break;

        /*
         * The WWV/H second consists of 9 BCD digits of width-
         * modulateod pulses 200, 500 and 800 ms at 100-Hz.
         */
        case WWV:
            sw = progx[second].sw;
            arg = progx[second].arg;
            switch(sw) {

            case DATA:		/* send data bit */
                sec(arg);
                break;

            case COEF:		/* send BCD bit */
                if (code[ptr] & arg) {
                    sec(DATA1);
                    printf("1");
                } else {
                    sec(DATA0);
                    printf("0");
                }
                break;

            case LEAP:		/* send leap bit */
                if (leap) {
                    sec(DATA1);
                    printf("L ");
                } else {
                    sec(DATA0);
                    printf("  ");
                }
                break;

            case DEC:		/* send data bit */
                ptr--;
                sec(arg);
                printf(" ");
                break;

            case MIN:		/* send minute sync */
                peep(arg, tone, HIGH);
                peep(1000 - arg, tone, OFF);
                break;

            case DUT1:		/* send DUT1 bits */
                if (dut1 & arg)
                    sec(DATA1);
                else
                    sec(DATA0);
                break;

            case DST1:		/* send DST1 bit */
                ptr--;
                if (dst)
                    sec(DATA1);
                else
                    sec(DATA0);
                printf(" ");
                break;

            case DST2:		/* send DST2 bit */
                if (dst)
                    sec(DATA1);
                else
                    sec(DATA0);
                break;
            }
        }
    }
}
Example #22
0
int
main(int argc, char **argv)
{
    cl_uint num;
    cl_int err;
    int platform_idx = -1;
    cl_platform_id *plat_ids;
    int i;
    size_t sz;
    cl_device_id *gpu_devs;
    cl_context_properties cps[3];
    cl_context context;
    int opt;
    char *input;
    int run_size = 1024;
    struct AIISA_Program prog;
    cl_command_queue queue;
    int ei;
    int nloop = 16;
    struct AIISA_CodeBuffer buf;

    aiisa_code_buffer_init(&buf);

    clGetPlatformIDs(0, NULL, &num);

    plat_ids = (cl_platform_id*)malloc(sizeof(*plat_ids) * num);
    clGetPlatformIDs(num, plat_ids, NULL);

    while ((opt = getopt(argc, argv, "n:")) != -1) {
        switch (opt) {
        case 'n':
            run_size = atoi(optarg);
            break;

        default:
            puts("usage : run in.cl");
            return 1;
        }
    }

    if (optind >= argc) {
        puts("usage : run in.cl");
        return 1;
    }

    input = argv[optind];

    for (i=0; i<(int)num; i++) {
        char name[1024];
        size_t len;
        clGetPlatformInfo(plat_ids[i], CL_PLATFORM_VENDOR, sizeof(name), name, &len);

        //puts(name);
        if (strcmp(name, "Advanced Micro Devices, Inc.") == 0) {
            platform_idx = i;
            break;
        }
    }

    if (platform_idx == -1) {
        puts("no amd");
        return -1;
    }

    clGetDeviceIDs(plat_ids[platform_idx], CL_DEVICE_TYPE_GPU, 0, NULL, &num);
    if (num == 0) {
        puts("no gpu");
        return -1;
    }

    gpu_devs = (cl_device_id*)malloc(sizeof(gpu_devs[0]) * 1);
    //clGetDeviceIDs(plat_ids[platform_idx], CL_DEVICE_TYPE_GPU, num, gpu_devs, NULL);

    cps[0] = CL_CONTEXT_PLATFORM;
    cps[1] = (cl_context_properties)plat_ids[platform_idx];
    cps[2] = 0;

    context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &err);
    clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(gpu_devs), gpu_devs, &sz);

    queue = clCreateCommandQueue(context, gpu_devs[0], 0, NULL);

    {
        char name[1024];
        size_t sz;
        clGetDeviceInfo(gpu_devs[0], CL_DEVICE_NAME, sizeof(name), name, &sz);

        puts(name);
    }

    //puts(input);

    aiisa_build_binary_from_cl(&prog, context, gpu_devs[0], input);

    for (ei=0; ei<nloop; ei++) {
        cl_program cl_prog;
        const unsigned char *bin[1];
        size_t bin_size[1];
        cl_kernel ker;
        cl_mem in, out;
        size_t global_size[3];
        double tb, te;

        tb = sec();
        gen_code(&prog, &buf);
        bin[0] = prog.cl_binary;
        bin_size[0] = prog.size;
        cl_prog = clCreateProgramWithBinary(context, 1, gpu_devs, bin_size, bin, NULL, NULL);
        clBuildProgram(cl_prog, 1, gpu_devs, NULL, NULL, NULL);
        ker = clCreateKernel(cl_prog, "f", &err);
        te = sec();
        printf("build : %f[usec]\n", (te-tb)*1000000);

        in = clCreateBuffer(context, CL_MEM_READ_WRITE, run_size * sizeof(int), NULL, &err);
        out = clCreateBuffer(context, CL_MEM_READ_WRITE, run_size * sizeof(int), NULL, &err);

        clSetKernelArg(ker, 0, sizeof(cl_mem), &in);
        clSetKernelArg(ker, 1, sizeof(cl_mem), &out);


        {
            int *ptr = (int*)clEnqueueMapBuffer(queue, in, CL_TRUE, CL_MAP_WRITE, 0, run_size*sizeof(int), 0, NULL, NULL, NULL);
            int i;
            for (i=0; i<run_size; i++) {
                ptr[i] = i;
            }
            clEnqueueUnmapMemObject(queue, in, ptr, 0, NULL, NULL);
        }

        {
            int *ptr = (int*)clEnqueueMapBuffer(queue, out, CL_TRUE, CL_MAP_WRITE, 0, run_size*sizeof(int), 0, NULL, NULL, NULL);
            int i;
            for (i=0; i<run_size; i++) {
                ptr[i] = 0xdeadbeef;
            }
            clEnqueueUnmapMemObject(queue, out, ptr, 0, NULL, NULL);
        }

        err = clFinish(queue);

        global_size[0] = run_size;
        err = clEnqueueNDRangeKernel(queue, ker, 1, NULL, global_size, NULL, 0, NULL, NULL);
        if (err != CL_SUCCESS) {
            puts("enqueue nd");
        }
        err = clFinish(queue);
        if (err != CL_SUCCESS) {
            puts("fini");
        }

        if (ei == 0) {
            int *ptr = (int*)clEnqueueMapBuffer(queue, out, CL_TRUE, CL_MAP_READ, 0, run_size*sizeof(int), 0, NULL, NULL, NULL);
            int i;
            for (i=0; i<run_size; i++) {
                printf("%d : %x\n", i, ptr[i]);
            }
            clEnqueueUnmapMemObject(queue, in, ptr, 0, NULL, NULL);
        }

        err = clFinish(queue);

        clReleaseMemObject(in);
        clReleaseMemObject(out);
        clReleaseKernel(ker);
        clReleaseProgram(cl_prog);
    }

    return 0;
}
Example #23
0
HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token)
{
	HandleSecurity sec(token, g_pCoreIdent);
	return g_HandleSys.FreeHandle(hndl, &sec);
}
Example #24
0
MStatus AbcImport::doIt(const MArgList & args)
{
    MStatus status;

    MArgParser argData(syntax(), args, &status);

    MString filename("");
    MString connectRootNodes("");
    MString filterString("");
    MString excludeFilterString("");

    MObject reparentObj = MObject::kNullObj;

    bool    swap = false;
    bool    createIfNotFound = false;
    bool    removeIfNoUpdate = false;

    bool    debugOn = false;

    if (argData.isFlagSet("help"))
    {
        MGlobal::displayInfo(usage);
        return status;
    }

    if (argData.isFlagSet("debug"))
        debugOn = true;

    if (argData.isFlagSet("reparent"))
    {
        MString parent("");
        MDagPath reparentDagPath;
        status = argData.getFlagArgument("reparent", 0, parent);
        if (status == MS::kSuccess
            && getDagPathByName(parent, reparentDagPath) == MS::kSuccess)
        {
            reparentObj = reparentDagPath.node();
        }
        else
        {
            MString theWarning = parent;
            theWarning += MString(" is not a valid DagPath");
            printWarning(theWarning);
        }
    }

    if (!argData.isFlagSet("connect") && argData.isFlagSet("mode"))
    {
        MString modeStr;
        argData.getFlagArgument("mode", 0, modeStr);
        if (modeStr == "replace")
            deleteCurrentSelection();
        else if (modeStr == "open")
        {
            MFileIO fileIo;
            fileIo.newFile(true);
        }
    }
    else if (argData.isFlagSet("connect"))
    {
        swap = true;
        argData.getFlagArgument("connect", 0, connectRootNodes);

        if (argData.isFlagSet("createIfNotFound"))
        {
            createIfNotFound = true;
        }

        if (argData.isFlagSet("removeIfNoUpdate"))
            removeIfNoUpdate = true;
    }

    if (argData.isFlagSet("filterObjects"))
    {
        argData.getFlagArgument("filterObjects", 0, filterString);
    }

    if (argData.isFlagSet("excludeFilterObjects"))
    {
        argData.getFlagArgument("excludeFilterObjects", 0, excludeFilterString);
    }

    // if the flag isn't specified we'll only do stuff marked with the Maya
    // meta data
    bool recreateColorSets = false;
    if (argData.isFlagSet("recreateAllColorSets"))
    {
        recreateColorSets = true;
    }

    status = argData.getCommandArgument(0, filename);
    MString abcNodeName;
    if (status == MS::kSuccess)
    {
        {
            MString fileRule, expandName;
            MString alembicFileRule = "alembicCache";
            MString alembicFilePath = "cache/alembic";

            MString queryFileRuleCmd;
            queryFileRuleCmd.format("workspace -q -fre \"^1s\"",
                alembicFileRule);
            MString queryFolderCmd;
            queryFolderCmd.format("workspace -en `workspace -q -fre \"^1s\"`",
                alembicFileRule);

            // query the file rule for alembic cache
            MGlobal::executeCommand(queryFileRuleCmd, fileRule);
            if (fileRule.length() > 0)
            {
                // we have alembic file rule, query the folder
                MGlobal::executeCommand(queryFolderCmd, expandName);
            }

            // resolve the expanded file rule
            if (expandName.length() == 0)
            {
                expandName = alembicFilePath;
            }

            // get the path to the alembic file rule
            MFileObject directory;
            directory.setRawFullName(expandName);
            MString directoryName = directory.resolvedFullName();

            // resolve the relative path
            MFileObject absoluteFile;
            absoluteFile.setRawFullName(filename);
#if MAYA_API_VERSION < 201300
            if (absoluteFile.resolvedFullName() != 
                absoluteFile.expandedFullName())
            {
#else
            if (!MFileObject::isAbsolutePath(filename)) {
#endif
                // this is a relative path
                MString absoluteFileName = directoryName + "/" + filename;
                absoluteFile.setRawFullName(absoluteFileName);
                filename = absoluteFile.resolvedFullName();
            }
            else
            {
                filename = absoluteFile.resolvedFullName();
            }
        }

        MFileObject fileObj;
        status = fileObj.setRawFullName(filename);
        if (status == MS::kSuccess && fileObj.exists())
        {
            ArgData inputData(filename, debugOn, reparentObj,
                swap, connectRootNodes, createIfNotFound, removeIfNoUpdate,
                recreateColorSets, filterString, excludeFilterString);
            abcNodeName = createScene(inputData);

            if (inputData.mSequenceStartTime != inputData.mSequenceEndTime &&
                inputData.mSequenceStartTime != -DBL_MAX &&
                inputData.mSequenceEndTime != DBL_MAX)
            {
                if (argData.isFlagSet("fitTimeRange"))
                {
                    MTime sec(1.0, MTime::kSeconds);
                    setPlayback(
                        inputData.mSequenceStartTime * sec.as(MTime::uiUnit()),
                        inputData.mSequenceEndTime * sec.as(MTime::uiUnit()) );
                }

                if (argData.isFlagSet("setToStartFrame"))
                {
                    MTime sec(1.0, MTime::kSeconds);
                    MGlobal::viewFrame( inputData.mSequenceStartTime *
                        sec.as(MTime::uiUnit()) );
                }
            }
        }
        else
        {
            MString theError("In AbcImport::doIt(), ");
            theError += filename;
            theError += MString(" doesn't exist");
            printError(theError);
        }
    }

    MPxCommand::setResult(abcNodeName);

    return status;
}
Example #25
0
int main()
{
	char cstr1[100] = "Timer1";
	char cstr2[100] = "Timer2";
    Tx_TimerHandle *pTimeHandle;
    void *p_retval;
    pthread_t a_thread;
    int iRet = 0;
    int isec = 0;
    int i = 0;
   
    printf("------ Run Timer1 once ------ \n");
	pTimeHandle = Tx_TimerCreate("Time1", sec(1), 0, false, threadFunction1, cstr1);
    sleep(5);
	Tx_TimerActivate(pTimeHandle);
    sleep(3);
    Tx_TimerDelete(pTimeHandle);  
    
    printf("------ Run Timer2 multi times ------ \n");
	pTimeHandle = Tx_TimerCreate("Time2", sec(3), sec(3), true, threadFunction2, cstr2);
    sleep(15);
    Tx_TimerDelete(pTimeHandle);    
    sleep(5);

    printf("------ Create one thread and Timer3 thread to monitor (without timeout) ------ \n");
    pthread_create(&a_thread, NULL, (void *)&threadFunction3, (void*) &isec);
    iRet = pthread_join(a_thread, &p_retval);	
    if(iRet == 0 && p_retval != NULL)
    {
        sAAASTR *p_param =(sAAASTR*) p_retval;  
        printf("Thread output [%s:%d]\n", p_param->str, p_param->a);
        free(p_param);
    }    
   
    printf("------ Create one thread and Timer4 thread to monitor (with timeout) ------ \n");
    pthread_create(&a_thread, NULL, (void *)&threadFunction4, (void*) &isec);
	for (i=0; i<5; i++) 
	{ 
		sleep(1); 
		printf("count=%lld\n", count); 
	}         
    iRet = pthread_join(a_thread, &p_retval);    
	for (i=0; i<5; i++) 
	{ 
		sleep(1); 
		printf("count=%lld\n", count); 
	} 


    if((intptr_t)  0xffffffffffffffff == (intptr_t)  p_retval)
    {
        printf("Thread timeout.... \n");    
    }
    else
    {
        int *a = ((int*) p_retval);
        printf("Thread run successfully, retval:%d\n", *a);
        free(p_retval);
    }
    
    printf("Done.......\n");    
	return 0;		
  
}
Example #26
0
void train_classifier(char *datacfg, 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 = 1024;

    list *options = read_data_cfg(datacfg);

    char *backup_directory = option_find_str(options, "backup", "/backup/");
    char *label_list = option_find_str(options, "labels", "data/labels.list");
    char *train_list = option_find_str(options, "train", "data/train.list");
    int classes = option_find_int(options, "classes", 2);

    char **labels = get_labels(label_list);
    list *plist = get_paths(train_list);
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
    int N = plist->size;
    clock_t time;
    pthread_t load_thread;
    data train;
    data buffer;

    load_args args = {0};
    args.w = net.w;
    args.h = net.h;

    args.min = net.w;
    args.max = net.max_crop;
    args.size = net.w;

    args.paths = paths;
    args.classes = classes;
    args.n = imgs;
    args.m = N;
    args.labels = labels;
    args.d = &buffer;
    args.type = CLASSIFICATION_DATA;

    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();

/*
        int u;
        for(u = 0; u < net.batch; ++u){
            image im = float_to_image(net.w, net.h, 3, train.X.vals[u]);
            show_image(im, "loaded");
            cvWaitKey(0);
        }
        */

        float loss = train_network(net, train);
        if(avg_loss == -1) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;
        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);
        free_data(train);
        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(*net.seen%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);

    pthread_join(load_thread, 0);
    free_data(buffer);
    free_network(net);
    free_ptrs((void**)labels, classes);
    free_ptrs((void**)paths, plist->size);
    free_list(plist);
    free(base);
}
Example #27
0
static int do_reload(int do_fork) {
  int r;
  char ibuf[150];
  int ip;
  struct dataset *ds;
  struct zone *zone;
  pid_t cpid = 0;	/* child pid; =0 to make gcc happy */
  int cfd = 0;		/* child stats fd; =0 to make gcc happy */
#ifndef NO_TIMES
  struct tms tms;
  clock_t utm, etm;
#ifndef HZ
  static clock_t HZ;
#endif
#endif /* NO_TIMES */

  ds = nextdataset2reload(NULL);
  if (!ds && call_hook(reload_check, (zonelist)) == 0) {
    check_expires();
    return 1;	/* nothing to reload */
  }

  if (do_fork) {
    int pfd[2];
    if (flog && !flushlog)
      fflush(flog);
    /* forking reload. if anything fails, just do a non-forking one */
    if (pipe(pfd) < 0)
      do_fork = 0;
    else if ((cpid = fork()) < 0) {	/* fork failed, close the pipe */
      close(pfd[0]);
      close(pfd[1]);
      do_fork = 0;
    }
    else if (!cpid) {	/* child, continue answering queries */
      signal(SIGALRM, SIG_IGN);
      signal(SIGHUP, SIG_IGN);
#ifndef NO_STATS
      signal(SIGUSR1, SIG_IGN);
      signal(SIGUSR2, SIG_IGN);
#endif
      close(pfd[0]);
      /* set up the fd#1 to write stats later on SIGTERM */
      if (pfd[1] != 1) {
        dup2(pfd[1], 1);
        close(pfd[1]);
      }
      fork_on_reload = -1;
      return 1;
    }
    else {
      close(pfd[1]);
      cfd = pfd[0];
    }
  }

#ifndef NO_TIMES
#ifndef HZ
  if (!HZ)
    HZ = sysconf(_SC_CLK_TCK);
#endif
  etm = times(&tms);
  utm = tms.tms_utime;
#endif /* NO_TIMES */

  r = 1;
  while(ds) {
    if (!loaddataset(ds))
      r = 0;
    ds = nextdataset2reload(ds);
  }

  for (zone = zonelist; zone; zone = zone->z_next) {
    time_t stamp = 0;
    time_t expires = 0;
    const struct dssoa *dssoa = NULL;
    const struct dsns *dsns = NULL;
    unsigned nsttl = 0;
    struct dslist *dsl;

    for(dsl = zone->z_dsl; dsl; dsl = dsl->dsl_next) {
      const struct dataset *ds = dsl->dsl_ds;
      if (!ds->ds_stamp) {
        stamp = 0;
        break;
      }
      if (stamp < ds->ds_stamp)
        stamp = ds->ds_stamp;
      if (ds->ds_expires && (!expires || expires > ds->ds_expires))
        expires = ds->ds_expires;
      if (!dssoa)
        dssoa = ds->ds_dssoa;
      if (!dsns)
        dsns = ds->ds_dsns, nsttl = ds->ds_nsttl;
    }

    zone->z_expires = expires;
    zone->z_stamp = stamp;
    if (!stamp) {
      zlog(LOG_WARNING, zone,
           "not all datasets are loaded, zone will not be serviced");
      r = 0;
    }
    else if (!update_zone_soa(zone, dssoa) ||
             !update_zone_ns(zone, dsns, nsttl, zonelist))
      zlog(LOG_WARNING, zone,
           "NS or SOA RRs are too long, will be ignored");
  }

  if (call_hook(reload, (zonelist)) != 0)
    r = 0;

  ip = ssprintf(ibuf, sizeof(ibuf), "zones reloaded");
#ifndef NO_TIMES
  etm = times(&tms) - etm;
  utm = tms.tms_utime - utm;
# define sec(tm) (unsigned long)(tm/HZ), (unsigned long)((tm*100/HZ)%100)
  ip += ssprintf(ibuf + ip, sizeof(ibuf) - ip,
        ", time %lu.%lue/%lu.%luu sec", sec(etm), sec(utm));
# undef sec
#endif /* NO_TIMES */
#ifndef NO_MEMINFO
  {
    struct mallinfo mi = mallinfo();
# define kb(x) ((mi.x + 512)>>10)
    ip += ssprintf(ibuf + ip, sizeof(ibuf) - ip,
          ", mem arena=%d free=%d mmap=%d Kb",
          kb(arena), kb(fordblks), kb(hblkhd));
# undef kb
  }
#endif /* NO_MEMINFO */
  dslog(LOG_INFO, 0, ibuf);

  check_expires();

  /* ok, (something) loaded. */

  if (do_fork) {
    /* here we should notify query-answering child (send SIGTERM to it),
     * and wait for it to complete.
     * Unfortunately at least on linux, the SIGTERM sometimes gets ignored
     * by the child process, so we're trying several times here, in a loop.
     */
    int s, n;
    fd_set fds;
    struct timeval tv;

    for(n = 1; ++n;) {
      if (kill(cpid, SIGTERM) != 0)
        dslog(LOG_WARNING, 0, "kill(qchild): %s", strerror(errno));
      FD_ZERO(&fds);
      FD_SET(cfd, &fds);
      tv.tv_sec = 0;
      tv.tv_usec = 500000;
      s = select(cfd+1, &fds, NULL, NULL, &tv);
      if (s > 0) break;
      dslog(LOG_WARNING, 0, "waiting for qchild process: %s, retrying",
            s ? strerror(errno) : "timeout");
    }
    ipc_read_stats(cfd);
    close(cfd);
    wait(&s);
  }

  return r;
}
Example #28
0
int main(int argc, char* argv[]) {

		int err=parseCmdLine(argc, argv);
		if (err>0){
		   std::cout<<"Wrong command line argument: \""<<argv[err]<<'\"'<<std::endl;
		   printHelp();
		   exit(-1);
		}

		/**********************************************************************/
		
	

		t_function funct(SHA1);
		t_cipher alg(parseCipherAlgorithm());

		int keylength(0), blocksize(0);

		switch (alg){
		  case AES:  
			  keylength = CryptoPP::AES::DEFAULT_KEYLENGTH; 
			  blocksize = CryptoPP::AES::BLOCKSIZE; 
			  break;
		  case GOST_28147_89 :
			  keylength = CryptoPP::GOST::DEFAULT_KEYLENGTH; 
			  blocksize = CryptoPP::GOST::BLOCKSIZE; 
			  break;
		}
		
		securityInfo sec(keylength,blocksize);
		sec.setPassword(cmdkey[key_pass].value);


		CipherMode *pCipherMode(NULL);


		t_mode mode(parseCipherMode());
		switch (mode){
		case	ECB : pCipherMode = new ECBCipherMode(alg); break;
		case    CBC : pCipherMode = new CBCCipherMode(alg); break; 
		case    CFB : pCipherMode = new CFBCipherMode(alg); break; 
		//case    OFB : pCipherMode = new OFBCipherMode(alg); break;

		}

	
		pCipherMode->AttachSecurityInfo(&sec);

		IOData data;


		if (cmdkey[key_enc].wasUsed) {
		   //Encryption
		   data.loadPlainText(cmdkey[key_inp].value);
		   pCipherMode->Encrypt(data);
		   data.saveBinary(cmdkey[key_out].value,sec,alg,mode,funct);
		}
		if (cmdkey[key_dec].wasUsed){
		   //Decryption
           data.loadBinary(cmdkey[key_inp].value,sec,alg,mode,funct);

		   pCipherMode->Decrypt(data);
		   data.savePlainText(cmdkey[key_out].value);
		}


		delete pCipherMode;

		/*system("pause");*/
return 0;
}
Example #29
0
void train_yolo(char *cfgfile, char *weightfile)
{
    char *train_images = "/data/voc/train.txt";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(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;

    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);
}
void Client::main_loop( ) { 
	MSG msg;

	time_mgr.end_record( RecordStrings::FRAME );

	time_mgr.begin_record( RecordStrings::UPDATE_PRE );
	if( time_mgr.get_record_curr( RecordStrings::FRAME ) > TIME_FRAME_MILLI / 1000.0f ) {
		if( time_mgr.get_record_curr( RecordStrings::FRAME ) > TIME_MILLISEC / 4.0f ) {
			time_mgr.add_time( TimeStrings::GAME_ACCUM, TIME_MILLISEC / 4.0f );
		}
		else {
			time_mgr.add_time( TimeStrings::GAME_ACCUM, time_mgr.get_record_curr( RecordStrings::FRAME ) );
		}

		//time_mgr.add_time( TimeStrings::RENDER_ACCUM, time_mgr.get_record_curr( RecordStrings::FRAME ) );

		time_mgr.push_record( RecordStrings::FRAME );
		time_mgr.begin_record( RecordStrings::FRAME );
	}

	while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}

	time_mgr.end_record( RecordStrings::UPDATE_PRE );
	time_mgr.push_record( RecordStrings::UPDATE_PRE );

	time_mgr.begin_record( RecordStrings::UPDATE );

	while( time_mgr.get_time( TimeStrings::GAME_ACCUM ) > TIME_FRAME_MILLI ) {
		if( time_mgr.get_time( TimeStrings::GAME ) -
			time_mgr.get_time( TimeStrings::SEC ) >
			TIME_MILLISEC ) {
			sec( );
			time_mgr.add_time( TimeStrings::SEC, TIME_MILLISEC );
		}

		update( );
		update_cnt++;

		time_mgr.add_time( TimeStrings::GAME_ACCUM, -TIME_FRAME_MILLI );
		time_mgr.add_time( TimeStrings::GAME, TIME_FRAME_MILLI );
	}

	time_mgr.end_record( RecordStrings::UPDATE );
	time_mgr.push_record( RecordStrings::UPDATE );

	time_mgr.begin_record( RecordStrings::RENDER );

	render( );
	render_cnt++;

	time_mgr.end_record( RecordStrings::RENDER );
	time_mgr.push_record( RecordStrings::RENDER );

	float time_main = TIME_FRAME_MILLI -
		time_mgr.get_record_curr( RecordStrings::UPDATE_PRE ) -
		time_mgr.get_record_curr( RecordStrings::UPDATE ) -
		time_mgr.get_record_curr( RecordStrings::RENDER ) -
		0.25f;
	//if( time_main < 0.0f ) time_main = 0.0f;
	if( time_main < 1.5f ) time_main = 1.5f;
	thread_mgr.loop_main( time_main );

	client.time_mgr.begin_record( RecordStrings::RENDER_SWAP );
	display_mgr.swap_buffers( );
	client.time_mgr.push_record( RecordStrings::RENDER_SWAP );
	client.time_mgr.end_record( RecordStrings::RENDER_SWAP );

	if( !display_mgr.is_vsync && display_mgr.is_limiter ) {
		
		///*
		float time_sleep = TIME_FRAME_MILLI -
			time_mgr.get_record_curr( RecordStrings::UPDATE_PRE ) -
			time_mgr.get_record_curr( RecordStrings::UPDATE ) -
			time_mgr.get_record_curr( RecordStrings::TASK_MAIN ) -
			time_mgr.get_record_curr( RecordStrings::RENDER ) -
			time_mgr.get_record_curr( RecordStrings::RENDER_SWAP );

		if( time_sleep > 0 ) {
			time_mgr.begin_record( RecordStrings::SLEEP );
			time_mgr.end_record( RecordStrings::SLEEP );

			float time_curr_sleep;
			while( ( time_curr_sleep = time_mgr.get_record_curr( RecordStrings::SLEEP ) ) < time_sleep ) {
				time_mgr.end_record( RecordStrings::SLEEP );
			}

			time_mgr.push_record( RecordStrings::SLEEP );
		}
		//*/
		
		/*
		static float time_min_prec = 5.0f;

		float time_sleep = TIME_FRAME_MILLI -
			time_mgr.get_record_curr( RecordStrings::UPDATE_PRE ) -
			time_mgr.get_record_curr( RecordStrings::UPDATE ) -
			time_mgr.get_record_curr( RecordStrings::TASK_MAIN ) -
			time_mgr.get_record_curr( RecordStrings::RENDER ) -
			time_mgr.get_record_curr( RecordStrings::RENDER_SWAP );

		if( time_sleep > 0 ) {
			time_mgr.begin_record( RecordStrings::SLEEP );
			time_mgr.end_record( RecordStrings::SLEEP );

			float time_curr_sleep;

			if( time_sleep > time_min_prec ) { 
				while( ( time_curr_sleep = time_mgr.get_record_curr( RecordStrings::SLEEP ) ) < time_sleep - time_min_prec ) {
					std::this_thread::sleep_for( std::chrono::nanoseconds( 0 ) );
					time_mgr.end_record( RecordStrings::SLEEP );
					cnt_sleep += 1;
				}
			}

			while( ( time_curr_sleep = time_mgr.get_record_curr( RecordStrings::SLEEP ) ) < time_sleep ) {
				time_mgr.end_record( RecordStrings::SLEEP );
			}

			time_mgr.push_record( RecordStrings::SLEEP );
		}

		*/
		
	}
}