text:
#
# Copyright (c) 2017 Intel Corporation
#
# 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.
#

from typing import Union

import numpy as np

from rl_coach.agents.ddpg_agent import DDPGAgent, DDPGAgentParameters, DDPGAlgorithmParameters
from rl_coach.core_types import RunPhase
from rl_coach.spaces import SpacesDefinition

class HACDDPGAlgorithmParameters(DDPGAlgorithmParameters):
    def __init__(self):
        super().__init__()
        self.time_limit = 40
        self.sub_goal_testing_rate = 0.5

class HACDDPGAgentParameters(DDPGAgentParameters):
    def __init__(self):
        super().__init__()
        self.algorithm = HACDDPGAlgorithmParameters()

    @property
    def path(self):
        return 'rl_coach.agents.hac_ddpg_agent:HACDDPGAgent'

# Hierarchical Actor Critic Generating Subgoals DDPG Agent - https://arxiv.org/pdf/1712.00948.pdf
class HACDDPGAgent(DDPGAgent):
    def __init__(self, agent_parameters, parent: Union['LevelManager', 'CompositeAgent']=None):
        super().__init__(agent_parameters, parent)
        self.sub_goal_testing_rate = self.ap.algorithm.sub_goal_testing_rate
        self.graph_manager = None

    def choose_action(self, curr_state):
        # top level decides, for each of his generated sub-goals, for all the layers beneath him if this is a sub-goal
        # testing phase

        graph_manager = self.parent_level_manager.parent_graph_manager
        if self.ap.is_a_highest_level_agent:
            graph_manager.should_test_current_sub_goal = np.random.rand() < self.sub_goal_testing_rate

        if self.phase == RunPhase.TRAIN:
            if graph_manager.should_test_current_sub_goal:
                self.exploration_policy.change_phase(RunPhase.TEST)
            else:
                self.exploration_policy.change_phase(self.phase)

        action_info = super().choose_action(curr_state)
        return action_info

    def update_transition_before_adding_to_replay_buffer(self, transition):
        graph_manager = self.parent_level_manager.parent_graph_manager

        # deal with goals given from a higher level agent
        if not self.ap.is_a_highest_level_agent:
            transition.state['desired_goal'] = self.current_hrl_goal
            transition.next_state['desired_goal'] = self.current_hrl_goal
            # TODO: allow setting goals which are not part of the state. e.g. state-embedding using get_prediction
            self.distance_from_goal.add_sample(self.spaces.goal.distance_from_goal(
                self.current_hrl_goal, transition.next_state))
            goal_reward, sub_goal_reached = self.spaces.goal.get_reward_for_goal_and_state(
                self.current_hrl_goal, transition.next_state)
            transition.reward = goal_reward
            transition.game_over = transition.game_over or sub_goal_reached

        # each level tests its own generated sub goals
        if not self.ap.is_a_lowest_level_agent and graph_manager.should_test_current_sub_goal:
            #TODO-fixme
            # _, sub_goal_reached = self.parent_level_manager.environment.agents['agent_1'].spaces.goal.\
            # get_reward_for_goal_and_state(transition.action, transition.next_state)

            _, sub_goal_reached = self.spaces.goal.get_reward_for_goal_and_state(
                transition.action, transition.next_state)

            sub_goal_is_missed = not sub_goal_reached

            if sub_goal_is_missed:
                    transition.reward = -self.ap.algorithm.time_limit
        return transition

    def set_environment_parameters(self, spaces: SpacesDefinition):
        super().set_environment_parameters(spaces)

        if self.ap.is_a_highest_level_agent:
            # the rest of the levels already have an in_action_space set to be of type GoalsSpace, thus they will have
            # their GoalsSpace set to the in_action_space in agent.set_environment_parameters()
            self.spaces.goal = self.spaces.action
            self.spaces.goal.set_target_space(self.spaces.state[self.spaces.goal.goal_name])

        if not self.ap.is_a_highest_level_agent:
            self.spaces.reward.reward_success_threshold = self.spaces.goal.reward_type.goal_reaching_reward


text:
from __future__ import print_function
import os
import time
import random
import numpy as np
from tqdm import tqdm
import tensorflow as tf

from .base import BaseModel
from .history import History
from .replay_memory import ReplayMemory

import cv2

class AgentFreezed(BaseModel):
  def __init__(self, config, environment, sess, model_type='ckptv2', prefix=None):
    super(AgentFreezed, self).__init__(config)
    self.sess = sess
    self.weight_dir = './checkpoints/' + self.env_name
    self.model_type = model_type

    self.env = environment
    self.history = History(self.config)
    self.memory = ReplayMemory(self.config, self.model_dir)

    self.input_name = 'prediction/s_t'
    self.output_name = 'prediction/ArgMax'

    self.load_dqn(prefix, model_type)

  def predict(self, s_t, test_ep=None):
    ep = test_ep
    if random.random() < ep:
      action = random.randrange(self.env.action_size)
    else:
      q = self.sess.run(self.output, feed_dict={self.input: [s_t]})
      action = q[0]
      # action = self.q_action.eval({self.s_t: [s_t]})[0]

    return action

  def load_dqn(self, prefix=None, model_type='ckptv2'):
    if prefix is not None:
      prefix = os.path.join(self.weight_dir, prefix)
    else:
      prefix = os.path.join(self.weight_dir, '-39800000')

    print("load model from {:s} in [{:s}]".format(model_type, prefix))
    if model_type == 'ckptv1':
      ckpt = tf.train.get_checkpoint_state(self.weight_dir)
      assert ckpt is not None
      meta_file = prefix + '.meta'
      saver = tf.train.import_meta_graph(meta_file, clear_devices=True)
      self.sess.run(tf.global_variables_initializer())
      saver.restore(self.sess, prefix)
    elif model_type == 'ckptv2':
      meta_file = prefix + '.meta'
      saver = tf.train.import_meta_graph(meta_file, clear_devices=True)
      self.sess.run(tf.global_variables_initializer())
      input_checkpoint = meta_file[0: len(meta_file) - 5]
      saver.restore(self.sess, input_checkpoint)
    elif model_type == 'freezePb':
      pb_file = prefix + '.pb'
      pb = open(pb_file, 'rb')
      self.graph_def = tf.GraphDef()
      self.graph_def.ParseFromString(pb.read())
      self.sess.run(tf.global_variables_initializer())
      tf.import_graph_def(self.graph_def, name='')
    elif model_type == "SavedModel":
      saved_model_tag = tf.saved_model.tag_constants.SERVING
      self.sess.run(tf.global_variables_initializer())
      tf.saved_model.loader.load(self.sess, [saved_model_tag], prefix)
    else:
      raise NotImplementedError

    # mark graph input + output
    input_tensor_name = self.input_name + ':0'
    self.input = self.sess.graph.get_tensor_by_name(input_tensor_name)
    output_tensor_name = self.output_name + ':0'
    self.output = self.sess.graph.get_tensor_by_name(output_tensor_name)

    return

  def save_dqn(self, saved_dir=None, model_type='freezePb'):
    if saved_dir is not None:
      pb_file = os.path.join(saved_dir, 'freezePb.pb')
    else:
      raise NotImplementedError

    if model_type == 'freezePb':
      output_node_name = self.output_name
      output_graph_def = tf.graph_util.convert_variables_to_constants(
        sess=self.sess,
        input_graph_def=self.sess.graph_def,
        output_node_names=output_node_name.split(","))
      with tf.gfile.GFile(pb_file, "wb") as f:  # 保存模型
        f.write(output_graph_def.SerializeToString())  # 序列化输出
    elif model_type == 'SavedModel':
      tf.saved_model.simple_save(self.sess,
                                 os.path.join(saved_dir, 'SavedModel'),
                                 inputs={"Input": self.input},
                                 outputs={"Output": self.output})
    elif model_type == 'ckptv1':
      from tensorflow.core.protobuf import saver_pb2
      saver2 = tf.train.Saver(write_version=saver_pb2.SaverDef.V1)
      saver2.save(self.sess, os.path.join(saved_dir, 'ckptv1/-39800000.ckpt'))

    return

  def play(self, n_step=10000, n_episode=100, test_ep=None, render=False):
    if test_ep == None:
      test_ep = self.ep_end

    test_history = History(self.config)

    # if not self.display:
    #   gym_dir = '/tmp/%s-%s' % (self.env_name, get_time())
    #   self.env.env.monitor.start(gym_dir)

    best_reward, best_idx = 0, 0

    self.env.env.reset()
    for idx in range(n_episode):
      screen, reward, action, terminal = self.env.new_random_game()
      current_reward = 0

      for _ in range(self.history_length):
        test_history.add(screen)

      img_id = 24
      max_img_id = 24

      for t in tqdm(range(n_step), ncols=70):
        # 1. predict
        action = self.predict(test_history.get(), test_ep)
        # 2. act
        screen, reward, terminal = self.env.act(action, is_training=False)
        # 3. observe
        test_history.add(screen)
        # 4. display
        if not self.display:
          self.env.env.render(mode='human')
          np.testing.assert_almost_equal(test_history.get()[:, :, -1], screen, decimal=4)
          screen_saved = (screen * 255).astype(np.uint8)
          if img_id < max_img_id:
            os.makedirs('./images/' + self.env_name, exist_ok=True)
            cv2.imwrite('./images/{:s}/{:s}-{:02d}.jpg'.format(self.env_name, self.env_name, img_id), screen_saved)
            img_id += 1
          # time.sleep(0.01)
        else:
          img = self.env.env.render(mode='rgb_array')
          cv2.imshow('win', img)
          cv2.waitKey(0)

        current_reward += reward
        if terminal:
          break

      if current_reward > best_reward:
        best_reward = current_reward
        best_idx = idx

      print("="*30)
      print(" [%d] Best reward : %d" % (best_idx, best_reward))
      print("="*30)

    self.env.env.close()
      #gym.upload(gym_dir, writeup='https://github.com/devsisters/DQN-tensorflow', api_key='')

ext: py

reason: import numpy


text:
import logging
import nig
import numpy as np
import os
import tensorflow as tf

from collections import OrderedDict
from functools import partial
from nig.data import loaders

from experiment.nig import experiments

__author__ = 'eaplatanios'

logger = logging.getLogger(__name__)

class MNISTExperiment(experiments.ExperimentBase):
    def __init__(self, architectures, use_one_hot_encoding=True,
                 activation=tf.nn.relu, labeled_batch_size=100,
                 unlabeled_batch_size=100, test_data_proportion=0.1,
                 max_iter=1000, abs_loss_chg_tol=1e-6, rel_loss_chg_tol=1e-6,
                 loss_chg_iter_below_tol=5, logging_frequency=10,
                 summary_frequency=100, checkpoint_frequency=1000,
                 evaluation_frequency=10, variable_statistics_frequency=-1,
                 run_meta_data_frequency=-1,
                 working_dir=os.path.join(os.getcwd(), 'working'),
                 checkpoint_file_prefix='ckpt', restore_sequentially=False,
                 save_trained=True, optimizer=lambda: tf.train.AdamOptimizer(),
                 gradients_processor=None, seed=None):
        self.architectures = architectures
        self.use_one_hot_encoding = use_one_hot_encoding
        # self.loss = nig.L2Loss()
        self.loss = nig.CrossEntropy(
            log_outputs=False, scaled_outputs=True,
            one_hot_train_outputs=self.use_one_hot_encoding)
        optimizer_opts = {
            'batch_size': labeled_batch_size,
            'max_iter': max_iter,
            'abs_loss_chg_tol': abs_loss_chg_tol,
            'rel_loss_chg_tol': rel_loss_chg_tol,
            'loss_chg_iter_below_tol': loss_chg_iter_below_tol,
            'grads_processor': gradients_processor}
        dataset_info = loaders.mnist.dataset_info
        num_features = dataset_info['num_features']
        num_labels = dataset_info['num_labels']
        models = [nig.MultiLayerPerceptron(
            input_size=num_features, output_size=num_labels,
            hidden_layer_sizes=architecture, activation=activation,
            softmax_output=True, log_output=False,
            train_outputs_one_hot=use_one_hot_encoding, loss=self.loss,
            loss_summary=False, optimizer=optimizer,
            optimizer_opts=optimizer_opts)
                  for architecture in self.architectures]
        eval_metrics = [
            nig.Accuracy(
                log_outputs=False, scaled_outputs=True,
                one_hot_train_outputs=True, thresholds=0.5, macro_average=True),
            nig.AreaUnderCurve(
                log_outputs=False, scaled_outputs=True,
                one_hot_train_outputs=True, curve='pr', num_thresholds=100,
                macro_average=True, name='auc'),
            nig.Precision(
                log_outputs=False, scaled_outputs=True,
                one_hot_train_outputs=True, thresholds=0.5, macro_average=True),
            nig.Recall(
                log_outputs=False, scaled_outputs=True,
                one_hot_train_outputs=True, thresholds=0.5, macro_average=True),
            nig.F1Score(
                log_outputs=False, scaled_outputs=True,
                one_hot_train_outputs=True, thresholds=0.5, macro_average=True)]
        predict_postprocess = lambda l: tf.argmax(l, 1)
        inputs_pipeline = nig.ColumnsExtractor(list(range(num_features)))
        outputs_pipeline = nig.ColumnsExtractor(num_features)
        if self.use_one_hot_encoding:
            outputs_pipeline = outputs_pipeline | \
                               nig.DataTypeEncoder(np.int8) | \
                               nig.OneHotEncoder(10)
        super(MNISTExperiment, self).__init__(
            models=models, eval_metrics=eval_metrics,
            predict_postprocess=predict_postprocess,
            inputs_pipeline=inputs_pipeline, outputs_pipeline=outputs_pipeline,
            labeled_batch_size=labeled_batch_size,
            unlabeled_batch_size=unlabeled_batch_size,
            test_data_proportion=test_data_proportion,
            logging_frequency=logging_frequency,
            summary_frequency=summary_frequency,
            checkpoint_frequency=checkpoint_frequency,
            evaluation_frequency=evaluation_frequency,
            variable_statistics_frequency=variable_statistics_frequency,
            run_meta_data_frequency=run_meta_data_frequency,
            working_dir=working_dir,
            checkpoint_file_prefix=checkpoint_file_prefix,
            restore_sequentially=restore_sequentially,
            save_trained=save_trained, seed=seed)

    def __str__(self):
        return 'mnist'

    def experiment_information(self):
        return {'architectures': str(self.architectures),
                'loss': str(self.loss)}

    def load_data(self, test_proportion=None):
        train_data, test_data = loaders.mnist.load(
            os.path.join(self.working_dir, 'data'), float_images=True)
        if test_proportion is None:
            return train_data, test_data
        data = self._merge_datasets(train_data, test_data)
        train_indices, test_indices = experiments.stratified_split(
            labels=data[:, -1], test_proportion=test_proportion, seed=self.seed)
        return data[train_indices], data[test_indices]

if __name__ == '__main__':
    seed = 9999
    architectures = [[16], [128], [128, 64, 32], [512, 256, 128], [1024]]
    use_one_hot_encoding = True
    activation = nig.leaky_relu(0.01)
    labeled_batch_size = 128
    unlabeled_batch_size = 128
    test_data_proportion = 0.95
    max_iter = 1000
    abs_loss_chg_tol = 1e-6
    rel_loss_chg_tol = 1e-6
    loss_chg_iter_below_tol = 5
    logging_frequency = 10
    summary_frequency = -1
    checkpoint_frequency = -1
    evaluation_frequency = 50
    variable_statistics_frequency = -1
    run_meta_data_frequency = -1
    working_dir = os.path.join(os.getcwd(), 'working', 'mnist')
    checkpoint_file_prefix = 'ckpt'
    restore_sequentially = False
    save_trained = False
    optimizer = lambda: tf.train.AdamOptimizer()  # nig.gradient_descent(1e0, decay_rate=0.99)
    gradients_processor = None  # lambda g: tf.clip_by_norm(g, 0.1)

    # optimizer = tf.contrib.opt.ScipyOptimizerInterface
    # optimizer_opts = {'options': {'maxiter': 10000}}

    # def consensus_loss_metric(outputs, consensus):
    #     with tf.name_scope('consensus_loss_metric'):
    #         outputs = tf.exp(outputs)
    #         metric = tf.square(tf.sub(outputs, consensus))
    #         metric = tf.reduce_sum(metric)
    #     return metric
    consensus_loss_metric = None
    consensus_configurations = experiments.get_consensus_configurations(
        consensus_loss_weights=[0.0, 1.0],
        multiplier=labeled_batch_size / unlabeled_batch_size)

    with nig.dummy():  # tf.device('/cpu:0'):
        experiment = MNISTExperiment(
            architectures=architectures,
            use_one_hot_encoding=use_one_hot_encoding,
            activation=activation, labeled_batch_size=labeled_batch_size,
            unlabeled_batch_size=unlabeled_batch_size,
            test_data_proportion=test_data_proportion, max_iter=max_iter,
            abs_loss_chg_tol=abs_loss_chg_tol,
            rel_loss_chg_tol=rel_loss_chg_tol,
            loss_chg_iter_below_tol=loss_chg_iter_below_tol,
            logging_frequency=logging_frequency,
            summary_frequency=summary_frequency,
            checkpoint_frequency=checkpoint_frequency,
            evaluation_frequency=evaluation_frequency,
            variable_statistics_frequency=variable_statistics_frequency,
            run_meta_data_frequency=run_meta_data_frequency,
            working_dir=working_dir,
            checkpoint_file_prefix=checkpoint_file_prefix,
            restore_sequentially=restore_sequentially,
            save_trained=save_trained, optimizer=optimizer,
            gradients_processor=gradients_processor, seed=seed)
        learners = []
        for name, configuration in consensus_configurations:
            learner = partial(nig.ConsensusLearner, **configuration)
            learners.append((name, learner))
        learners.append(('CV', nig.CrossValidationLearner))
        learners = OrderedDict(learners)
        results = experiment.run(learners)

        # maj_trust_based_learner = partial(nig.TrustBasedLearner, first_trust_update=max_iter + 1)
        # trust_based_learner = partial(
        #     nig.TrustBasedLearner, first_trust_update=10, trust_update_frequency=10)

        # test_predictions = learner.predict(
        #     _get_iterator(test_data, False), ckpt=False, working_dir=working_dir,
        #     ckpt_file_prefix=checkpoint_file_prefix)
        # test_truth = test_data[:, -1]
        # logger.info(np.mean(test_predictions == test_truth))

    experiments.save_results(
        results, filename=os.path.join(working_dir, 'results.pk'),
        update=True, use_backup=True, delete_backup=False, yaml_format=False)
    # results = experiments.load_results(
    #     filename=os.path.join(working_dir, 'results.pk'), yaml_format=False)
    experiments.plot_results(results)

max_stars_repo_name: eaplatanios/nig

max_issues_repo_name: eaplatanios/nig

max_forks_repo_name: eaplatanios/nig


text:
import numpy as np
softmax_outputs = np.array([[ 0.7 , 0.1 , 0.2 ],
                            [ 0.1 , 0.5 , 0.4 ],
                            [ 0.02 , 0.9 , 0.08 ]])
class_targets = np.array([[ 1 , 0 , 0 ],
                          [ 0 , 1 , 0 ],
                          [ 0 , 1 , 0 ]])
# Probabilities for target values - # only if categorical labels if len (class_targets.shape) == 1 : correct_confidences = softmax_outputs[ range ( len (softmax_outputs)), class_targets
if len (class_targets.shape) == 1 :   # for [0,1,1]
    correct_confidences = softmax_outputs[ range ( len (softmax_outputs)), class_targets
]
elif len (class_targets.shape) == 2 :  #for upper dimension like 2
    correct_confidences = np.sum( softmax_outputs * class_targets, axis = 1)

neg_log = - np.log(correct_confidences)
average_losses = np.mean(neg_log)    #average loss
print(average_losses)

#check spme

max_stars_repo_name: Dev-Gaju/NNFS-book-with-Implementation

max_issues_repo_name: Dev-Gaju/NNFS-book-with-Implementation

max_forks_repo_name: Dev-Gaju/NNFS-book-with-Implementation


text:
# This script is borrowed from https://github.com/nkolot/SPIN/blob/master/models/smpl.py

import torch
import numpy as np
from smplx import SMPL as _SMPL
from smplx.body_models import ModelOutput
from smplx.lbs import vertices2joints
from collections import namedtuple

from lib.pymaf.core import path_config, constants

SMPL_MEAN_PARAMS = path_config.SMPL_MEAN_PARAMS
SMPL_MODEL_DIR = path_config.SMPL_MODEL_DIR

# Indices to get the 14 LSP joints from the 17 H36M joints
H36M_TO_J17 = [6, 5, 4, 1, 2, 3, 16, 15, 14, 11, 12, 13, 8, 10, 0, 7, 9]
H36M_TO_J14 = H36M_TO_J17[:14]

class SMPL(_SMPL):
    """ Extension of the official SMPL implementation to support more joints """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        joints = [constants.JOINT_MAP[i] for i in constants.JOINT_NAMES]
        J_regressor_extra = np.load(path_config.JOINT_REGRESSOR_TRAIN_EXTRA)
        self.register_buffer(
            'J_regressor_extra',
            torch.tensor(J_regressor_extra, dtype=torch.float32))
        self.joint_map = torch.tensor(joints, dtype=torch.long)
        self.ModelOutput = namedtuple(
            'ModelOutput_', ModelOutput._fields + (
                'smpl_joints',
                'joints_J19',
            ))
        self.ModelOutput.__new__.__defaults__ = (None, ) * len(
            self.ModelOutput._fields)

    def forward(self, *args, **kwargs):
        kwargs['get_skin'] = True
        smpl_output = super().forward(*args, **kwargs)
        extra_joints = vertices2joints(self.J_regressor_extra,
                                       smpl_output.vertices)
        # smpl_output.joints: [B, 45, 3]  extra_joints: [B, 9, 3]
        vertices = smpl_output.vertices
        joints = torch.cat([smpl_output.joints, extra_joints], dim=1)
        smpl_joints = smpl_output.joints[:, :24]
        joints = joints[:, self.joint_map, :]  # [B, 49, 3]
        joints_J24 = joints[:, -24:, :]
        joints_J19 = joints_J24[:, constants.J24_TO_J19, :]
        output = self.ModelOutput(vertices=vertices,
                                  global_orient=smpl_output.global_orient,
                                  body_pose=smpl_output.body_pose,
                                  joints=joints,
                                  joints_J19=joints_J19,
                                  smpl_joints=smpl_joints,
                                  betas=smpl_output.betas,
                                  full_pose=smpl_output.full_pose)
        return output

def get_smpl_faces():
    smpl = SMPL(SMPL_MODEL_DIR, batch_size=1, create_transl=False)
    return smpl.faces

def get_part_joints(smpl_joints):
    batch_size = smpl_joints.shape[0]

    # part_joints = torch.zeros().to(smpl_joints.device)

    one_seg_pairs = [(0, 1), (0, 2), (0, 3), (3, 6), (9, 12), (9, 13), (9, 14),
                     (12, 15), (13, 16), (14, 17)]
    two_seg_pairs = [(1, 4), (2, 5), (4, 7), (5, 8), (16, 18), (17, 19),
                     (18, 20), (19, 21)]

    one_seg_pairs.extend(two_seg_pairs)

    single_joints = [(10), (11), (15), (22), (23)]

    part_joints = []

    for j_p in one_seg_pairs:
        new_joint = torch.mean(smpl_joints[:, j_p], dim=1, keepdim=True)
        part_joints.append(new_joint)

    for j_p in single_joints:
        part_joints.append(smpl_joints[:, j_p:j_p + 1])

    part_joints = torch.cat(part_joints, dim=1)

    return part_joints

max_stars_repo_name: YuliangXiu/ICON

max_issues_repo_name: YuliangXiu/ICON

max_forks_repo_name: YuliangXiu/ICON


text:
'''
################################################################################################################
Manuscript Title: Evaluation of tools used to predict the impact of missense variants is hindered by two types of circularity
################################################################################################################
Manuscripts Authors: Dominik G. Grimm, Chloe Agathe Azencott, Fabian Aicheler, Udo Gieraths, Daniel G. MacArthur, Kaitlin E. Samocha, David N. Cooper, Peter D. Stenson, Mark J. Daly, Jordan W. Smoller, Laramie E. Duncan, Karsten M. Borgwardt

Code by: Dominik Gerhard Grimm
Year: 2014/2015
Group: Machine Learning and Computational Biology Research Group
Insitute: Max Planck Institute for Intelligent Systems and Max Planck Institue for Developmental Biology

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''
import pylab as pl
import numpy as np
import utils,os
import matplotlib as mpl

def plotFigure(score_data=None,release=True):
    pl.ion()

    font_size = 13
    mpl.rcParams['font.family']="sans-serif"
    mpl.rcParams['font.sans-serif']="Arial"
    mpl.rcParams['font.size']=font_size
    mpl.rcParams['font.weight']='medium'
    mpl.rcParams['figure.dpi'] = 150
    mpl.rcParams['figure.facecolor'] = 'white'
    mpl.rcParams['lines.linewidth'] = 2
    mpl.rcParams['axes.facecolor'] = 'white'
    mpl.rcParams['font.size'] = font_size
    mpl.rcParams['patch.edgecolor'] = 'black'
    color_t = ['#F7977A','#FDC68A','#A2D39C','#6ECFF6','#8493CA','#BC8DBF','#F6989D',
           '#FFF79A','#998675','#A4A4A4','#5AFF00','#29A3A3','#F53DD6','#F2800D','#3399FF']

    hatch = pl.Rectangle((0,0),1,1,fill=None,hatch="///")
    circle = pl.Rectangle((0,0),1,1,fill=None,hatch="ooo")

    #load data
    val = utils.Validation()
    biased_map = {'mutationtaster':['humvar','exovar','varibench',
                    'varibench_selected','swissvar_selected'],
              'mutationassessor':['humvar','exovar','varibench'],
              'polyphen2':['humvar','exovar','varibench'],
              'logit_condel_old':['humvar','exovar','varibench'],
              'logit_condel_new':['humvar','exovar','varibench'],
              'condel_old':['humvar','exovar','varibench'],
              'condel_new':['humvar','exovar','varibench'],
              'sift':[''],
              'fathmm_u':[''],
              'fathmm_w':['humvar','exovar','varibench'],
              'gerp++':[''],
              'phylop':[''],
              'CADD':['']
    }

    datasets = ['humvar','exovar','varibench_selected','predictSNP_selected','swissvar_selected']
    n = len(datasets)
    mass_biased = np.zeros(n)
    mass = np.zeros(n)
    pp2 = np.zeros(n)
    pp2_biased = np.zeros(n)
    sift = np.zeros(n)
    logit_biased = np.zeros(n)
    logit = np.zeros(n)
    condel_biased = np.zeros(n)
    condel = np.zeros(n)

    fathmmw = np.zeros(n)
    fathmmw_biased = np.zeros(n)
    fathmmw_type2_biased = np.zeros(n)
    logit_p_biased = np.zeros(n)
    logit_p_type2_biased = np.zeros(n)
    logit_p = np.zeros(n)
    condel_p_biased = np.zeros(n)
    condel_p_type2_biased = np.zeros(n)
    condel_p = np.zeros(n)

    for i,dataset in enumerate(datasets):
        score_data.selectDataset(dataset)
        labels = score_data.getTrueLabels()
        print "\tComputing AUC values for dataset: " + dataset
        if dataset in biased_map['mutationassessor']:
            mass_biased[i] = val.getROCStats(labels,score_data.getScores('mutationassessor'))['auc']
        else:
            mass[i] = val.getROCStats(labels,score_data.getScores('mutationassessor'))['auc']
        if dataset in biased_map['polyphen2']:
            pp2_biased[i] = val.getROCStats(labels,score_data.getScores('polyphen2'))['auc']
        else:
            pp2[i] = val.getROCStats(labels,score_data.getScores('polyphen2'))['auc']
        if dataset in biased_map['logit_condel_old']:
            logit_biased[i] = val.getROCStats(labels,score_data.getScores('logit_condel_old'))['auc']
        else:
            logit[i] = val.getROCStats(labels,score_data.getScores('logit_condel_old'))['auc']
        if dataset in biased_map['condel_old']:
            condel_biased[i] = val.getROCStats(labels,score_data.getScores('condel_old'))['auc']
        else:
            condel[i] = val.getROCStats(labels,score_data.getScores('condel_old'))['auc']
        if dataset in biased_map['fathmm_w']:
            fathmmw_biased[i] = val.getROCStats(labels,score_data.getScores('fathmm_w'))['auc']
        else:
            if dataset == "varibench_selected" or dataset=="predictSNP_selected" or dataset=="swissvar_selected":
                fathmmw_type2_biased[i] = val.getROCStats(labels,score_data.getScores('fathmm_w'))['auc']
            else:
                fathmmw[i] = val.getROCStats(labels,score_data.getScores('fathmm_w'))['auc']
        if dataset in biased_map['condel_new']:
            condel_p_biased[i] = val.getROCStats(labels,score_data.getScores('condel_new'))['auc']
        else:
            if dataset == "varibench_selected" or dataset=="predictSNP_selected" or dataset=="swissvar_selected":
                condel_p_type2_biased[i] = val.getROCStats(labels,score_data.getScores('condel_new'))['auc']
            else:
                condel_p[i] = val.getROCStats(labels,score_data.getScores('condel_new'))['auc']
        if dataset in biased_map['logit_condel_new']:
            logit_p_biased[i] = val.getROCStats(labels,score_data.getScores('logit_condel_new'))['auc']
        else:
            if dataset == "varibench_selected" or dataset=="predictSNP_selected" or dataset=="swissvar_selected":
                logit_p_type2_biased[i] = val.getROCStats(labels,score_data.getScores('logit_condel_new'))['auc']
            else:
                logit_p[i] = val.getROCStats(labels,score_data.getScores('logit_condel_new'))['auc']
        sift[i] = val.getROCStats(labels,score_data.getScores('sift'))['auc']

    pl.figure(figsize=(10,5))
    fig1 = pl.subplot(111)

    width = 0.07

    x=np.arange(n)

    tool_names = np.array(['FatHMM-W','PolyPhen-2','MutationAssessor','SIFT','Condel','Logit','Condel+','Logit+','Type 1 Biased','Type 2 Biased'])

    spines_to_remove = ['top','right','bottom']
    ax = fig1.get_axes()
    for spine in spines_to_remove:
        ax.spines[spine].set_visible(False)
    ax.xaxis.set_ticks_position('none')
    ax.yaxis.set_ticks_position('left')
    ax.yaxis.grid(True)

    t0 = fig1.bar(x-width/2.0-3*width-0.06,fathmmw,width=width,color=color_t[5])
    t1 = fig1.bar(x-width/2.0-2*width-0.04,pp2,width=width,color=color_t[1])
    t2 = fig1.bar(x-width/2.0-width-0.02,mass,width=width,color=color_t[8])
    t3 = fig1.bar(x-width/2.0,sift,width=width,color=color_t[2])
    t4 = fig1.bar(x+width/2.0+0.02,condel,width=width,color=color_t[11])
    t5 = fig1.bar(x+width/2.0+width+0.04,logit,width=width,color=color_t[12])
    t6 = fig1.bar(x+width/2.0+2.0*width+0.06,condel_p,width=width,color=color_t[13])
    t7 = fig1.bar(x+width/2.0+3.0*width+0.08,logit_p,width=width,color=color_t[14])

    light_grey = np.array([float(248)/float(255)]*3)
    light_grey = "#FFFFFF"
    almost_black = '#262626'
    legend = ax.legend([t0,t1,t2,t3,t4,t5,t6,t7,hatch,circle],tool_names,frameon=True, scatterpoints=1,
            prop={'size':font_size},ncol=5,loc="upper center",fancybox=True,bbox_to_anchor=(0.5,1.0))
    rect = legend.get_frame()
    rect.set_facecolor(light_grey)
    rect.set_linewidth(0.0)
    texts = legend.texts
    for t in texts:
        t.set_color(almost_black)

    fig1.bar(x-width/2.0-3*width-0.06,fathmmw_biased,width=width,color=color_t[5],hatch="/o/o/")
    fig1.bar(x-width/2.0-3*width-0.06,fathmmw_type2_biased,width=width,color=color_t[5],hatch="ooo")
    fig1.bar(x-width/2.0-2*width-0.04,pp2_biased,width=width,color=color_t[1],hatch="///")
    fig1.bar(x-width/2.0-width-0.02,mass_biased,width=width,color=color_t[8],hatch="///")
    fig1.bar(x+width/2.0+0.02,condel_biased,width=width,color=color_t[11],hatch="///")
    fig1.bar(x+width/2.0+width+0.04,logit_biased,width=width,color=color_t[12],hatch="///")
    fig1.bar(x+width/2.0+2.0*width+0.06,condel_p_biased,width=width,color=color_t[13],hatch="/o/o/")
    fig1.bar(x+width/2.0+3.0*width+0.08,logit_p_type2_biased,width=width,color=color_t[14],hatch="ooo")
    fig1.bar(x+width/2.0+2.0*width+0.06,condel_p_type2_biased,width=width,color=color_t[13],hatch="ooo")
    fig1.bar(x+width/2.0+3.0*width+0.08,logit_p_biased,width=width,color=color_t[14],hatch="/o/o/")

    pl.xticks(x,['HumVar','ExoVar','VaribenchSelected','predictSNPSelected','SwissVarSelected'],fontsize=font_size)
    pl.ylabel("AUC")
    fig1.set_ylim(0.5,1.03)
    fig1.set_xlim(-0.5,n-0.5)
    fig1.text(-0.05,1.02,"a",fontsize=15,fontweight="bold",va="top",transform=fig1.transAxes)
    pl.yticks([0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1.0])
    pl.subplots_adjust(left=0.07,bottom=0.07,right=0.99,top=0.97,wspace=0.05)

    if release:
        pl.savefig(os.path.abspath('Output/Figures/Figure4.tiff'),dpi=300)
    else:
        pl.savefig(os.path.abspath('Output/Figures/Figure4.pdf'))
        pl.savefig(os.path.abspath('Output/Figures/Figure4.tiff'),dpi=300)
        pl.savefig(os.path.abspath('Output/Figures/Figure4.jpg'))
    pl.close()

max_stars_repo_name: Zhenxuyang/PPSNV

max_issues_repo_name: Zhenxuyang/PPSNV

max_forks_repo_name: Zhenxuyang/PPSNV


text:
import math
import numpy as np

def rot_matrix(axis, theta):
    """
    Return the rotation matrix associated with counterclockwise rotation about
    the given axis by theta radians.
    """
    axis = np.asarray(axis)
    axis = axis / math.sqrt(np.dot(axis, axis))
    a = math.cos(theta / 2.0)
    b, c, d = -axis * math.sin(theta / 2.0)
    aa, bb, cc, dd = a * a, b * b, c * c, d * d
    bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d

    return np.array([
        [aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
        [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
        [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]
    ])

max_stars_repo_name: maximecb/ledburn

max_issues_repo_name: maximecb/ledburn

max_forks_repo_name: maximecb/ledburn


text:
from hopfield_network import *
from visuals import *
from alphabet import *
import numpy as np
from pylab import *

def to_state(letter):
    return array([1 if char == 'X' else -1 for char in letter.replace('\n','')])

def show_state(state):
    state = state.reshape((5, 5))
    imshow(state, cmap=cm.binary, interpolation='nearest')
    show()

myNet = VisualHopfield(25)
alphabet = [A, B, C, D, E, F, G, H, I, J, K, L, M,
        N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
training_data = [to_state(letter) for letter in alphabet]
learning_data = [to_state(l1)]
myNet.run_visualization(training_data, learning_data)

max_stars_repo_name: robclewley/fovea

max_issues_repo_name: robclewley/fovea

max_forks_repo_name: robclewley/fovea


text:
import pandas as pd
import numpy as np

def _split_samples_for_processes(inp, process_number, len_sections):

	L = len(inp)
	start_ind = process_number * len_sections
	end_ind = min((process_number+1)*len_sections, L)

	if isinstance(inp, pd.DataFrame):
		samples = inp.iloc[start_ind: end_ind, :]
	elif isinstance(inp, pd.Series):
		samples = inp.iloc[start_ind: end_ind]
	elif isinstance(inp, (list, np.ndarray)):
		samples = inp[start_ind: end_ind]
	else:
		raise TypeError ("Unknown type for inp")

	return samples


text:
'''
This code was written by Adam Luxon and team as part of the McQuade and Ferri research groups.
'''
from timeit import default_timer

from numpy.random import randint
from rdkit import RDLogger
from py2neo import Graph
from sqlalchemy.exc import OperationalError

from core.ingest import load_smiles
from core.name import name
from core.storage import MLMySqlConn
from core.neo4j import ModelToNeo4j

rds = ['Lipophilicity-ID.csv', 'ESOL.csv', 'water-energy.csv', 'logP14k.csv', 'jak2_pic50.csv', 'Lipo-short.csv']
RDLogger.DisableLog('rdApp.*')

cds = ['BBBP.csv', 'HIV.csv', 'bace.csv']

multi_label_classification_datasets = ['sider.csv', 'clintox.csv']  # List of multi-label classification data sets

class MlModel:  # TODO update documentation here
    """
    Class to set up and run machine learning algorithm.
    """
    from core.regressors import get_regressor, hyperTune, build_cnn
    from core.grid import make_grid
    from core.train import train_reg, train_cls
    from core.analysis import impgraph, pva_graph, classification_graphs, hist, plot_learning_curves
    from core.classifiers import get_classifier
    from core.storage.util import original_param
    from core.features import featurize
    from core.split import data_split
    from core.storage import pickle_model, store, org_files, featurize_from_mysql, QsarDB_export

    def __init__(self, algorithm, dataset, target, feat_meth, tune=False, opt_iter=10, cv=3, random=None):
        """
        Requires: learning algorithm, dataset, target property's column name, hyperparamter tune, number of
        optimization cycles for hyper tuning, and number of Cross Validation folds for tuning.
        """

        self.algorithm = algorithm
        self.dataset = dataset
        if self.algorithm in ['svm', 'ada'] and self.dataset in ['BBBP.csv', 'sider.csv', 'clintox.csv', 'bace.csv']:
            tune = False

        # Sets self.task_type based on which dataset is being used.
        if self.dataset in cds:
            self.task_type = 'single_label_classification'
        elif self.dataset in rds:
            self.task_type = 'regression'
        elif self.dataset in multi_label_classification_datasets:
            self.task_type = 'multi_label_classification'
        else:
            # raise Exception(
            #     '{} is an unknown dataset! Cannot choose classification or regression.'.format(self.dataset))
            self.task_type = 'regression'

        self.target_name = target
        self.feat_meth = feat_meth

        if random is None:
            self.random_seed = randint(low=1, high=50)
        else:
            self.random_seed = random

        self.opt_iter = opt_iter
        self.cv_folds = cv
        self.tuned = tune

        # ingest data.  collect full data frame (self.data)
        # collect pandas series of the SMILES (self.smiles_col)

        if self.dataset in multi_label_classification_datasets:
            # Makes drop = False for multi-target classification
            self.data, self.smiles_series = load_smiles(self, dataset, drop=False)
        else:
            self.data, self.smiles_series = load_smiles(self, dataset)

        # define run name used to save all outputs of model
        self.run_name = name(self)

        if not tune:  # if no tuning, no optimization iterations or CV folds.
            self.opt_iter = None
            self.cv_folds = None
            self.tune_time = None

        self.mysql_params = None
        self.neo4j_params = None

    def reg(self):  # broke this out because input shape is needed for NN regressor to be defined.
        """
        Function to fetch regressor.  Should be called after featurization has occured and input shape defined.
        :return:
        """
        if self.task_type == 'regression':
            self.get_regressor(call=False)  # returns instantiated model estimator

        if self.task_type in ['single_label_classification', 'multi_label_classification']:
            self.get_classifier()

    def run(self, tuner="bayes"):
        """
        Runs machine learning model. Stores results as class attributes.
        :param tuner: the default parameter is "bayes" which is BayesianSearchCV. The other parameters are "random" for
            RandomSearchCV and grid for GridSearchCV.
        """

        if self.tuned:  # Do hyperparameter tuning
            self.make_grid(tuner=tuner)
            self.hyperTune(n_jobs=8, tuner=tuner)
        else:  # Return original parameter if not tuned
            self.original_param()
        # Done tuning, time to fit and predict
        if self.task_type == 'regression':
            self.train_reg()

        if self.task_type in ['single_label_classification', 'multi_label_classification']:
            self.train_cls()

    def analyze(self):
        # Variable importance for tree based estimators
                # if self.feat_meth == [0]:
        #     self.permutation_importance()
        if self.algorithm in ['rf', 'gdb', 'ada'] and self.feat_meth == [0]:
            self.impgraph()

        # make predicted vs actual graph
        if self.task_type == 'regression':
            self.pva_graph()
            self.pva_graph(use_scaled=True)  # Plot scaled pva data
            # self.plot_learning_curves()
            # if self.algorithm != "cnn":  # CNN is running into OverflowError: cannot convert float infinity to integer
            #     self.hist()

        if self.task_type in ['single_label_classification', 'multi_label_classification']:
            self.classification_graphs()

    def to_neo4j(self, port="bolt://localhost:7687", username="neo4j", password="password"):
        # Create Neo4j graphs from pipeline
        t1 = default_timer()
        self.neo4j_params = {'port': port, 'username': username, 'password': password}  # Pass Neo4j Parameters
        Graph(self.neo4j_params["port"], username=self.neo4j_params["username"],
              password=self.neo4j_params["password"])  # Test connection to Neo4j
        ModelToNeo4j(model=self, port=port, molecules_per_batch=1000, username=username, password=password)
        t2 = default_timer() - t1
        print(f"Time it takes to finish graphing {self.run_name}: {t2}sec")

    def connect_mysql(self, user, password, host, database, initialize_all_data=False):
        # Gather MySql Parameters
        self.mysql_params = {'user': user, 'password': password, 'host': host, 'database': database}

        # Test connection
        try:
            conn = MLMySqlConn(user=self.mysql_params['user'], password=self.mysql_params['password'],
                               host=self.mysql_params['host'], database=self.mysql_params['database'])
        except OperationalError:
            return Exception("Bad parameters passed to connect to MySql database or MySql server"
                             "not properly configured")

        # Insert featurized data into MySql. This will only run once per dataset/feat combo,
        # even if initialize_data=True
        if initialize_all_data:
            conn.insert_all_data_mysql()

reason: from numpy


text:
# -*- coding:utf-8 -*-

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

import tensorflow as tf
import tensorflow.contrib.slim as slim
import os, sys
import numpy as np
import time
sys.path.append("../")

from libs.configs import cfgs
from libs.networks import build_whole_network
from data.io.read_tfrecord_multi_gpu import next_batch
from libs.box_utils import show_box_in_tensor
from help_utils import tools

os.environ["CUDA_VISIBLE_DEVICES"] = cfgs.GPU_GROUP

def average_gradients(tower_grads):
    """Calculate the average gradient for each shared variable across all towers.
    Note that this function provides a synchronization point across all towers.
    Args:
      tower_grads: List of lists of (gradient, variable) tuples. The outer list
        is over individual gradients. The inner list is over the gradient
        calculation for each tower.
    Returns:
       List of pairs of (gradient, variable) where the gradient has been averaged
       across all towers.
    """
    average_grads = []
    for grad_and_vars in zip(*tower_grads):
        # Note that each grad_and_vars looks like the following:
        #   ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
        grads = []
        for g, _ in grad_and_vars:
            # Add 0 dimension to the gradients to represent the tower.
            expanded_g = tf.expand_dims(g, 0)

            # Append on a 'tower' dimension which we will average over below.
            grads.append(expanded_g)

        # Average over the 'tower' dimension.
        grad = tf.concat(axis=0, values=grads)
        grad = tf.reduce_mean(grad, 0)

        # Keep in mind that the Variables are redundant because they are shared
        # across towers. So .. we will just return the first tower's pointer to
        # the Variable.
        v = grad_and_vars[0][1]
        grad_and_var = (grad, v)
        average_grads.append(grad_and_var)
    return average_grads

def sum_gradients(tower_grads):
    """Calculate the average gradient for each shared variable across all towers.
    Note that this function provides a synchronization point across all towers.
    Args:
      tower_grads: List of lists of (gradient, variable) tuples. The outer list
        is over individual gradients. The inner list is over the gradient
        calculation for each tower.
    Returns:
       List of pairs of (gradient, variable) where the gradient has been averaged
       across all towers.
    """
    sum_grads = []
    for grad_and_vars in zip(*tower_grads):
        # Note that each grad_and_vars looks like the following:
        #   ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
        grads = []
        for g, _ in grad_and_vars:
            # Add 0 dimension to the gradients to represent the tower.
            expanded_g = tf.expand_dims(g, 0)

            # Append on a 'tower' dimension which we will average over below.
            grads.append(expanded_g)

        # Average over the 'tower' dimension.
        grad = tf.concat(axis=0, values=grads)
        grad = tf.reduce_sum(grad, 0)

        # Keep in mind that the Variables are redundant because they are shared
        # across towers. So .. we will just return the first tower's pointer to
        # the Variable.
        v = grad_and_vars[0][1]
        grad_and_var = (grad, v)
        sum_grads.append(grad_and_var)
    return sum_grads

def get_gtboxes_and_label(gtboxes_and_label, num_objects):
    return gtboxes_and_label[:int(num_objects), :]

def warmup_lr(init_lr, global_step, warmup_step, num_gpu):
    def warmup(end_lr, global_step, warmup_step):
        start_lr = end_lr * 0.1
        global_step = tf.cast(global_step, tf.float32)
        return start_lr + (end_lr - start_lr) * global_step / warmup_step

    def decay(start_lr, global_step, num_gpu):
        lr = tf.train.piecewise_constant(global_step,
                                         boundaries=[np.int64(cfgs.DECAY_STEP[0] // num_gpu),
                                                     np.int64(cfgs.DECAY_STEP[1] // num_gpu),
                                                     np.int64(cfgs.DECAY_STEP[2] // num_gpu)],
                                         values=[start_lr, start_lr / 10., start_lr / 100., start_lr / 1000.])
        return lr

    return tf.cond(tf.less_equal(global_step, warmup_step),
                   true_fn=lambda: warmup(init_lr, global_step, warmup_step),
                   false_fn=lambda: decay(init_lr, global_step, num_gpu))

def train():

    with tf.Graph().as_default(), tf.device('/cpu:0'):

        num_gpu = len(cfgs.GPU_GROUP.strip().split(','))
        global_step = slim.get_or_create_global_step()
        lr = warmup_lr(cfgs.LR, global_step, cfgs.WARM_SETP, num_gpu)
        tf.summary.scalar('lr', lr)

        optimizer = tf.train.MomentumOptimizer(lr, momentum=cfgs.MOMENTUM)
        faster_rcnn = build_whole_network.DetectionNetwork(base_network_name=cfgs.NET_NAME,
                                                           is_training=True)

        with tf.name_scope('get_batch'):
            img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch, img_h_batch, img_w_batch = \
                next_batch(dataset_name=cfgs.DATASET_NAME,  # 'pascal', 'coco'
                           batch_size=cfgs.BATCH_SIZE * num_gpu,
                           shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
                           is_training=True)
            # gtboxes_and_label = tf.reshape(gtboxes_and_label_batch, [-1, 5])
            # if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']:
            #     img_batch = img_batch / tf.constant([cfgs.PIXEL_STD])

        # data processing
        inputs_list = []
        for i in range(num_gpu):
            # img_name = img_name_batch[i]
            img = tf.expand_dims(img_batch[i], axis=0)
            if cfgs.NET_NAME in ['resnet101_v1d', 'resnet50_v1d']:
                img = img / tf.constant([cfgs.PIXEL_STD])

            gtboxes_and_label = tf.cast(tf.reshape(gtboxes_and_label_batch[i], [-1, 5]), tf.float32)
            num_objects = num_objects_batch[i]
            num_objects = tf.cast(tf.reshape(num_objects, [-1, ]), tf.float32)

            img_h = img_h_batch[i]
            img_w = img_w_batch[i]
            # img_h = tf.cast(tf.reshape(img_h, [-1, ]), tf.float32)
            # img_w = tf.cast(tf.reshape(img_w, [-1, ]), tf.float32)

            inputs_list.append([img, gtboxes_and_label, num_objects, img_h, img_w])

        # put_op_list = []
        # get_op_list = []
        # for i in range(num_gpu):
        #     with tf.device("/GPU:%s" % i):
        #         area = tf.contrib.staging.StagingArea(
        #             dtypes=[tf.float32, tf.float32, tf.float32])
        #         put_op_list.append(area.put(inputs_list[i]))
        #         get_op_list.append(area.get())

        tower_grads = []
        biases_regularizer = tf.no_regularizer
        weights_regularizer = tf.contrib.layers.l2_regularizer(cfgs.WEIGHT_DECAY)

        total_loss_dict = {
            'rpn_cls_loss': tf.constant(0., tf.float32),
            'rpn_loc_loss': tf.constant(0., tf.float32),
            'fastrcnn_cls_loss': tf.constant(0., tf.float32),
            'fastrcnn_loc_loss': tf.constant(0., tf.float32),
            'total_losses': tf.constant(0., tf.float32),

        }

        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(num_gpu):
                with tf.device('/gpu:%d' % i):
                    with tf.name_scope('tower_%d' % i):
                        with slim.arg_scope(
                                [slim.model_variable, slim.variable],
                                device='/device:CPU:0'):
                            with slim.arg_scope([slim.conv2d, slim.conv2d_in_plane,
                                                 slim.conv2d_transpose, slim.separable_conv2d, slim.fully_connected],
                                                weights_regularizer=weights_regularizer,
                                                biases_regularizer=biases_regularizer,
                                                biases_initializer=tf.constant_initializer(0.0)):

                                gtboxes_and_label = tf.py_func(get_gtboxes_and_label,
                                                               inp=[inputs_list[i][1], inputs_list[i][2]],
                                                               Tout=tf.float32)
                                gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 5])

                                img = inputs_list[i][0]
                                img_shape = inputs_list[i][-2:]
                                img = tf.image.crop_to_bounding_box(image=img,
                                                                    offset_height=0,
                                                                    offset_width=0,
                                                                    target_height=tf.cast(img_shape[0], tf.int32),
                                                                    target_width=tf.cast(img_shape[1], tf.int32))

                                outputs = faster_rcnn.build_whole_detection_network(input_img_batch=img,
                                                                                gtboxes_batch=gtboxes_and_label)
                                gtboxes_in_img = show_box_in_tensor.draw_boxes_with_categories(img_batch=img,
                                                                                boxes=gtboxes_and_label[
                                                                                :, :-1],
                                                                                labels=gtboxes_and_label[
                                                                                :, -1])
                                tf.summary.image('Compare/gtboxes_gpu:%d' % i, gtboxes_in_img)

                                if cfgs.ADD_BOX_IN_TENSORBOARD:
                                    detections_in_img = show_box_in_tensor.draw_boxes_with_categories_and_scores(
                                        img_batch=img,
                                        boxes=outputs[0],
                                        scores=outputs[1],
                                        labels=outputs[2])
                                    tf.summary.image('Compare/final_detection_gpu:%d' % i, detections_in_img)

                                loss_dict = outputs[-1]

                                total_losses = 0.0
                                for k in loss_dict.keys():
                                    total_losses += loss_dict[k]
                                    total_loss_dict[k] += loss_dict[k] / num_gpu

                                total_losses = total_losses / num_gpu
                                total_loss_dict['total_losses'] += total_losses

                                if i == num_gpu - 1:
                                    regularization_losses = tf.get_collection(
                                        tf.GraphKeys.REGULARIZATION_LOSSES)
                                    # weight_decay_loss = tf.add_n(slim.losses.get_regularization_losses())
                                    total_losses = total_losses + tf.add_n(regularization_losses)

                        tf.get_variable_scope().reuse_variables()
                        grads = optimizer.compute_gradients(total_losses)
                        tower_grads.append(grads)

        for k in total_loss_dict.keys():
            tf.summary.scalar('{}/{}'.format(k.split('_')[0], k), total_loss_dict[k])

        if len(tower_grads) > 1:
            grads = sum_gradients(tower_grads)
        else:
            grads = tower_grads[0]

        # final_gvs = []
        # with tf.variable_scope('Gradient_Mult'):
        #     for grad, var in grads:
        #         scale = 1.
        #         # if '/biases:' in var.name:
        #         #    scale *= 2.
        #         if 'conv_new' in var.name:
        #             scale *= 3.
        #         if not np.allclose(scale, 1.0):
        #             grad = tf.multiply(grad, scale)
        #         final_gvs.append((grad, var))

        apply_gradient_op = optimizer.apply_gradients(grads, global_step=global_step)

        variable_averages = tf.train.ExponentialMovingAverage(0.9999, global_step)
        variables_averages_op = variable_averages.apply(tf.trainable_variables())

        train_op = tf.group(apply_gradient_op, variables_averages_op)
        # train_op = optimizer.apply_gradients(final_gvs, global_step=global_step)
        summary_op = tf.summary.merge_all()

        restorer, restore_ckpt = faster_rcnn.get_restorer()
        saver = tf.train.Saver(max_to_keep=5)

        init_op = tf.group(
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        )

        tfconfig = tf.ConfigProto(
            allow_soft_placement=True, log_device_placement=False)
        tfconfig.gpu_options.allow_growth = True
        with tf.Session(config=tfconfig) as sess:
            sess.run(init_op)

            # sess.run(tf.initialize_all_variables())
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord, sess=sess)

            summary_path = os.path.join(cfgs.SUMMARY_PATH, cfgs.VERSION)
            tools.mkdir(summary_path)
            summary_writer = tf.summary.FileWriter(summary_path, graph=sess.graph)

            if not restorer is None:
                restorer.restore(sess, restore_ckpt)
                print('restore model')

            for step in range(cfgs.MAX_ITERATION // num_gpu):
                training_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

                if step % cfgs.SHOW_TRAIN_INFO_INTE != 0 and step % cfgs.SMRY_ITER != 0:
                    _, global_stepnp = sess.run([train_op, global_step])

                else:
                    if step % cfgs.SHOW_TRAIN_INFO_INTE == 0 and step % cfgs.SMRY_ITER != 0:
                        start = time.time()

                        _, global_stepnp, total_loss_dict_ = \
                            sess.run([train_op, global_step, total_loss_dict])

                        end = time.time()

                        print('***'*20)
                        print("""%s: global_step:%d  current_step:%d"""
                              % (training_time, (global_stepnp-1)*num_gpu, step*num_gpu))
                        print("""per_cost_time:%.3fs"""
                              % ((end - start) / num_gpu))
                        loss_str = ''
                        for k in total_loss_dict_.keys():
                            loss_str += '%s:%.3f\n' % (k, total_loss_dict_[k])
                        print(loss_str)

                    else:
                        if step % cfgs.SMRY_ITER == 0:
                            _, global_stepnp, summary_str = sess.run([train_op, global_step, summary_op])
                            summary_writer.add_summary(summary_str, (global_stepnp-1)*num_gpu)
                            summary_writer.flush()

                if (step > 0 and step % (cfgs.SAVE_WEIGHTS_INTE // num_gpu) == 0) or (step >= cfgs.MAX_ITERATION // num_gpu - 1):

                    save_dir = os.path.join(cfgs.TRAINED_CKPT, cfgs.VERSION)
                    if not os.path.exists(save_dir):
                        os.mkdir(save_dir)

                    save_ckpt = os.path.join(save_dir, 'coco_' + str((global_stepnp-1)*num_gpu) + 'model.ckpt')
                    saver.save(sess, save_ckpt)
                    print(' weights had been saved')

            coord.request_stop()
            coord.join(threads)

if __name__ == '__main__':

    train()