int main( int argc, char* argv[] ) { if( argc != 3 ) { cerr << "Usage: " << argv[0] << " infile outfile." << endl; return -1; } errno = 0; ifstream in; in.open( argv[1], ios::binary ); if( errno ) { cerr << "Error in " << argv[1] << ": " << strerror( errno ) << endl; return -1; } ofstream out; out.open( argv[2], ios::binary ); if( errno ) { cerr << "Error in " << argv[2] << ": " << strerror( errno ) << endl; in.close(); return -1; } vector<int>* freqs = new vector<int>( 256, 0 ); BitInputStream* input = new BitInputStream( in ); BitOutputStream* output = new BitOutputStream( out ); int msgCnt = input->readInt(); int symCnt = input->readInt(); for( int i = 1; i <= symCnt; i++ ) { int c = input->readByte(); int cCnt = input->readInt(); freqs->at( c ) = cCnt; } HCTree* decodeTree = new HCTree(); decodeTree->build( *freqs ); input->fill(); for( int i = 0; i < msgCnt; i++ ) { output->writeByte( decodeTree->decode( *input ) ); } in.close(); out.close(); delete output; delete decodeTree; delete input; delete freqs; return 0; }
int main(int argc, char** argv) { string tempString; //create io files ifstream inFile; ofstream outFile; vector<int> freqs(256,0); int sum = 0; int nextChar; //read header, count frequency inFile.open(argv[1],ifstream::binary); BitInputStream bitInFile = BitInputStream(inFile); int unique = bitInFile.readByte(); if (inFile.good()) { //for each symbol in the header for (int i=0; i < unique; i++) { //read the index of unique chars int index = bitInFile.readByte(); //read its frequency nextChar = bitInFile.readInt(); //set frequency at specific index of freqs freqs[index] = nextChar; sum += nextChar; } } //build tree / HCTree huffmanTree; huffmanTree.build(freqs); //call decode on each encoded symbol outFile.open(argv[2], ofstream::binary); //changed to binary //decode each binary input for (int i=0; i<sum; i++) { outFile << (char)huffmanTree.decode(bitInFile); } //close files inFile.close(); outFile.close(); return 0; }
/** Return symbol coded in the next sequence of bits from the stream. * PRECONDITION: build() has been called, to create the coding * tree, and initialize root pointer and leaves vector. */ int HCTree::decode(BitInputStream& in) const{ HCNode* curr = root; if (root->c0 == 0 && root->c1 == 0) { in.readBit(); return (int)root->symbol; } while (curr->c0 != 0 || curr->c1 != 0) { if (in.readBit() == 0){ curr = curr->c0; } else { curr = curr->c1; } } return (int)curr->symbol; }
int HCTree::decode (BitInputStream& in) const { int b = 0; HCNode * current = root; // check if root is the only node in the tree if (current->c0 == 0 && current->c1 == 0) { return current->symbol; } // traverse the tree until a leaf is found while ((b = in.readBit()) != -1) { if (b == 0) { current = current->c0; } else if (b == 1) { current = current->c1; } // check if a leaf if (current->c0 == 0 && current->c1 == 0) { return current->symbol; } } }
/** Return symbol coded in the next sequence of bits from the stream. * PRECONDITION: build() has been called, to create the coding * tree, and initialize root pointer and leaves vector. */ byte HCTree::decode(BitInputStream& in) const { HCNode *pointer = root; while((pointer->c0 != 0) && (pointer->c1 != 0)) { if(in.readBit() == 0) pointer = pointer->c0; else pointer = pointer->c1; } return (byte)(pointer->symbol); }
//Decode the read in bits: If bit is 1 go to child1 if 0 go to child0 until a leaf is reached int HCTree::decode(BitInputStream& in) const { HCNode* node = this->root; while(node->child0 != 0 || node->child1 != 0) { int i = in.readBit(); if(i==1) { node = node->child1; } else if (i==0) { node = node->child0; } else { return -1; } } return node->symbol; }
int HCTree::decode(BitInputStream& in) const { if (!isBuilt) { cerr << "HCTree Error: build must be called before decode." << endl; exit(EXIT_FAILURE); } if (!root) return -1; //start from the root and traverse until we get to a leaf HCNode *curr = root; while (curr->c0) { if (in.readBit() == 0) curr = curr->c0; else curr = curr->c1; } return curr->symbol; }
/** Return symbol coded in the next sequence of bits from the stream. * PRECONDITION: build() has been called, to create the coding * tree, and initialize root pointer and leaves vector. */ int HCTree::decode(BitInputStream& in) const{ HCNode* node = this->root; unsigned int bitRead = 0; while ( node -> c0 != NULL || node-> c1 != NULL){ // Get the bit from the stream bitRead = in.readBit(); if(bitRead){ node = node -> c1; // If bit is 1, go to right cild } else { node = node-> c0; // If bit is 0, go to left child } } return node -> symbol; // Return the leaf's symbol }
int HCTree::decode(BitInputStream &in) const { HCNode *current = root; int bit; // traverse the tree until we run out of bits or get a leaf while (current->c0 || current->c1) { bit = in.next(); if (bit < 0) { return -1; } if (bit) { current = current->c1; } else { current = current->c0; } } return current->symbol; }
/** Return symbol coded in the next sequence of bits from the stream. * PRECONDITION: build() has been called, to create the coding * tree, and initialize root pointer and leaves vector. */ int HCTree::decode(BitInputStream& in) const{ HCNode* temp = root; while(temp->c1 != 0 || temp->c0 != 0){ int tempbit = in.readBit(); if(tempbit == 1){ temp = temp->c1; } else{ temp = temp->c0; } } return temp->symbol; }
//Method that reads in a compress file and decodes it to it's original form int HCTree::decode(BitInputStream& in) const { HCNode *currNode = root; int bit; //Traverses the tree to each node and returns the respective symbol while(currNode->c0 || currNode->c1) { bit = in.readBit(); if(bit == 1) { currNode = currNode->c1; } else { currNode = currNode->c0; } } return currNode->symbol; }
int HCTree::decode(BitInputStream& in) const { //start from the root and traverse down until if it is either //the end of the tree of we find the node HCNode *currNode = root; while (currNode->c0 != nullptr || currNode->c1 != nullptr){ int bit = in.readBit(); if (bit == 0){ currNode = currNode->c0; }else if (bit == 1){ currNode = currNode->c1; }else{ return -1; //probable cause is EOF } } return currNode->symbol; }
/** Return symbol coded in the next sequence of bits from the stream. * PRECONDITION: build() has been called, to create the coding * tree, and initialize root pointer and leaves vector. */ int HCTree::decode(BitInputStream& in) const { HCNode * currNode = root; int bit; while(currNode->c0 != 0 && currNode->c1 != 0) { bit = in.readBit(); if(bit == 1) { currNode = currNode->c1; } else if (bit == 0) { currNode = currNode->c0; } else { return 256; } } int ret = (int) currNode->symbol; return ret; }
int main(int argc, char** argv) { std::ifstream imageFile; std::ifstream labelFile; if(argc != 5) { std::cerr << "Invalid command." << std::endl; return -1; } // Open training images and their labels for reading. imageFile.open(argv[1], std::ios::binary); labelFile.open(argv[2], std::ios::binary); labelFile.seekg(8); BitInputStream* input = new BitInputStream(imageFile); BitInputStream* label = new BitInputStream(labelFile); // Get the magic number, the rows, and the columns of each training image. int magic = input->readInt(); int total = input->readInt(); std::cerr << "Loading training images" << std::endl; std::cerr << "Total image: " << total << std::endl; int rows = input->readInt(); std::cerr << "Each image contains " << rows << " rows." << std::endl; int columns = input->readInt(); std::cerr << "Each image contains " << columns << " columns." << std::endl; // Load the training data to memory. Training* training = new Training(total, rows * columns); for(int i = 0; i < total; i++) { int lbl = (int)(label->readChar()); TRPoint* point = new TRPoint(lbl, rows * columns, i); for(int j = 0; j < (rows * columns); j++) { int pixel = (int)(input->readChar()); point->addPixel(pixel); } training->addElement(point); } imageFile.close(); labelFile.close(); std::ifstream testImageFile; std::ifstream testLabelFile; // Open testing images and their actual labels for reading. testImageFile.open(argv[3], std::ios::binary); testLabelFile.open(argv[4], std::ios::binary); testLabelFile.seekg(8); input = new BitInputStream(testImageFile); label = new BitInputStream(testLabelFile); magic = input->readInt(); total = input->readInt(); std::cerr << "Loading testing images" << std::endl; std::cerr << "Total image: " << total << std::endl; rows = input->readInt(); std::cerr << "Each image contains " << rows << " rows." << std::endl; columns = input->readInt(); std::cerr << "Each image contains " << columns << " columns." << std::endl; // Construct the K-D for nearest neighbor search. std::cerr << "Please enter the number of elements in each leaf for your K-D tree: "; std::string numImages; getline(std::cin, numImages); std::cerr << "Ok, at least " << atoi(numImages.c_str()) << " images." << std::endl; std::cerr << "Constructing K-D tree for training set." << std::endl; srand(time(0)); RDTree* tree = new RDTree(); tree->root = tree->build(tree->root, training, training->size(), atoi(numImages.c_str())); // Load the testing data to memory. Training* testing = new Training(total, rows * columns); for(int i = 0; i < total; i++) { int lbl = (int)(label->readChar()); TRPoint* point = new TRPoint(lbl, rows * columns); for(int j = 0; j < (rows * columns); j++) { int pixel = (int)(input->readChar()); point->addPixel(pixel); } testing->addElement(point); } testImageFile.close(); testLabelFile.close(); // Loading the actual true nearest neighbor of each testing images to memory. std::ifstream actualLabels; actualLabels.open("actual"); std::string in; int* trueLabels = new int[total]; int index = 0; for(int i = 0; getline(actualLabels, in); i++) trueLabels[i] = atoi(in.c_str()); int errors = 0; int notTrueNN = 0; // Perform the nearest neighbor search. for(int o = 0; o < testing->size(); o++) { std::cerr << "classifying image " << o+1 << std::endl; int currentDist = INT_MAX; int currentLabel = -1; int imageNumber = -1; // Find the leaf that contains the elements closest to the test point. TRPoint* testPoint = testing->getElement(o); Training* set = tree->find(testing->getElement(o))->set; // Compute the shortest distance from training images, update if needed. for(int i = 0; i < set->size(); i++) { TRPoint* trainPoint = set->getElement(i); int distance = 0; for(int j = 0; j < trainPoint->size; j++) { int difference = trainPoint->feature[j] - testPoint->feature[j]; if(difference < 0) difference = (-1) * difference; distance = distance + difference; } if(distance < currentDist) { currentDist = distance; currentLabel = trainPoint->label; imageNumber = trainPoint->index; } } std::cerr << "classified label: " << currentLabel << std::endl; std::cerr << "actual label: " << testPoint->label << std::endl; // There's an error if this image is not classified correctly. if(currentLabel != testPoint->label) { std::cerr << "image " << o+1 << " has an error" << std::endl; //std::cout << "image " << o+1 << " has an error" << std::endl; std::cerr << "classified with label " << currentLabel << std::endl; //std::cout << "classified with label " << currentLabel << std::endl; //printImage(training->getElement(imageNumber), columns, rows); std::cerr << "actual label is " << testPoint->label << std::endl; //std::cout << "actual label is " << testPoint->label << std::endl; //printImage(testPoint, columns, rows); errors++; } // Not a true nearest neighbor. if(imageNumber != trueLabels[o]) notTrueNN++; } // Print out the result here. std::cerr << "errors: " << errors << std::endl; std::cerr << "not true nearest neighbors: " << notTrueNN << std::endl; return 0; }
/** * Get the error rate first! * For errors, print out both the errors and the training data classified. * The error rate for 1-nn is 369 out of 10000. * The error rate for 3-nn is 367 out of 10000. */ int main(int argc, char** argv) { std::ifstream imageFile; std::ifstream labelFile; if(argc != 5) { std::cerr << "Invalid command." << std::endl; return -1; } imageFile.open(argv[1], std::ios::binary); labelFile.open(argv[2], std::ios::binary); labelFile.seekg(8); BitInputStream* input = new BitInputStream(imageFile); BitInputStream* label = new BitInputStream(labelFile); // Get the magic number, the rows, and the columns of each image. int magic = input->readInt(); int total = input->readInt(); std::cerr << "Total image: " << total << std::endl; int rows = input->readInt(); std::cerr << "Each image contains " << rows << " rows." << std::endl; int columns = input->readInt(); std::cerr << "Each image contains " << columns << " columns." << std::endl; // Load the training data to memory. Training* training = new Training(total, rows * columns); for(int i = 0; i < total; i++) { int lbl = (int)(label->readChar()); TRPoint* point = new TRPoint(lbl, rows * columns); for(int j = 0; j < (rows * columns); j++) { int pixel = (int)(input->readChar()); point->addPixel(pixel); } training->addElement(point); } imageFile.close(); labelFile.close(); std::ifstream testImageFile; std::ifstream testLabelFile; testImageFile.open(argv[3], std::ios::binary); testLabelFile.open(argv[4], std::ios::binary); testLabelFile.seekg(8); input = new BitInputStream(testImageFile); label = new BitInputStream(testLabelFile); magic = input->readInt(); total = input->readInt(); std::cerr << "Total image: " << total << std::endl; rows = input->readInt(); std::cerr << "Each image contains " << rows << " rows." << std::endl; columns = input->readInt(); std::cerr << "Each image contains " << columns << " columns." << std::endl; // Load the testing data to memory. Training* testing = new Training(total, rows * columns); for(int i = 0; i < total; i++) { int lbl = (int)(label->readChar()); TRPoint* point = new TRPoint(lbl, rows * columns); for(int j = 0; j < (rows * columns); j++) { int pixel = (int)(input->readChar()); point->addPixel(pixel); } testing->addElement(point); } testImageFile.close(); testLabelFile.close(); /* std::cerr << "Make sure pixels are correct." << std::endl; for(int i = 0; i < training->size(); i++) { TRPoint* trainPoint = training->getElement(i); TRPoint* testPoint = testing->getElement(i); std::cout << "train " << i << " "; for(int j = 0; j < trainPoint->size; j++) { int tr = trainPoint->feature[j]; int tt = testPoint->feature[j]; if(tr != tt) std::cerr << i << " is not the same, wtf" << std::endl; } } */ // This is a test for 1-nn. int error = 0; for(int o = 0; o < testing->size(); o++) { std::cerr << "classifying image " << o+1 << std::endl; //std::cerr << "classifying image " << 1 << std::endl; TRPoint* testPoint = testing->getElement(o); //TRPoint* testPoint = testing->getElement(10); int currentDist = INT_MAX; int currentLabel = INT_MAX; int imageNumber = INT_MAX; for(int i = 0; i < training->size(); i++) { TRPoint* trainPoint = training->getElement(i); int distance = 0; for(int j = 0; j < trainPoint->size; j++) { int difference = trainPoint->feature[j] - testPoint->feature[j]; // difference = difference * difference; if(difference < 0) difference = (-1) * difference; distance = distance + difference; } if(distance < currentDist) { // std::cerr << "found shorter distance " << distance << " with label " << trainPoint->label << " in image " << i << std::endl; currentDist = distance; currentLabel = trainPoint->label; imageNumber = i; } } if(currentLabel != testPoint->label) { std::cerr << "image " << o+1 << " has an error" << std::endl; //std::cout << "image " << o+1 << " has an error" << std::endl; // std::cerr << "image " << 1 << " has an error" << std::endl; std::cerr << "classified with label " << currentLabel << std::endl; // std::cout << "classified with label " << currentLabel << std::endl; //printImage(training->getElement(imageNumber), columns, rows); std::cerr << "actual label is " << testPoint->label << std::endl; // std::cout << "actual label is " << testPoint->label << std::endl; //printImage(testPoint, columns, rows); error++; } std::cout << imageNumber << std::endl; } // This is a test for 1-nn. /* int error = 0; for(int o = 0; o < testing->size(); o++) { std::cerr << "classifying image " << o+1 << std::endl; // Get the testing point to be classified. TRPoint* testPoint = testing->getElement(o); int currentDist = INT_MAX; int currentLabel = INT_MAX; int imageNumber = INT_MAX; std::vector<DLabel> pairs; // Compute the distance of the test point, and each training point. for(int i = 0; i < training->size(); i++) { TRPoint* trainPoint = training->getElement(i); int distance = 0; for(int j = 0; j < trainPoint->size; j++) { int difference = trainPoint->feature[j] - testPoint->feature[j]; if(difference < 0) difference = (-1) * difference; distance = distance + difference; } pairs.push_back(DLabel(distance, trainPoint->label, i)); // if(distance < currentDist) { // currentDist = distance; // currentLabel = trainPoint->label; // imageNumber = i; // } } std::vector<DLabel> nn = k_nn(3, pairs); currentLabel = majority(nn); if(currentLabel != testPoint->label) { std::cerr << "image " << o+1 << " has an error" << std::endl; std::cout << "image " << o+1 << " has an error" << std::endl; std::cerr << "classified with label " << currentLabel << std::endl; std::cout << "classified with label " << currentLabel << std::endl; //printImage(training->getElement(imageNumber), columns, rows); std::cerr << "actual label is " << testPoint->label << std::endl; std::cout << "actual label is " << testPoint->label << std::endl; printImage(testPoint, columns, rows); error++; } } */ std::cerr << "total error " << error << std::endl; return 0; }