Skip to content

Instantly share code, notes, and snippets.

@Tob-iee
Tob-iee / evaluate_model.py
Created September 13, 2023 14:23
Match the local copy of the images to the ones in Picsellia and push the predictions of the corresponding images to your experiment evaluation dashboard
eval_image_path = os.path.join(os.getcwd(), eval_set_local_dir)
list_eval_image = os.listdir(eval_image_path)
for img_path in list_eval_image:
eval_image_ = os.path.join(eval_image_path, img_path)
print(eval_image_)
eval_image = Image.open(eval_image_)
with torch.no_grad():
@Tob-iee
Tob-iee / fetch_img_labels.py
Created September 13, 2023 14:21
Fetch images and labels for the evaluation dataset version from your Picsellia experiment.
picsellia_eval_ds = experiment.get_dataset(name=eval_version_name)
labels_picsellia = {k: picsellia_eval_ds.get_label(k) for k in label_names}
@Tob-iee
Tob-iee / detr_model_loader.py
Created September 13, 2023 14:19
Load the image processor (encoder) and object detector (decoder).
image_processor = AutoImageProcessor.from_pretrained(finetuned_output_dir)
model = AutoModelForObjectDetection.from_pretrained(finetuned_output_dir)
@Tob-iee
Tob-iee / model_to_picsellia.py
Created September 13, 2023 14:17
Store all the model files in your Picsellia experiment artifact.
# send the trained model to picsellia
model_latest_path = os.path.join(cwd, save_dir)
file_list = os.listdir(model_latest_path)
print(file_list)
for files in file_list:
model_latest_files = os.path.join(model_latest_path, files)
experiment.store(files, model_latest_files)
@Tob-iee
Tob-iee / save_model.py
Created September 13, 2023 14:15
Save the model to your local directory.
trainer.save_model(finetuned_output_dir)
finetuned_model_path = os.path.join(os.getcwd(), finetuned_output_dir)
@Tob-iee
Tob-iee / detr_trainer.py
Created September 13, 2023 14:14
Initialize the training callback and train the model.
# initialize trainer callback
picsellia_callback = detr.CustomPicselliaCallback(experiment=experiment)
trainer = Trainer(
model=model,
args=training_args,
data_collator=collate_fn,
train_dataset=train_dataset,
tokenizer=image_processor,
callbacks=[picsellia_callback],
@Tob-iee
Tob-iee / custom_callback.py
Created September 13, 2023 14:12
Custom training callback for logging training activities to Picsellia experiment from the Trainer API class
class CustomPicselliaCallback(TrainerCallback):
def __init__(self, experiment: Experiment):
self.experiment=experiment
def on_train_begin(self, args: TrainingArguments, state: TrainerState, control, **kwargs):
print("Starting training")
def on_train_end(self, args: TrainingArguments, state: TrainerState, control, **kwargs):
"""
@Tob-iee
Tob-iee / hyperparams_log.py
Created September 13, 2023 14:10
Log the training hyperparameters to your Picsellia experiment.
# Log hyperparameters to Picesllia
training_hyp_params = training_args.to_dict()
experiment.log("hyper-parameters", training_hyp_params, type=LogType.TABLE)
@Tob-iee
Tob-iee / detr_model.py
Created September 13, 2023 14:09
Load DETR's object detection model and specify your hyperparameters for the model's training.
# Training the DETR model
model = AutoModelForObjectDetection.from_pretrained(
checkpoint,
id2label=id2label,
label2id=label2id,
ignore_mismatched_sizes=True,
)
training_args = TrainingArguments(
@Tob-iee
Tob-iee / detr_img_processor.py
Last active October 25, 2023 16:43
Load DETR’s image processor, encode the images and reformat the annotations with the image processor.
# Load transformer image processor for DetrModel
checkpoint = "facebook/detr-resnet-50"
image_processor = AutoImageProcessor.from_pretrained(checkpoint)
# converting targets to DETR format & resizing + normalization both image & target
encoding=image_processor(images=images_trans, annotations=targets, return_tensors="pt")