Created
October 8, 2015 09:39
-
-
Save mmmikael/c233ca535c33a4059e01 to your computer and use it in GitHub Desktop.
fcn benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
when i compile the code it shows,
Traceback (most recent call last):
File "/Users/kamal/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 3, in
model.compile(optimizer='adagrad', loss=categorical_crossentropy_2d)
File "build/bdist.macosx-10.5-x86_64/egg/keras/models.py", line 499, in compile
self.y_train = self.get_output(train=True)
File "build/bdist.macosx-10.5-x86_64/egg/keras/layers/containers.py", line 130, in get_output
return self.layers[-1].get_output(train)
File "build/bdist.macosx-10.5-x86_64/egg/keras/layers/core.py", line 742, in get_output
return self.activation(X)
File "build/bdist.macosx-10.5-x86_64/egg/keras/activations.py", line 19, in softmax
'Here, ndim=' + str(ndim))
Exception: Cannot apply softmax to a tensor that is not 2D or 3D. Here, ndim=4
any hint would be helpful .... thanks