industry_datasets / sfm_code /flow_lstm.py
FelixOtt's picture
Upload folder using huggingface_hub (part 6)
1403119 verified
Raw History Blame Contribute Delete
2.09 kB
import tensorflow as tf
from tensorflow.keras import models
from tensorflow.keras import layers
class FlowNet:
"""
Uses the optical flow calculated by the Lucas-Kanade algorithm as input.
It splits the x and y shifts and runs them though lstms before combining them again.
The output are 2 tensors. One for the xyz position values and one for the 4 quaternions.
"""
def __init__(self, lstms=1, activ=None, bn=0):
self.lstms = lstms
self.activ = activ
self.bn = bn
print('build IMU ...')
def create_model(self):
inputs = tf.keras.Input(shape=(120, 160, 2))
if self.bn == 1:
bnorm = layers.BatchNormalization()(inputs)
r = layers.Lambda(lambda x: x[:, :, :, 0], name="lambda_layer1")(bnorm)
g = layers.Lambda(lambda x: x[:, :, :, 1], name="lambda_layer2")(bnorm)
else:
r = layers.Lambda(lambda x: x[:, :, :, 0], name="lambda_layer1")(inputs)
g = layers.Lambda(lambda x: x[:, :, :, 1], name="lambda_layer2")(inputs)
if self.lstms == 2:
lstm1 = layers.LSTM(units=100, return_sequences=True)(r)
cls3_fc1 = layers.LSTM(units=50)(lstm1)
lstm2 = layers.LSTM(units=100, return_sequences=True)(g)
cls3_fc2 = layers.LSTM(units=50)(lstm2)
elif self.lstms == 1:
cls3_fc1 = layers.LSTM(units=50)(r)
cls3_fc2 = layers.LSTM(units=50)(g)
concat = layers.Concatenate(name='concat', axis=-1)([cls3_fc1, cls3_fc2])
if self.activ == "relu":
concat = layers.Activation("relu")(concat)
elif self.activ == "softmax":
concat = layers.Activation("softmax")(concat)
pose_xyz = layers.Dense(3,name='pose_xyz')(concat)
pose_wpqr = layers.Dense(4,name='pose_wpqr')(concat)
return models.Model(inputs=inputs, outputs=[pose_xyz, pose_wpqr])
if __name__ == "__main__":
# Calculates and displays the number of parameters in the model.
m = FlowNet(lstms=1)
Model = m.create_model()
Model.summary()