# Path: settings.py
# FLAGS = flags.FLAGS
# OUT_DIR = '/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21'
# DUMP_DIR = "/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21"
# TF_RECORDS_DIR = "/localhome/rothfuss/data/20bn-something/tf_records_train"
# MODE = 'valid_mode'
# VALID_MODE = 'data_frame' #'data_frame gif'
# NUM_IMAGES = 15
# NUM_DEPTH = 3
# WIDTH = 128
# HEIGHT = 128
# NUM_THREADS_QUEUERUNNER = 32 # specifies the number of pre-processing threads
# PRETRAINED_MODEL = "/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21"
# EXCLUDE_FROM_RESTORING = None
# FINE_TUNING_WEIGHTS_LIST = None
# INPUT_DIR = ""
# MEMORY_PATH = ""
# LOSS_FUNCTIONS = ['mse', 'gdl', 'mse_gdl', 'vae']
# MODES = ["train_mode", "valid_mode", "feeding_mode"]
# VALID_MODES = ['count_trainable_weights', 'vector', 'gif', 'similarity', 'data_frame', 'psnr', 'memory_prep', 'measure_test_time']
# 
# Path: models/loss_functions.py
# def gradient_difference_loss(true, pred, alpha=2.0):
# def difference_gradient(image, vertical=True):
# def mean_squared_error(true, pred):
# def peak_signal_to_noise_ratio(true, pred):
# def kl_penalty(mu, sigma):
# def decoder_loss(frames_gen, frames_original, loss_fun):
# def decoder_psnr(frames_gen, frames_original):
# def composite_loss(original_frames, frames_pred, frames_reconst, loss_fun='mse',
#                    encoder_length=5, decoder_future_length=5,
#                    decoder_reconst_length=5, mu_latent=None, sigm_latent=None):


import tensorflow as tf
import math
import numpy as np
import data_prep.model_input as input
from pprint import pprint
from settings import FLAGS, model
from models import loss_functions
  :param device number for assining queue runner to CPU
  :param train: boolean that indicates whether train or validation mode
  :param compute_loss: boolean that specifies whether loss should be computed (for feed mode / production compute_loss might be disabled)
  :return batch loss (scalar)
  """
  #only dropout in train mode
  keep_prob_dropout = FLAGS.keep_prob_dopout if train else 1.0
  mu = sigma = None, None

  if FLAGS.loss_function == 'vae':
    frames_pred, frames_reconst, hidden_repr, mu, sigma = model.composite_model(video_batch, FLAGS.encoder_length,
                                                         FLAGS.decoder_future_length,
                                                         FLAGS.decoder_reconst_length, keep_prob_dropout=keep_prob_dropout,
                                                         noise_std=FLAGS.noise_std, uniform_init=FLAGS.uniform_init,
                                                         num_channels=FLAGS.num_channels, fc_conv_layer=FLAGS.fc_layer)
    if use_vae_mu:
      hidden_repr = mu

  else:
    frames_pred, frames_reconst, hidden_repr = model.composite_model(video_batch, FLAGS.encoder_length,
                                                                   FLAGS.decoder_future_length,
                                                                   FLAGS.decoder_reconst_length,
                                                                   keep_prob_dropout=keep_prob_dropout,
                                                                   noise_std=FLAGS.noise_std,
                                                                   uniform_init=FLAGS.uniform_init,
                                                                   num_channels=FLAGS.num_channels,
                                                                   fc_conv_layer=FLAGS.fc_layer)
    

  if compute_loss:

# Path: settings.py
# FLAGS = flags.FLAGS
# OUT_DIR = '/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21'
# DUMP_DIR = "/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21"
# TF_RECORDS_DIR = "/localhome/rothfuss/data/20bn-something/tf_records_train"
# MODE = 'valid_mode'
# VALID_MODE = 'data_frame' #'data_frame gif'
# NUM_IMAGES = 15
# NUM_DEPTH = 3
# WIDTH = 128
# HEIGHT = 128
# NUM_THREADS_QUEUERUNNER = 32 # specifies the number of pre-processing threads
# PRETRAINED_MODEL = "/common/homes/students/rothfuss/Documents/selected_trainings/9_20bn_vae_no_OF/08-06-18_10-21"
# EXCLUDE_FROM_RESTORING = None
# FINE_TUNING_WEIGHTS_LIST = None
# INPUT_DIR = ""
# MEMORY_PATH = ""
# LOSS_FUNCTIONS = ['mse', 'gdl', 'mse_gdl', 'vae']
# MODES = ["train_mode", "valid_mode", "feeding_mode"]
# VALID_MODES = ['count_trainable_weights', 'vector', 'gif', 'similarity', 'data_frame', 'psnr', 'memory_prep', 'measure_test_time']
# 
# Path: models/loss_functions.py
# def gradient_difference_loss(true, pred, alpha=2.0):
# def difference_gradient(image, vertical=True):
# def mean_squared_error(true, pred):
# def peak_signal_to_noise_ratio(true, pred):
# def kl_penalty(mu, sigma):
# def decoder_loss(frames_gen, frames_original, loss_fun):
# def decoder_psnr(frames_gen, frames_original):
# def composite_loss(original_frames, frames_pred, frames_reconst, loss_fun='mse',
#                    encoder_length=5, decoder_future_length=5,
#                    decoder_reconst_length=5, mu_latent=None, sigm_latent=None):


# Path: core/Model.py
import tensorflow as tf
import math
import numpy as np
import data_prep.model_input as input
from pprint import pprint
from settings import FLAGS, model
from models import loss_functions

  :param device number for assining queue runner to CPU
  :param train: boolean that indicates whether train or validation mode
  :param compute_loss: boolean that specifies whether loss should be computed (for feed mode / production compute_loss might be disabled)
  :return batch loss (scalar)
  """
  #only dropout in train mode
  keep_prob_dropout = FLAGS.keep_prob_dopout if train else 1.0
  mu = sigma = None, None

  if FLAGS.loss_function == 'vae':
    frames_pred, frames_reconst, hidden_repr, mu, sigma = model.composite_model(video_batch, FLAGS.encoder_length,
                                                         FLAGS.decoder_future_length,
                                                         FLAGS.decoder_reconst_length, keep_prob_dropout=keep_prob_dropout,
                                                         noise_std=FLAGS.noise_std, uniform_init=FLAGS.uniform_init,
                                                         num_channels=FLAGS.num_channels, fc_conv_layer=FLAGS.fc_layer)
    if use_vae_mu:
      hidden_repr = mu

  else:
    frames_pred, frames_reconst, hidden_repr = model.composite_model(video_batch, FLAGS.encoder_length,
                                                                   FLAGS.decoder_future_length,
                                                                   FLAGS.decoder_reconst_length,
                                                                   keep_prob_dropout=keep_prob_dropout,
                                                                   noise_std=FLAGS.noise_std,
                                                                   uniform_init=FLAGS.uniform_init,
                                                                   num_channels=FLAGS.num_channels,
                                                                   fc_conv_layer=FLAGS.fc_layer)
    

  if compute_loss:

    tower_loss = loss_functions.composite_loss(video_batch, frames_pred, frames_reconst, loss_fun=FLAGS.loss_function,