"""
Get the memory usage of a Keras model.

From https://stackoverflow.com/a/46216013.
"""

def get_model_memory_usage(batch_size, model):
    """
    Get the memory usage of a Keras model in GB.

    From https://stackoverflow.com/a/46216013.
    """

    import numpy as np
    try:
        from keras import backend as K
    except ImportError:
        from tensorflow.keras import backend as K

    shapes_mem_count = 0
    internal_model_mem_count = 0
    for l in model.layers:
        layer_type = l.__class__.__name__
        if layer_type == 'Model':
            internal_model_mem_count += get_model_memory_usage(batch_size, l)
        single_layer_mem = 1
        out_shape = l.output_shape
        if isinstance(out_shape, list):
            out_shape = out_shape[0]
        for s in out_shape:
            if s is None:
                continue
            single_layer_mem *= s
        shapes_mem_count += single_layer_mem

    trainable_count = np.sum([K.count_params(p) for p in model.trainable_weights])
    non_trainable_count = np.sum([K.count_params(p) for p in model.non_trainable_weights])

    number_size = 4.0
    if K.floatx() == 'float16':
        number_size = 2.0
    if K.floatx() == 'float64':
        number_size = 8.0

    total_memory = number_size * (batch_size * shapes_mem_count + trainable_count + non_trainable_count)
    gbytes = np.round(total_memory / (1024.0 ** 3), 3) + internal_model_mem_count
    return gbytes

[(40, 'tensorflow.keras.backend.floatx', 'K.floatx', True, 'from tensorflow.keras import backend as K\n'), (42, 'tensorflow.keras.backend.floatx', 'K.floatx', True, 'from tensorflow.keras import backend as K\n'), (46, 'numpy.round', 'np.round', True, 'import numpy as np\n'), (36, 'tensorflow.keras.backend.count_params', 'K.count_params', True, 'from tensorflow.keras import backend as K\n'), (37, 'tensorflow.keras.backend.count_params', 'K.count_params', True, 'from tensorflow.keras import backend as K\n')]