#!/usr/bin/python
#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implements Resnet model.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

import tensorflow as tf


def get_shape_as_list(x):
  return x.get_shape().as_list()


def fixed_padding(x, kernel_size):
  pad_total = kernel_size - 1
  pad_beg = pad_total // 2
  pad_end = pad_total - pad_beg

  x = tf.pad(x, [[0, 0],
                 [pad_beg, pad_end], [pad_beg, pad_end],
                 [0, 0]])
  return x


def batch_norm(x, training):
  return tf.layers.batch_normalization(x, fused=True, training=training)


def identity_norm(x, training):
  del training
  return x


def bottleneck_v1(x, filters, training,  # pylint: disable=missing-docstring
                  strides=1,
                  activation_fn=tf.nn.relu,
                  normalization_fn=batch_norm,
                  kernel_regularizer=None):

  # Record input tensor, such that it can be used later in as skip-connection
  x_shortcut = x

  # Project input if necessary
  if (strides > 1) or (filters != x.shape[-1]):
    x_shortcut = tf.layers.conv2d(x_shortcut, filters=filters, kernel_size=1,
                                  strides=strides,
                                  kernel_regularizer=kernel_regularizer,
                                  use_bias=False,
                                  padding='SAME')
    x_shortcut = normalization_fn(x_shortcut, training=training)

  # First convolution
  # Note, that unlike original Resnet paper we never use stride in the first
  # convolution. Instead, we apply stride in the second convolution. The reason
  # is that the first convolution has kernel of size 1x1, which results in
  # information loss when combined with stride bigger than one.
  x = tf.layers.conv2d(x, filters=filters // 4,
                       kernel_size=1,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='SAME')
  x = normalization_fn(x, training=training)
  x = activation_fn(x)

  # Second convolution
  x = fixed_padding(x, kernel_size=3)
  x = tf.layers.conv2d(x, filters=filters // 4,
                       strides=strides,
                       kernel_size=3,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='VALID')
  x = normalization_fn(x, training=training)
  x = activation_fn(x)

  # Third convolution
  x = tf.layers.conv2d(x, filters=filters,
                       kernel_size=1,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='SAME')
  x = normalization_fn(x, training=training)

  # Skip connection
  x = x_shortcut + x
  x = activation_fn(x)

  return x


def bottleneck_v2(x, filters, training,  # pylint: disable=missing-docstring
                  strides=1,
                  activation_fn=tf.nn.relu,
                  normalization_fn=batch_norm,
                  kernel_regularizer=None,
                  no_shortcut=False):

  # Record input tensor, such that it can be used later in as skip-connection
  x_shortcut = x

  x = normalization_fn(x, training=training)
  x = activation_fn(x)

  # Project input if necessary
  if (strides > 1) or (filters != x.shape[-1]):
    x_shortcut = tf.layers.conv2d(x, filters=filters, kernel_size=1,
                                  strides=strides,
                                  kernel_regularizer=kernel_regularizer,
                                  use_bias=False,
                                  padding='VALID')

  # First convolution
  # Note, that unlike original Resnet paper we never use stride in the first
  # convolution. Instead, we apply stride in the second convolution. The reason
  # is that the first convolution has kernel of size 1x1, which results in
  # information loss when combined with stride bigger than one.
  x = tf.layers.conv2d(x, filters=filters // 4,
                       kernel_size=1,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='SAME')

  # Second convolution
  x = normalization_fn(x, training=training)
  x = activation_fn(x)
  # Note, that padding depends on the dilation rate.
  x = fixed_padding(x, kernel_size=3)
  x = tf.layers.conv2d(x, filters=filters // 4,
                       strides=strides,
                       kernel_size=3,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='VALID')

  # Third convolution
  x = normalization_fn(x, training=training)
  x = activation_fn(x)
  x = tf.layers.conv2d(x, filters=filters,
                       kernel_size=1,
                       kernel_regularizer=kernel_regularizer,
                       use_bias=False,
                       padding='SAME')

  if no_shortcut:
    return x
  else:
    return x + x_shortcut


def resnet(x,  # pylint: disable=missing-docstring
           is_training,
           num_layers,
           strides=(2, 2, 2),
           num_classes=1000,
           filters_factor=4,
           weight_decay=1e-4,
           include_root_block=True,