Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <op_boilerplate.h>
- #include <pointercast.h>
- #include <NativeOps.h>
- #include <graph/Node.h>
- #include <graph/Variable.h>
- #include <graph/VariableSpace.h>
- #include <NDArray.h>
- #include <cnpy.h>
- #include <ops/ops.h>
- #include <helpers/shape.h>
- #include <ops/gemm.h>
- #include <GraphExecutioner.h>
- #include <iostream>
- #include <vector>
- #include <stdio.h>
- #include <libpng/png.h>
- using std::cout;
- using std::cerr;
- using std::endl;
- using std::string;
- using namespace nd4j;
- void abort_(const char * s, ...)
- {
- va_list args;
- va_start(args, s);
- vfprintf(stderr, s, args);
- fprintf(stderr, "\n");
- va_end(args);
- abort();
- }
- void read_size(const char *file_name, int& width, int& height){
- png_structp png_ptr;
- png_infop info_ptr;
- unsigned char header[8]; // 8 is the maximum size that can be checked
- /* open file and test for it being a png */
- FILE *fp = fopen(file_name, "rb");
- if (!fp)
- abort_("[read_png_file] File %s could not be opened for reading", file_name);
- fread(header, 1, 8, fp);
- if (png_sig_cmp(header, 0, 8))
- abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
- /* initialize stuff */
- png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr)
- abort_("[read_png_file] png_create_read_struct failed");
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr)
- abort_("[read_png_file] png_create_info_struct failed");
- if (setjmp(png_jmpbuf(png_ptr)))
- abort_("[read_png_file] Error during init_io");
- png_init_io(png_ptr, fp);
- png_set_sig_bytes(png_ptr, 8);
- png_read_info(png_ptr, info_ptr);
- width = png_get_image_width(png_ptr, info_ptr);
- height = png_get_image_height(png_ptr, info_ptr);
- }
- void read_png_file(const char *file_name, NDArray* output) {
- int x, y;
- int width, height;
- png_byte color_type;
- png_byte bit_depth;
- png_structp png_ptr;
- png_infop info_ptr;
- int number_of_passes;
- png_bytep * row_pointers;
- unsigned char header[8]; // 8 is the maximum size that can be checked
- /* open file and test for it being a png */
- FILE *fp = fopen(file_name, "rb");
- if (!fp)
- abort_("[read_png_file] File %s could not be opened for reading", file_name);
- fread(header, 1, 8, fp);
- if (png_sig_cmp(header, 0, 8))
- abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
- /* initialize stuff */
- png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr)
- abort_("[read_png_file] png_create_read_struct failed");
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr)
- abort_("[read_png_file] png_create_info_struct failed");
- if (setjmp(png_jmpbuf(png_ptr)))
- abort_("[read_png_file] Error during init_io");
- png_init_io(png_ptr, fp);
- png_set_sig_bytes(png_ptr, 8);
- png_read_info(png_ptr, info_ptr);
- width = png_get_image_width(png_ptr, info_ptr);
- height = png_get_image_height(png_ptr, info_ptr);
- color_type = png_get_color_type(png_ptr, info_ptr);
- bit_depth = png_get_bit_depth(png_ptr, info_ptr);
- printf("bpp: %d\n", bit_depth);
- number_of_passes = png_set_interlace_handling(png_ptr);
- png_read_update_info(png_ptr, info_ptr);
- /* read file */
- if (setjmp(png_jmpbuf(png_ptr)))
- abort_("[read_png_file] Error during read_image");
- row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
- for (y=0; y<height; y++)
- row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
- png_read_image(png_ptr, row_pointers);
- printf("rowbytes: %d \n", png_get_rowbytes(png_ptr,info_ptr) );
- for(int y = 0; y < height; y++) {
- for(int x = 0; x < width; x++) {
- unsigned char r,g,b;
- r = ((unsigned char *)(row_pointers[y]))[x*3];
- g = ((unsigned char *)(row_pointers[y]))[x*3+1];
- b = ((unsigned char *)(row_pointers[y]))[x*3+2];
- output->p<float>(0,y,x,0, (((float)(r))/128.0f) + 1.0f);
- output->p<float>(0,y,x,1, (((float)(g))/128.0f) + 1.0f);
- output->p<float>(0,y,x,2, (((float)(b))/128.0f) + 1.0f);
- /*output->p<float>(0,y,x,0, (float)(r)/255.0f);
- output->p<float>(0,y,x,1, (float)(g)/255.0f);
- output->p<float>(0,y,x,2, (float)(b)/255.0f);*/
- if(y==50){
- //printf("x=%d, y=%d, pixel = %d %d %d %f\n", x,y,r,g,b, 1.0f-(float)(r)/128.0f);
- }
- }
- }
- fclose(fp);
- }
- void read_csv(string filename, NDArray* output, int width){
- char * line;
- FILE *fp;
- size_t size=256;
- fp = fopen(filename.c_str(),"r");
- if(!fp) {
- printf("Could not open file %s\n", filename.c_str());
- exit(-1);
- }
- int ind=0;
- line = (char*)(malloc(256));
- ssize_t read=1;
- read=getline(&line, &size, fp);
- while(read>0){
- int x = (ind/3) % width;
- int y = ind/(3*width);
- output->p<float>(0,y,x, ind%3, atof(line));
- read=getline(&line, &size, fp);
- ind++;
- }
- free(line);
- return;
- }
- void showVariable(Graph* graph){
- std::vector<Variable*> variablesVector = graph->getVariableSpace()->getVariables();
- for(int i=0;i<variablesVector.size();i++){
- Variable* var = variablesVector[i];
- //varNames[*(var->getName())] = var;
- printf("_%s_ \n", var->getName()->c_str());
- }
- }
- int main(int argc, char** argv){
- /*if(argc<2){
- cerr << "Missing argument." << endl << "Usage: ./GraphExecutor model.fb [image.png] [input_layer_name] [output_layer_name]" <<endl;
- return -1;
- }*/
- string inFilename = "/home/yves/dl4j/models/flatBufferModels/master_version/mobilenet_v1_0.5_128_frozen.fb";
- string imageFile = "/home/yves/dl4j/datasets/carsAndCats_128/cat1.png";
- string inputLayerName = "input";
- string outputLayerName = "MobilenetV1/Predictions/Reshape_1";
- if(argc>1) inFilename.assign(argv[1]);
- if(argc>2) imageFile.assign(argv[2]);
- if(argc>3) inputLayerName.assign(argv[3]);
- if(argc>4) outputLayerName.assign(argv[4]);
- /*nd4j::Environment::getInstance()->setElementwiseThreshold(100000000);
- nd4j::Environment::getInstance()->setTadThreshold(100000000);
- nd4j::Environment::getInstance()->setDebug(false);
- nd4j::Environment::getInstance()->setVerbose(false);*/
- printf("Import FlatBuffer\n");
- auto graph = GraphExecutioner::importFromFlatBuffers(inFilename.c_str());
- printf("Build Graph\n");
- graph->buildGraph();
- showVariable(graph);
- //graph->printOut();
- Variable* input = graph->getVariableSpace()->getVariable(&inputLayerName);
- std::vector<Nd4jLong> shape;
- int width, height;
- read_size(imageFile.c_str(), width, height);
- shape.push_back(1);
- shape.push_back(height);
- shape.push_back(width);
- shape.push_back(3);
- NDArray* inputArray = new NDArray('f', shape, nd4j::DataType::FLOAT32);
- inputArray->assign(0.0f);
- //read_png_file(imageFile.c_str(), &inputArray);
- //read_csv("/home/yves/dl4j/datasets/mobilev1.csv", &inputArray, width);
- //read_png_file(imageFile.c_str(), graph->getVariableSpace()->getVariable(&inputLayerName)->getNDArray());
- input->setNDArray(inputArray);
- printf("Before:%s \n", input->getName()->c_str());
- Variable *v = input->clone();
- printf("After:%s \n", v->getName()->c_str());
- //graph->getVariableSpace()->replaceVariable(new Variable(input));
- graph->getVariableSpace()->replaceVariable(input->clone());
- printf("Executing graph\n");
- Nd4jStatus status = GraphExecutioner::execute(graph);
- printf("Execution finished\n");
- std::vector<Variable*> results = *graph->fetchOutputs();
- auto resultVar = graph->getVariableSpace()->getVariable(&outputLayerName);
- printf("Empty? %d\n", resultVar->isEmpty());
- NDArray* result = graph->getVariableSpace()->getVariable(&outputLayerName)->getNDArray();
- //resultVar = graph->fetchOutputs()->at(0);
- //result = resultVar->getNDArray();
- printf("%s: \n", resultVar->getName()->c_str());
- std::vector<float> rvec = result->getBufferAsVector<float>();
- printf("argMax %d: %f \n", result->argMax(), rvec[result->argMax()]);
- /*for( int i=0;i<rvec.size();i++){
- if(rvec[i]>0.001)
- printf("%d: %f \n", i, rvec[i]);
- }
- printf("\n");*/
- nd4j_printf("Execution status=%d\n",status);
- }
Add Comment
Please, Sign In to add comment