jason71995 / keras_odenet Goto Github PK
View Code? Open in Web Editor NEWImplementation of (2018) Neural Ordinary Differential Equations on Keras
Implementation of (2018) Neural Ordinary Differential Equations on Keras
Could you share which version of tf and keras you use? As this no longer works with latest versions.
Hello,
Thanks for a great project. I learnt a lot from this!
I was wondering one thing: Why does value of t has to be between [0 - 1]? I notice when I change it to 10, it is significantly lower to train the model.
Also, I notice t is a variable. Do we optimize t together with network's parameters, or t fixed? If t is not fixed, what is the value of t after training? (Maybe I don't understand the paper is well enough and this can be a naive question).
Thanks a lot!
Best,
It would be useful to know training times of both solutions in order to compare them and see what solution is better.
Hi Jason,
Thanks for sharing. When I try training the ode-block, seeing no response in terminal, it keeps waiting for long time and I am killing the process at the end. When trying for Resnet, it is working and getting results as normal. Do you have any suggestion about this?
I am actually using gtx 1080 Ti with 12 gb and I think it should work.
Using TensorFlow backend.
Traceback (most recent call last):
File "predict.py", line 63, in
model = load_model('./saved_models/OdeNetF95.h5')
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/anaconda3/envs/experiment/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown layer: ODEBlock
FOR THE CODE:
from keras.models import load_model
import cv2
from keras.layers import Conv2D, Dense, Flatten, Input, MaxPooling2D, Layer
import numpy as np
class ODEBlock(Layer):
def __init__(self, filters, kernel_size, **kwargs):
self.filters = filters
self.kernel_size = kernel_size
super(ODEBlock, self).__init__(**kwargs)
def build(self, input_shape):
self.conv2d_w1 = self.add_weight("conv2d_w1", self.kernel_size + (self.filters + 1, self.filters), initializer='glorot_uniform')
self.conv2d_w2 = self.add_weight("conv2d_w2", self.kernel_size + (self.filters + 1, self.filters), initializer='glorot_uniform')
self.conv2d_b1 = self.add_weight("conv2d_b1", (self.filters,), initializer='zero')
self.conv2d_b2 = self.add_weight("conv2d_b2", (self.filters,), initializer='zero')
super(ODEBlock, self).build(input_shape)
def call(self, x):
t = K.constant([0, 1], dtype="float32")
return tf.contrib.integrate.odeint(self.ode_func, x, t, rtol=1e-3, atol=1e-3)[1]
def compute_output_shape(self, input_shape):
return input_shape
def ode_func(self, x, t):
y = self.concat_t(x, t)
y = K.conv2d(y, self.conv2d_w1, padding="same")
y = K.bias_add(y, self.conv2d_b1)
y = K.relu(y)
y = self.concat_t(y, t)
y = K.conv2d(y, self.conv2d_w2, padding="same")
y = K.bias_add(y, self.conv2d_b2)
y = K.relu(y)
return y
def concat_t(self, x, t):
new_shape = tf.concat(
[
tf.shape(x)[:-1],
tf.constant([1],dtype="int32",shape=(1,))
], axis=0)
t = tf.ones(shape=new_shape) * tf.reshape(t, (1, 1, 1, 1))
return tf.concat([x, t], axis=-1)
def build_model(input_shape, num_classes):
x = Input(input_shape)
y = Conv2D(32, (3, 3), activation='relu')(x)
y = MaxPooling2D((2,2))(y)
y = Conv2D(64, (3, 3), activation='relu')(y)
y = MaxPooling2D((2,2))(y)
y = ODEBlock(64, (3, 3))(y)
y = Flatten()(y)
y = Dense(num_classes, activation='softmax')(y)
return Model(x,y)
model = load_model('./saved_models/OdeNetF95.h5')
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
img = cv2.imread('test.png')
img = np.reshape(img,[-1,71, 64,1])
classes = model.predict_classes(img)
print(classes)
Are we using the adjoint method as described in the paper?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.