Skip to content

Instantly share code, notes, and snippets.

@mmmikael
Created October 8, 2015 09:39
Show Gist options
  • Save mmmikael/c233ca535c33a4059e01 to your computer and use it in GitHub Desktop.
Save mmmikael/c233ca535c33a4059e01 to your computer and use it in GitHub Desktop.
fcn benchmark
from keras.models import Sequential
from keras.layers.core import Permute
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Activation
import theano
import theano.tensor as T
import datetime
import numpy as np
now = datetime.datetime.now
if theano.config.floatX == 'float64':
epsilon = 1.0e-9
else:
epsilon = 1.0e-7
def categorical_crossentropy_2d(y_true, y_pred):
y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
# scale preds so that the class probas of each sample sum to 1
y_pred /= y_pred.sum(axis=-1, keepdims=True)
cce = - (y_true * T.log(y_pred)).sum()
return cce
# input data
np.random.seed(123456)
n_images = 10
s = (850, 649)
x = np.random.rand(n_images, 1, s[0], s[1]).astype('float32')
y = np.random.rand(n_images, s[0], s[1], 21).astype('float32')
layers = [
Convolution2D(64, 3, 3, activation='relu', border_mode='same', input_shape=(1, s[0], s[1])),
Convolution2D(64, 3, 3, activation='relu', border_mode='same'),
Convolution2D(21, 3, 3, activation='relu', border_mode='same'),
Permute((2, 3, 1)),
Activation('softmax')
]
model = Sequential()
for l in layers:
model.add(l)
# compile
t = now()
model.compile(optimizer='adagrad', loss=categorical_crossentropy_2d)
print('Compilation time %s' % (now() - t))
# train
t = now()
model.fit(x, y, batch_size=1, nb_epoch=1)
print('Training time for %d images (batch_size=1): %s' % (n_images, (now() - t)))
t = now()
model.fit(x, y, batch_size=n_images, nb_epoch=1)
print('Training time for %d images (batch_size=%s): %s' % (n_images, n_images, (now() - t)))
model.save_weights('w.h5', overwrite=True)
# predict
t = now()
model.predict(x)
print('Prediction time for %d images: %s' % (n_images, (now() - t)))
@RagMeh11
Copy link

RagMeh11 commented Aug 6, 2016

This code will not work properly with keras main branch code as @mmmikael has modified his code of softmax activation. Look at his softmax activation code here, add this softmax definition in your code and it will work as desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment