Skip to content

Commit 0037528

Browse files
Merge pull request #32 from CodeWithKyrian/bump-math-matrix
Comprehensive Tensor Class Overhaul and Library Downloader
2 parents a326386 + 511fc12 commit 0037528

101 files changed

Lines changed: 2466 additions & 1185 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ playground/*
99
.idea
1010
.transformers-cache/*
1111
tests/models/*
12+
dist

bin/transformers

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $application = new Application();
1212
try {
1313
$application->setName('Transformers PHP CLI');
1414

15-
// $application->add(new Codewithkyrian\Transformers\Commands\InitCommand());
15+
$application->add(new Codewithkyrian\Transformers\Commands\InstallCommand());
1616
$application->add(new Codewithkyrian\Transformers\Commands\DownloadModelCommand());
1717

1818
$application->run();

composer.json

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
"type": "library",
1515
"require": {
1616
"php": "^8.1",
17-
"guzzlehttp/guzzle": "^7.0",
18-
"ankane/onnxruntime": "^0.2.0",
19-
"rindow/rindow-math-matrix": "^1.2",
17+
"ext-ffi": "*",
2018
"codewithkyrian/jinja-php": "^1.0",
21-
"codewithkyrian/onnxruntime-downloader-plugin": "^1.1",
22-
"symfony/console": "^6.4|^7.0",
19+
"codewithkyrian/transformers-libraries-downloader": "^1.0",
2320
"imagine/imagine": "^1.3",
24-
"rokka/imagine-vips": "^0.31.0"
21+
"symfony/console": "^6.4|^7.0",
22+
"rokka/imagine-vips": "^0.31.0",
23+
"rindow/rindow-math-matrix": "^2.0",
24+
"rindow/rindow-matlib-ffi": "^1.0",
25+
"rindow/rindow-openblas-ffi": "^1.0"
2526
},
2627
"require-dev": {
2728
"pestphp/pest": "^2.31",
@@ -46,17 +47,21 @@
4647
"config": {
4748
"allow-plugins": {
4849
"pestphp/pest-plugin": true,
49-
"codewithkyrian/onnxruntime-downloader-plugin": true
50+
"codewithkyrian/transformers-libraries-downloader": true
5051
}
5152
},
5253
"bin": [
5354
"bin/transformers"
5455
],
5556
"scripts": {
5657
"test": "vendor/bin/pest",
57-
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
58+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage",
59+
"post-install-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check",
60+
"post-update-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check"
5861
},
5962
"archive": {
60-
"exclude": ["/docs"]
63+
"exclude": [
64+
"/docs"
65+
]
6166
}
6267
}

docs/utils/image.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ can leverage this interoperability to perform advanced image processing tasks wi
268268

269269
```php
270270
use Codewithkyrian\Transformers\Utils\Image;
271-
use Codewithkyrian\Transformers\Utils\Tensor;
272271

273272
// Read an image
274273
$image = Image::read('path/to/image.jpg');

docs/utils/tensor.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ fromArray method. Below are examples of how to create tensors:
4747
### Using the Constructor
4848

4949
```php
50-
use Codewithkyrian\Transformers\Utils\Tensor;
51-
use Interop\Polite\Math\Matrix\NDArray;
50+
use Codewithkyrian\Transformers\Tensor\Tensor;use Interop\Polite\Math\Matrix\NDArray;
5251

5352
$data = [1, 2, 3, 4, 5, 6];
5453
$shape = [2, 3];
@@ -59,7 +58,7 @@ $tensor = new Tensor($data, $dtype, $shape); // If dtype is not provided, it def
5958
### Using the fromArray Method
6059

6160
```php
62-
use Codewithkyrian\Transformers\Utils\Tensor;
61+
use Codewithkyrian\Transformers\Tensor\Tensor;
6362

6463
$data = [[1, 2, 3], [4, 5, 6]];
6564
$tensor = Tensor::fromArray($data);
@@ -68,7 +67,7 @@ $tensor = Tensor::fromArray($data);
6867
### Using zeros and ones methods
6968

7069
```php
71-
use Codewithkyrian\Transformers\Utils\Tensor;
70+
use Codewithkyrian\Transformers\Tensor\Tensor;
7271

7372
$shape = [2, 3];
7473
$tensor = Tensor::zeros($shape); // Creates a tensor of zeros with the specified shape
@@ -78,7 +77,7 @@ $tensor = Tensor::ones($shape); // Creates a tensor of ones with the specified s
7877
### Using zerosLike and onesLike methods
7978

8079
```php
81-
use Codewithkyrian\Transformers\Utils\Tensor;
80+
use Codewithkyrian\Transformers\Tensor\Tensor;
8281

8382
$data = [[1, 2, 3], [4, 5, 6]];
8483
$tensor = Tensor::fromArray($data);
@@ -114,7 +113,7 @@ and buffer.
114113
Returns the tensor's flat buffer as a regular PHP array.
115114

116115
```php
117-
use Codewithkyrian\Transformers\Utils\Tensor;
116+
use Codewithkyrian\Transformers\Tensor\Tensor;
118117

119118
$data = [[1, 2, 3], [4, 5, 6]];
120119

examples/composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
"require": {
1515
"php": "^8.1",
1616
"symfony/console": "^7.0",
17-
"codewithkyrian/transformers": "dev-main"
17+
"codewithkyrian/transformers": "dev-bump-math-matrix"
1818
},
19-
"minimum-stability": "dev",
2019
"require-dev": {
2120
"symfony/var-dumper": "^7.0"
2221
},
@@ -28,7 +27,7 @@
2827
],
2928
"config": {
3029
"allow-plugins": {
31-
"codewithkyrian/onnxruntime-downloader-plugin": true
30+
"codewithkyrian/transformers-libraries-downloader": true
3231
}
3332
}
3433
}

examples/image-test.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
declare(strict_types=1);
44

5+
use Codewithkyrian\Transformers\Tensor\Tensor;
56
use Codewithkyrian\Transformers\Transformers;
67
use Codewithkyrian\Transformers\Utils\Image;
78
use Codewithkyrian\Transformers\Utils\ImageDriver;
8-
use Codewithkyrian\Transformers\Utils\Tensor;
99
use function Codewithkyrian\Transformers\Utils\timeUsage;
1010

1111
require_once './bootstrap.php';
1212

13+
ini_set('memory_limit', '2048M');
14+
1315
function toTensorTest(ImageDriver $imageDriver): Tensor
1416
{
1517
timeUsage();
@@ -18,13 +20,13 @@ function toTensorTest(ImageDriver $imageDriver): Tensor
1820
->setImageDriver($imageDriver)
1921
->apply();
2022

21-
$image = Image::read('images/butterfly.jpg');
23+
$image = Image::read('images/kyrian-cartoon.jpeg');
2224

2325
$image->rgb();
2426

2527
$tensor = $image->toTensor();
2628

27-
dump("$imageDriver->name (toTensor) : ". timeUsage());
29+
dump("$imageDriver->name (toTensor) : ". timeUsage(true));
2830

2931
return $tensor;
3032
}
@@ -37,7 +39,7 @@ function fromTensorTest(ImageDriver $imageDriver, Tensor $tensor) : Image
3739

3840
$image = Image::fromTensor($tensor);
3941

40-
dump("$imageDriver->name (fromTensor) : ". timeUsage());
42+
dump("$imageDriver->name (fromTensor) : ". timeUsage(true));
4143

4244
return $image;
4345
}
@@ -54,3 +56,6 @@ function fromTensorTest(ImageDriver $imageDriver, Tensor $tensor) : Image
5456
$image = fromTensorTest(ImageDriver::IMAGICK, $tensor);
5557
$image = fromTensorTest(ImageDriver::GD, $tensor);
5658
$image = fromTensorTest(ImageDriver::VIPS, $tensor);
59+
60+
// Save the image
61+
//$image->save('images/images/kyrian-cartoon-converted.jpeg');

examples/image_detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
$pixels = getPixels($img);
1212

13-
$model = new OnnxRuntime\Model(__DIR__ . '/models/ssd_mobilenet_v1_10.onnx');
13+
$model = new Codewithkyrian\Transformers\Utils\Model(__DIR__ . '/models/ssd_mobilenet_v1_10.onnx');
1414
$result = $model->predict(['image_tensor:0' => [$pixels]]);
1515

1616
foreach ($result['num_detections:0'] as $idx => $n) {

examples/pipelines/image-to-text.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
require_once './bootstrap.php';
1010

1111
ini_set('memory_limit', -1);
12-
//$captioner = pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning');
13-
$captioner = pipeline('image-to-text', 'Xenova/trocr-small-handwritten');
12+
$captioner = pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning');
13+
//$captioner = pipeline('image-to-text', 'Xenova/trocr-small-handwritten');
1414

1515
//$streamer = StdOutStreamer::make($captioner->tokenizer);
1616

1717
$url = __DIR__ . '/../images/beach.png';
1818
//$url = __DIR__. '/../images/handwriting.jpg';
1919
//$url = __DIR__. '/../images/handwriting3.png';
20-
$url = __DIR__ . '/../images/handwriting4.jpeg';
20+
//$url = __DIR__ . '/../images/handwriting4.jpeg';
2121

2222
$output = $captioner($url);
2323

examples/pipelines/object-detection.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Codewithkyrian\Transformers\Pipelines;
66

7-
use Codewithkyrian\Transformers\Utils\Image;
7+
use function Codewithkyrian\Transformers\Utils\memoryUsage;
8+
use function Codewithkyrian\Transformers\Utils\timeUsage;
89

910
require_once './bootstrap.php';
1011

@@ -16,16 +17,16 @@
1617

1718
$output = $detector($img, threshold: 0.9);
1819

19-
//dd($output, timeUsage(), memoryUsage());
20-
21-
$image = Image::read($img);
22-
23-
foreach ($output as $item) {
24-
$box = $item['box'];
25-
$image->drawRectangle($box['xmin'], $box['ymin'], $box['xmax'], $box['ymax'], '0099FF', thickness: 2);
26-
$image->drawText($item['label'], $box['xmin'], max($box['ymin'] - 5, 0), '/Users/Kyrian/Library/Fonts/JosefinSans-Bold.ttf', 14, '0099FF');
27-
}
28-
29-
$image->save(__DIR__ . '/../images/cats-detection.jpg');
20+
dd($output, timeUsage(), memoryUsage());
21+
22+
//$image = Image::read($img);
23+
//
24+
//foreach ($output as $item) {
25+
// $box = $item['box'];
26+
// $image->drawRectangle($box['xmin'], $box['ymin'], $box['xmax'], $box['ymax'], '0099FF', thickness: 2);
27+
// $image->drawText($item['label'], $box['xmin'], max($box['ymin'] - 5, 0), '/Users/Kyrian/Library/Fonts/JosefinSans-Bold.ttf', 14, '0099FF');
28+
//}
29+
//
30+
//$image->save(__DIR__ . '/../images/cats-detection.jpg');
3031

3132

0 commit comments

Comments
 (0)