TensorFlow DLL for Windows (64 bits)

TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 7:12 am

http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/

http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/lastSuccessfulBuild/artifact/lib_package/libtensorflow-cpu-windows-x86_64.zip

Using tensorflow.dll from a FWH 64 bits app:

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

function Main()

   MsgInfo( TF_Version() )

return nil

DLL FUNCTION TF_Version() AS LPSTR LIB "tensorflow.dll"


Image

TensorFlow documentation:
https://tensorflow.github.io/rust/tensorflow_sys/index.html
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 7:20 am

I have asked them for a 32 bits tensorflow.dll version:

https://github.com/migueldeicaza/TensorFlowSharp/issues/49
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 7:56 am

Starting to use TensorFlow:

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

static hDLL

function Main()

   local hSession, hStatus

   hDLL = LoadLibrary( "tensorflow.dll" )

   MsgInfo( TF_Version() )

   hSession = TF_NewSessionOptions()
   hStatus = TF_NewStatus()

   MsgInfo( hSession )
   MsgInfo( hStatus )

   FreeLibrary( hDLL )

return nil

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL


Using this example as a tutorial:
https://docs.rs/crate/tensorflow-sys/0.3.1/source/examples/workflow.rs
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 8:08 am

Creating our first TF session:

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

static hDLL

function Main()

   local hGraph, hOptions, hStatus, hSession

   hDLL = LoadLibrary( "tensorflow.dll" )

   MsgInfo( TF_Version() )

   hGraph = TF_NewGraph()
   hOptions = TF_NewSessionOptions()
   hStatus = TF_NewStatus()
   hSession = TF_NewSession( hGraph, hOptions, hStatus )

   MsgInfo( hSession )

   FreeLibrary( hDLL )

return nil

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 8:19 am

Cleaning the created objetcs:

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

static hDLL

function Main()

   local hGraph, hOptions, hStatus, hSession

   hDLL = LoadLibrary( "tensorflow.dll" )

   MsgInfo( TF_Version() )

   hGraph = TF_NewGraph()
   hOptions = TF_NewSessionOptions()
   hStatus = TF_NewStatus()
   hSession = TF_NewSession( hGraph, hOptions, hStatus )

   MsgInfo( hSession )

   TF_CloseSession( hSession, hStatus )
   TF_DeleteSession( hSession, hStatus )
   TF_DeleteStatus( hStatus )
   TF_DeleteSessionOptions( hOptions )

   FreeLibrary( hDLL )

return nil

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby thefull » Fri Jun 02, 2017 12:26 pm

Ohh fantástico!!! :-)
User avatar
thefull
 
Posts: 731
Joined: Fri Oct 07, 2005 7:42 am
Location: Barcelona

Re: TensorFlow DLL for Windows (64 bits)

Postby cnavarro » Fri Jun 02, 2017 4:33 pm

Genial Antonio
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6549
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Fri Jun 02, 2017 8:33 pm

Importing a TensorFlow Graph definition file:

Download the Graph definition file from here:
https://bitbucket.org/fivetech/fivewin-contributions/downloads/graph.pb
It has been created using this python code: https://docs.rs/crate/tensorflow-sys/0.3.1/source/examples/fixtures/graph.py

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

static hDLL

function Main()

   local hGraph, hOptions, hStatus, hSession
   local hGraphDefOptions
   local cGraph := MemoRead( "graph.pb" )

   hDLL = LoadLibrary( "tensorflow.dll" )

   MsgInfo( TF_Version() )

   hGraph = TF_NewGraph()
   hOptions = TF_NewSessionOptions()
   hStatus = TF_NewStatus()
   hSession = TF_NewSession( hGraph, hOptions, hStatus )

   MsgInfo( hSession )
   
   hGraphDefOptions = TF_NewImportGraphDefOptions()
   
   TF_GraphImportGraphDef( hGraph, TF_NewBufferFromString( cGraph, Len( cGraph ) ), hGraphDefOptions, hStatus )

   MsgInfo( TF_GetCode( hStatus ), "zero means success" )

   TF_CloseSession( hSession, hStatus )
   TF_DeleteSession( hSession, hStatus )
   TF_DeleteStatus( hStatus )
   TF_DeleteSessionOptions( hOptions )
   TF_DeleteImportGraphDefOptions( hGraphDefOptions )

   FreeLibrary( hDLL )

return nil

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_NewImportGraphDefOptions() AS LONG LIB hDLL

DLL FUNCTION TF_GraphImportGraphDef( hGraph AS LONG, hBuffer AS LONG, hGraphDefOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewBufferFromString( cString AS LPSTR, nLegth AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_GetCode( hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_DeleteImportGraphDefOptions( hGraphDefOptions AS LONG ) AS LONG LIB hDLL
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Sat Jun 03, 2017 7:27 am

Creating a Tensor from a string and also implementing a Class TensorFlow to simplify the code:

TensorFlow programs use a tensor data structure to represent all data. You can think of a TensorFlow tensor as an n-dimensional array or list. A tensor has a static type and dynamic dimensions. Only tensors may be passed between nodes in the computation graph.

https://www.tensorflow.org/programmers_guide/dims_types

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define TF_STRING  6

static hDLL

function Main()

   local oTF := TensorFlow():New()

   MsgInfo( oTF:Version() )

   MsgInfo( oTF:TensorString( "Hello TensorFlow" ) )

   oTF:End()

return nil

CLASS TensorFlow

   DATA hGraph
   DATA hOptions
   DATA hStatus
   DATA hSession

   METHOD New()
   
   METHOD Version() INLINE TF_Version()

   METHOD TensorString( cString )

   METHOD End()

ENDCLASS

METHOD New() CLASS TensorFlow

   hDLL = LoadLibrary( "tensorflow.dll" )

   ::hGraph = TF_NewGraph()
   ::hOptions = TF_NewSessionOptions()
   ::hStatus = TF_NewStatus()
   ::hSession = TF_NewSession( ::hGraph, ::hOptions, ::hStatus )
     
return Self    

METHOD End() CLASS TensorFlow

   TF_CloseSession( ::hSession, ::hStatus )
   TF_DeleteSession( ::hSession, ::hStatus )
   TF_DeleteStatus( ::hStatus )
   TF_DeleteSessionOptions( ::hOptions )

   FreeLibrary( hDLL )

return nil

METHOD TensorString( cString ) CLASS TensorFlow

   local hTensor := TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( Len( cString ) ) )
   local nError
   
   TF_StringEncode( cString, Len( cString ), 8 + TF_TensorData( hTensor ), TF_StringEncodedSize( Len( cString ) ), ::hStatus )

   if ( nError := TF_GetCode( ::hStatus ) ) != 0
      MsgAlert( nError, "Error creating a Tensor string" )
   endif
   
return hTensor  

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_NewImportGraphDefOptions() AS LONG LIB hDLL

DLL FUNCTION TF_GraphImportGraphDef( hGraph AS LONG, hBuffer AS LONG, hGraphDefOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewBufferFromString( cString AS LPSTR, nLegth AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_GetCode( hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_DeleteImportGraphDefOptions( hGraphDefOptions AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, pData AS LONG, nLength AS LONG, pDeallocator AS LONG,;
                           pDeallocatorArgs AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_AllocateTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, nLegth AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_StringEncodedSize( nLength AS LONG ) AS LONG LIB hDLL    

DLL FUNCTION TF_TensorData( hTensor AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_StringEncode( cString AS LPSTR, nLength AS LONG, pDest AS LONG, nDestLength AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_SessionRun( hSession AS LONG, hRunOptions AS LONG, hInputs AS LONG, hInputValues AS LONG, nInputs AS LONG,;
                            hOutputs AS LONG, hOutputValues AS LONG, nOutputs AS LONG, hTargetOperations AS LONG, nTargets AS LONG,;
                            hRunMetadata AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Sat Jun 03, 2017 7:49 am

Implemented Method ImportGraph( cFileName )

Download the Graph definition file from here:
https://bitbucket.org/fivetech/fivewin-contributions/downloads/graph.pb

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define TF_STRING  6

static hDLL

function Main()

   local oTF := TensorFlow():New()

   MsgInfo( oTF:Version() )

   MsgInfo( oTF:TensorString( "Hello TensorFlow" ) )

   oTF:ImportGraph( "graph.pb" )

   oTF:End()

return nil

CLASS TensorFlow

   DATA hGraph
   DATA hOptions
   DATA hStatus
   DATA hSession

   METHOD New()
   
   METHOD Version() INLINE TF_Version()

   METHOD ImportGraph( cFileName )

   METHOD TensorString( cString )

   METHOD End()

ENDCLASS

METHOD New() CLASS TensorFlow

   hDLL = LoadLibrary( "tensorflow.dll" )

   ::hGraph = TF_NewGraph()
   ::hOptions = TF_NewSessionOptions()
   ::hStatus = TF_NewStatus()
   ::hSession = TF_NewSession( ::hGraph, ::hOptions, ::hStatus )
     
return Self    

METHOD End() CLASS TensorFlow

   TF_CloseSession( ::hSession, ::hStatus )
   TF_DeleteSession( ::hSession, ::hStatus )
   TF_DeleteStatus( ::hStatus )
   TF_DeleteSessionOptions( ::hOptions )

   FreeLibrary( hDLL )

return nil

METHOD ImportGraph( cFileName ) CLASS TensorFlow

   local cGraph := MemoRead( cFileName )
   local hGraphDefOptions := TF_NewImportGraphDefOptions()
   
   TF_GraphImportGraphDef( ::hGraph, TF_NewBufferFromString( cGraph, Len( cGraph ) ), hGraphDefOptions, ::hStatus )
   
   if ( nError := TF_GetCode( ::hStatus ) ) != 0
      MsgAlert( nError, "Error importing a Graph file" )
   endif
   
   TF_DeleteImportGraphDefOptions( hGraphDefOptions )
   
return nil  

METHOD TensorString( cString ) CLASS TensorFlow

   local hTensor := TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( Len( cString ) ) )
   local nError
   
   TF_StringEncode( cString, Len( cString ), 8 + TF_TensorData( hTensor ), TF_StringEncodedSize( Len( cString ) ), ::hStatus )

   if ( nError := TF_GetCode( ::hStatus ) ) != 0
      MsgAlert( nError, "Error creating a Tensor string" )
   endif
   
return hTensor  

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_NewImportGraphDefOptions() AS LONG LIB hDLL

DLL FUNCTION TF_GraphImportGraphDef( hGraph AS LONG, hBuffer AS LONG, hGraphDefOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewBufferFromString( cString AS LPSTR, nLegth AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_GetCode( hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_DeleteImportGraphDefOptions( hGraphDefOptions AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, pData AS LONG, nLength AS LONG, pDeallocator AS LONG,;
                           pDeallocatorArgs AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_AllocateTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, nLegth AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_StringEncodedSize( nLength AS LONG ) AS LONG LIB hDLL    

DLL FUNCTION TF_TensorData( hTensor AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_StringEncode( cString AS LPSTR, nLength AS LONG, pDest AS LONG, nDestLength AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_SessionRun( hSession AS LONG, hRunOptions AS LONG, hInputs AS LONG, hInputValues AS LONG, nInputs AS LONG,;
                            hOutputs AS LONG, hOutputValues AS LONG, nOutputs AS LONG, hTargetOperations AS LONG, nTargets AS LONG,;
                            hRunMetadata AS LONG, hStatus AS LONG ) AS VOID LIB hDLL
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Sat Jun 03, 2017 10:46 pm

TensorFlow C API header file:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api_test.cc

Code: Select all  Expand view  RUN
TF_CAPI_EXPORT extern void TF_SessionRun(
    TF_Session* session,
    // RunOptions
    const TF_Buffer* run_options,
    // Input tensors
    const TF_Output* inputs, TF_Tensor* const* input_values, int ninputs,
    // Output tensors
    const TF_Output* outputs, TF_Tensor** output_values, int noutputs,
    // Target operations
    const TF_Operation* const* target_opers, int ntargets,
    // RunMetadata
    TF_Buffer* run_metadata,
    // Output status
    TF_Status*);


Code: Select all  Expand view  RUN
// Represents a specific output of an operation.
typedef struct TF_Output {
  TF_Operation* oper;
  int index;  // The index of the output within oper.
} TF_Output;


https://tensorflow.github.io/rust/tensorflow_sys/fn.TF_NewOperation.html

Code: Select all  Expand view  RUN
pub unsafe extern fn TF_NewOperation(graph: *mut TF_Graph,
                                     op_type: *const c_char,
                                     operation_name: *const c_char)
                                     -> *mut TF_OperationDescription


Code: Select all  Expand view  RUN
pub unsafe extern fn TF_FinishOperation(desc: *mut TF_OperationDescription,
                                        status: *mut TF_Status)
                                        -> *mut TF_Operation
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Sun Jun 04, 2017 12:01 pm

Running our first TensorFlow session:

(not sure yet how to retrieve the result)

tf.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define TF_STRING  6

static hDLL

function Main()

   local oTF := TensorFlow():New()
   local hTensor, hOperationDescription, hOperation

   MsgInfo( oTF:Version() )

   hTensor = oTF:TensorString( "Hello TensorFlow" )
   hOperationDescription = TF_NewOperation( oTF:hGraph, "Const", "hello" )
   TF_SetAttrTensor( hOperationDescription, "value", hTensor, oTF:hStatus )
   TF_SetAttrType( hOperationDescription, "dtype", TF_TensorType( hTensor ) )
   hOperation = TF_FinishOperation( hOperationDescription, oTF:hStatus )


   TF_SessionRun( oTF:hSession, 0,;
                  0, 0, 0,;  // Inputs
                  TF_Output( hOperation ), hTensor, 1,;  // Outputs
                  hOperation, 1,;  // Operations
                  0, oTF:hStatus )
   
   MsgInfo( oTF:StatusCode(), "zero means ok" )

   oTF:End()

return nil

CLASS TensorFlow

   DATA hGraph
   DATA hOptions
   DATA hStatus
   DATA hSession

   METHOD New()
   
   METHOD Version() INLINE TF_Version()

   METHOD ImportGraph( cFileName )

   METHOD TensorString( cString )

   METHOD End()
   
   METHOD StatusCode() INLINE TF_GetCode( ::hStatus )

   METHOD Variable( cVarName, nDataType )

ENDCLASS

METHOD New() CLASS TensorFlow

   hDLL = LoadLibrary( "tensorflow.dll" )

   ::hGraph = TF_NewGraph()
   ::hOptions = TF_NewSessionOptions()
   ::hStatus = TF_NewStatus()
   ::hSession = TF_NewSession( ::hGraph, ::hOptions, ::hStatus )
     
return Self    

METHOD End() CLASS TensorFlow

   TF_CloseSession( ::hSession, ::hStatus )
   TF_DeleteSession( ::hSession, ::hStatus )
   TF_DeleteStatus( ::hStatus )
   TF_DeleteSessionOptions( ::hOptions )

   FreeLibrary( hDLL )

return nil

METHOD ImportGraph( cFileName ) CLASS TensorFlow

   local cGraph := MemoRead( cFileName )
   local hGraphDefOptions := TF_NewImportGraphDefOptions()
   
   TF_GraphImportGraphDef( ::hGraph, TF_NewBufferFromString( cGraph, Len( cGraph ) ), hGraphDefOptions, ::hStatus )
   
   if TF_GetCode( ::hStatus ) != 0
      MsgAlert( TF_Message( ::hStatus ), "Error importing a Graph file" )
   endif
   
   TF_DeleteImportGraphDefOptions( hGraphDefOptions )
   
return nil  

METHOD TensorString( cString ) CLASS TensorFlow

   local hTensor := TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( Len( cString ) ) )
   
   TF_StringEncode( cString, Len( cString ), 8 + TF_TensorData( hTensor ), TF_StringEncodedSize( Len( cString ) ), ::hStatus )

   if TF_GetCode( ::hStatus ) != 0
      MsgAlert( TF_Message( ::hStatus ), "Error creating a Tensor string" )
   endif
   
return hTensor  

METHOD Variable( cVarName, nDataType ) CLASS TensorFlow

   local hOperationDescription := TF_NewOperation( ::hGraph, "Variable", cVarName )

   TF_SetAttrType( hOperationDescription, "dtype", nDataType )

return TF_FinishOperation( hOperationDescription, ::hStatus )

DLL FUNCTION TF_Version() AS LPSTR LIB hDLL

DLL FUNCTION TF_NewGraph() AS LONG LIB hDLL

DLL FUNCTION TF_NewSessionOptions() AS LONG LIB hDLL

DLL FUNCTION TF_NewStatus() AS LONG LIB hDLL

DLL FUNCTION TF_NewSession( hGraph AS LONG, hOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_CloseSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSession( hSession AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteStatus( hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_DeleteSessionOptions( hOptions AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_NewImportGraphDefOptions() AS LONG LIB hDLL

DLL FUNCTION TF_GraphImportGraphDef( hGraph AS LONG, hBuffer AS LONG, hGraphDefOptions AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewBufferFromString( cString AS LPSTR, nLegth AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_GetCode( hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_DeleteImportGraphDefOptions( hGraphDefOptions AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_NewTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, pData AS LONG, nLength AS LONG, pDeallocator AS LONG,;
                           pDeallocatorArgs AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_AllocateTensor( nType AS LONG, pDims AS LONG, nDims AS LONG, nLegth AS LONG ) AS LONG LIB hDLL
                           
DLL FUNCTION TF_StringEncodedSize( nLength AS LONG ) AS LONG LIB hDLL    

DLL FUNCTION TF_TensorData( hTensor AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_StringEncode( cString AS LPSTR, nLength AS LONG, pDest AS LONG, nDestLength AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_SessionRun( hSession AS LONG, hRunOptions AS LONG, hInputs AS LONG, @hInputValues AS LONG, nInputs AS LONG,;
                            hOutputs AS LONG, @hOutputValues AS LONG, nOutputs AS LONG, @hTargetOperations AS LONG, nTargets AS LONG,;
                            hRunMetadata AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_Message( hStatus AS LONG ) AS LPSTR LIB hDLL

DLL FUNCTION TF_NewOperation( hGraph AS LONG, cOperationType AS LPSTR, cOperationName AS LPSTR ) AS LONG LIB hDLL

DLL FUNCTION TF_SetAttrTensor( hOperationDescription AS LONG, cAttributeName AS LPSTR, hTensor AS LONG, hStatus AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_TensorType( hTensor AS LONG ) AS LONG LIB hDLL

DLL FUNCTION TF_SetAttrType( hOperationDescription AS LONG, cAttributeName AS LPSTR, nDataType AS LONG ) AS VOID LIB hDLL

DLL FUNCTION TF_FinishOperation( hOperationDescription AS LONG, hStatus AS LONG ) AS LONG LIB hDLL

#pragma BEGINDUMP

#include <hbapi.h>

typedef struct TF_Output {
  void * oper;
  int index;
} TF_Output;

HB_FUNC( TF_OUTPUT )
{
   TF_Output * hOutput = ( TF_Output * ) hb_xgrab( sizeof( TF_Output ) );
   
   hOutput->oper = ( void * ) hb_parnll( 1 );
   hOutput->index = hb_parnl( 2 );
   
   hb_retnll( ( HB_LONGLONG ) hOutput );
}  

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Sun Jun 04, 2017 6:59 pm

Building a C++ (using C_API) example:

c:\bcc7164\bin\bcc64 tf.cpp -Ic:\bcc7164\include\windows\crtl -Ic:\bcc7164\include\dinkumware64 -Ic:\bcc7164\include\dinkumware -Ic:\bcc7164\include\windows\sdk -Lc:\bcc7164\lib tensorflow.a

To build the TensorFlow import library do:
c:\bcc7164\bin\mkexp tensorflow.a tensorflow.dll

example copied from here:
https://github.com/tensorflow/tensorflow/issues/5106

tf.cpp
Code: Select all  Expand view  RUN
#include <string.h>
#include <iostream.h>
#include "c_api.h"

int main() {

  // creating a TF_Graph
  TF_Graph* graph = TF_NewGraph();

  // creating a "Variable" operation
  TF_OperationDescription* opDesc = TF_NewOperation(graph, "Variable", "w");
  const long long dims[2] = {2, 2};
  TF_SetAttrShape(opDesc, "shape", dims, 2);
  TF_SetAttrType(opDesc, "dtype", TF_DOUBLE);

  TF_Status* status = TF_NewStatus();
  TF_Operation* w = TF_FinishOperation(opDesc, status);
  std::string finish_message = std::string(TF_Message(status));

  TF_Output w_port = {w, 0};
  int w_num_dims = TF_GraphGetTensorNumDims(graph, w_port, status);
  std::string num_dims_message = std::string(TF_Message(status));

  // std::cout << finish_message << '\n';
  // std::cout << w_num_dims << '\n';
  // std::cout << num_dims_message << '\n';

  TF_DeleteGraph(graph);
  TF_DeleteStatus(status);

  return 0;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Mon Jun 05, 2017 10:40 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: TensorFlow DLL for Windows (64 bits)

Postby Antonio Linares » Tue Jun 06, 2017 10:19 am

The help arrived and also a bug discovered and fixed :-)

The C code is already working so we are ready to fix the Harbour implementation too :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to TensorFlow.dll for Harbour and FWH

Who is online

Users browsing this forum: No registered users and 0 guests