Skip to content

Commit dae4eb6

Browse files
Cleanup and Code style review
1 parent 740a797 commit dae4eb6

5 files changed

Lines changed: 36 additions & 29 deletions

File tree

examples/image-test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function toTensorTest(ImageDriver $imageDriver): Tensor
2424

2525
$tensor = $image->toTensor();
2626

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

2929
return $tensor;
3030
}
@@ -37,7 +37,7 @@ function fromTensorTest(ImageDriver $imageDriver, Tensor $tensor) : Image
3737

3838
$image = Image::fromTensor($tensor);
3939

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

4242
return $image;
4343
}
@@ -47,10 +47,10 @@ function fromTensorTest(ImageDriver $imageDriver, Tensor $tensor) : Image
4747
dump("------------ toTensor ------------");
4848
$tensor = toTensorTest(ImageDriver::IMAGICK);
4949
$tensor = toTensorTest(ImageDriver::GD);
50-
$tensor = toTensorTest(ImageDriver::VIPS);
50+
//$tensor = toTensorTest(ImageDriver::VIPS);
5151

5252

5353
dump("------------ fromTensor ------------");
5454
$image = fromTensorTest(ImageDriver::IMAGICK, $tensor);
5555
$image = fromTensorTest(ImageDriver::GD, $tensor);
56-
$image = fromTensorTest(ImageDriver::VIPS, $tensor);
56+
//$image = fromTensorTest(ImageDriver::VIPS, $tensor);

src/Pipelines/ZeroShotClassificationPipeline.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public function __invoke(array|string $inputs, ...$args): array
9898
// Insert labels into hypothesis template
9999
$hypotheses = array_map(fn($x) => str_replace('{}', $x, $hypothesisTemplate), $candidateLabels);
100100

101-
102101
// Determine whether to perform softmax over each label independently
103102
$softmaxEach = $multiLabel || count($candidateLabels) === 1;
104103

@@ -129,23 +128,16 @@ public function __invoke(array|string $inputs, ...$args): array
129128
: Math::softmax($entailsLogits);
130129

131130
// Sort by scores (desc) and return scores with indices
132-
$scoresSorted = array_map(function ($x, $i) {
133-
return [$x, $i];
134-
}, $scores, array_keys($scores));
135-
136-
137-
usort($scoresSorted, function ($a, $b) {
138-
return $b[0] <=> $a[0];
139-
});
131+
$scores = array_map(fn($x, $i) => [$x, $i], $scores, array_keys($scores));
132+
usort($scores, fn($a, $b) => $b[0] <=> $a[0]);
140133

141134
$toReturn[] = [
142135
'sequence' => $premise,
143-
'labels' => array_map(fn($x) => $candidateLabels[$x[1]], $scoresSorted),
144-
'scores' => array_map(fn($x) => array_shift($x), $scoresSorted),
136+
'labels' => array_map(fn($x) => $candidateLabels[$x[1]], $scores),
137+
'scores' => array_map(fn($x) => array_shift($x), $scores),
145138
];
146139
}
147140

148-
149141
return $isBatched ? $toReturn : $toReturn[0];
150142
}
151143
}

src/Processors/Processor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ public static function centerToCornersFormat(array $arr): array
114114
{
115115
[$centerX, $centerY, $width, $height] = $arr;
116116

117-
$topLeftX = Math::clamp($centerX - $width / 2, 0.0, 1.0);
118-
$topLeftY = Math::clamp($centerY - $height / 2, 0.0, 1.0);
119-
$bottomRightX = Math::clamp($centerX + $width / 2, 0.0, 1.0);
120-
$bottomRightY = Math::clamp($centerY + $height / 2, 0.0, 1.0);
117+
$clampFn = fn(float $value, float $min, float $max) => max($min, min($max, $value));
118+
119+
$topLeftX = $clampFn($centerX - $width / 2, 0.0, 1.0);
120+
$topLeftY = $clampFn($centerY - $height / 2, 0.0, 1.0);
121+
$bottomRightX = $clampFn($centerX + $width / 2, 0.0, 1.0);
122+
$bottomRightY = $clampFn($centerY + $height / 2, 0.0, 1.0);
121123

122124
return [$topLeftX, $topLeftY, $bottomRightX, $bottomRightY];
123125
}

src/Utils/Image.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ public static function fromTensor(Tensor $tensor, string $channelFormat = 'CHW')
279279
default => throw new Exception("Unsupported number of channels: $channels"),
280280
};
281281

282-
$image->getImagick()->importImagePixels(0, 0, $width, $height, $map, Imagick::PIXEL_CHAR, $tensor->buffer()->toArray());
282+
$bufferArray = [];
283+
for ($i = 0; $i < $tensor->size(); $i++) {
284+
$bufferArray[] = $tensor->buffer()[$i];
285+
}
286+
287+
$image->getImagick()->importImagePixels(0, 0, $width, $height, $map, Imagick::PIXEL_CHAR, $bufferArray);
283288

284289
return new self($image, $channels);
285290
}

src/Utils/TensorBuffer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ public function __construct(int $size, int $dtype)
7272
$code = file_get_contents(__DIR__ . '/../../libs/buffer.h');
7373
self::$ffi = FFI::cdef($code);
7474
}
75+
7576
if (!isset(self::$typeString[$dtype])) {
7677
throw new InvalidArgumentException("Invalid data type");
7778
}
79+
7880
$limitsize = intdiv(self::MAX_BYTES, self::$valueSize[$dtype]);
7981
if ($size >= $limitsize) {
8082
throw new InvalidArgumentException("Data size is too large.");
8183
}
84+
8285
$this->size = $size;
8386
$this->dtype = $dtype;
8487
$declaration = self::$typeString[$dtype];
@@ -185,20 +188,25 @@ public function offsetUnset(mixed $offset): void
185188

186189
public function dump(): string
187190
{
188-
$byte = self::$valueSize[$this->dtype] * $this->size;
189-
if ($byte === 0) return '';
190-
$buf = FFI::new("char[$byte]");
191-
FFI::memcpy($buf, $this->data, $byte);
192-
return FFI::string($buf, $byte);
191+
$byteSize = self::$valueSize[$this->dtype] * $this->size;
192+
193+
if ($byteSize === 0) return '';
194+
195+
$buf = FFI::new("char[$byteSize]");
196+
FFI::memcpy($buf, $this->data, $byteSize);
197+
198+
return FFI::string($buf, $byteSize);
193199
}
194200

195201
public function load(string $string): void
196202
{
197-
$byte = self::$valueSize[$this->dtype] * $this->size;
203+
$byteSize = self::$valueSize[$this->dtype] * $this->size;
204+
198205
$strlen = strlen($string);
199-
if ($strlen != $byte) {
200-
throw new InvalidArgumentException("Unmatched data size. buffer size is $byte. $strlen byte given.");
206+
if ($strlen != $byteSize) {
207+
throw new InvalidArgumentException("Unmatched data size. buffer size is $byteSize. $strlen byte given.");
201208
}
209+
202210
FFI::memcpy($this->data, $string, $strlen);
203211
}
204212
}

0 commit comments

Comments
 (0)