Classification
Components that assign labels to texts, images, audio, videos and multimodal samples.
AudioClassifier(*, labels=None, instructions=None, k_shot=None, adaptation='examples', backend=None, batch_size=8, on_error='raise', random_state=None)
Bases: BaseClassifier
Assign labels to audio, such as sound events, music genres or emotions, zero-shot or few-shot.
With labels and no targets, the classifier predicts zero-shot. With targets, it learns the classes from them and
passes training samples to the backend as examples, one per class by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list | dict | None
|
The candidate labels, as a list, or as a dict that maps every label to a description. |
None
|
instructions
|
str | None
|
Extra guidance on how to assign the labels. |
None
|
k_shot
|
int | ArrayLike | None
|
How many training samples are passed to the backend as examples. |
None
|
adaptation
|
Literal['examples', 'finetune']
|
|
'examples'
|
backend
|
str | Backend | None
|
The backend, as |
None
|
batch_size
|
int
|
The number of samples submitted to the backend at once. |
8
|
on_error
|
Literal['raise', 'ignore']
|
|
'raise'
|
random_state
|
int | None
|
Controls the selection of examples and the seeds sent to the backend. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
backend_ |
Backend
|
The resolved backend. |
classes_ |
ndarray
|
The class labels. |
multilabel_ |
bool
|
Whether the targets are a multilabel indicator matrix. |
examples_ |
tuple
|
The labeled examples passed to the backend. |
model_ |
str
|
The id of the fine-tuned model. It is set only with |
Examples:
>>> import numpy as np
>>> from skai.classification import AudioClassifier
>>> classifier = AudioClassifier(labels=['dog bark', 'siren'], backend='dummy').fit()
>>> classifier.predict_proba([(np.zeros(16000), 16000), (np.ones(8000), 16000)]).shape
(2, 2)
Source code in src/skai/classification/_audio.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
ImageClassifier(*, labels=None, instructions=None, k_shot=None, adaptation='examples', backend=None, batch_size=8, on_error='raise', random_state=None)
Bases: BaseClassifier
Assign labels to images with zero-shot or few-shot prediction.
With labels and no targets, the classifier predicts zero-shot. With targets, it learns the classes from them and
passes k_shot of the training images to the backend as labeled examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list | dict | None
|
The candidate labels, as a list, or as a dict that maps every label to a description. |
None
|
instructions
|
str | None
|
Extra guidance on how to assign the labels. |
None
|
k_shot
|
int | ArrayLike | None
|
How many training images are passed to the backend as examples. |
None
|
adaptation
|
Literal['examples', 'finetune']
|
|
'examples'
|
backend
|
str | Backend | None
|
The backend, as |
None
|
batch_size
|
int
|
The number of images submitted to the backend at once. |
8
|
on_error
|
Literal['raise', 'ignore']
|
|
'raise'
|
random_state
|
int | None
|
Controls the selection of examples and the seeds sent to the backend. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
backend_ |
Backend
|
The resolved backend. |
classes_ |
ndarray
|
The class labels. |
multilabel_ |
bool
|
Whether the targets are a multilabel indicator matrix. |
examples_ |
tuple
|
The labeled examples passed to the backend. |
model_ |
str
|
The id of the fine-tuned model. It is set only with |
Examples:
>>> from PIL import Image
>>> from skai.classification import ImageClassifier
>>> classifier = ImageClassifier(labels=['cat', 'dog'], backend='dummy').fit()
>>> classifier.predict_proba([Image.new('RGB', (32, 32))]).shape
(1, 2)
Source code in src/skai/classification/_image.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
MultimodalClassifier(*, labels=None, instructions=None, n_frames=8, k_shot=None, adaptation='examples', backend=None, batch_size=4, on_error='raise', random_state=None)
Bases: BaseClassifier
Assign labels to multimodal samples, zero-shot or few-shot, sending every named part in one request.
A sample is a dict, or a DataFrame row, of named texts, images, audio and videos. Strings are texts unless they are
URLs or paths of existing media files. With labels and no targets, the classifier predicts zero-shot. With
targets, it learns the classes from them and passes training samples to the backend as examples, one per class by
default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list | dict | None
|
The candidate labels, as a list, or as a dict that maps every label to a description. |
None
|
instructions
|
str | None
|
Extra guidance on how to assign the labels. |
None
|
n_frames
|
int
|
The number of frames sampled from every video part. |
8
|
k_shot
|
int | ArrayLike | None
|
How many training samples are passed to the backend as examples. |
None
|
adaptation
|
Literal['examples', 'finetune']
|
|
'examples'
|
backend
|
str | Backend | None
|
The backend, as |
None
|
batch_size
|
int
|
The number of samples submitted to the backend at once. |
4
|
on_error
|
Literal['raise', 'ignore']
|
|
'raise'
|
random_state
|
int | None
|
Controls the selection of examples and the seeds sent to the backend. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
backend_ |
Backend
|
The resolved backend. |
classes_ |
ndarray
|
The class labels. |
multilabel_ |
bool
|
Whether the targets are a multilabel indicator matrix. |
examples_ |
tuple
|
The labeled examples passed to the backend. |
model_ |
str
|
The id of the fine-tuned model. It is set only with |
Examples:
>>> from PIL import Image
>>> from skai.classification import MultimodalClassifier
>>> post = {'image': Image.new('RGB', (8, 8)), 'caption': 'hello'}
>>> classifier = MultimodalClassifier(labels=['safe', 'unsafe'], backend='dummy').fit()
>>> classifier.predict([post]).shape
(1,)
Source code in src/skai/classification/_multimodal.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
TextClassifier(*, labels=None, instructions=None, k_shot=None, adaptation='examples', backend=None, batch_size=16, on_error='raise', random_state=None)
Bases: BaseClassifier
Assign labels to texts with zero-shot or few-shot prediction.
With labels and no targets, the classifier predicts zero-shot. With targets, it learns the classes from them and
passes k_shot of the training texts to the backend as labeled examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list | dict | None
|
The candidate labels, as a list, or as a dict that maps every label to a description. |
None
|
instructions
|
str | None
|
Extra guidance on how to assign the labels. |
None
|
k_shot
|
int | ArrayLike | None
|
How many training texts are passed to the backend as examples. |
None
|
adaptation
|
Literal['examples', 'finetune']
|
|
'examples'
|
backend
|
str | Backend | None
|
The backend, as |
None
|
batch_size
|
int
|
The number of texts submitted to the backend at once. |
16
|
on_error
|
Literal['raise', 'ignore']
|
|
'raise'
|
random_state
|
int | None
|
Controls the selection of examples and the seeds sent to the backend. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
backend_ |
Backend
|
The resolved backend. |
classes_ |
ndarray
|
The class labels. |
multilabel_ |
bool
|
Whether the targets are a multilabel indicator matrix. |
examples_ |
tuple
|
The labeled examples passed to the backend. |
model_ |
str
|
The id of the fine-tuned model. It is set only with |
Examples:
>>> from skai.classification import TextClassifier
>>> classifier = TextClassifier(labels=['positive', 'negative'], backend='dummy').fit()
>>> classifier.predict(['I love it.', 'I hate it.']).shape
(2,)
Source code in src/skai/classification/_text.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
VideoClassifier(*, labels=None, instructions=None, n_frames=8, k_shot=None, adaptation='examples', backend=None, batch_size=4, on_error='raise', random_state=None)
Bases: BaseClassifier
Assign labels to videos, zero-shot or few-shot, from frames sampled evenly over each video.
With labels and no targets, the classifier predicts zero-shot. With targets, it learns the classes from them and
passes training videos to the backend as examples, one per class by default. Backends that accept audio also
receive the audio track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list | dict | None
|
The candidate labels, as a list, or as a dict that maps every label to a description. |
None
|
instructions
|
str | None
|
Extra guidance on how to assign the labels. |
None
|
n_frames
|
int
|
The number of frames sampled from every video. |
8
|
k_shot
|
int | ArrayLike | None
|
How many training videos are passed to the backend as examples. |
None
|
adaptation
|
Literal['examples', 'finetune']
|
|
'examples'
|
backend
|
str | Backend | None
|
The backend, as |
None
|
batch_size
|
int
|
The number of videos submitted to the backend at once. |
4
|
on_error
|
Literal['raise', 'ignore']
|
|
'raise'
|
random_state
|
int | None
|
Controls the selection of examples and the seeds sent to the backend. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
backend_ |
Backend
|
The resolved backend. |
classes_ |
ndarray
|
The class labels. |
multilabel_ |
bool
|
Whether the targets are a multilabel indicator matrix. |
examples_ |
tuple
|
The labeled examples passed to the backend. |
model_ |
str
|
The id of the fine-tuned model. It is set only with |
Examples:
>>> import numpy as np
>>> from skai.classification import VideoClassifier
>>> from skai.core import Video
>>> video = Video(np.zeros((20, 8, 8, 3), dtype=np.uint8), fps=10)
>>> classifier = VideoClassifier(labels=['cooking', 'sports'], n_frames=4, backend='dummy').fit()
>>> classifier.predict([video, video]).shape
(2,)
Source code in src/skai/classification/_video.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |