bool ranking_synthesis_seneschalt::write_input_file(const exprt &e) { assert(e.id()=="and" && e.type()==bool_typet()); std::ofstream f("seneschal.input"); exprt e_ext = e; replace_mapt rmap; std::set<irep_idt> inputs, outputs; collect_variables(e_ext, rmap, inputs, outputs); // write variable declarations write_variables(f, inputs, outputs); f << std::endl; // translate constraints if(!write_constraints(f, rmap, e_ext)) return false; f << std::endl; f.close(); return true; }
void write_extended_bundler(std::ofstream &file, CameraArray& cam_list, PointArray& point_list, const std::vector<CameraConstraint>& constraints) { size_t nConstraints = constraints.size(); file << "# Extended Bundle file v0.1" << std::endl; if (nConstraints > 0) { file << cam_list.size() << " " << point_list.size() << " " << nConstraints << std::endl; } else { file << cam_list.size() << " " << point_list.size() << std::endl; } write_constraints(file, constraints); write_extended_cams(file, cam_list); write_points(file, point_list); }
int main (int argc, char* argv[]) { int o, i, j, cad_num; char filename[256]; CAD **cads, *cad; SAMPLE sample, sample_part; /* training sample */ LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; STRUCT_LEARN_PARM struct_parm; STRUCTMODEL structmodel; int alg_type; int rank; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* select GPU */ select_gpu(rank); svm_struct_learn_api_init(argc,argv); read_input_parameters(argc, argv, trainfile, cadfile, testfile, &verbosity, &struct_verbosity, &struct_parm, &learn_parm, &kernel_parm, &alg_type); /* read cad models */ cads = read_cad_model(cadfile, &cad_num, 0, &struct_parm); /* if cad_num == 1, train the final model */ if(cad_num == 1) { printf("Train the hierarchical model\n"); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; printf("Read training samples\n"); sample = read_struct_examples(trainfile, &struct_parm, &structmodel); printf("Read training samples done\n"); if(struct_parm.is_root == 1 || struct_parm.is_aspectlet == 1) { /* first train weights for root parts */ printf("Train root models\n"); cad = cads[0]; struct_parm.cad_index = 0; /* for each part */ for(i = 0; i < cad->part_num; i++) { /* choose what parts to train */ if((cad->roots[i] == -1 && struct_parm.is_root == 1) || (cad->roots[i] == 1 && struct_parm.is_aspectlet == 1)) { printf("Train part %d\n", i); /* training iteration index */ struct_parm.iter = 0; struct_parm.part_index = i; /* select training samples for part */ if(rank == 0) select_examples_part(trainfile, sample, cad, 0, i); MPI_Barrier(MPI_COMM_WORLD); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); /* train the root template */ struct_parm.deep = 0; /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* if aspectlet, train the subtree */ if(cad->roots[i] == 1 && struct_parm.is_aspectlet == 1) { printf("Train subtree for part %d\n", i); struct_parm.deep = 1; struct_parm.iter++; /* select training samples for part */ if(rank == 0) select_examples_part(trainfile, sample, cad, 0, i); MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } /* data mining hard examples */ for(j = 0; j < struct_parm.hard_negative; j++) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample_part); sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } /* save constraints for training the full model */ if(rank == 0) save_constraints(&struct_parm, &structmodel); MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; } } /* end for each part */ if(rank == 0) { write_constraints(struct_parm.cset, &struct_parm); free(struct_parm.cset.rhs); for(i = 0; i < struct_parm.cset.m; i++) free_example(struct_parm.cset.lhs[i], 1); free(struct_parm.cset.lhs); } MPI_Barrier(MPI_COMM_WORLD); } /* end if is_root == 1 */ /* train the full model */ printf("Train the full model\n"); struct_parm.iter = 0; struct_parm.cad_index = -1; struct_parm.part_index = -1; struct_parm.deep = 1; /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* data mining hard examples */ while(struct_parm.hard_negative > 0) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples(trainfile, testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample); sample = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = cads; if(alg_type == 1) svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); struct_parm.hard_negative--; } if(rank == 0) { if(struct_verbosity >= 1) { printf("Writing learned model..."); fflush(stdout); } sprintf(modelfile, "%s.mod", struct_parm.cls); write_struct_model(modelfile, &structmodel, &struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } } MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample); free_struct_model(structmodel); } /* train weights for aspectlets */ /* start with the second aspectlet, the first one is the whole object */ for(o = 1; o < cad_num; o++) { printf("Train aspectlet %d\n", o); cad = cads[o]; /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = &cad; /* training iteration index */ struct_parm.iter = 0; struct_parm.cad_index = -1; struct_parm.part_index = -1; struct_parm.deep = 1; /* select training samples for the aspectlet */ if(rank == 0) select_examples_aspectlet(trainfile, cads, o); MPI_Barrier(MPI_COMM_WORLD); printf("Read training samples\n"); sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel); printf("Read training samples done\n"); /* Do the learning and return structmodel. */ if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); /* data mining hard examples */ for(i = 0; i < struct_parm.hard_negative; i++) { /* increase iteration number */ struct_parm.iter++; data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel); /* read the training examples */ if(struct_verbosity>=1) { printf("Reading training examples..."); fflush(stdout); } free_struct_sample(sample_part); sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } /* Do the learning and return structmodel. */ free_struct_model(structmodel); /* set the cad model for structmodel */ structmodel.cad_num = 1; structmodel.cads = &cad; if(alg_type == 1) svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel); else if(alg_type == 2) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG); else if(alg_type == 3) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG); else if(alg_type == 4) svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG); else exit(1); } if(rank == 0) { if(struct_verbosity>=1) { printf("Writing learned model..."); fflush(stdout); } sprintf(modelfile, "%s_cad%03d.mod", struct_parm.cls, o); write_struct_model(modelfile, &structmodel, &struct_parm); if(struct_verbosity>=1) { printf("done\n"); fflush(stdout); } } MPI_Barrier(MPI_COMM_WORLD); free_struct_sample(sample_part); free_struct_model(structmodel); } /* end for each cad model */ for(i = 0; i < cad_num; i++) destroy_cad(cads[i]); free(cads); svm_struct_learn_api_exit(); MPI_Finalize(); return 0; }