text: import sys
  import models
  import model_utils
  import math
  import numpy as np
  import video_level_models
  import tensorflow as tf
  import utils
  import tensorflow.contrib.slim as slim
  from tensorflow import flags
  FLAGS = flags.FLAGS
  
  class DbofModel(models.BaseModel):
    """Creates a Deep Bag of Frames model.
  
    The model projects the features for each frame into a higher dimensional
    'clustering' space, pools across frames in that space, and then
    uses a configurable video-level model to classify the now aggregated features.
  
    The model will randomly sample either frames or sequences of frames during
    training to speed up convergence.
  
    Args:
      model_input: A 'batch_size' x 'max_frames' x 'num_features' matrix of
                   input features.
      vocab_size: The number of classes in the dataset.
      num_frames: A vector of length 'batch' which indicates the number of
           frames for each video (before padding).
  
    Returns:
      A dictionary with a tensor containing the probability predictions of the
      model in the 'predictions' key. The dimensions of the tensor are
      'batch_size' x 'num_classes'.
    """
  
    def create_model(self,
                     model_input,
                     vocab_size,
                     num_frames,
                     iterations=None,
                     add_batch_norm=None,
                     sample_random_frames=None,
                     cluster_size=None,
                     hidden_size=None,
                     is_training=True,
                     **unused_params):
      iterations = iterations or FLAGS.iterations
      add_batch_norm = add_batch_norm or FLAGS.dbof_add_batch_norm
      random_frames = sample_random_frames or FLAGS.sample_random_frames
      cluster_size = cluster_size or FLAGS.dbof_cluster_size
      hidden1_size = hidden_size or FLAGS.dbof_hidden_size
  
      num_frames = tf.cast(tf.expand_dims(num_frames, 1), tf.float32)
      if random_frames:
        model_input = model_utils.SampleRandomFrames(model_input, num_frames,
                                               iterations)
      else:
        model_input = model_utils.SampleRandomSequence(model_input, num_frames,
                                                 iterations)
      max_frames = model_input.get_shape().as_list()[1]
      feature_size = model_input.get_shape().as_list()[2]
      reshaped_input = tf.reshape(model_input, [-1, feature_size])
      tf.summary.histogram("input_hist", reshaped_input)
  
      if add_batch_norm:
        reshaped_input = slim.batch_norm(
            reshaped_input,
            center=True,
            scale=True,
            is_training=is_training,
            scope="input_bn")
      cluster_weights = tf.compat.v1.get_variable(
          "cluster_weights", [feature_size, cluster_size],
          initializer=tf.random_normal_initializer(stddev=1 /
                                                   math.sqrt(feature_size)))
  
      tf.summary.histogram("cluster_weights", cluster_weights)
      activation = tf.matmul(reshaped_input, cluster_weights)
      if add_batch_norm:
        activation = slim.batch_norm(
            activation,
            center=True,
            scale=True,
            is_training=is_training,
            scope="cluster_bn")
      else:
        cluster_biases = tf.Variable(
            tf.random_normal(
                [cluster_size], stddev=1 / math.sqrt(feature_size)))
        tf.summary.histogram("cluster_biases", cluster_biases)
        activation += cluster_biases
      activation = tf.nn.relu6(activation)
      tf.summary.histogram("cluster_output", activation)
  
      activation = tf.reshape(activation, [-1, max_frames, cluster_size])
      activation = model_utils.FramePooling(activation, FLAGS.dbof_pooling_method)
      
      
      hidden1_weights = tf.compat.v1.get_variable(
          "hidden1_weights", [cluster_size, hidden1_size],
          initializer=tf.random_normal_initializer(stddev=1 /
                                                   math.sqrt(cluster_size)))
  
      tf.summary.histogram("hidden1_weights", hidden1_weights)
      activation = tf.matmul(activation, hidden1_weights)
      if add_batch_norm:
        activation = slim.batch_norm(
            activation,
            center=True,
            scale=True,
            is_training=is_training,
            scope="hidden1_bn")
      else:
        hidden1_biases = tf.Variable(
            tf.random_normal(
                [hidden1_size], stddev=0.01))
        tf.summary.histogram("hidden1_biases", hidden1_biases)
        activation += hidden1_biases
      activation = tf.nn.relu6(activation)
      tf.summary.histogram("hidden1_output", activation)
  
      aggregated_model = getattr(video_level_models,
                                 FLAGS.video_level_classifier_model)
      return aggregated_model().create_model(
          model_input=activation,
          original_input=model_input,
          vocab_size=vocab_size,
          **unused_params)
  
  