Skip to content

Instantly share code, notes, and snippets.

View angeligareta's full-sized avatar
I'd like to help create the technology of the future, not only use it.

Ángel Igareta angeligareta

I'd like to help create the technology of the future, not only use it.
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@angeligareta
angeligareta / dhash_z_transformed.py
Last active December 31, 2023 17:06
DHash Image Hashing algorithm with z-transformation
# Inspired from https://github.com/JohannesBuchner/imagehash repository
import imagehash
from PIL import Image
def alpharemover(image):
if image.mode != 'RGBA':
return image
canvas = Image.new('RGBA', image.size, (255,255,255,255))
canvas.paste(image, mask=image)
return canvas.convert('RGB')
@angeligareta
angeligareta / custom_early_stopping.py
Created June 22, 2021 23:11
Custom Early Stopping callback to monitor multiple metrics by combining them using a harmonic mean calculation.
import tensorflow as tf
import numpy as np
class CustomEarlyStopping(tf.keras.callbacks.Callback):
"""
Custom Early Stopping callback to monitor multiple metrics by combining them using a harmonic mean calculation.
Adapted from (TensorFlow EarlyStopping source)[https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/python/keras/callbacks.py#L1683-L1823].
@angeligareta
angeligareta / generate_class_weights.py
Created June 8, 2021 10:28
Method to generate class weights given a multi-class or multi-label set of classes using Python, supporting one-hot-encoded labels.
import numpy as np
from sklearn.utils.class_weight import compute_class_weight
from sklearn.preprocessing import MultiLabelBinarizer
def generate_class_weights(class_series, multi_class=True, one_hot_encoded=False):
"""
Method to generate class weights given a set of multi-class or multi-label labels, both one-hot-encoded or not.
Some examples of different formats of class_series and their outputs are:
@angeligareta
angeligareta / get_dataset_partitions_pd.py
Created June 1, 2021 22:12
Method to split a Pandas dataset into train, validation and test splits
def get_dataset_partitions_pd(df, train_split=0.8, val_split=0.1, test_split=0.1):
assert (train_split + test_split + val_split) == 1
# Only allows for equal validation and test splits
assert val_split == test_split
# Specify seed to always have the same split distribution between runs
df_sample = df.sample(frac=1, random_state=12)
indices_or_sections = [int(train_split * len(df)), int((1 - val_split - test_split) * len(df))]
@angeligareta
angeligareta / get_dataset_partitions_tf.py
Last active July 4, 2022 20:26
Method to split a tensorflow dataset (tf.data.Dataset) into train, validation and test splits
def get_dataset_partitions_tf(ds, ds_size, train_split=0.8, val_split=0.1, test_split=0.1, shuffle=True, shuffle_size=10000):
assert (train_split + test_split + val_split) == 1
if shuffle:
# Specify seed to always have the same split distribution between runs
ds = ds.shuffle(shuffle_size, seed=12)
train_size = int(train_split * ds_size)
val_size = int(val_split * ds_size)
@angeligareta
angeligareta / get_best_epoch_per_model.py
Last active May 12, 2021 06:48
A better comparison of TensorBoard experiments
import pandas as pd
import tensorboard as tb
import statistics
def get_best_epoch_per_model(
experiment_id,
metrics,
format_as_percentage = False,
sort_values = False,
group_by_model_names = False,
@angeligareta
angeligareta / language_selector_buddypress.php
Created December 21, 2020 20:45
Add language selector field for Extended Profile Fields in BuddyPress
<?php
// Add custom language selector field for XProfileFields Buddypress
// Author: Angel Igareta, adapted from https://tulipemedia.com/en/buddypress-auto-bulk-import-countries-profile-field/
function bp_add_custom_language_list() {
if ( !xprofile_get_field_id_from_name('Languages Spoken') && 'bp-profile-setup' == $_GET['page'] ) {
$language_list_args = array(
'field_group_id' => 1,
'name' => 'Languages Spoken',
'description' => 'Please select the languages you speak',
'can_delete' => true,
👉🏼<⚫>👈🏼 ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎ ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎ ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎ ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎ ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎ ‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏‎‏