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 = model.fit(train_generator, validation_data=val_generator, epochs=30, callbacks=[callback])
# create a model
def create_model():
METRICS = [
'accuracy',
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
# create training and validation data generators
IMG_SIZE = (224,224)
BATCH_SIZE = 8
def train_val_generators(train_dir, val_dir):
train_gen = ImageDataGenerator(rescale=1/255.,
horizontal_flip=True,
# 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")
print(train_dir)
@Tob-iee
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
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
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 .
ARG REBUILD_ALL
RUN python3.10 -m pip install -r requirements.txt --no-cache-dir
ARG REBUILD_PICSELLIA
RUN python3.10 -m pip install picsellia --upgrade
WORKDIR /picsellia
COPY . ./
ENTRYPOINT ["run", "train.py"]
@Tob-iee
Tob-iee / model_version.py
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(
name=model_name,
)
experiment.export_in_existing_model(my_model)
@Tob-iee
Tob-iee / picsellia_model_reg.py
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 = "https://trial.picsellia.com"
else:
host = os.environ["host"]
@Tob-iee
Tob-iee / eval_compute.py
Created September 13, 2023 14:25
Spin up a job on Picsellia to compute the evaluation metrics for all the predictions you just added
experiment.compute_evaluations_metrics(inference_type=InferenceType.OBJECT_DETECTION)