-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from CodeWithKyrian/add-object-detection-pipeline
Add Object Detection Pipeline
- Loading branch information
Showing
21 changed files
with
686 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codewithkyrian\Transformers\Pipelines; | ||
|
||
use function Codewithkyrian\Transformers\Utils\memoryUsage; | ||
use function Codewithkyrian\Transformers\Utils\timeUsage; | ||
|
||
require_once './bootstrap.php'; | ||
|
||
ini_set('memory_limit', '-1'); | ||
|
||
$detector = pipeline('object-detection', 'Xenova/detr-resnet-50'); | ||
|
||
$img = __DIR__. '/../images/cats.jpg'; | ||
|
||
$output = $detector($img, threshold: 0.9); | ||
|
||
dd($output, timeUsage(), memoryUsage()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\FeatureExtractors; | ||
|
||
use Codewithkyrian\Transformers\Models\Output\ObjectDetectionOutput; | ||
use Codewithkyrian\Transformers\Models\Output\ModelOutput; | ||
use Codewithkyrian\Transformers\Processors\Processor; | ||
use Codewithkyrian\Transformers\Utils\Image; | ||
use Codewithkyrian\Transformers\Utils\Tensor; | ||
use Interop\Polite\Math\Matrix\NDArray; | ||
|
||
class DetrFeatureExtractor extends ImageFeatureExtractor | ||
{ | ||
/** | ||
* Calls the feature extraction process on an array of images, preprocesses | ||
* each image, and concatenates the resulting features into a single Tensor. | ||
* @param Image|array $images The image(s) to extract features from. | ||
* @return array An object containing the concatenated pixel values of the preprocessed images. | ||
*/ | ||
public function __invoke(Image|array $images, ...$args): array | ||
{ | ||
$result = parent::__invoke($images, $args); | ||
|
||
|
||
// TODO support differently-sized images, for now assume all images are the same size. | ||
// TODO support different mask sizes (not just 64x64) | ||
// Currently, just fill pixel mask with 1s | ||
$maskSize = [$result['pixel_values']->shape()[0], 64, 64]; | ||
|
||
$pixelMaskData = array_fill(0, array_product($maskSize), 1); | ||
|
||
$pixelMask = new Tensor($pixelMaskData, NDArray::int64, $maskSize); | ||
|
||
return ['pixel_values' => $result['pixel_values'], 'pixel_mask' => $pixelMask]; | ||
} | ||
|
||
|
||
/** | ||
* Post-processes the outputs of the model (for object detection). | ||
* @param ObjectDetectionOutput $outputs The outputs of the model that must be post-processed | ||
* @param float $threshold The threshold to use for the scores. | ||
* @param array|null $targetSizes The sizes of the original images. | ||
* @param bool $isZeroShot Whether zero-shot object detection was performed. | ||
* @return array An array of objects containing the post-processed outputs. | ||
*/ | ||
public function postProcessObjectDetection(ObjectDetectionOutput $outputs, float $threshold = 0.5, ?array $targetSizes = null, bool $isZeroShot = false): array | ||
{ | ||
return Processor::postProcessObjectDetection($outputs, $threshold, $targetSizes, $isZeroShot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Auto; | ||
|
||
class AutoModelForObjectDetection extends PretrainedMixin | ||
{ | ||
const MODEL_CLASS_MAPPING = [ | ||
'detr' => \Codewithkyrian\Transformers\Models\Pretrained\DetrForObjectDetection::class, | ||
'yolos' => \Codewithkyrian\Transformers\Models\Pretrained\YolosForObjectDetection::class, | ||
]; | ||
|
||
const MODEL_CLASS_MAPPINGS = [ | ||
self::MODEL_CLASS_MAPPING, | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Output; | ||
|
||
use Codewithkyrian\Transformers\Utils\Tensor; | ||
|
||
class DetrSegmentationOutput implements ModelOutput | ||
{ | ||
/** | ||
* These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). | ||
* | ||
* @param Tensor $logits Classification logits (including no-object) for all queries. | ||
* @param Tensor $predBoxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). | ||
* @param Tensor $predMasks Segmentation masks for all queries. | ||
*/ | ||
public function __construct(public readonly Tensor $logits, public readonly Tensor $predBoxes, public readonly Tensor $predMasks) | ||
{ | ||
} | ||
|
||
|
||
public static function fromOutput(array $array): self | ||
{ | ||
return new self($array['logits'], $array['pred_boxes'], $array['pred_masks']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Output; | ||
|
||
use Codewithkyrian\Transformers\Utils\Tensor; | ||
|
||
class ObjectDetectionOutput implements ModelOutput | ||
{ | ||
/** | ||
* These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). | ||
* | ||
* @param Tensor $logits Classification logits (including no-object) for all queries. | ||
* @param Tensor $predBoxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). | ||
*/ | ||
public function __construct(public readonly Tensor $logits, public readonly Tensor $predBoxes) | ||
{ | ||
} | ||
|
||
|
||
public static function fromOutput(array $array): self | ||
{ | ||
return new self($array['logits'], $array['pred_boxes']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
use Codewithkyrian\Transformers\Models\Output\ObjectDetectionOutput; | ||
|
||
class DetrForObjectDetection extends DetrPretrainedModel | ||
{ | ||
public function __invoke(array $modelInputs): ObjectDetectionOutput | ||
{ | ||
return ObjectDetectionOutput::fromOutput(parent::__invoke($modelInputs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
use Codewithkyrian\Transformers\Models\Output\DetrSegmentationOutput; | ||
|
||
class DetrForSegmentation extends DetrPretrainedModel | ||
{ | ||
public function __invoke(array $modelInputs): DetrSegmentationOutput | ||
{ | ||
return DetrSegmentationOutput::fromOutput(parent::__invoke($modelInputs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
class DetrModel extends DetrPretrainedModel | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
class DetrPretrainedModel extends PretrainedModel | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
use Codewithkyrian\Transformers\Models\Output\ObjectDetectionOutput; | ||
|
||
class YolosForObjectDetection extends YolosPretrainedModel | ||
{ | ||
public function __invoke(array $modelInputs): ObjectDetectionOutput | ||
{ | ||
return ObjectDetectionOutput::fromOutput(parent::__invoke($modelInputs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
class YolosModel extends YolosPretrainedModel | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Codewithkyrian\Transformers\Models\Pretrained; | ||
|
||
class YolosPretrainedModel extends PretrainedModel | ||
{ | ||
|
||
} |
Oops, something went wrong.