from typing import List, Union
from code_loader import leap_binder
from code_loader.contract.datasetclasses import PreprocessResponse
from code_loader.contract.enums import DatasetMetadataType, Metric
from code_loader.contract.decoder_classes import LeapHorizontalBar
def preprocess_func() -> List[PreprocessResponse]:
train = PreprocessResponse(length=len(train_X), data=train_df)
val = PreprocessResponse(length=len(val_X), data=val_df)
test = PreprocessResponse(length=len(test_X), data=test_df)
return [train, val, test]
def input_encoder(idx: int, preprocess: PreprocessResponse) -> np.ndarray:
return preprocess.data.iloc[idx]['samples'].astype('float32')
# Ground Truth Encoder(s):
def gt_encoder(idx: int, preprocess: Union[PreprocessResponse, list]) -> np.ndarray:
return preprocess.data.iloc[idx]['ground_truth'].astype('float32')
def metadata_label(idx: int, preprocess: Union[PreprocessResponse, list]) -> Union[int, float, str, bool]:
return preprocess.data.iloc[idx]['class_name']
LABELS = ['cat', 'dog', 'tiger', 'cow', 'goat', 'zebra', 'horse']
leap_binder.set_preprocess(function=preprocess_func)
leap_binder.set_input(function=input_encoder,input_name='image')
leap_binder.set_ground_truth(function=gt_encoder, gt_name='classes',)
leap_binder.set_metadata(function=metadata_label, metadata_type=DatasetMetadataType.string, name='label')
leap_binder.add_prediction_type(name='animal', labels=LABELS, metrics=[Metric.Accuracy])
def is_pet_decoder(animal_prediction: np.ndarray) -> LeapHorizontalBar:
np_labels = np.array(LABELS)
pet_confidence = animal_prediction[np_labels == 'cat'][0] + animal_prediction[np_labels == 'dog'][0]
body=np.array([pet_confidence, 1-pet_confidence])
return LeapHorizontalBar(body=body, labels=['pet', 'not-pet')
decoder_type=LeapHorizontalBar.type