Skip to content

Instantly share code, notes, and snippets.

def train(net, train_iter, val_iter, batch_size, epochs, ctx):
learning_rate = 0.01
wd = 0.002
log_interval=100
if isinstance(ctx, mx.Context):
ctx = [ctx]
trainer = gluon.Trainer(net.collect_params(), 'adam',
{'learning_rate': learning_rate})
loss = gluon.loss.SigmoidBinaryCrossEntropyLoss()
@mongoose54
mongoose54 / UNet_Segmentation.ipynb
Created April 3, 2017 23:10
3D UNet 30 to 1 ratio
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mongoose54
mongoose54 / gist:2420a55ef51a8561eb6a3a2930ca4520
Created February 22, 2017 21:33
UNet with cross-entropy
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
____________________________________________________________________________________________________
convolution2d_134 (Convolution2D (None, 2, 64, 80) 66 convolution2d_133[0][0]
____________________________________________________________________________________________________
permute_5 (Permute) (None, 64, 80, 2) 0 convolution2d_134[0][0]
____________________________________________________________________________________________________
reshape_7 (Reshape) (None, 5120, 2) 0 permute_5[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 5120, 2) 0 reshape_7[0][0]
_author__ = 'Fabian Isensee'
from collections import OrderedDict
from lasagne.layers import InputLayer, ConcatLayer, Pool2DLayer, ReshapeLayer, DimshuffleLayer, NonlinearityLayer, DropoutLayer, Upscale2DLayer, Upscale3DLayer, BatchNormLayer, batch_norm
from lasagne.layers.dnn import Conv3DDNNLayer as ConvLayer, Pool3DDNNLayer as PoolLayer
import lasagne
from lasagne.init import HeNormal
def build_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), depth=32, base_n_filters=32, kernel_size=3, do_dropout=False):
net = OrderedDict()
@mongoose54
mongoose54 / gist:71e174587fbec8c2fe970e8a1c14eff4
Created February 21, 2017 03:23
Calculate dice score for arbitrary number of classes (beyond 2)
#to_categorical() acquired from Keras implementation
#dice_coef() inspired by Marco Jocic's implementation: https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py
def to_categorical(y, nb_classes=None):
"""Converts a class vector (integers) to binary class matrix.
E.g. for use with categorical_crossentropy.
# Arguments
y: class vector to be converted into a matrix
(integers from 0 to nb_classes).