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
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):
assert [a.n==image_data_generators[0].n for a in image_data_generators], 'all inputs should have same length'
"""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
# we create two instances with the same arguments
data_gen_args = dict(
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)
datapath: filepath of h5 file
dataset: dataset name within the file
mod to Allow the ImageDataGenerator to have multiple channels instead of just 1,3,4
modified from
from keras.preprocessing.image import Iterator
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator as _ImageDataGenerator
from path import Path
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.
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 [
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)
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."""