From 235176d8fb64232d675ca84239f18d810e5c37f7 Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Mon, 2 Dec 2019 15:43:12 -0800 Subject: Updates to JSON parser. --- src/base/io/io.c | 18 +++++--- src/base/io/ioJson.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 7 deletions(-) (limited to 'src/base') diff --git a/src/base/io/io.c b/src/base/io/io.c index 843b0ff6..8cc78623 100644 --- a/src/base/io/io.c +++ b/src/base/io/io.c @@ -3309,14 +3309,18 @@ usage: int IoCommandWriteJson( Abc_Frame_t * pAbc, int argc, char **argv ) { extern void Json_Write( char * pFileName, Abc_Nam_t * pStr, Vec_Wec_t * vObjs ); + extern void Json_Extract( char * pFileName, Abc_Nam_t * pStr, Vec_Wec_t * vObjs ); + int c, fExtract = 0; char * pFileName; - int c; Extra_UtilGetoptReset(); - while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF ) + while ( ( c = Extra_UtilGetopt( argc, argv, "ch" ) ) != EOF ) { switch ( c ) { + case 'c': + fExtract ^= 1; + break; case 'h': goto usage; default: @@ -3330,15 +3334,17 @@ int IoCommandWriteJson( Abc_Frame_t * pAbc, int argc, char **argv ) } if ( argc != globalUtilOptind + 1 ) goto usage; - // get the output file name pFileName = argv[globalUtilOptind]; - // call the corresponding file writer - Json_Write( pFileName, Abc_FrameReadJsonStrs(Abc_FrameReadGlobalFrame()), Abc_FrameReadJsonObjs(Abc_FrameReadGlobalFrame()) ); + if ( fExtract ) + Json_Extract( pFileName, Abc_FrameReadJsonStrs(Abc_FrameReadGlobalFrame()), Abc_FrameReadJsonObjs(Abc_FrameReadGlobalFrame()) ); + else + Json_Write( pFileName, Abc_FrameReadJsonStrs(Abc_FrameReadGlobalFrame()), Abc_FrameReadJsonObjs(Abc_FrameReadGlobalFrame()) ); return 0; usage: - fprintf( pAbc->Err, "usage: write_json [-h] \n" ); + fprintf( pAbc->Err, "usage: write_json [-ch] \n" ); fprintf( pAbc->Err, "\t write the network in JSON format\n" ); + fprintf( pAbc->Err, "\t-c : output extracted version\n" ); fprintf( pAbc->Err, "\t-h : print the help message\n" ); fprintf( pAbc->Err, "\tfile : the name of the file to write (extension .json)\n" ); return 1; diff --git a/src/base/io/ioJson.c b/src/base/io/ioJson.c index 07c152bc..14e3bfc4 100644 --- a/src/base/io/ioJson.c +++ b/src/base/io/ioJson.c @@ -38,6 +38,131 @@ static inline Vec_Int_t * Json_EntryNode( Vec_Wec_t * vObjs, int Fan ) { asse /// FUNCTION DEFINITIONS /// //////////////////////////////////////////////////////////////////////// +/**Function************************************************************* + + Synopsis [Writes JSON into a file.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +char * Nnc_LayerType2Str( char * pStr ) +{ + if ( !strcmp(pStr, "InputLayer") ) + return "input "; + if ( !strcmp(pStr, "Conv2D") ) + return "convo "; + if ( !strcmp(pStr, "BatchNormalization") ) + return "batch "; + if ( !strcmp(pStr, "Activation") ) + return "relu "; + if ( !strcmp(pStr, "Add") ) + return "eltwise"; + if ( !strcmp(pStr, "MaxPooling2D") ) + return "pool "; + if ( !strcmp(pStr, "GlobalAveragePooling2D") ) + return "pool "; + if ( !strcmp(pStr, "Dense") ) + return "fullcon"; + if ( !strcmp(pStr, "ZeroPadding2D") ) + return "pad"; +// if ( !strcmp(pStr, "InputLayer") ) +// return "softmax"; + return NULL; +} + +void Json_Extract_rec( FILE * pFile, Abc_Nam_t * pStr, Vec_Wec_t * vObjs, Vec_Int_t * vArray, int fWrite, int * pCount ) +{ + int i, Entry1, Entry2; + if ( Vec_IntEntry(vArray, 0) ) // array + { + if ( Vec_IntSize(vArray) == 1 ) + return; + if ( Vec_IntSize(vArray) == 2 && Json_EntryIsName(Vec_IntEntry(vArray,1)) ) + { + if ( fWrite ) + fprintf( pFile, "%s", Json_EntryName(pStr, Vec_IntEntry(vArray,1)) ); + return; + } + else + { + Vec_IntForEachEntryStart( vArray, Entry1, i, 1 ) + { + if ( Json_EntryIsName(Entry1) ) + { + int Digit = Json_EntryName(pStr, Entry1)[0]; + if ( fWrite && Digit != '0' ) + fprintf( pFile, "%s%s", Json_EntryName(pStr, Entry1), Digit >= '0' && Digit <= '9' ? "" : " " ); + } + else + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), fWrite, pCount ); + } + return; + } + } + else // list of pairs + { + int fHaveConfig = 0; + assert( Vec_IntSize(vArray) % 2 != 0 ); + Vec_IntForEachEntryDoubleStart( vArray, Entry1, Entry2, i, 1 ) + { + char * pName1 = Json_EntryIsName(Entry1) ? Json_EntryName(pStr, Entry1) : NULL; + char * pName2 = Json_EntryIsName(Entry2) ? Json_EntryName(pStr, Entry2) : NULL; + char * pName3 = pName2 ? Nnc_LayerType2Str(pName2) : NULL; + if ( pName1 == NULL ) + continue; + if ( !strcmp(pName1, "class_name") ) + { + if ( pName3 ) + fprintf( pFile, "\n%3d : %-8s ", (*pCount)++, pName3 ); + } + else if ( !strcmp(pName1, "name") ) + { + if ( fHaveConfig ) + fprintf( pFile, " N=%s ", pName2 ? pName2 : "???" ); + } + else if ( !strcmp(pName1, "kernel_size") ) + { + fprintf( pFile, " K=" ); + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount ); + } + else if ( !strcmp(pName1, "strides") ) + { + fprintf( pFile, " S=" ); + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount ); + } + else if ( !strcmp(pName1, "filters") ) + fprintf( pFile, " C=%s", pName2 ); + else if ( !strcmp(pName1, "inbound_nodes") ) + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount ); + else if ( !strcmp(pName1, "layers") ) + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount ); + else if ( !strcmp(pName1, "config") ) + { + fHaveConfig = 1; + Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 0, pCount ); + } + } + } +} +void Json_Extract( char * pFileName, Abc_Nam_t * pStr, Vec_Wec_t * vObjs ) +{ + int Count = 0; + FILE * pFile = fopen( pFileName, "wb" ); + if ( pFile == NULL ) + { + printf( "Cannot open file \"%s\" for writing.\n", pFileName ); + return; + } + fprintf( pFile, "# Data extracted from JSON file:\n" ); + Json_Extract_rec( pFile, pStr, vObjs, Vec_WecEntry(vObjs, 0), 0, &Count ); + fprintf( pFile, "\n" ); + fclose( pFile ); +} + /**Function************************************************************* Synopsis [Parsing.] @@ -51,7 +176,7 @@ static inline Vec_Int_t * Json_EntryNode( Vec_Wec_t * vObjs, int Fan ) { asse ***********************************************************************/ static inline int Json_CharIsSpace( char c ) { - return (c == ' ' || c == '\t' || c == '\r' || c == '\n'); + return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == ':'); } static inline char * Json_SkipSpaces( char * pCur ) { -- cgit v1.2.3