Skip to content

Commit f523672

Browse files
refactor: reorder model resolution in AutoModel to prioritize generic models in AutoModel
This ensures `AutoModel::fromPretrained` returns the most generic model class when no specific task model is found, improving expected behavior and reducing accidental task-specific model selection.
1 parent 7f48433 commit f523672

File tree

3 files changed

+21
-58
lines changed

3 files changed

+21
-58
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

examples/misc/custom-object-detection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66

77
use Codewithkyrian\Transformers\Models\Auto\AutoModel;
88
use Codewithkyrian\Transformers\Processors\AutoProcessor;
9+
use Codewithkyrian\Transformers\Transformers;
910
use Codewithkyrian\Transformers\Utils\Image;
11+
use Codewithkyrian\Transformers\Utils\ImageDriver;
1012

1113
require_once './bootstrap.php';
1214

1315
ini_set('memory_limit', '-1');
1416

17+
Transformers::setup()->setImageDriver(ImageDriver::GD);
18+
1519
$processor = AutoProcessor::fromPretrained('Xenova/yolov9-c_all');
1620
$model = AutoModel::fromPretrained('Xenova/yolov9-c_all');
1721

18-
$image = Image::read(__DIR__.'/../images/multitask.png');
22+
$image = Image::read(__DIR__ . '/../images/multitask.png');
1923

2024
$inputs = $processor($image);
2125

@@ -42,10 +46,10 @@
4246
$fontScalingFactor = 0.75;
4347
$fontFile = '/Users/Kyrian/Library/Fonts/JosefinSans-Bold.ttf';
4448
$labelBoxHeight = $fontSize * 2 * $fontScalingFactor;
45-
$colors = array_map(fn () => sprintf('#%06x', mt_rand(0, 0xFFFFFF)), range(0, count($boxes) - 1));
49+
$colors = array_map(fn() => sprintf('#%06x', mt_rand(0, 0xFFFFFF)), range(0, count($boxes) - 1));
4650

4751
foreach ($boxes as $box) {
48-
$detectionLabel = $box['label'].' '.round($box['score'], 2);
52+
$detectionLabel = $box['label'] . ' ' . round($box['score'], 2);
4953
$color = $colors[array_search($box, $boxes)];
5054

5155
$image = $image
@@ -69,4 +73,4 @@
6973
);
7074
}
7175

72-
$image->save(__DIR__.'/../images/corgi-detected.jpg');
76+
$image->save(__DIR__ . '/../images/corgi-detected.jpg');

src/Models/Auto/AutoModel.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ class AutoModel extends AutoModelBase
5252
];
5353

5454
const MODELS = [
55-
...self::ENCODER_ONLY_MODELS,
56-
...self::ENCODER_DECODER_MODELS,
57-
...self::DECODER_ONLY_MODELS,
58-
59-
...AutoModelForSequenceClassification::MODELS,
60-
...AutoModelForTokenClassification::MODELS,
61-
...AutoModelForSeq2SeqLM::MODELS,
62-
...AutoModelForCausalLM::MODELS,
63-
...AutoModelForMaskedLM::MODELS,
64-
...AutoModelForQuestionAnswering::MODELS,
65-
...AutoModelForImageClassification::MODELS,
66-
...AutoModelForVision2Seq::MODELS,
67-
...AutoModelForObjectDetection::MODELS,
6855
...AutoModelForZeroShotObjectDetection::MODELS,
56+
...AutoModelForObjectDetection::MODELS,
57+
...AutoModelForVision2Seq::MODELS,
58+
...AutoModelForImageClassification::MODELS,
59+
...AutoModelForQuestionAnswering::MODELS,
60+
...AutoModelForMaskedLM::MODELS,
61+
...AutoModelForCausalLM::MODELS,
62+
...AutoModelForSeq2SeqLM::MODELS,
63+
...AutoModelForTokenClassification::MODELS,
64+
...AutoModelForSequenceClassification::MODELS,
65+
66+
...self::DECODER_ONLY_MODELS,
67+
...self::ENCODER_DECODER_MODELS,
68+
...self::ENCODER_ONLY_MODELS,
6969
];
7070

7171
const BASE_IF_FAIL = true;

0 commit comments

Comments
 (0)