Skip to content

Instantly share code, notes, and snippets.

View karolzak's full-sized avatar
😷
Working from home

Karol Żak karolzak

😷
Working from home
  • Microsoft
View GitHub Profile
@karolzak
karolzak / faiss_nn.py
Last active April 9, 2021 12:28
Nearest Neighbors algo implementation with FAISS
import numpy as np
import faiss
class FaissNearestNeighbors:
def __init__(self, k=5):
self.index = None
self.y = None
self.k = k
@karolzak
karolzak / video_indexer.py
Created March 30, 2021 09:50
Azure Video Indexer python client
# Original source code: https://github.com/bklim5/python_video_indexer_lib
import re
import time
import datetime
import requests
def get_retry_after_from_message(message):
@karolzak
karolzak / keras_callbacks.py
Created August 22, 2019 21:39
Some callbacks for keras
from keras.callbacks import ModelCheckpoint
checkpoint_callback = ModelCheckpoint(
"c3d_v5_chkpt-{epoch:02d}-{val_loss:.2f}-{val_acc:.2f}.hdf5",
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto',
period=1)
@karolzak
karolzak / reset_keras.py
Created August 21, 2019 16:37
Reset keras and TF
from keras.backend.tensorflow_backend import set_session
from keras.backend.tensorflow_backend import clear_session
from keras.backend.tensorflow_backend import get_session
import tensorflow
# Reset Keras Session
def reset_keras():
sess = get_session()
clear_session()
sess.close()
@karolzak
karolzak / list_objects_in_memory.py
Created August 21, 2019 16:27
Get info about objects in memory
import sys
# These are the usual ipython objects, including this one you are creating
ipython_vars = ['In', 'Out', 'exit', 'quit', 'get_ipython', 'ipython_vars']
# Get a sorted list of the objects and their sizes
sorted([(x, sys.getsizeof(globals().get(x))) for x in dir() if not x.startswith('_') and x not in sys.modules and x not in ipython_vars], key=lambda x: x[1], reverse=True)
@karolzak
karolzak / sem_losses.py
Created June 27, 2019 13:48
semantic segmentation losses
# https://lars76.github.io/neural-networks/object-detection/losses-for-segmentation/
def weighted_cross_entropy(beta):
def convert_to_logits(y_pred):
# see https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3525
y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())
return tf.log(y_pred / (1 - y_pred))
def loss(y_true, y_pred):
@karolzak
karolzak / segm_get_augmented.py
Last active January 16, 2019 22:27
image augmentation for semantic segmentation models
from keras.preprocessing.image import ImageDataGenerator
# Runtime data augmentation
def get_augmented(
X_train,
Y_train,
X_val,
Y_val,
batch_size=32,
@karolzak
karolzak / segm_plot_history.py
Last active January 18, 2019 13:01
plotting training history for keras unet implementation
import numpy as np
import matplotlib.pyplot as plt
def plot_segm_history(history):
# summarize history for iou
plt.figure(figsize=(12,6))
plt.plot(history.history['iou'], linewidth=3)
plt.plot(history.history['val_iou'], linewidth=3)
plt.suptitle('iou metric', fontsize=20)
@karolzak
karolzak / jaccard_distance.py
Last active January 16, 2019 22:27
jaccard_distance loss function
#https://github.com/keras-team/keras-contrib/blob/master/keras_contrib/losses/jaccard.py
from keras import backend as K
def jaccard_distance(y_true, y_pred, smooth=100):
"""Jaccard distance for semantic segmentation.
Also known as the intersection-over-union loss.
@karolzak
karolzak / iou.py
Last active January 16, 2019 22:28
simple iou metric with 0.5 rounding (treshold)
################### metrics ############################
from keras import backend as K
SMOOTH = 1e-12
def iou(true, pred):
y_pred_pos = K.round(K.clip(pred, 0, 1))
intersection = true * y_pred_pos
union = true + y_pred_pos
return K.sum(intersection + SMOOTH) / K.sum(union - intersection + SMOOTH)