View jaccard_coef_loss.py
from keras import backend as K
def jaccard_distance_loss(y_true, y_pred, smooth=100):
"""
Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|)
= sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|))
The jaccard distance loss is usefull for unbalanced datasets. This has been
shifted so it converges on 0 and is smoothed to avoid exploding or disapearing
gradient.
View DualFlowGenerators.py
from skimage.transform import resize
class DualFlowGenerators(object):
"""
Class to deliver X from multiple keras NumpyIterators.
Can also resize the image
"""
def __init__(self, image_data_generators, resize=False):
super().__init__()
assert [a.n==image_data_generators[0].n for a in image_data_generators], 'all inputs should have same length'
View augumented_hdf5_matrix.py
"""Another way, note this one will load the whole array into memory ."""
from keras.preprocessing.image import ImageDataGenerator
import h5py
from keras.utils.io_utils import HDF5Matrix
seed=0
batch_size=32
# we create two instances with the same arguments
data_gen_args = dict(
rotation_range=90.,
View hdfstore.py
import numpy as np
import h5py
class HDF5Store(object):
"""
Simple class to append value to a hdf5 file on disc (usefull for building keras datasets)
Params:
datapath: filepath of h5 file
dataset: dataset name within the file
View multi_image_datagenerator.py
"""
mod to Allow the ImageDataGenerator to have multiple channels instead of just 1,3,4
modified from https://github.com/fchollet/keras/blob/master/keras/preprocessing/image.py
"""
from keras.preprocessing.image import Iterator
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator as _ImageDataGenerator
from path import Path
View md5_streamed_progress.py
import os
from tqdm import tqdm
import hashlib
def md5_streamed_progress(fname):
"""Streamed md5 with progress bar."""
hash_md5 = hashlib.md5()
filesize = os.path.getsize(fname)
with tqdm(total=filesize, unit='B', unit_scale=True, miniters=1, desc=os.path.basename(fname), leave=False) as t:
chunk_size = 4096
View visualising keras loss functions.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View torch_summarize.py
from torch.nn.modules.module import _addindent
import torch
import numpy as np
def torch_summarize(model, show_weights=True, show_parameters=True):
"""Summarizes torch model by showing trainable parameters and weights"""
tmpstr = model.__class__.__name__ + ' (\n'
for key, module in model._modules.items():
# if it contains layers let call it recurvisvly to get params and weights
if type(module) in [
torch.nn.modules.container.Container,
View subimshow.py
import numpy as np
from matplotlib import pyplot as plt
def subimshow(image, title=''):
"""
Show each band seperately.
The shape si a square or rectangle of images
image: array of shape (channel, height, width)
View TrainIntervalLoggerTQDMNotebook.py
from rl.callbacks import Callback
from rl.callbacks import TrainIntervalLogger
from keras import backend as K
import warnings
from tqdm import tqdm_notebook
import timeit
import numpy as np
class TrainIntervalLoggerTQDMNotebook(TrainIntervalLogger):
"""TrainIntervalLogger for keras-rl using tqdm_notebook for jupyter-notebook."""