Skip to content

Commit 54bdee0

Browse files
fix: Vips RGBA -> RGBA conversion error
1 parent 75f5d9c commit 54bdee0

File tree

8 files changed

+35
-32
lines changed

8 files changed

+35
-32
lines changed

examples/misc/background-removal.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use function Codewithkyrian\Transformers\Utils\{memoryPeak, memoryUsage, timeUsage};
99

1010
require_once './bootstrap.php';
11-
12-
$modelConfig = ['model_type' => 'custom'];
11+
$modelConfig = ['model_type' => 'vit'];
1312
$processorConfig = [
1413
'do_normalize' => true,
1514
'do_pad' => false,
@@ -24,9 +23,9 @@
2423
];
2524

2625
$model = AutoModel::fromPretrained(modelNameOrPath: 'briaai/RMBG-1.4', config: $modelConfig);
27-
$processor = AutoProcessor::fromPretrained(modelNameOrPath: 'briaai/RMBG-1.4', config: $processorConfig);
26+
$processor = AutoProcessor::fromPretrained(modelNameOrPath: 'briaai/RMBG-1.4');
2827

29-
$url = __DIR__ . '/../images/woman-w-bag.jpeg';
28+
$url = __DIR__ . '/../images/multitask.png';
3029

3130
$image = Image::read($url);
3231

@@ -42,7 +41,4 @@
4241

4342
$maskedImage = $image->applyMask($mask);
4443

45-
$maskedImage->save($fileName . '-masked.png');
46-
47-
dd('Done Processing!', timeUsage(), memoryUsage(), memoryPeak());
48-
44+
$maskedImage->save($fileName . '-masked.png');

examples/pipelines/asr.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-base');
1717
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/wav2vec2-large-xlsr-53-english');
1818

19-
$audioUrl = __DIR__ . '/../sounds/kyrian-dev.wav';
20-
$audioUrl = __DIR__ . '/../sounds/jfk.wav';
21-
$audioUrl = __DIR__ . '/../sounds/preamble.wav';
19+
//$audioUrl = __DIR__ . '/../sounds/kyrian-dev.wav';
20+
//$audioUrl = __DIR__ . '/../sounds/jfk.wav';
21+
//$audioUrl = __DIR__ . '/../sounds/preamble.wav';
2222
//$audioUrl = __DIR__ . '/../sounds/taunt.wav';
2323
//$audioUrl = __DIR__ . '/../sounds/gettysburg.wav';
2424
//$audioUrl = __DIR__ . '/../sounds/kyrian-speaking.wav';
2525
//$audioUrl = __DIR__ . '/../sounds/ted_60.wav';
26-
//$audioUrl = __DIR__ . '/../sounds/sample-1.mp3';
26+
$audioUrl = __DIR__ . '/../sounds/sample-1.mp3';
2727

2828

2929
$output = $transcriber($audioUrl,

src/Generation/Samplers/Sampler.php

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function getLogits(Tensor $logits, int $index): Tensor
5555
// array_splice($size, -2, replacement: [1, $vocabSize]);
5656
//
5757
// $logs = $logits->sliceWithBounds($start, $size);
58-
5958
$logits = $logits->slice($index);
6059

6160
if ($this->generationConfig->temperature > 0) {

src/Models/Auto/PretrainedMixin.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function fromPretrained(
7272
}
7373

7474
if (static::BASE_IF_FAIL) {
75-
trigger_error("Unknown model class for model type {$config->modelType}. Using base class PreTrainedModel.", E_USER_WARNING);
75+
// echo "Unknown model class for model type {$config->modelType}. Using base class PreTrainedModel.";
7676

7777
return PretrainedModel::fromPretrained(
7878
modelNameOrPath: $modelNameOrPath,

src/Models/Pretrained/PretrainedModel.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class PretrainedModel
4141
public string $mainInputName = 'input_ids';
4242

4343
/**
44-
* @param array $config The model configuration.
45-
* @param mixed $session The ONNX session.
44+
* @param AutoConfig $config The model configuration.
45+
* @param InferenceSession $session The ONNX session.
4646
*/
4747
public function __construct(
4848
public AutoConfig $config,

src/Utils/Audio.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function toTensor(int $samplerate = 41000, int $chunkSize = 2048): Tensor
106106
$audioTensor = Tensor::fromString($tensorData, Tensor::float32, [$totalOutputFrames, $this->channels()]);
107107

108108
if ($this->channels() > 1) {
109-
$audioTensor = $audioTensor->mean(1);
109+
$audioTensor = $audioTensor->mean(1)->multiply(sqrt(2));
110110
}
111111

112112
return $audioTensor->squeeze();

src/Utils/Image.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,11 @@ public function rgb(bool $force = false): static
224224

225225
// If it's a Vips image, we can extract the RGB channels
226226
if ($this->image instanceof \Imagine\Vips\Image) {
227-
$vipImage = $this->image->copy()->getVips()->extract_band(0, ['n' => 3]);
228-
return new self($vipImage, 3);
227+
/** @var \Imagine\Vips\Image $image */
228+
$image = $this->image->copy();
229+
$vipImage = $image->getVips()->extract_band(0, ['n' => 3]);
230+
$image->setVips($vipImage);
231+
return new self($image, 3);
229232
}
230233

231234
return new self($this->image->copy(), 3);
@@ -400,16 +403,21 @@ public function applyMask(Image $mask): static
400403
$this->image instanceof \Imagine\Vips\Image => $this->image->copy()->applyMask($mask->image),
401404

402405
$this->image instanceof \Imagine\Imagick\Image => (function () use ($mask) {
403-
$maskImagick = $mask->image->copy()->mask()->getImagick();
404-
$imageImagick = clone $this->image->getImagick();
405-
406-
$maskImagick->compositeImage($imageImagick, Imagick::COMPOSITE_DSTIN, 0, 0);
407-
$imageImagick->compositeImage($maskImagick, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
408-
409-
$maskImagick->clear();
410-
$maskImagick->destroy();
411-
412-
return new \Imagine\Imagick\Image($imageImagick, $this->image->palette(), $this->image->metadata());
406+
// $maskImagick = $mask->image->copy()->mask()->getImagick();
407+
// $imageImagick = clone $this->image->getImagick();
408+
//
409+
// $maskImagick->compositeImage($imageImagick, Imagick::COMPOSITE_DSTIN, 0, 0);
410+
// $imageImagick->compositeImage($maskImagick, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
411+
//
412+
// $maskImagick->clear();
413+
// $maskImagick->destroy();
414+
//
415+
// return new \Imagine\Imagick\Image($imageImagick, $this->image->palette(), $this->image->metadata());
416+
$image = $this->image->copy();
417+
$maskImage = $mask->image->copy();
418+
$maskImage->effects()->negative();
419+
$image->applyMask($maskImage);
420+
return $image;
413421
})(),
414422

415423
$this->image instanceof \Imagine\Gd\Image => (function () use ($mask) {

src/Utils/InferenceSession.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function __construct(
4343
$providers = []
4444
)
4545
{
46+
// $providers = ['CoreMLExecutionProvider', 'CPUExecutionProvider'];
4647
// session options
4748
$sessionOptions = OnnxRuntime::CreateSessionOptions();
4849

@@ -100,6 +101,7 @@ public function __construct(
100101
OnnxRuntime::AddSessionConfigEntry($sessionOptions, $k, $v);
101102
}
102103
}
104+
103105
foreach ($providers as $provider) {
104106
if (!in_array($provider, $this->providers())) {
105107
trigger_error('Provider not available: ' . $provider, E_USER_WARNING);
@@ -111,14 +113,13 @@ public function __construct(
111113
OnnxRuntime::SessionOptionsAppendExecutionProvider_CUDA_V2($sessionOptions, $cudaOptions);
112114
OnnxRuntime::ReleaseCUDAProviderOptions($cudaOptions);
113115
} elseif ($provider == 'CoreMLExecutionProvider') {
114-
OnnxRuntime::OrtSessionOptionsAppendExecutionProvider_CoreML($sessionOptions, 0);
116+
OnnxRuntime::OrtSessionOptionsAppendExecutionProvider_CoreML($sessionOptions, 1);
115117
} elseif ($provider == 'CPUExecutionProvider') {
116118
break;
117119
} else {
118120
throw new \InvalidArgumentException('Provider not supported: ' . $provider);
119121
}
120122
}
121-
122123
$this->session = $this->loadSession($path, $sessionOptions);
123124
$this->allocator = OnnxRuntime::GetAllocatorWithDefaultOptions();
124125
$this->inputs = $this->loadInputs();
@@ -260,7 +261,6 @@ private function loadSession($path, $sessionOptions): ?CData
260261
} else {
261262
$session = OnnxRuntime::CreateSession(self::env(), $this->ortString($path), $sessionOptions);
262263
}
263-
264264
return $session;
265265
}
266266

0 commit comments

Comments
 (0)