# Copyright 2017 The TensorFlow Authors All Rights Reserved.
#
# 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.
# ==============================================================================

"""Base class for voxel generation model."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import os

import numpy as np
import tensorflow as tf

import input_generator
import utils

slim = tf.contrib.slim


class Im2Vox(object):
  """Defines the voxel generation model."""

  __metaclass__ = abc.ABCMeta

  def __init__(self, params):
    self._params = params

  @abc.abstractmethod
  def get_metrics(self, inputs, outputs):
    """Gets dictionaries from metrics to value `Tensors` & update `Tensors`."""
    pass

  @abc.abstractmethod
  def get_loss(self, inputs, outputs):
    pass

  @abc.abstractmethod
  def get_regularization_loss(self, scopes):
    pass

  def set_params(self, params):
    self._params = params

  def get_inputs(self,
                 dataset_dir,
                 dataset_name,
                 split_name,
                 batch_size,
                 image_size,
                 vox_size,
                 is_training=True):
    """Loads data for a specified dataset and split."""
    del image_size, vox_size
    with tf.variable_scope('data_loading_%s/%s' % (dataset_name, split_name)):
      common_queue_min = 64
      common_queue_capacity = 256
      num_readers = 4

      inputs = input_generator.get(
          dataset_dir,
          dataset_name,
          split_name,
          shuffle=is_training,
          num_readers=num_readers,
          common_queue_min=common_queue_min,
          common_queue_capacity=common_queue_capacity)

      images, voxels = tf.train.batch(
          [inputs['image'], inputs['voxel']],
          batch_size=batch_size,
          num_threads=8,
          capacity=8 * batch_size,
          name='batching_queues/%s/%s' % (dataset_name, split_name))

      outputs = dict()
      outputs['images'] = images
      outputs['voxels'] = voxels
      outputs['num_samples'] = inputs['num_samples']

    return outputs

  def preprocess(self, raw_inputs, step_size):
    """Selects the subset of viewpoints to train on."""