Skip to content

Instantly share code, notes, and snippets.

@kirk86
Created March 24, 2017 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirk86/99211bd0ea3489915738701285c58c30 to your computer and use it in GitHub Desktop.
Save kirk86/99211bd0ea3489915738701285c58c30 to your computer and use it in GitHub Desktop.
from keras.models import Model
from keras.layers import Input, merge, Convolution2D, MaxPooling2D
from keras.layers import UpSampling2D, Reshape, Activation, Dropout
from keras.layers import Deconvolution2D, Dense, Flatten, Input
from keras.layers import Permute
from keras.optimizers import Adam, SGD
from keras import backend as K
def dice_coef(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = (2. * K.sum(y_true_f * y_pred_f) + smooth)
normalization = (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
return intersection / normalization
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def net(img_shape):
inputs = Input(shape=img_shape)
conv1 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(inputs)
conv1 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Convolution2D(64, 3, 3, activation='relu',
border_mode='same')(pool1)
conv2 = Convolution2D(64, 3, 3, activation='relu',
border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Convolution2D(128, 3, 3, activation='relu',
border_mode='same')(pool2)
conv3 = Convolution2D(128, 3, 3, activation='relu',
border_mode='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Convolution2D(256, 3, 3, activation='relu',
border_mode='same')(pool3)
conv4 = Convolution2D(256, 3, 3, activation='relu',
border_mode='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Convolution2D(512, 3, 3, activation='relu',
border_mode='same')(pool4)
conv5 = Convolution2D(512, 3, 3, activation='relu',
border_mode='same')(conv5)
pool5 = MaxPooling2D(pool_size=(2, 2))(conv5)
conv_ext1 = Convolution2D(1024, 3, 3, activation='relu',
border_mode='same')(pool5)
conv_ext1 = Convolution2D(1024, 3, 3, activation='relu',
border_mode='same')(conv_ext1)
up_ext6 = merge([Deconvolution2D(512, 2, 2,
output_shape=(None, 32, 32, 512),
activation='relu', subsample=(2, 2),
border_mode='same')(conv_ext1), conv5],
mode='concat', concat_axis=3)
conv_ext2 = Convolution2D(512, 3, 3, activation='relu',
border_mode='same')(up_ext6)
conv_ext2 = Convolution2D(512, 3, 3, activation='relu',
border_mode='same')(conv_ext2)
up6 = merge([Deconvolution2D(256, 2, 2,
output_shape=(None, 64, 64, 256),
activation='relu', subsample=(2, 2),
border_mode='same')(conv_ext2), conv4],
mode='concat', concat_axis=3)
conv6 = Convolution2D(256, 3, 3, activation='relu',
border_mode='same')(up6)
conv6 = Convolution2D(256, 3, 3, activation='relu',
border_mode='same')(conv6)
up7 = merge([Deconvolution2D(128, 2, 2,
output_shape=(None, 128, 128, 128),
activation='relu', subsample=(2, 2),
border_mode='same')(conv6), conv3],
mode='concat', concat_axis=3)
conv7 = Convolution2D(128, 3, 3, activation='relu',
border_mode='same')(up7)
conv7 = Convolution2D(128, 3, 3, activation='relu',
border_mode='same')(conv7)
up8 = merge([Deconvolution2D(64, 2, 2,
output_shape=(None, 256, 256, 64),
activation='relu', subsample=(2, 2),
border_mode='same')(conv7), conv2],
mode='concat', concat_axis=3)
conv8 = Convolution2D(64, 3, 3, activation='relu',
border_mode='same')(up8)
conv8 = Convolution2D(64, 3, 3, activation='relu',
border_mode='same')(conv8)
up9 = merge([Deconvolution2D(32, 2, 2,
output_shape=(None, 512, 512, 32),
activation='relu', subsample=(2, 2),
border_mode='same')(conv8), conv1],
mode='concat', concat_axis=3)
conv9 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(up9)
conv9 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(conv9)
conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9)
flat = Flatten(name='flatten')(conv9)
dense1 = Dense(64, activation='relu', name='fc1')(flat)
dense2 = Dense(64, activation='relu', name='fc2')(dense1)
dense = Dense(8, activation='softmax', name='predictions')(dense2)
model = Model(input=inputs, output=[conv10, dense])
model.compile(optimizer=Adam(lr=1e-5), loss=[dice_coef_loss,
'categorical_crossentropy'],
metrics=[dice_coef, 'accuracy'])
return model
import numpy as np
X = np.random.randn(25000, 512, 512, 3)
y = np.eye(25000, 8)
mask = np.random.randn(25000, 512, 512, 1)
model = net((512,512,3))
model.fit(X, [mask, y], nb_epoch=5, batch_size=32, shuffle=True, verbose=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment