Skip to content

Instantly share code, notes, and snippets.

model = create_model()
print("\n[INFO] Ready to train. Training is starting!\n")
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3) # limitation to stop somewhere early
hist =, validation_data=val_generator, epochs=30, callbacks=[callback])
# create a model
def create_model():
# create training and validation data generators
IMG_SIZE = (224,224)
def train_val_generators(train_dir, val_dir):
train_gen = ImageDataGenerator(rescale=1/255.,
# paths to training and validation data
train_dir = os.path.join(path/to/save/bach_data, "/bach-train/training")
val_dir = os.path.join(path/to/save/bach_data, "/bach-train/validation")
Tob-iee / .py
Created December 12, 2023 21:27
Class Weighting
# Import class weights function
from sklearn.utils.class_weight import compute_class_weight
# Calculate class weights
class_weights = compute_class_weight('balanced', np.unique(y_train), y_train)
# Create a dictionary to map class indices to their respective weights
class_weight_dict = dict(zip(np.unique(y_train), class_weights))
# Define and train a classifier with class weights
Tob-iee / .py
Created December 12, 2023 21:25
Data Augmentation
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
# Set the path to your dataset directory
dataset_directory = r"Path to your folder"
# Create an ImageDataGenerator with augmentation parameters
Tob-iee / Dockerfile
Created September 13, 2023 14:31
Make a custom Docker image structure with a Dockerfile.
FROM picsellia/cuda:11.7.1-cudnn8-ubuntu20.04
COPY ./requirements.txt .
RUN python3.10 -m pip install -r requirements.txt --no-cache-dir
RUN python3.10 -m pip install picsellia --upgrade
WORKDIR /picsellia
COPY . ./
ENTRYPOINT ["run", ""]
Tob-iee /
Created September 13, 2023 14:29
model version in the custom model folder to store the fine-tuned model from your training experiments in the model registry
# Get the model that will receive this new version
my_model = client.get_model(
Tob-iee /
Created September 13, 2023 14:27
custom model folder for the different versions of your model in the Picsellia model registry
model_name = "detr-resnet-50_"
model_description = "Finetuned DETR model"
if 'api_token' not in os.environ:
raise Exception("You must set an api_token to run this image")
api_token = os.environ["api_token"]
if "host" not in os.environ:
host = ""
host = os.environ["host"]
Tob-iee /
Created September 13, 2023 14:25
Spin up a job on Picsellia to compute the evaluation metrics for all the predictions you just added