text: """
      VGG for ImageNet-1K, implemented in TensorFlow.
      Original paper: 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  """
  
  __all__ = ['VGG', 'vgg11', 'vgg13', 'vgg16', 'vgg19', 'bn_vgg11', 'bn_vgg13', 'bn_vgg16', 'bn_vgg19', 'bn_vgg11b',
             'bn_vgg13b', 'bn_vgg16b', 'bn_vgg19b']
  
  import os
  import tensorflow as tf
  import tensorflow.keras.layers as nn
  from .common import conv3x3_block, MaxPool2d, flatten
  
  
  class VGGDense(nn.Layer):
      """
      VGG specific dense block.
  
      Parameters:
      ----------
      in_channels : int
          Number of input channels.
      out_channels : int
          Number of output channels.
      """
  
      def __init__(self,
                   in_channels,
                   out_channels,
                   **kwargs):
          super(VGGDense, self).__init__(**kwargs)
          self.fc = nn.Dense(
              units=out_channels,
              input_dim=in_channels,
              name="fc")
          self.activ = nn.ReLU()
          self.dropout = nn.Dropout(
              rate=0.5,
              name="dropout")
  
      def call(self, x, training=None):
          x = self.fc(x)
          x = self.activ(x)
          x = self.dropout(x, training=training)
          return x
  
  
  class VGGOutputBlock(nn.Layer):
      """
      VGG specific output block.
  
      Parameters:
      ----------
      in_channels : int
          Number of input channels.
      classes : int
          Number of classification classes.
      """
      def __init__(self,
                   in_channels,
                   classes,
                   **kwargs):
          super(VGGOutputBlock, self).__init__(**kwargs)
          mid_channels = 4096
  
          self.fc1 = VGGDense(
              in_channels=in_channels,
              out_channels=mid_channels,
              name="fc1")
          self.fc2 = VGGDense(
              in_channels=mid_channels,
              out_channels=mid_channels,
              name="fc2")
          self.fc3 = nn.Dense(
              units=classes,
              input_dim=mid_channels,
              name="fc3")
  
      def call(self, x, training=None):
          x = self.fc1(x, training=training)
          x = self.fc2(x, training=training)
          x = self.fc3(x)
          return x
  
  
  class VGG(tf.keras.Model):
      """
      VGG models from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      channels : list of list of int
          Number of output channels for each unit.
      use_bias : bool, default True
          Whether the convolution layer uses a bias vector.
      use_bn : bool, default False
          Whether to use BatchNorm layers.
      in_channels : int, default 3
          Number of input channels.
      in_size : tuple of two ints, default (224, 224)
          Spatial size of the expected input image.
      classes : int, default 1000
          Number of classification classes.
      data_format : str, default 'channels_last'
          The ordering of the dimensions in tensors.
      """
      def __init__(self,
                   channels,
                   use_bias=True,
                   use_bn=False,
                   in_channels=3,
                   in_size=(224, 224),
                   classes=1000,
                   data_format="channels_last",
                   **kwargs):
          super(VGG, self).__init__(**kwargs)
          self.in_size = in_size
          self.classes = classes
          self.data_format = data_format
  
          self.features = tf.keras.Sequential(name="features")
          for i, channels_per_stage in enumerate(channels):
              stage = tf.keras.Sequential(name="stage{}".format(i + 1))
              for j, out_channels in enumerate(channels_per_stage):
                  stage.add(conv3x3_block(
                      in_channels=in_channels,
                      out_channels=out_channels,
                      use_bias=use_bias,
                      use_bn=use_bn,
                      data_format=data_format,
                      name="unit{}".format(j + 1)))
                  in_channels = out_channels
              stage.add(MaxPool2d(
                  pool_size=2,
                  strides=2,
                  padding=0,
                  data_format=data_format,
                  name="pool{}".format(i + 1)))
              self.features.add(stage)
  
          self.output1 = VGGOutputBlock(
              in_channels=(in_channels * 7 * 7),
              classes=classes,
              name="output1")
  
      def call(self, x, training=None):
          x = self.features(x, training=training)
          x = flatten(x, self.data_format)
          x = self.output1(x)
          return x
  
  
  def get_vgg(blocks,
              use_bias=True,
              use_bn=False,
              model_name=None,
              pretrained=False,
              root=os.path.join("~", ".tensorflow", "models"),
              **kwargs):
      """
      Create VGG model with specific parameters.
  
      Parameters:
      ----------
      blocks : int
          Number of blocks.
      use_bias : bool, default True
          Whether the convolution layer uses a bias vector.
      use_bn : bool, default False
          Whether to use BatchNorm layers.
      model_name : str or None, default None
          Model name for loading pretrained model.
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      if blocks == 11:
          layers = [1, 1, 2, 2, 2]
      elif blocks == 13:
          layers = [2, 2, 2, 2, 2]
      elif blocks == 16:
          layers = [2, 2, 3, 3, 3]
      elif blocks == 19:
          layers = [2, 2, 4, 4, 4]
      else:
          raise ValueError("Unsupported VGG with number of blocks: {}".format(blocks))
  
      channels_per_layers = [64, 128, 256, 512, 512]
      channels = [[ci] * li for (ci, li) in zip(channels_per_layers, layers)]
  
      net = VGG(
          channels=channels,
          use_bias=use_bias,
          use_bn=use_bn,
          **kwargs)
  
      if pretrained:
          if (model_name is None) or (not model_name):
              raise ValueError("Parameter `model_name` should be properly initialized for loading pretrained model.")
          from .model_store import get_model_file
          in_channels = kwargs["in_channels"] if ("in_channels" in kwargs) else 3
          input_shape = (1,) + (in_channels,) + net.in_size if net.data_format == "channels_first" else\
              (1,) + net.in_size + (in_channels,)
          net.build(input_shape=input_shape)
          net.load_weights(
              filepath=get_model_file(
                  model_name=model_name,
                  local_model_store_dir_path=root))
  
      return net
  
  
  def vgg11(**kwargs):
      """
      VGG-11 model from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=11, model_name="vgg11", **kwargs)
  
  
  def vgg13(**kwargs):
      """
      VGG-13 model from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=13, model_name="vgg13", **kwargs)
  
  
  def vgg16(**kwargs):
      """
      VGG-16 model from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=16, model_name="vgg16", **kwargs)
  
  
  def vgg19(**kwargs):
      """
      VGG-19 model from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=19, model_name="vgg19", **kwargs)
  
  
  def bn_vgg11(**kwargs):
      """
      VGG-11 model with batch normalization from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=11, use_bias=False, use_bn=True, model_name="bn_vgg11", **kwargs)
  
  
  def bn_vgg13(**kwargs):
      """
      VGG-13 model with batch normalization from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=13, use_bias=False, use_bn=True, model_name="bn_vgg13", **kwargs)
  
  
  def bn_vgg16(**kwargs):
      """
      VGG-16 model with batch normalization from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=16, use_bias=False, use_bn=True, model_name="bn_vgg16", **kwargs)
  
  
  def bn_vgg19(**kwargs):
      """
      VGG-19 model with batch normalization from 'Very Deep Convolutional Networks for Large-Scale Image Recognition,'
      https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=19, use_bias=False, use_bn=True, model_name="bn_vgg19", **kwargs)
  
  
  def bn_vgg11b(**kwargs):
      """
      VGG-11 model with batch normalization and biases in convolution layers from 'Very Deep Convolutional Networks for
      Large-Scale Image Recognition,' https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=11, use_bias=True, use_bn=True, model_name="bn_vgg11b", **kwargs)
  
  
  def bn_vgg13b(**kwargs):
      """
      VGG-13 model with batch normalization and biases in convolution layers from 'Very Deep Convolutional Networks for
      Large-Scale Image Recognition,' https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=13, use_bias=True, use_bn=True, model_name="bn_vgg13b", **kwargs)
  
  
  def bn_vgg16b(**kwargs):
      """
      VGG-16 model with batch normalization and biases in convolution layers from 'Very Deep Convolutional Networks for
      Large-Scale Image Recognition,' https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=16, use_bias=True, use_bn=True, model_name="bn_vgg16b", **kwargs)
  
  
  def bn_vgg19b(**kwargs):
      """
      VGG-19 model with batch normalization and biases in convolution layers from 'Very Deep Convolutional Networks for
      Large-Scale Image Recognition,' https://arxiv.org/abs/1409.1556.
  
      Parameters:
      ----------
      pretrained : bool, default False
          Whether to load the pretrained weights for model.
      root : str, default '~/.tensorflow/models'
          Location for keeping the model parameters.
      """
      return get_vgg(blocks=19, use_bias=True, use_bn=True, model_name="bn_vgg19b", **kwargs)
  
  
  def _test():
      import numpy as np
      import tensorflow.keras.backend as K
  
      pretrained = False
  
      models = [
          vgg11,
          vgg13,
          vgg16,
          vgg19,
          bn_vgg11,
          bn_vgg13,
          bn_vgg16,
          bn_vgg19,
          bn_vgg11b,
          bn_vgg13b,
          bn_vgg16b,
          bn_vgg19b,
      ]
  
      for model in models:
  
          net = model(pretrained=pretrained)
  
          batch = 14
          x = tf.random.normal((batch, 224, 224, 3))
          y = net(x)
          assert (tuple(y.shape.as_list()) == (batch, 1000))
  
          weight_count = sum([np.prod(K.get_value(w).shape) for w in net.trainable_weights])
          print("m={}, {}".format(model.__name__, weight_count))
          assert (model != vgg11 or weight_count == 132863336)
          assert (model != vgg13 or weight_count == 133047848)
          assert (model != vgg16 or weight_count == 138357544)
          assert (model != vgg19 or weight_count == 143667240)
          assert (model != bn_vgg11 or weight_count == 132866088)
          assert (model != bn_vgg13 or weight_count == 133050792)
          assert (model != bn_vgg16 or weight_count == 138361768)
          assert (model != bn_vgg19 or weight_count == 143672744)
          assert (model != bn_vgg11b or weight_count == 132868840)
          assert (model != bn_vgg13b or weight_count == 133053736)
          assert (model != bn_vgg16b or weight_count == 138365992)
          assert (model != bn_vgg19b or weight_count == 143678248)
  
  
  if __name__ == "__main__":
      _test()
  