# Path: util/config.py
# def load_config(filename = "pose_cfg.yaml"):
#     if 'POSE_PARAM_PATH' in os.environ:
#         filename = os.environ['POSE_PARAM_PATH'] + '/' + filename
#     return cfg_from_file(filename)
# 
# Path: dataset/factory.py
# def create(cfg):
#     dataset_type = cfg.dataset_type
#     if dataset_type == "mpii":
#         from dataset.mpii import MPII
#         data = MPII(cfg)
#     elif dataset_type == "coco":
#         from dataset.mscoco import MSCOCO
#         data = MSCOCO(cfg)
#     elif dataset_type == "penn_action":
#         from dataset.penn_action import PennAction
#         data = PennAction(cfg)
#     elif dataset_type == "default":
#         data = PoseDataset(cfg)
#     else:
#         raise Exception("Unsupported dataset_type: \"{}\"".format(dataset_type))
#     return data
# 
# Path: nnet/net_factory.py
# def pose_net(cfg):
#     if cfg.video:
#         from nnet.pose_seq_net import PoseSeqNet
#         cls = PoseSeqNet
#     else:
#         cls = PoseNet
#     return cls(cfg)
# 
# Path: nnet/pose_net.py
# def get_batch_spec(cfg):
#     num_joints = cfg.num_joints
#     batch_size = cfg.batch_size
#     batch_spec = {
#         Batch.inputs: [batch_size, None, None, 3],
#         Batch.part_score_targets: [batch_size, None, None, num_joints],
#         Batch.part_score_weights: [batch_size, None, None, num_joints]
#     }
#     if cfg.location_refinement:
#         batch_spec[Batch.locref_targets] = [batch_size, None, None, num_joints * 2]
#         batch_spec[Batch.locref_mask] = [batch_size, None, None, num_joints * 2]
#     if cfg.pairwise_predict:
#         batch_spec[Batch.pairwise_targets] = [batch_size, None, None, num_joints * (num_joints - 1) * 2]
#         batch_spec[Batch.pairwise_mask] = [batch_size, None, None, num_joints * (num_joints - 1) * 2]
#     return batch_spec
# 
# Path: util/logging.py
# def setup_logging():
#     FORMAT = '%(asctime)-15s %(message)s'
#     logging.basicConfig(filename='log.txt', filemode='w',
#                         datefmt='%Y-%m-%d %H:%M:%S',
#                         level=logging.INFO, format=FORMAT)
#     console = logging.StreamHandler()
#     console.setLevel(logging.INFO)
#     logging.getLogger('').addHandler(console)


import logging
import threading
import tensorflow as tf
import tensorflow.contrib.slim as slim
from util.config import load_config
from dataset.factory import create as create_dataset
from nnet.net_factory import pose_net
from nnet.pose_net import get_batch_spec
from util.logging import setup_logging
def start_preloading(sess, enqueue_op, dataset, placeholders):
    coord = tf.train.Coordinator()

    t = threading.Thread(target=load_and_enqueue,
                         args=(sess, enqueue_op, coord, dataset, placeholders))
    t.start()

    return coord, t


def get_optimizer(loss_op, cfg):
    learning_rate = tf.placeholder(tf.float32, shape=[])

    if cfg.optimizer == "sgd":
        optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
    elif cfg.optimizer == "adam":
        optimizer = tf.train.AdamOptimizer(cfg.adam_lr)
    else:
        raise ValueError('unknown optimizer {}'.format(cfg.optimizer))
    train_op = slim.learning.create_train_op(loss_op, optimizer)

    return learning_rate, train_op


def train():
    setup_logging()

    cfg = load_config()
    dataset = create_dataset(cfg)


# Path: util/config.py
# def load_config(filename = "pose_cfg.yaml"):
#     if 'POSE_PARAM_PATH' in os.environ:
#         filename = os.environ['POSE_PARAM_PATH'] + '/' + filename
#     return cfg_from_file(filename)
# 
# Path: dataset/factory.py
# def create(cfg):
#     dataset_type = cfg.dataset_type
#     if dataset_type == "mpii":
#         from dataset.mpii import MPII
#         data = MPII(cfg)
#     elif dataset_type == "coco":
#         from dataset.mscoco import MSCOCO
#         data = MSCOCO(cfg)
#     elif dataset_type == "penn_action":
#         from dataset.penn_action import PennAction
#         data = PennAction(cfg)
#     elif dataset_type == "default":
#         data = PoseDataset(cfg)
#     else:
#         raise Exception("Unsupported dataset_type: \"{}\"".format(dataset_type))
#     return data
# 
# Path: nnet/net_factory.py
# def pose_net(cfg):
#     if cfg.video:
#         from nnet.pose_seq_net import PoseSeqNet
#         cls = PoseSeqNet
#     else:
#         cls = PoseNet
#     return cls(cfg)
# 
# Path: nnet/pose_net.py
# def get_batch_spec(cfg):
#     num_joints = cfg.num_joints
#     batch_size = cfg.batch_size
#     batch_spec = {
#         Batch.inputs: [batch_size, None, None, 3],
#         Batch.part_score_targets: [batch_size, None, None, num_joints],
#         Batch.part_score_weights: [batch_size, None, None, num_joints]
#     }
#     if cfg.location_refinement:
#         batch_spec[Batch.locref_targets] = [batch_size, None, None, num_joints * 2]
#         batch_spec[Batch.locref_mask] = [batch_size, None, None, num_joints * 2]
#     if cfg.pairwise_predict:
#         batch_spec[Batch.pairwise_targets] = [batch_size, None, None, num_joints * (num_joints - 1) * 2]
#         batch_spec[Batch.pairwise_mask] = [batch_size, None, None, num_joints * (num_joints - 1) * 2]
#     return batch_spec
# 
# Path: util/logging.py
# def setup_logging():
#     FORMAT = '%(asctime)-15s %(message)s'
#     logging.basicConfig(filename='log.txt', filemode='w',
#                         datefmt='%Y-%m-%d %H:%M:%S',
#                         level=logging.INFO, format=FORMAT)
#     console = logging.StreamHandler()
#     console.setLevel(logging.INFO)
#     logging.getLogger('').addHandler(console)


# Path: train.py
import logging
import threading
import tensorflow as tf
import tensorflow.contrib.slim as slim
from util.config import load_config
from dataset.factory import create as create_dataset
from nnet.net_factory import pose_net
from nnet.pose_net import get_batch_spec
from util.logging import setup_logging

def start_preloading(sess, enqueue_op, dataset, placeholders):
    coord = tf.train.Coordinator()

    t = threading.Thread(target=load_and_enqueue,
                         args=(sess, enqueue_op, coord, dataset, placeholders))
    t.start()

    return coord, t


def get_optimizer(loss_op, cfg):
    learning_rate = tf.placeholder(tf.float32, shape=[])

    if cfg.optimizer == "sgd":
        optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
    elif cfg.optimizer == "adam":
        optimizer = tf.train.AdamOptimizer(cfg.adam_lr)
    else:
        raise ValueError('unknown optimizer {}'.format(cfg.optimizer))
    train_op = slim.learning.create_train_op(loss_op, optimizer)

    return learning_rate, train_op


def train():
    setup_logging()

    cfg = load_config()
    dataset = create_dataset(cfg)


    batch_spec = get_batch_spec(cfg)