PHP code example of ankane / onnxruntime

1. Go to this page and download the library: Download ankane/onnxruntime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ankane / onnxruntime example snippets


$model = new OnnxRuntime\Model('model.onnx');
$model->predict(['x' => [1, 2, 3]]);

$model->inputs();

$model->outputs();

$model->metadata();

$stream = fopen('model.onnx', 'rb');
$model = new OnnxRuntime\Model($stream);

$model->predict(['x' => [1, 2, 3]], outputNames: ['label']);

use OnnxRuntime\ExecutionMode;
use OnnxRuntime\GraphOptimizationLevel;

new OnnxRuntime\Model(
    $path,
    enableCpuMemArena: true,
    enableMemPattern: true,
    enableProfiling: false,
    executionMode: ExecutionMode::Sequential, // or Parallel
    freeDimensionOverridesByDenotation: null,
    freeDimensionOverridesByName: null,
    graphOptimizationLevel: GraphOptimizationLevel::None, // or Basic, Extended, All
    interOpNumThreads: null,
    intraOpNumThreads: null,
    logSeverityLevel: 2,
    logVerbosityLevel: 0,
    logid: 'tag',
    optimizedModelFilepath: null,
    profileFilePrefix: 'onnxruntime_profile_',
    sessionConfigEntries: null
);

$model->predict(
    $inputFeed,
    outputNames: null,
    logSeverityLevel: 2,
    logVerbosityLevel: 0,
    logid: 'tag',
    terminate: false
);

$session = new OnnxRuntime\InferenceSession('model.onnx');
$session->run(null, ['x' => [1, 2, 3]]);

OnnxRuntime\Datasets::example('sigmoid.onnx');

OnnxRuntime\FFI::$lib = 'path/to/lib/libonnxruntime.so'; // onnxruntime.dll for Windows

$model = new OnnxRuntime\Model('model.onnx', providers: ['CUDAExecutionProvider']);

$model = new OnnxRuntime\Model('model.onnx', providers: ['CoreMLExecutionProvider']);
sh
git clone https://github.com/ankane/onnxruntime-php.git
cd onnxruntime-php
composer install
composer test