Advertisement
Guest User

Untitled

a guest
Oct 24th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import numpy as np
  4. import tensorflow as tf
  5. from tensorflow import keras
  6. from tensorflow.keras import layers
  7.  
  8. # Model takes 8 sets of input data per inference.
  9. FRAMES = 8
  10.  
  11. # Image dimensions
  12. IMG_WIDTH = 32
  13. IMG_HEIGHT = 32
  14.  
  15. # Size of the input 1-d dense layer (per frame).
  16. DENSE_IN = 64
  17.  
  18. # Ranges for the two output one-hot tensors.
  19. OUT_A_SIZE = 9
  20. OUT_B_SIZE = 2
  21.  
  22.  
  23. def get_img_frontend(name):
  24.     input = keras.Input(shape=[IMG_WIDTH, IMG_HEIGHT, FRAMES], name=name)
  25.     img = layers.Conv2D(
  26.         64 * FRAMES,
  27.         8,
  28.         activation="relu",
  29.         kernel_initializer="random_normal",
  30.         bias_initializer="zeros",
  31.     )(input)
  32.     img = layers.MaxPooling2D(2)(img)
  33.     img = layers.Conv2D(
  34.         16 * FRAMES,
  35.         4,
  36.         activation="relu",
  37.         kernel_initializer="random_normal",
  38.         bias_initializer="zeros",
  39.     )(img)
  40.     img = layers.MaxPooling2D(2)(img)
  41.     img = layers.Reshape((2048,), name=f"{name}_flat")(img)
  42.     return input, img
  43.  
  44.  
  45. def get_model():
  46.     img_a_in, img_a = get_img_frontend("img_a_in")
  47.     img_b_in, img_b = get_img_frontend("img_b_in")
  48.  
  49.     foo_in = keras.Input(shape=[DENSE_IN * FRAMES], name="foo_in")
  50.     foo = layers.Dense(
  51.         DENSE_IN * FRAMES,
  52.         kernel_initializer="random_normal",
  53.         bias_initializer="zeros",
  54.     )(foo_in)
  55.  
  56.     # Merge images and 'foo' togther, then run through a few dense layers.
  57.     all = layers.concatenate([img_a, img_b, foo], name="all")
  58.     all = layers.Dense(
  59.         1024, kernel_initializer="random_normal", bias_initializer="zeros"
  60.     )(all)
  61.     all = layers.Dense(
  62.         512, kernel_initializer="random_normal", bias_initializer="zeros"
  63.     )(all)
  64.     all = layers.Dense(
  65.         256, kernel_initializer="random_normal", bias_initializer="zeros"
  66.     )(all)
  67.  
  68.     # Want two outputs.
  69.     out_a = layers.Dense(
  70.         OUT_A_SIZE, kernel_initializer="random_normal", bias_initializer="zeros"
  71.     )(all)
  72.     out_a = keras.backend.argmax(out_a, name="out_a_one_hot")
  73.  
  74.     out_b = layers.Dense(
  75.         OUT_B_SIZE, kernel_initializer="random_normal", bias_initializer="zeros"
  76.     )(all)
  77.     out_b = keras.backend.argmax(out_b, name="out_b_one_hot")
  78.  
  79.     # Create the model.
  80.     model = keras.Model(
  81.         inputs=[img_a_in, img_b_in, foo_in], outputs=[out_a, out_b], name="baz"
  82.     )
  83.     model.compile(optimizer="adam", loss="mean_squared_error")
  84.     return model
  85.  
  86.  
  87. def call_model_with_random_inputs(model):
  88.     img_a = np.random.randn(IMG_WIDTH, IMG_HEIGHT, FRAMES)
  89.     img_b = np.random.randn(IMG_WIDTH, IMG_HEIGHT, FRAMES)
  90.     foo = np.random.randn(DENSE_IN * FRAMES)
  91.  
  92.     inputs = {"img_a_in": img_a, "img_b_in": img_b, "foo_in": foo}
  93.  
  94.     r = model.call(inputs)
  95.     print(r)
  96.  
  97.  
  98. def main():
  99.     print("tf.version: ", tf.__version__)
  100.     model = get_model()
  101.     keras.utils.plot_model(model, "summary.png", show_shapes=True)
  102.     call_model_with_random_inputs(model)
  103.  
  104.  
  105. if __name__ == "__main__":
  106.     main()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement