static partition_result_t get_label(partition_private_t *partition_privatep) { anch_vol_desc_ptr_t *anchor_vol_desc_ptrp; int32_t block_size; partition_result_t partition_result; anchor_vol_desc_ptrp = NULL; block_size = 0; partition_result = PARTITION_SUCCESS; if (partition_result == PARTITION_SUCCESS) { partition_result = get_anch_vol_desc(partition_privatep, &anchor_vol_desc_ptrp, &block_size); } if (partition_result == PARTITION_SUCCESS) { partition_result = create_label(&(partition_privatep->labelp)); } if (partition_result == PARTITION_SUCCESS) { partition_result = read_label(partition_privatep, anchor_vol_desc_ptrp, block_size); } if (partition_result != PARTITION_SUCCESS) { destroy_label(&(partition_privatep->labelp)); } if (anchor_vol_desc_ptrp != NULL) { /* * allocated by get_anch_vol_desc() */ free(anchor_vol_desc_ptrp); } return (partition_result); }
PCFOP * read_instr(struct PCFState * st, const char * line, uint32_t iptr) { char buf[LINE_MAX], *bitr; buf[0] = '\0'; bitr = buf; assert(line[0] == '('); line++; while((line[0] != ' ') && (line[0] != ')')) { bitr[0] = line[0]; line++; bitr++; } bitr[0] = '\0'; if(strcmp(buf, "LABEL") == 0) return read_label(line, st, iptr); else if(strcmp(buf, "INITBASE") == 0) return read_initbase(line); else if(strcmp(buf, "CONST") == 0) return read_const(line); else if(strcmp(buf, "GATE") == 0) return read_gate(line); else if(strcmp(buf, "BITS") == 0) return read_bits(line); else if(strcmp(buf, "MKPTR") == 0) return read_mkptr(line); else if(strcmp(buf, "COPY") == 0) return read_copy(line); else if(strcmp(buf, "COPY-INDIR") == 0) return read_copy_indir(line); else if(strcmp(buf, "INDIR-COPY") == 0) return read_indir_copy(line); else if(strcmp(buf, "CALL") == 0) return read_call(line); else if(strcmp(buf, "RET") == 0) return read_ret(line); else if(strcmp(buf, "BRANCH") == 0) return read_branch(line); else if(strcmp(buf, "CLEAR") == 0) return read_clear(line); else if(strcmp(buf, "JOIN") == 0) return read_join(line); else if(strcmp(buf, "ADD") == 0) return read_add(line); else if(strcmp(buf, "MUL") == 0) return read_mul(line); assert(0); }
void indexed_force_tri_3D::load(std::ifstream& in, point_cloud* pPC) { // read the label in and set it LABEL label = read_label(in); set_label(label); if (in.eof()) return; // set the point cloud ppoint_cloud_instance = pPC; // read the point cloud indices for each vertex for (int t=0; t<3; t++) p[t] = read_int(in); // calculate the centroid calculate_centroid(); // read the target index ds_index = read_int(in); // read the length of index list in int n_idx = read_int(in); for (int i=0; i<n_idx; i++) { grid_index gr_idx; gr_idx.i = read_int(in); gr_idx.j = read_int(in); gr_idx.cart_coord = read_vector(in); grid_indices.push_back(gr_idx); } // read the point and adjacency list for (int a=0; a<2; a++) { // get the size first int s = read_int(in); for (int i=0; i<s; i++) adjacency[a].push_back(read_label(in)); } }
void read_instruction() { instruction_t instr = {0}; // skip empty lines if (next_token == EOL_TOKEN || next_token == EOF_TOKEN) { read_token(); return; } if (next_token == LABEL_TOKEN) { read_label(&instr.label); } else { instr.label = 0; } read_opcode(&instr.opcode); if (next_token != EOL_TOKEN) { read_argument(&instr.arg1); } if (next_token == COMMA_TOKEN) { read_token(); assert(token == COMMA_TOKEN); read_argument(&instr.arg2); } read_token(); if (token != EOL_TOKEN) { fprintf(stderr, "%s:%d: error: To much input.\n", get_scan_file(), get_scan_line()); result = -1; recover_error(); return; } add_instruction(prog, instr); }
void steering_extremum::load(std::ifstream& in_file) { // read the binary chunk in as a extremum lon = read_float(in_file); lat = read_float(in_file); intensity = read_float(in_file); delta = read_float(in_file); sv_u = read_float(in_file); sv_v = read_float(in_file); // read the object labels int n_tris_in_obj = read_int(in_file); for (int o=0; o<n_tris_in_obj; o++) { LABEL obj_label = read_label(in_file); object_labels.push_back(obj_label); } }
static int read_partitions(struct biosdisk *d) { int error; error = -1; #ifndef NO_GPT error = read_gpt(d); if (error == 0) return 0; #endif #ifndef NO_DISKLABEL error = read_label(d); #endif return error; }
int main(int argc, char* argv[]) { CNN net; double time_cost; //-------- CNN Initializing -------- //---------------------------------- //Read parameters file net.readPara(parameter_file); //-------- Load Dataset ------------ //---------------------------------- #ifdef _HANY_NET_WITH_LABEL_NAMES ifstream read_label(label_file); for(int c = 0; c < net.class_count; c++) { string new_label_name; read_label >> new_label_name; label_list.push_back(make_pair(c, new_label_name)); } #endif #ifdef _HANY_NET_LOAD_MNIST #ifdef _HANY_NET_PRINT_MSG cout << "Loading MNIST dataset..." << endl; time_cost = (double)getTickCount(); #endif loadMNIST("train-images.idx3-ubyte", "train-labels.idx1-ubyte", net.train_set); loadMNIST("t10k-images.idx3-ubyte", "t10k-labels.idx1-ubyte", net.test_set); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_LOAD_SAMPLE_FROM_PIC #ifdef _HANY_NET_PRINT_MSG cout << "Loading samples..." << endl; time_cost = (double)getTickCount(); #endif for(int c = 0; c < net.class_count; c++) { for(int i = 0; i < sample_num; i++) { string file_name = sample_file_pre + to_string(c) + "_" + to_string(i) + ".jpg"; Mat img_read = imread(file_name, CV_LOAD_IMAGE_GRAYSCALE); if(img_read.data == NULL) { break; } Mat img_nor; resize(img_read, img_nor, Size(net.sample_width, net.sample_height)); net.train_set.push_back(make_pair(img_nor, (uchar)(c))); } } #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #ifdef _HANY_NET_CAPTURE_FACE_FROM_CAMERA #ifdef _HANY_NET_PRINT_MSG cout << "Capturing samples..." << endl; time_cost = (double)getTickCount(); #endif VideoCapture cap_in(0); if(!cap_in.isOpened()) { cout << "Cannot access camera. Press ANY key to exit." << endl; cin.get(); exit(-1); } CascadeClassifier cascade_in; cascade_in.load(haar_file); Mat frame; int frame_count = 0; int capture_count = 0; int class_idx = 0; int class_count = 0; bool sample_suff = false; bool cap_sample = true; while(cap_in.read(frame)) { capture_count++; vector<Rect> faces; Mat frame_gray, img_gray; cvtColor(frame, frame_gray, CV_BGR2GRAY); equalizeHist(frame_gray, img_gray); cascade_in.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); int face_area = 0; int face_idx = 0; if(faces.size() > 0) { for(int f = 0; f < faces.size(); f++) { if(faces[f].area() > face_area) { face_area = faces[f].area(); face_idx = f; } } rectangle(frame, faces[face_idx], Scalar(255, 0, 0), 3); if(frame_count % 5 == 0 && cap_sample && !sample_suff) { Mat face, face_nor; img_gray(faces[face_idx]).copyTo(face); resize(face, face_nor, Size(net.sample_width, net.sample_height)); net.train_set.push_back(make_pair(face_nor, (uchar)class_idx)); class_count++; } } putText(frame, "Class: " + to_string(class_idx), Point(50, 100), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); putText(frame, "Sample: " + to_string(class_count), Point(50, 150), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); if(sample_suff) { putText(frame, "Enough samples. Press SPACE.", Point(50, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); }else { putText(frame, "Capturing...", Point(50, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } if(!cap_sample) { putText(frame, "Wait for another person. Press SPACE.", Point(50, 200), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } imshow(camera_window_name, frame); if(class_count >= sample_num) { sample_suff = true; } frame_count++; int key = waitKey(20); if(key == 27){ cap_in.release(); break; } else if(key == ' ') { if(cap_sample && sample_suff) { cap_sample = false; continue; } if(!cap_sample && sample_suff) { cap_sample = true; sample_suff = false; class_idx++; class_count = 0; continue; } } } #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #endif //-------- CNN Initializing -------- //---------------------------------- #ifdef _HANY_NET_PRINT_MSG cout << "Initializing neural networks..." << endl; time_cost = (double)getTickCount(); #endif //Initialize CNN with knowledge of samples net.initCNN(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Total number of samples: " << (int)(net.train_set.size() + net.test_set.size()) << endl; cout << "Initializing neural networks done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif //Load pre-trained CNN parameters from file and continue to train // net.uploadCNN(pretrained_cnn_file); //-------- CNN Training ---------- //-------------------------------- #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Start training CNN..." << endl; time_cost = (double)getTickCount(); #endif //Train CNN with train sample set net.trainCNN(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "CNN training done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif for(int i = 0; i < net.time_ff.size(); i++) { cout << "FeedForward stage " << i << ": " << net.time_ff[i] << "s" << endl; } for(int i = 0; i < net.time_bp.size(); i++) { cout << "BackPropagation stage " << i << ": " << net.time_bp[i] << "s" << endl; } //Draw stage loss graph Mat stage_loss_graph = Mat::zeros(600, 1100, CV_8UC3); Point2d pt1, pt2; pt1 = Point2d(50.0, 50.0); for(int stage = 0; stage < net.stage_loss.size(); stage++) { pt2 = Point2d(50.0 + 1200.0 / net.stage_loss.size() * stage, 550.0 - 500.0 * net.stage_loss[stage] / net.stage_loss[0]); line(stage_loss_graph, pt1, pt2, Scalar(255, 255, 255)); pt1 = pt2; } imshow("Stage Loss Graph", stage_loss_graph); imwrite("stage_loss_graph.jpg", stage_loss_graph); waitKey(10); #endif //-------- Save Trained Network ----- //----------------------------------- #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Dumping trained CNN parameters to file " << pretrained_cnn_file << "..." << endl; #endif //Dump trained CNN parameters to file net.downloadCNN(trained_cnn_file); #ifdef _HANY_NET_PRINT_MSG cout << "Dumping trained CNN parameters to file done." << endl << endl; #endif #endif //-------- Load Pre-trained Network ----- //--------------------------------------- #ifndef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Loading pre-trained CNN parameters from file " << pretrained_cnn_file << "..." << endl; #endif //Load pre-trained CNN parameters from file net.uploadCNN(pretrained_cnn_file); #ifdef _HANY_NET_PRINT_MSG cout << "Loading pre-trained CNN parameters from file done." << endl << endl; #endif #endif //-------- Predict New Samples------- //-------------------------------------- #ifdef _HANY_NET_PREDICT_MNIST #ifdef _HANY_NET_PRINT_MSG cout << "Predicting MNIST test dataset..." << endl; time_cost = (double)getTickCount(); #endif //Calculate correctness ratio with test samples int total_correct_count = 0; for(int sample_idx = 0; sample_idx < net.test_set.size(); sample_idx++) { vector<Mat> input_sample; input_sample.push_back(net.test_set[sample_idx].first); vector<Mat> predict_result = net.predictCNN(input_sample); if((int)predict_result[0].ptr<uchar>(0)[0] == net.test_set[sample_idx].second) { total_correct_count++; } } double total_correct_ratio = (double)total_correct_count / net.test_set.size(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "MNIST testing done." << endl << "Time cost: " << time_cost << "s." << endl; cout << "Total correctness ratio: " << total_correct_ratio << endl << endl; #endif #endif #ifdef _HANY_NET_PREDICT_IMAGE_SERIES #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from image series..." << endl; #endif // VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); for(int c = 0; c < net.class_count; c++) { for(int i = 0; i < sample_num; i++) { string file_name = sample_file_pre + to_string(c) + "_" + to_string(i) + ".jpg"; Mat img_read = imread(file_name, CV_LOAD_IMAGE_GRAYSCALE); if(img_read.data == NULL) { break; } Mat img_nor, img_show; resize(img_read, img_show, Size(400, 400)); resize(img_read, img_nor, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(img_nor); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, label_list[pred_rst].second, Point(10, 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); putText(img_show, to_string(c)+"-"+to_string(i), Point(img_show.cols-80, 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); int frame_count = 25; while(--frame_count) { // wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); switch(key_get) { case 27: // wri.release(); return 0; default: break; } } } #endif #ifdef _HANY_NET_PREDICT_VEDIO_SERIES #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from video series..." << endl; #endif VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); namedWindow(camera_window_name); CascadeClassifier cascade_out; cascade_out.load(haar_file); for(int c = 1; c <= net.class_count; c++) { string file_name = "path_to_face_videos\\" + to_string(c) + ".wmv"; VideoCapture cap(file_name); if(!cap.isOpened()) continue; Mat img_read; while(cap.read(img_read)) { Mat img_gray, nor_gray, img_show; img_read.copyTo(img_show); cvtColor(img_read, img_gray, CV_BGR2GRAY); vector<Rect> faces; equalizeHist(img_gray, img_gray); cascade_out.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); for(int f = 0; f < faces.size(); f++) { rectangle(img_show, faces[f], Scalar(0, 255, 255), 3); resize(img_gray(faces[f]), nor_gray, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(nor_gray); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, to_string(pred_rst), Point(faces[f].x+faces[f].width, faces[f].y+faces[f].height), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } int frame_count = 2; while(--frame_count) { wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); switch(key_get) { case 27: wri.release(); return 0; default: break; } } } wri.release(); #endif #ifdef _HANY_NET_PREDICT_CAMERA #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from camera..." << endl; #endif VideoCapture cap_out(0); if(!cap_out.isOpened()) { cout << "Cannot access camera." << endl; cin.get(); exit(-1); } CascadeClassifier cascade_out; cascade_out.load(haar_file); // VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); Mat src_frame; namedWindow(camera_window_name); Mat img_read; while(cap_out.read(img_read)) { Mat img_gray, nor_gray, img_show; img_read.copyTo(img_show); cvtColor(img_read, img_gray, CV_BGR2GRAY); vector<Rect> faces; equalizeHist(img_gray, img_gray); cascade_out.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); for(int f = 0; f < faces.size(); f++) { rectangle(img_show, faces[f], Scalar(0, 255, 255), 3); resize(img_gray(faces[f]), nor_gray, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(nor_gray); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, label_list[pred_rst].second, Point(faces[f].x+faces[f].width, faces[f].y+faces[f].height), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } int frame_count = 2; while(--frame_count) { // wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); if(key_get == 27) { // wri.release(); cap_out.release(); return 0; } } #endif cout << "Press any key to quit..." << endl; // waitKey(0); cin.get(); return 0; }
/* * Disk label editor for sun disklabels. */ int main(int ac, char **av) { struct sun_disklabel sl; const char *bootpath; const char *proto; const char *disk; int ch; bootpath = _PATH_BOOT; while ((ch = getopt(ac, av, "b:BcehnrRw")) != -1) switch (ch) { case 'b': bflag = 1; bootpath = optarg; break; case 'B': Bflag = 1; break; case 'c': cflag = 1; break; case 'e': eflag = 1; break; case 'h': hflag = 1; break; case 'n': nflag = 1; break; case 'r': fprintf(stderr, "Obsolete -r flag ignored\n"); break; case 'R': Rflag = 1; break; case 'w': wflag = 1; break; default: usage(); break; } if (bflag && !Bflag) usage(); if (nflag && !(Bflag || eflag || Rflag || wflag)) usage(); if (eflag && (Rflag || wflag)) usage(); if (eflag) hflag = 0; ac -= optind; av += optind; if (ac == 0) usage(); bzero(&sl, sizeof(sl)); disk = av[0]; if (wflag) { if (ac != 2 || strcmp(av[1], "auto") != 0) usage(); read_label(&sl, disk); bzero(sl.sl_part, sizeof(sl.sl_part)); sl.sl_part[SUN_RAWPART].sdkp_cyloffset = 0; sl.sl_part[SUN_RAWPART].sdkp_nsectors = sl.sl_ncylinders * sl.sl_ntracks * sl.sl_nsectors; write_label(&sl, disk, bootpath); } else if (eflag) { if (ac != 1) usage(); read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); edit_label(&sl, disk, bootpath); } else if (Rflag) { if (ac != 2) usage(); proto = av[1]; read_label(&sl, disk); if (parse_label(&sl, proto) != 0) errx(1, "%s: invalid label", proto); write_label(&sl, disk, bootpath); } else if (Bflag) { read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); write_label(&sl, disk, bootpath); } else { read_label(&sl, disk); if (sl.sl_magic != SUN_DKMAGIC) errx(1, "%s%s has no sun disklabel", _PATH_DEV, disk); print_label(&sl, disk, stdout); } return (0); }
int main(int argc, char ** argv){ // set random number seed std::srand(unsigned(std::time(0))); // read training image and labels vector<vector<double>> train_image; vector<int> train_label; printf("read training files\n"); read_image("train-images.idx3-ubyte", train_image); read_label("train-labels.idx1-ubyte", train_label); // read testing image and labels vector<vector<double>> test_image; vector<int> test_label; printf("read testing files\n"); read_image("t10k-images.idx3-ubyte", test_image); read_label("t10k-labels.idx1-ubyte", test_label); // set iteration number and learning rate int maxIter = 100; double step = 0.02; // construct the network printf("construct neural network\n"); int numInput = train_image[0].size(); int numLayers = 2; int numNodesPerLayers[2] = { 300, 10 }; neuralNetwork<double, double> nl(numInput, numLayers, numNodesPerLayers, new tanhFunction, new tanhFunctionD); // initialize weight with random numbers double *** weights; weights = new double **[numLayers]; for (int i = 0; i < numLayers; i++) weights[i] = new double*[numNodesPerLayers[i]]; for (int i = 0; i < numLayers; i++) for (int j = 0; j < numNodesPerLayers[i]; j++) weights[i][j] = new double[(i == 0 ? numInput : numNodesPerLayers[i - 1])]; for (int i = 0; i < numLayers; i++) for (int j = 0; j < numNodesPerLayers[i]; j++) for (int k = 0; k < (i == 0 ? numInput : numNodesPerLayers[i - 1]); k++) weights[i][j][k] = 0.0001 * (rand() % 201 - 100); nl.setWeights((double ***)weights); // construct permutation array int numImage = (int)train_image.size();// / 100; vector<int> perm; for (int i = 0; i < numImage; i++) perm.push_back(i); // assign inputs from training images double ** train_input = new double*[numImage]; for (int i = 0; i < numImage; i++){ train_input[i] = new double[train_image[i].size()]; for (int j = 0; j < (int)train_image[i].size(); j++) train_input[i][j] = train_image[i][j] / 255; } // assign inputs from testing images double ** test_input = new double*[test_image.size()]; for (int i = 0; i < (int)test_image.size(); i++){ test_input[i] = new double[test_image[i].size()]; for (int j = 0; j < (int)test_image[i].size(); j++) test_input[i][j] = test_image[i][j] / 255; } double ** weightsRBM = new double*[numInput]; for(int i = 0; i < numInput; i++){ weightsRBM[i] = new double[numNodesPerLayers[0]]; } for(int i = 0; i < numInput; i++){ for(int j = 0; j < numNodesPerLayers[0]; j++){ weightsRBM[i][j] = weights[0][j][i]; } } int RBMapply = 1; if(RBMapply == 1){ hiddenLayer<double, double> * tempHiddenLayer = new hiddenLayer<double, double>(numNodesPerLayers[0], numInput, new tanhFunction); double epsilon = 0.02; int t1 = 0; for (int n = 0; n < numImage; n++){ if((n * 100) / numImage >= t1 + 1){ printf("%d%%\n", (n * 100) / numImage); t1 = (n * 100) / numImage; } nl.RBM(train_input[n], 0, tempHiddenLayer, weightsRBM, epsilon); } nl.setWeights(); } // releease memory for weights for (int i = 0; i < numLayers; i++) for (int j = 0; j < numNodesPerLayers[i]; j++) delete[] weights[i][j]; for (int i = 0; i < numLayers; i++) delete[] weights[i]; delete[] weights; double * train_err = new double[maxIter]; double * test_err = new double[maxIter]; for (int t = 1; t <= maxIter; t++){ double err = 0; printf("Iteration %d : \n", t); // random permutation of samples std::random_shuffle(perm.begin(), perm.end(), myrandom); // train the netword with all random permuted samples for (int n = 0; n < numImage; n++){ if (n % (numImage / 20) == 0) printf(">\n"); nl.backPropagation(train_input[perm[n]], train_label[perm[n]], step); } // count correct predictions int count = 0; for (int n = 0; n < numImage; n++){ // do prediction with updated weights double * outputs = nl.feedForward(train_input[n]); // find most probable label int maxIndex = 0; double max = outputs[0]; for (int i = 1; i < numNodesPerLayers[numLayers-1]; i++){ if (outputs[i] > max) { max = outputs[i]; maxIndex = i; } } // compute accumulate error for (int i = 1; i < numNodesPerLayers[numLayers - 1]; i++){ double ti = 2 * (double)(train_label[n] == maxIndex) - 1; err += (outputs[i] - ti) *(outputs[i] - ti); } delete[] outputs; // if prediction matches true label increment count by 1 if (train_label[n] == maxIndex) count++; } printf("\ttotal error is %.4f\n", err); printf("error rate on training set %.4f, ", 1 - (double)count / numImage); train_err[t] = 1 - (double)count / numImage; count = 0; for (int n = 0; n < (int)test_image.size(); n++){ // do prediction double * outputs = nl.feedForward(test_input[n]); // find most probable label int maxIndex = 0; double max = outputs[0]; for (int i = 1; i < numNodesPerLayers[numLayers - 1]; i++){ if (outputs[i] > max) { max = outputs[i]; maxIndex = i; } } delete[] outputs; // if prediction matches true label increment count by 1 if (test_label[n] == maxIndex) count++; } printf("error rate on testing set %.4f\n", 1 - (double)count / test_image.size()); test_err[t] = 1 - (double)count / test_image.size(); } // release memories for (int i = 0; i < (int)train_image.size(); i++) vector<double > ().swap(train_image[i]); vector<vector<double>>().swap(train_image); vector<int>().swap(train_label); for (int i = 0; i < numImage; i++) delete[] train_input[i]; delete[] train_input; for (int i = 0; i < (int)test_image.size(); i++) vector<double >().swap(test_image[i]); vector<vector<double>>().swap(test_image); vector<int>().swap(test_label); for (int i = 0; i < (int)test_image.size(); i++) delete[] test_input[i]; delete[] test_input; // store weights in file ofstream file; file.open("weight1.csv",ios::trunc); for (int i = 0; i < numNodesPerLayers[0]; i++) { for (int j = 0; j < numInput; j++) file << nl.getWeight(0,i,j) << ","; file << endl; } file.close(); file.open("weight2.csv", ios::trunc); for (int i = 0; i < numNodesPerLayers[1]; i++) { for (int j = 0; j < numNodesPerLayers[0]; j++) file << nl.getWeight(1, i, j) << ","; file << endl; } file.close(); file.open("error.csv", ios::trunc); for (int t = 1; t <= maxIter; t++) file << train_err[t] << ","; file << endl; for (int t = 1; t <= maxIter; t++) file << test_err[t] << ","; file << endl; file.close(); getchar(); return EXIT_SUCCESS; }
static int get_solaris_part(int fd, struct ipart *ipart) { int i; struct ipart ip; int status; char *bootptr; struct dk_label update_label; (void) lseek(fd, 0, 0); status = read(fd, (caddr_t)&boot_sec, NBPSCTR); if (status != NBPSCTR) { err_print("Bad read of fdisk partition. Status = %x\n", status); err_print("Cannot read fdisk partition information.\n"); return (-1); } for (i = 0; i < FD_NUMPART; i++) { int ipc; ipc = i * sizeof (struct ipart); /* Handling the alignment problem of struct ipart */ bootptr = &boot_sec.parts[ipc]; (void) fill_ipart(bootptr, &ip); /* * we are interested in Solaris and EFI partition types */ if (ip.systid == SUNIXOS || ip.systid == SUNIXOS2 || ip.systid == EFI_PMBR) { /* * if the disk has an EFI label, nhead and nsect may * be zero. This test protects us from FPE's, and * format still seems to work fine */ if (nhead != 0 && nsect != 0) { pcyl = lel(ip.numsect) / (nhead * nsect); xstart = lel(ip.relsect) / (nhead * nsect); ncyl = pcyl - acyl; } #ifdef DEBUG else { err_print("Critical geometry values are zero:\n" "\tnhead = %d; nsect = %d\n", nhead, nsect); } #endif /* DEBUG */ solaris_offset = lel(ip.relsect); break; } } if (i == FD_NUMPART) { err_print("Solaris fdisk partition not found\n"); return (-1); } /* * compare the previous and current Solaris partition * but don't use bootid in determination of Solaris partition changes */ ipart->bootid = ip.bootid; status = bcmp(&ip, ipart, sizeof (struct ipart)); bcopy(&ip, ipart, sizeof (struct ipart)); /* if the disk partitioning has changed - get the VTOC */ if (status) { status = ioctl(fd, DKIOCGVTOC, &cur_parts->vtoc); if (status == -1) { i = errno; err_print("Bad ioctl DKIOCGVTOC.\n"); err_print("errno=%d %s\n", i, strerror(i)); err_print("Cannot read vtoc information.\n"); return (-1); } status = read_label(fd, &update_label); if (status == -1) { err_print("Cannot read label information.\n"); return (-1); } #if defined(_SUNOS_VTOC_16) /* * this is to update the slice table on x86 * we don't care about VTOC8 here */ for (i = 0; i < NDKMAP; i ++) { cur_parts->pinfo_map[i].dkl_cylno = update_label.dkl_vtoc.v_part[i].p_start / ((int)(update_label.dkl_nhead * update_label.dkl_nsect)); cur_parts->pinfo_map[i].dkl_nblk = update_label.dkl_vtoc.v_part[i].p_size; } #endif /* defined(_SUNOS_VTOC_16) */ cur_dtype->dtype_ncyl = update_label.dkl_ncyl; cur_dtype->dtype_pcyl = update_label.dkl_pcyl; cur_dtype->dtype_acyl = update_label.dkl_acyl; cur_dtype->dtype_nhead = update_label.dkl_nhead; cur_dtype->dtype_nsect = update_label.dkl_nsect; ncyl = cur_dtype->dtype_ncyl; acyl = cur_dtype->dtype_acyl; pcyl = cur_dtype->dtype_pcyl; nsect = cur_dtype->dtype_nsect; nhead = cur_dtype->dtype_nhead; } return (0); }