# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import os

from yacs.config import CfgNode as CN

# -----------------------------------------------------------------------------
# Convention about Training / Test specific parameters
# -----------------------------------------------------------------------------
# Whenever an argument can be either used for training or for testing, the
# corresponding name will be post-fixed by a _TRAIN for a training parameter,
# or _TEST for a test-specific parameter.
# For example, the maximum image side during training will be
# INPUT.MAX_SIZE_TRAIN, while for testing it will be
# INPUT.MAX_SIZE_TEST

# -----------------------------------------------------------------------------
# Config definition
# -----------------------------------------------------------------------------

_C = CN()

_C.DEBUG = False

_C.GLOBAL_BUFFER_ON = False

_C.EXPERIMENT_NAME = ""
_C.USE_CLUSTER = False

_C.MODEL = CN()
_C.MODEL.TWO_STAGE_ON = False
_C.MODEL.FLIP_AUG = False
_C.MODEL.RPN_ONLY = False
_C.MODEL.MASK_ON = False
_C.MODEL.RETINANET_ON = False
_C.MODEL.KEYPOINT_ON = False
_C.MODEL.ATTRIBUTE_ON = False
_C.MODEL.RELATION_ON = False

_C.MODEL.NUM_REL_GROUP = 3
_C.MODEL.DEVICE = "cuda"
_C.MODEL.META_ARCHITECTURE = "GeneralizedRCNN"
_C.MODEL.CLS_AGNOSTIC_BBOX_REG = False

# If the WEIGHT starts with a catalog://, like :R-50, the code will look for
# the path in paths_catalog. Else, it will use it as the specified absolute
# path
_C.MODEL.WEIGHT = ""

# checkpoint of detector, for relation prediction
_C.MODEL.PRETRAINED_DETECTOR_CKPT = ""

# -----------------------------------------------------------------------------
# INPUT
# -----------------------------------------------------------------------------
_C.INPUT = CN()
# Size of the smallest side of the image during training
_C.INPUT.MIN_SIZE_TRAIN = (800,)  # (800,)
# Maximum size of the side of the image during training
_C.INPUT.MAX_SIZE_TRAIN = 1333
# Size of the smallest side of the image during testing
_C.INPUT.MIN_SIZE_TEST = 800
# Maximum size of the side of the image during testing
_C.INPUT.MAX_SIZE_TEST = 1333
# Values to be used for image normalization
_C.INPUT.PIXEL_MEAN = [102.9801, 115.9465, 122.7717]
# Values to be used for image normalization
_C.INPUT.PIXEL_STD = [1., 1., 1.]
# Convert image to BGR format (for Caffe2 models), in range 0-255
_C.INPUT.TO_BGR255 = True

# Image ColorJitter
_C.INPUT.BRIGHTNESS = 0.0
_C.INPUT.CONTRAST = 0.0
_C.INPUT.SATURATION = 0.0
_C.INPUT.HUE = 0.0

_C.INPUT.VERTICAL_FLIP_PROB_TRAIN = 0.0

# -----------------------------------------------------------------------------
# Dataset
# -----------------------------------------------------------------------------
_C.DATASETS = CN()
# List of the dataset names for training, as present in paths_catalog.py
_C.DATASETS.TRAIN = ()
# List of the dataset names for val, as present in paths_catalog.py
# Note that except dataset names, all remaining val configs reuse those of test
_C.DATASETS.VAL = ()
# List of the dataset names for testing, as present in paths_catalog.py
_C.DATASETS.TEST = ()

# the precomputed_det_box for relation networks
_C.DATASETS.LOAD_PRECOMPUTE_DETECTION_BOX = False
# -----------------------------------------------------------------------------
# DataLoader
# -----------------------------------------------------------------------------
_C.DATALOADER = CN()
# Number of data loading threads
_C.DATALOADER.NUM_WORKERS = 4
# If > 0, this enforces that each collated batch should have a size divisible
# by SIZE_DIVISIBILITY
_C.DATALOADER.SIZE_DIVISIBILITY = 0
# If True, each batch should contain only images for which the aspect ratio
# is compatible. This groups portrait images together, and landscape images
# are not batched with portrait images.
_C.DATALOADER.ASPECT_RATIO_GROUPING = True
_C.MODEL.TRAIN_FIRST_STAGE_ONLY = False
# ---------------------------------------------------------------------------- #
# Backbone options
# ---------------------------------------------------------------------------- #
_C.MODEL.BACKBONE = CN()

# The backbone conv body to use
# The string must match a function that is imported in modeling.model_builder
# (e.g., 'FPN.add_fpn_ResNet101_conv5_body' to specify a ResNet-101-FPN
# backbone)
_C.MODEL.BACKBONE.CONV_BODY = "R-50-C4"

# Add StopGrad at a specified stage so the bottom layers are frozen
_C.MODEL.BACKBONE.FREEZE_CONV_BODY_AT = 2

# ---------------------------------------------------------------------------- #
# FPN options
# ---------------------------------------------------------------------------- #
_C.MODEL.FPN = CN()
_C.MODEL.FPN.USE_GN = False
_C.MODEL.FPN.USE_RELU = False

# ---------------------------------------------------------------------------- #
# Group Norm options
# ---------------------------------------------------------------------------- #
_C.MODEL.GROUP_NORM = CN()
# Number of dimensions per group in GroupNorm (-1 if using NUM_GROUPS)
_C.MODEL.GROUP_NORM.DIM_PER_GP = -1
# Number of groups in GroupNorm (-1 if using DIM_PER_GP)
_C.MODEL.GROUP_NORM.NUM_GROUPS = 32
# GroupNorm's small constant in the denominator
_C.MODEL.GROUP_NORM.EPSILON = 1e-5

# ---------------------------------------------------------------------------- #
# RPN options
# ---------------------------------------------------------------------------- #
_C.MODEL.RPN = CN()
_C.MODEL.RPN.USE_FPN = False
_C.MODEL.RPN.RPN_MID_CHANNEL = 512
# Base RPN anchor sizes given in absolute pixels w.r.t. the scaled network input
_C.MODEL.RPN.ANCHOR_SIZES = (32, 64, 128, 256, 512)
# Stride of the feature map that RPN is attached.
# For FPN, number of strides should match number of scales
_C.MODEL.RPN.ANCHOR_STRIDE = (16,)
# RPN anchor aspect ratios
_C.MODEL.RPN.ASPECT_RATIOS = (0.5, 1.0, 2.0)
# Remove RPN anchors that go outside the image by RPN_STRADDLE_THRESH pixels
# Set to -1 or a large value, e.g. 100000, to disable pruning anchors
_C.MODEL.RPN.STRADDLE_THRESH = 0
# Minimum overlap required between an anchor and ground-truth box for the
# (anchor, gt box) pair to be a positive example (IoU >= FG_IOU_THRESHOLD
# ==> positive RPN example)
_C.MODEL.RPN.FG_IOU_THRESHOLD = 0.7
# Maximum overlap allowed between an anchor and ground-truth box for the
# (anchor, gt box) pair to be a negative examples (IoU < BG_IOU_THRESHOLD
# ==> negative RPN example)
_C.MODEL.RPN.BG_IOU_THRESHOLD = 0.3
# Total number of RPN examples per image
_C.MODEL.RPN.BATCH_SIZE_PER_IMAGE = 256
# Target fraction of foreground (positive) examples per RPN minibatch
_C.MODEL.RPN.POSITIVE_FRACTION = 0.5
# Number of top scoring RPN proposals to keep before applying NMS
# When FPN is used, this is *per FPN level* (not total)
_C.MODEL.RPN.PRE_NMS_TOP_N_TRAIN = 12000
_C.MODEL.RPN.PRE_NMS_TOP_N_TEST = 6000
# Number of top scoring RPN proposals to keep after applying NMS
_C.MODEL.RPN.POST_NMS_TOP_N_TRAIN = 2000
_C.MODEL.RPN.POST_NMS_TOP_N_TEST = 1000
# NMS threshold used on RPN proposals
_C.MODEL.RPN.NMS_THRESH = 0.7
# Proposal height and width both need to be greater than RPN_MIN_SIZE
# (a the scale used during training or inference)
_C.MODEL.RPN.MIN_SIZE = 0
# Number of top scoring RPN proposals to keep after combining proposals from
# all FPN levels
_C.MODEL.RPN.FPN_POST_NMS_TOP_N_TRAIN = 2000
_C.MODEL.RPN.FPN_POST_NMS_TOP_N_TEST = 2000
# Apply the post NMS per batch (default) or per image during training
# (default is True to be consistent with Detectron, see Issue #672)
_C.MODEL.RPN.FPN_POST_NMS_PER_BATCH = True
# Custom rpn head, empty to use default conv or separable conv
_C.MODEL.RPN.RPN_HEAD = "SingleConvRPNHead"

# ---------------------------------------------------------------------------- #
# ROI HEADS options
# ---------------------------------------------------------------------------- #
_C.MODEL.ROI_HEADS = CN()
_C.MODEL.ROI_HEADS.USE_FPN = False
# Overlap threshold for an RoI to be considered foreground (if >= FG_IOU_THRESHOLD)
_C.MODEL.ROI_HEADS.FG_IOU_THRESHOLD = 0.5
# Overlap threshold for an RoI to be considered background
# (class = 0 if overlap in [0, BG_IOU_THRESHOLD))
_C.MODEL.ROI_HEADS.BG_IOU_THRESHOLD = 0.3

# Default weights on (dx, dy, dw, dh) for normalizing bbox regression targets
# These are empirically chosen to approximately lead to unit variance targets
_C.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS = (10., 10., 5., 5.)
# RoI minibatch size *per image* (number of regions of interest [ROIs])
# Total number of RoIs per training minibatch =
#   TRAIN.BATCH_SIZE_PER_IM * TRAIN.IMS_PER_BATCH
# E.g., a common configuration is: 512 * 2 * 8 = 8192
_C.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 256
# Target fraction of RoI minibatch that is labeled foreground (i.e. class > 0)
_C.MODEL.ROI_HEADS.POSITIVE_FRACTION = 0.25

# Only used on test mode

# Minimum score threshold (assuming scores in a [0, 1] range); a value chosen to
# balance obtaining high recall with not having too many low precision
# detections that will slow down inference post processing steps (like NMS)
_C.MODEL.ROI_HEADS.SCORE_THRESH = 0.01
# Overlap threshold used for non-maximum suppression (suppress boxes with
# IoU >= this threshold)
_C.MODEL.ROI_HEADS.NMS = 0.3
_C.MODEL.ROI_HEADS.POST_NMS_PER_CLS_TOPN = 300
# Remove duplicated assigned labels for a single bbox in nms
_C.MODEL.ROI_HEADS.NMS_FILTER_DUPLICATES = False
# Maximum number of detections to return per image (100 is based on the limit
# established for the COCO dataset)
_C.MODEL.ROI_HEADS.DETECTIONS_PER_IMG = 256

_C.MODEL.ROI_BOX_HEAD = CN()
_C.MODEL.ROI_BOX_HEAD.FEATURE_EXTRACTOR = "ResNet50Conv5ROIFeatureExtractor"
_C.MODEL.ROI_BOX_HEAD.PREDICTOR = "FastRCNNPredictor"
_C.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION = 14
_C.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO = 0
_C.MODEL.ROI_BOX_HEAD.POOLER_SCALES = (1.0 / 16,)
_C.MODEL.ROI_BOX_HEAD.NUM_CLASSES = 81
# Hidden layer dimension when using an MLP for the RoI box head
_C.MODEL.ROI_BOX_HEAD.MLP_HEAD_DIM = 2048
# GN
_C.MODEL.ROI_BOX_HEAD.USE_GN = False
# Dilation
_C.MODEL.ROI_BOX_HEAD.DILATION = 1
_C.MODEL.ROI_BOX_HEAD.CONV_HEAD_DIM = 256
_C.MODEL.ROI_BOX_HEAD.NUM_STACKED_CONVS = 4

_C.MODEL.ROI_ATTRIBUTE_HEAD = CN()
_C.MODEL.ROI_ATTRIBUTE_HEAD.FEATURE_EXTRACTOR = "FPN2MLPFeatureExtractor"
_C.MODEL.ROI_ATTRIBUTE_HEAD.PREDICTOR = "FPNPredictor"
_C.MODEL.ROI_ATTRIBUTE_HEAD.SHARE_BOX_FEATURE_EXTRACTOR = True
# Add attributes to each box
_C.MODEL.ROI_ATTRIBUTE_HEAD.USE_BINARY_LOSS = True
_C.MODEL.ROI_ATTRIBUTE_HEAD.ATTRIBUTE_LOSS_WEIGHT = 0.1
_C.MODEL.ROI_ATTRIBUTE_HEAD.NUM_ATTRIBUTES = 201
_C.MODEL.ROI_ATTRIBUTE_HEAD.MAX_ATTRIBUTES = 10  # max number of attribute per bbox
_C.MODEL.ROI_ATTRIBUTE_HEAD.ATTRIBUTE_BGFG_SAMPLE = True
_C.MODEL.ROI_ATTRIBUTE_HEAD.ATTRIBUTE_BGFG_RATIO = 3
_C.MODEL.ROI_ATTRIBUTE_HEAD.POS_WEIGHT = 5.0

_C.MODEL.ROI_MASK_HEAD = CN()
_C.MODEL.ROI_MASK_HEAD.FEATURE_EXTRACTOR = "ResNet50Conv5ROIFeatureExtractor"
_C.MODEL.ROI_MASK_HEAD.PREDICTOR = "MaskRCNNC4Predictor"
_C.MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION = 14
_C.MODEL.ROI_MASK_HEAD.POOLER_SAMPLING_RATIO = 0
_C.MODEL.ROI_MASK_HEAD.POOLER_SCALES = (1.0 / 16,)
_C.MODEL.ROI_MASK_HEAD.MLP_HEAD_DIM = 1024
_C.MODEL.ROI_MASK_HEAD.CONV_LAYERS = (256, 256, 256, 256)
_C.MODEL.ROI_MASK_HEAD.RESOLUTION = 14
_C.MODEL.ROI_MASK_HEAD.SHARE_BOX_FEATURE_EXTRACTOR = True
# Whether or not resize and translate masks to the input image.
_C.MODEL.ROI_MASK_HEAD.POSTPROCESS_MASKS = False
_C.MODEL.ROI_MASK_HEAD.POSTPROCESS_MASKS_THRESHOLD = 0.5
# Dilation
_C.MODEL.ROI_MASK_HEAD.DILATION = 1
# GN
_C.MODEL.ROI_MASK_HEAD.USE_GN = False

_C.MODEL.ROI_KEYPOINT_HEAD = CN()
_C.MODEL.ROI_KEYPOINT_HEAD.FEATURE_EXTRACTOR = "KeypointRCNNFeatureExtractor"
_C.MODEL.ROI_KEYPOINT_HEAD.PREDICTOR = "KeypointRCNNPredictor"
_C.MODEL.ROI_KEYPOINT_HEAD.POOLER_RESOLUTION = 14
_C.MODEL.ROI_KEYPOINT_HEAD.POOLER_SAMPLING_RATIO = 0
_C.MODEL.ROI_KEYPOINT_HEAD.POOLER_SCALES = (1.0 / 16,)
_C.MODEL.ROI_KEYPOINT_HEAD.MLP_HEAD_DIM = 1024
_C.MODEL.ROI_KEYPOINT_HEAD.CONV_LAYERS = tuple(512 for _ in range(8))
_C.MODEL.ROI_KEYPOINT_HEAD.RESOLUTION = 14
_C.MODEL.ROI_KEYPOINT_HEAD.NUM_CLASSES = 17
_C.MODEL.ROI_KEYPOINT_HEAD.SHARE_BOX_FEATURE_EXTRACTOR = True

_C.MODEL.TWO_STAGE_HEAD = CN()
_C.MODEL.TWO_STAGE_HEAD.PURE_SENMENTIC=True
_C.MODEL.TWO_STAGE_HEAD.USE_GLOVE=True
_C.MODEL.TWO_STAGE_HEAD.UNION_BOX=False
_C.MODEL.TWO_STAGE_HEAD.INDIVIDUAL_BOX=False
_C.MODEL.TWO_STAGE_HEAD.transformer_pos=False
_C.MODEL.TWO_STAGE_HEAD.HIDDEN_DIM=1024
_C.MODEL.TWO_STAGE_HEAD.PREDICTOR='TwoStagePredictor'
_C.MODEL.TWO_STAGE_HEAD.NUM_REL_GROUP= 50
_C.MODEL.TWO_STAGE_HEAD.BATCH_SIZE_PER_IMAGE=1000
_C.MODEL.TWO_STAGE_HEAD.POSITIVE_FRACTION=1
_C.MODEL.TWO_STAGE_HEAD.USE_TWOSTAGE_LOSS=True
_C.MODEL.TWO_STAGE_HEAD.loss_distribution=True
_C.MODEL.TWO_STAGE_HEAD.LOSS_TYPE='cos_loss'
_C.MODEL.TWO_STAGE_HEAD.DYNAMIC_GT= False
_C.MODEL.TWO_STAGE_HEAD.DYNAMIC_GT_FACTOR=0.5

_C.MODEL.ROI_RELATION_HEAD = CN()
_C.MODEL.ROI_RELATION_HEAD.LM_CONV_BODY='R-101-C2'
_C.MODEL.ROI_RELATION_HEAD.BOX_FEATURE_EXTRACTOR = "ResNet50Conv5ROIFeatureExtractor"
_C.MODEL.ROI_RELATION_HEAD.POOLER_RESOLUTION = 14
_C.MODEL.ROI_RELATION_HEAD.POOLER_SAMPLING_RATIO = 0
_C.MODEL.ROI_RELATION_HEAD.POOLER_SCALES = (1.0/4,)
_C.MODEL.ROI_RELATION_HEAD.LM_MULTI_LAYERS= False
_C.MODEL.ROI_RELATION_HEAD.USE_CLS_TOCKEN=False
_C.MODEL.ROI_RELATION_HEAD.USE_FOCAL_LOSS= False
_C.MODEL.ROI_RELATION_HEAD.LOSS_WEIGHT_PATH = ''
_C.MODEL.ROI_RELATION_HEAD.use_possibility_merger=False
_C.MODEL.ROI_RELATION_HEAD.VISUAL_LANGUAGE_MERGER_OBJ=False
_C.MODEL.ROI_RELATION_HEAD.VISUAL_LANGUAGE_MERGER_EDGE=False
# share box feature extractor should be set False for neural-motifs
_C.MODEL.ROI_RELATION_HEAD.PREDICTOR = "MotifPredictor"
_C.MODEL.ROI_RELATION_HEAD.FEATURE_EXTRACTOR = "RelationFeatureExtractor"
_C.MODEL.ROI_RELATION_HEAD.POOLING_ALL_LEVELS = True
_C.MODEL.ROI_RELATION_HEAD.NUM_CLASSES = 51
_C.MODEL.ROI_RELATION_HEAD.BATCH_SIZE_PER_IMAGE = 64
_C.MODEL.ROI_RELATION_HEAD.POSITIVE_FRACTION = 0.25
_C.MODEL.ROI_RELATION_HEAD.MAX_PROPOSAL_PAIR = 2048
_C.MODEL.ROI_RELATION_HEAD.USE_GT_BOX = True
_C.MODEL.ROI_RELATION_HEAD.USE_GT_OBJECT_LABEL = False
_C.MODEL.ROI_RELATION_HEAD.EMBED_DIM = 200
_C.MODEL.ROI_RELATION_HEAD.CONTEXT_DROPOUT_RATE = 0.2
_C.MODEL.ROI_RELATION_HEAD.USE_SEM_WORD_EMBEDDING=False
_C.MODEL.ROI_RELATION_HEAD.LANGUAGE_SPATIAL_ATTENTION=False

# focal loss for relationship

_C.MODEL.ROI_RELATION_HEAD.FOCAL_LOSS = CN()
_C.MODEL.ROI_RELATION_HEAD.FOCAL_LOSS.GAMMA = 2.0
_C.MODEL.ROI_RELATION_HEAD.FOCAL_LOSS.ALPHA = 0.25
_C.MODEL.ROI_RELATION_HEAD.FOCAL_LOSS.SIZE_AVERAGE = True

_C.MODEL.ROI_RELATION_HEAD.USE_BINARY_LOSS = False
# the hidden dimension of geometry and word embedding context feature representation
_C.MODEL.ROI_RELATION_HEAD.CONTEXT_HIDDEN_DIM = 512
# the dimension of relation head to extract the union features
_C.MODEL.ROI_RELATION_HEAD.CONTEXT_POOLING_DIM = 4096
_C.MODEL.ROI_RELATION_HEAD.CONTEXT_OBJ_LAYER = 1  # assert >= 1
_C.MODEL.ROI_RELATION_HEAD.CONTEXT_REL_LAYER = 1  # assert >= 1

_C.MODEL.ROI_RELATION_HEAD.EVALUATE_REL_PROPOSAL = True
#whether concat object class embedding
_C.MODEL.ROI_RELATION_HEAD.CAT_WORD_EMBEDDING= True
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER = CN()
# for TransformerPredictor only
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.DROPOUT_RATE = 0.1
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.OBJ_LAYER = 4
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.REL_LAYER = 2
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.NUM_HEAD = 8
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.INNER_DIM = 2048
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.KEY_DIM = 64
_C.MODEL.ROI_RELATION_HEAD.TRANSFORMER.VAL_DIM = 64

_C.MODEL.ROI_RELATION_HEAD.LABEL_SMOOTHING_LOSS = False
_C.MODEL.ROI_RELATION_HEAD.PREDICT_USE_VISION = True
_C.MODEL.ROI_RELATION_HEAD.PREDICT_USE_BIAS = True
_C.MODEL.ROI_RELATION_HEAD.REQUIRE_BOX_OVERLAP = True
_C.MODEL.ROI_RELATION_HEAD.NUM_SAMPLE_PER_GT_REL = 4  # when sample fg relationship from gt, the max number of corresponding proposal pairs

# in sgdet, to make sure the detector won't missing any ground truth bbox, 
# we add grount truth box to the output of RPN proposals during Training
_C.MODEL.ROI_RELATION_HEAD.ADD_GTBOX_TO_PROPOSAL_IN_TRAIN = False

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE = CN()

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.MESSAGE_PASSING_STEP = 3

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.GRAPH_HIDDEN_DIM = 512

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.STATISTICS_PRIOR_KNOWLEDGE = True

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.REL_PRIOR_MATRIX_DIR = "datasets/vg/stanford_spilt/kern_prior/rel_matrix.npy"

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.INST_PRIOR_MATRIX_DIR = "datasets/vg/stanford_spilt/kern_prior/obj_matrix.npy"

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.FUSE_PAIRWISE_OBJ_FEATURES = False

_C.MODEL.ROI_RELATION_HEAD.KERN_MODULE.AVERAGE_GRAPH_SUMMARY = False

_C.MODEL.ROI_RELATION_HEAD.IMP_MODULE = CN()
# the relation feature representation source, can be the union feature or the subject objects pair features
_C.MODEL.ROI_RELATION_HEAD.EDGE_FEATURES_REPRESENTATION = "union"  # obj_pair
#
_C.MODEL.ROI_RELATION_HEAD.IMP_MODULE.EDGE_FEATURES_REPRESENTATION = "union"  # obj_pair
# the feature representation for the relationship feature, can be the union features, instance pair features and thire fused features

_C.MODEL.ROI_RELATION_HEAD.IMP_MODULE.GRAPH_HIDDEN_DIM = 512
_C.MODEL.ROI_RELATION_HEAD.IMP_MODULE.GRAPH_ITERATION_NUM = 2

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE = CN()

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE.GRAPH_HIDDEN_DIM = 512 # the hidden dimension of graph model

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE.SHARE_PARAMETERS_EACH_ITER=True

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE.GRAPH_ITERATION_NUM = 3

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE.EDGE_FEATURES_REPRESENTATION = "union"  # obj_pair, 
# the feature representation for the relationship feature, can be the union features, instance pair features and thire fused features

_C.MODEL.ROI_RELATION_HEAD.MSDN_MODULE.APPLY_GT = False




_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE = CN()

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.GRAPH_HIDDEN_DIM = 512 # the hidden dimension of graph model

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.SHARE_PARAMETERS_EACH_ITER=True

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.GRAPH_ITERATION_NUM = 3

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.EDGE_FEATURES_REPRESENTATION = "union"  # obj_pair, 
# the feature representation for the relationship feature, can be the union features, instance pair features and thire fused features

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.RELATION_CONFIDENCE_AWARE = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.APPLY_GT = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.MP_ON_VALID_PAIRS = False # graph will only message passing on edges filtered by the rel pn structure

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.MP_VALID_PAIRS_NUM = 200 # the mp will take the top 150 relatedness score for mp

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.ITERATE_MP_PAIR_REFINE = 0

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.RELNESS_MP_WEIGHTING = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.RELNESS_MP_WEIGHTING_SCORE_RECALIBRATION_METHOD = "minmax" # "learnable_scaling"

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.LEARNABLE_SCALING_WEIGHT = (2.5, 0.03)  # (alpha, beta)

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.SHARE_RELATED_MODEL_ACROSS_REFINE_ITER = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.SPLIT_GRAPH4OBJ_REL = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.GATING_WITH_RELNESS_LOGITS = False

_C.MODEL.ROI_RELATION_HEAD.BGNN_MODULE.SKIP_CONNECTION_ON_OUTPUT= False # add the skip connection on the graph output and initial input


#### CVPR 2021 RTPB
# ### bias module
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE = CN()
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.EPSILON = 1e-3
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.USE_PENALTY = False
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.DROPOUT = 0.0

_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS = CN()
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_THRESHOLD = 0.5
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_WEIGHT = 0.1
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.SCALE_WEIGHT = 1.0
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.BG_DEFAULT_VALUE = 0.02
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_TYPE = 'static'  # 'weight' 'count' 'set_low' 'tail_low'
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_FUSION_WEIGHTS = [1.0, 1.0]
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_K = 10
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.PENALTY_EPSILON = 1e-3
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.WEIGHT_PATH = ''

_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.CB_CLS_FUSION_TYPE = 'sum'  # 'mean', 'iter_l2'
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.CLS_TRANS = 'none'  # 'linear'
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.CB_CLS_FUSION_WEIGHT = 0.8  # for cb_cls

_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.EVAL_WITH_PENALTY = False
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.USE_NEG_PENALTY = False

_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.POSSIBLE_BIAS_DEFAULT_VALUE = 1.0
_C.MODEL.ROI_RELATION_HEAD.BIAS_MODULE.PENALTY_BIAS.POSSIBLE_BIAS_THRESHOLD = 100.0
#dual transformer
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS = CN()
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.USE_GTRANS_CONTEXT = True
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.USE_REL_GRAPH = True

_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.EVAL_USE_FC = False

_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.USE_GRAPH_ENCODE = True

_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.GRAPH_ENCODE_STRATEGY = 'trans'  # cat_gcn

_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER = CN()
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER.REL_LAYER = 2
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER.NUM_HEAD = 8
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER.DROPOUT_RATE = 0.1
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER.KEY_DIM = 64
_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.TRANSFORMER.VAL_DIM = 64

_C.MODEL.ROI_RELATION_HEAD.DUAL_TRANS.REMOVE_BIAS = False

#### CVPR 2020 GPSNET_MODULE

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE = CN()

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.GRAPH_ITERATION_NUM = 2

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.ITERATE_MP_PAIR_REFINE = 2

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.MP_ON_VALID_PAIRS = False # graph will only message passing on edges filtered by the rel pn structure

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.MP_VALID_PAIRS_NUM = 200 # the mp will take the top 150 relatedness score for mp

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.RELNESS_MP_WEIGHTING = False

_C.MODEL.ROI_RELATION_HEAD.GPSNET_MODULE.GRAPH_HIDDEN_DIM = 512

##### CVPR 2018 AGRCNN 
_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE = CN()

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.FEATURE_UPDATE_STEP = 2

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.SCORES_UPDATE_STEP = 2

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.MP_ON_VALID_PAIRS = False # graph will only message passing on edges filtered by the rel pn structure

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.MP_VALID_PAIRS_NUM = 200 # the mp will take the top 150 relatedness score for mp

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.RELNESS_MP_WEIGHTING = False

_C.MODEL.ROI_RELATION_HEAD.GRCNN_MODULE.GRAPH_HIDDEN_DIM = 1024
#####

_C.MODEL.ROI_RELATION_HEAD.CAUSAL = CN()
# direct and indirect effect analysis
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.EFFECT_ANALYSIS = False
# Fusion
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.FUSION_TYPE = 'sum'  # features  # gate

_C.MODEL.ROI_RELATION_HEAD.CAUSAL.AUXILIARY_LOSS = True
# causal context feature layer
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.CONTEXT_LAYER = 'motifs'

# the relation proposal module for the graph model, here we will predict a confidence for each proposed relationship
# pairs, the graph model will use this relatedness score as a reference to filter the graph
_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL = CN()

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.SET_ON = False

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.PRETRAIN_RELNESS_MODULE = False

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.METHOD = "gt" # rel proposal method, can be "gt" for upper bound and "rel_pn" for predictions results


_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.PRE_CLSER_LOSS = 'bce' #focal, bce, ce

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.REL_AWARE_PREDICTOR_TYPE = "single" # "hybrid  

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.FIX_MODEL_AT_ITER = -1

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.PRETRAIN_ITER_RELNESS_MODULE = 2000
# we find that the relpn model is easy to over fitting, so we will fix it in few iteration for better performance

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.PAIR_NUMS_AFTER_FILTERING = -1

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.IGNORE_FOREGROUND_BOXES_PAIRS = False
# the rel pn take the geometery and sematics information in default, but we can take the visual features of instances
_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.VISUAL_FEATURES_ON = True # we will take the visual features in default 
# the relationness score can be use for the ranking in final prediction, in default we will set is as false for better performances
_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.USE_RELATEDNESS_FOR_PREDICTION_RANKING = False

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.EVAL_MODEL_AUC = False

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.USE_SAME_LABEL_WITH_CLSER = True

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.FOCAL_LOSS_ALPHA = 1.0

_C.MODEL.ROI_RELATION_HEAD.RELATION_PROPOSAL_MODEL.FOCAL_LOSS_GAMMA = 0.0


############## pairwise augment features #########################
# switch of geometry information in union feature
_C.MODEL.ROI_RELATION_HEAD.GEOMETRIC_FEATURES = True

_C.MODEL.ROI_RELATION_HEAD.WORD_EMBEDDING_FEATURES = True

_C.MODEL.ROI_RELATION_HEAD.USE_GT_OBJ_LABEL_FOR_EMBEDDING = False

_C.MODEL.ROI_RELATION_HEAD.REL_OBJ_MULTI_TASK_LOSS = False#是否为其添加loss,目前对SHA无效#todo 添加支持，目前看，只有对使用自身更新obj rep的起作用（i.e., 不包括通过使用pairwise_extractor产生的）

_C.MODEL.ROI_RELATION_HEAD.OBJECT_CLASSIFICATION_REFINE = False#false代表不使用模型更新的obj rep预测，该选项会影响REL_OBJ_MULTI_TASK_LOSS,#todo check 其是否对所有context layer中的obj refine都无效，如果是，则其独立使用没有意义

_C.MODEL.ROI_RELATION_HEAD.OBJECT_CLASSIFICATION_MANNER = "replace" # add

# separate spatial in union feature
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.SEPARATE_SPATIAL = False

# the features in pairwise relationship features
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.SPATIAL_FOR_VISION = True

_C.MODEL.ROI_RELATION_HEAD.FREQUENCY_BAIS = True

_C.MODEL.ROI_RELATION_HEAD.CAUSAL.EFFECT_TYPE = 'none'  # 'TDE', 'TIE', 'TE'

# enable the frequency branch to the causal inference pipeline
_C.MODEL.ROI_RELATION_HEAD.CAUSAL.OBJ_PAIR_LABEL_FREQUENCY_BIAS_BRANCH = True

# data resampling
_C.MODEL.ROI_RELATION_HEAD.LONGTAIL_PART_DICT = [None, 'b', 't', 't', 't', 't', 't', 't', 'b', 't', 't', 't', 't', 't',
                                                 't', 't', 't', 't', 't', 't', 'h', 'b', 'b', 'b', 't', 't', 't', 't',
                                                 't', 'b', 'h', 'h', 't', 't', 't', 't', 't', 't', 'b', 't', 'b', 'b',
                                                 't', 'b', 't', 't', 't', 't', 'h', 'b', 'b']

_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING = False

_C.MODEL.ROI_RELATION_HEAD.REPEAT_FACTOR = 0.02

_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING_PARAM = CN()

_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING_PARAM.REPEAT_FACTOR=0.012
_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING_PARAM.INSTANCE_DROP_RATE=0.4


_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING_PARAM.REPEAT_DICT_DIR = ""



_C.MODEL.ROI_RELATION_HEAD.TAIL_REPEAT_FACTOR = 0.012
_C.MODEL.ROI_RELATION_HEAD.BODY_REPEAT_FACTOR = 0.018

_C.MODEL.ROI_RELATION_HEAD.HEAD_CATE_DROP_RATE = 0.4

_C.MODEL.ROI_RELATION_HEAD.BODY_CATE_DROP_RATE = 1.0

_C.MODEL.ROI_RELATION_HEAD.DATA_RESAMPLING_METHOD = "bilvl" # lvis

# fix the feature extract modules only train the classifier
_C.MODEL.ROI_RELATION_HEAD.FIX_FEATURE = False

_C.MODEL.ROI_RELATION_HEAD.CLASSIFIER = "linear"  # weighted_norm

_C.MODEL.ROI_RELATION_HEAD.CLASSIFIER_WEIGHT_SCALE = False  # weighted_norm

_C.MODEL.ROI_RELATION_HEAD.FIX_CLASSIFIER_WEIGHT = False

_C.MODEL.ROI_RELATION_HEAD.RE_INITIALIZE_CLASSIFIER = False

# set to "None" will generate repeat dict on the fly

_C.MODEL.ROI_RELATION_HEAD.REPEAT_DICT = "None"
# _C.MODEL.ROI_RELATION_HEAD.REPEAT_DICT = "/root/projects/Scene-Graph-Benchmark.pytorch/repeat_factor_dict.json"

_C.MODEL.ROI_RELATION_HEAD.REMOVE_TAIL_CLASSES = False

# proportion of predicates
_C.MODEL.ROI_RELATION_HEAD.REL_PROP = [0.01858, 0.00057, 0.00051, 0.00109, 0.00150, 0.00489, 0.00432, 0.02913, 0.00245,
                                       0.00121,
                                       0.00404, 0.00110, 0.00132, 0.00172, 0.00005, 0.00242, 0.00050, 0.00048, 0.00208,
                                       0.15608,
                                       0.02650, 0.06091, 0.00900, 0.00183, 0.00225, 0.00090, 0.00028, 0.00077, 0.04844,
                                       0.08645,
                                       0.31621, 0.00088, 0.00301, 0.00042, 0.00186, 0.00100, 0.00027, 0.01012, 0.00010,
                                       0.01286,
                                       0.00647, 0.00084, 0.01077, 0.00132, 0.00069, 0.00376, 0.00214, 0.11424, 0.01205,
                                       0.02958]

_C.MODEL.VGG = CN()
_C.MODEL.VGG.VGG16_OUT_CHANNELS = 512
# ---------------------------------------------------------------------------- #
# ResNe[X]t options (ResNets = {ResNet, ResNeXt}
# Note that parts of a resnet may be used for both the backbone and the head
# These options apply to both
# ---------------------------------------------------------------------------- #
_C.MODEL.RESNETS = CN()

# Number of groups to use; 1 ==> ResNet; > 1 ==> ResNeXt
_C.MODEL.RESNETS.NUM_GROUPS = 1

# Baseline width of each group
_C.MODEL.RESNETS.WIDTH_PER_GROUP = 64

# Place the stride 2 conv on the 1x1 filter
# Use True only for the original MSRA ResNet; use False for C2 and Torch models
_C.MODEL.RESNETS.STRIDE_IN_1X1 = True

# Residual transformation function
_C.MODEL.RESNETS.TRANS_FUNC = "BottleneckWithFixedBatchNorm"
# ResNet's stem function (conv1 and pool1)
_C.MODEL.RESNETS.STEM_FUNC = "StemWithFixedBatchNorm"

# Apply dilation in stage "res5"
_C.MODEL.RESNETS.RES5_DILATION = 1

_C.MODEL.RESNETS.BACKBONE_OUT_CHANNELS = 256 * 4
_C.MODEL.RESNETS.RES2_OUT_CHANNELS = 256
_C.MODEL.RESNETS.STEM_OUT_CHANNELS = 64

_C.MODEL.RESNETS.STAGE_WITH_DCN = (False, False, False, False)
_C.MODEL.RESNETS.WITH_MODULATED_DCN = False
_C.MODEL.RESNETS.DEFORMABLE_GROUPS = 1

# ---------------------------------------------------------------------------- #
# RetinaNet Options (Follow the Detectron version)
# ---------------------------------------------------------------------------- #
_C.MODEL.RETINANET = CN()

# This is the number of foreground classes and background.
_C.MODEL.RETINANET.NUM_CLASSES = 81

# Anchor aspect ratios to use
_C.MODEL.RETINANET.ANCHOR_SIZES = (32, 64, 128, 256, 512)
_C.MODEL.RETINANET.ASPECT_RATIOS = (0.5, 1.0, 2.0)
_C.MODEL.RETINANET.ANCHOR_STRIDES = (8, 16, 32, 64, 128)
_C.MODEL.RETINANET.STRADDLE_THRESH = 0

# Anchor scales per octave
_C.MODEL.RETINANET.OCTAVE = 2.0
_C.MODEL.RETINANET.SCALES_PER_OCTAVE = 3

# Use C5 or P5 to generate P6
_C.MODEL.RETINANET.USE_C5 = True

# Convolutions to use in the cls and bbox tower
# NOTE: this doesn't include the last conv for logits
_C.MODEL.RETINANET.NUM_CONVS = 4

# Weight for bbox_regression loss
_C.MODEL.RETINANET.BBOX_REG_WEIGHT = 4.0

# Smooth L1 loss beta for bbox regression
_C.MODEL.RETINANET.BBOX_REG_BETA = 0.11

# During inference, #locs to select based on cls score before NMS is performed
# per FPN level
_C.MODEL.RETINANET.PRE_NMS_TOP_N = 1000

# IoU overlap ratio for labeling an anchor as positive
# Anchors with >= iou overlap are labeled positive
_C.MODEL.RETINANET.FG_IOU_THRESHOLD = 0.5

# IoU overlap ratio for labeling an anchor as negative
# Anchors with < iou overlap are labeled negative
_C.MODEL.RETINANET.BG_IOU_THRESHOLD = 0.4

# Focal loss parameter: alpha
_C.MODEL.RETINANET.LOSS_ALPHA = 0.25

# Focal loss parameter: gamma
_C.MODEL.RETINANET.LOSS_GAMMA = 2.0

# Prior prob for the positives at the beginning of training. This is used to set
# the bias init for the logits layer
_C.MODEL.RETINANET.PRIOR_PROB = 0.01

# Inference cls score threshold, anchors with score > INFERENCE_TH are
# considered for inference
_C.MODEL.RETINANET.INFERENCE_TH = 0.05

# NMS threshold used in RetinaNet
_C.MODEL.RETINANET.NMS_TH = 0.4

# ---------------------------------------------------------------------------- #
# FBNet options
# ---------------------------------------------------------------------------- #
_C.MODEL.FBNET = CN()
_C.MODEL.FBNET.ARCH = "default"
# custom arch
_C.MODEL.FBNET.ARCH_DEF = ""
_C.MODEL.FBNET.BN_TYPE = "bn"
_C.MODEL.FBNET.SCALE_FACTOR = 1.0
# the output channels will be divisible by WIDTH_DIVISOR
_C.MODEL.FBNET.WIDTH_DIVISOR = 1
_C.MODEL.FBNET.DW_CONV_SKIP_BN = True
_C.MODEL.FBNET.DW_CONV_SKIP_RELU = True

# > 0 scale, == 0 skip, < 0 same dimension
_C.MODEL.FBNET.DET_HEAD_LAST_SCALE = 1.0
_C.MODEL.FBNET.DET_HEAD_BLOCKS = []
# overwrite the stride for the head, 0 to use original value
_C.MODEL.FBNET.DET_HEAD_STRIDE = 0

# > 0 scale, == 0 skip, < 0 same dimension
_C.MODEL.FBNET.KPTS_HEAD_LAST_SCALE = 0.0
_C.MODEL.FBNET.KPTS_HEAD_BLOCKS = []
# overwrite the stride for the head, 0 to use original value
_C.MODEL.FBNET.KPTS_HEAD_STRIDE = 0

# > 0 scale, == 0 skip, < 0 same dimension
_C.MODEL.FBNET.MASK_HEAD_LAST_SCALE = 0.0
_C.MODEL.FBNET.MASK_HEAD_BLOCKS = []
# overwrite the stride for the head, 0 to use original value
_C.MODEL.FBNET.MASK_HEAD_STRIDE = 0

# 0 to use all blocks defined in arch_def
_C.MODEL.FBNET.RPN_HEAD_BLOCKS = 0
_C.MODEL.FBNET.RPN_BN_TYPE = ""

# ---------------------------------------------------------------------------- #
# Solver
# ---------------------------------------------------------------------------- #
_C.SOLVER = CN()
_C.SOLVER.MAX_ITER = 40000

_C.SOLVER.BASE_LR = 0.002
_C.SOLVER.BIAS_LR_FACTOR = 2

_C.SOLVER.MOMENTUM = 0.9

_C.SOLVER.WEIGHT_DECAY = 0.0005
_C.SOLVER.WEIGHT_DECAY_BIAS = 0.0
_C.SOLVER.CLIP_NORM = 5.0

_C.SOLVER.GAMMA = 0.1
_C.SOLVER.STEPS = (30000,)

_C.SOLVER.WARMUP_FACTOR = 1.0 / 3
_C.SOLVER.WARMUP_ITERS = 500
_C.SOLVER.WARMUP_METHOD = "linear"

_C.SOLVER.SCHEDULE = CN()
_C.SOLVER.SCHEDULE.TYPE = "WarmupMultiStepLR"  # "WarmupReduceLROnPlateau"
# the following paramters are only used for WarmupReduceLROnPlateau
_C.SOLVER.SCHEDULE.PATIENCE = 2
_C.SOLVER.SCHEDULE.THRESHOLD = 1e-4
_C.SOLVER.SCHEDULE.COOLDOWN = 1
_C.SOLVER.SCHEDULE.FACTOR = 0.5
_C.SOLVER.SCHEDULE.MAX_DECAY_STEP = 7

_C.SOLVER.CHECKPOINT_PERIOD = 2500

_C.SOLVER.GRAD_NORM_CLIP = 5.0

_C.SOLVER.PRINT_GRAD_FREQ = 5000
# whether validate and validate period
_C.SOLVER.TO_VAL = True
_C.SOLVER.PRE_VAL = False
_C.SOLVER.VAL_PERIOD = 2500
_C.SOLVER.VAL_2STAGE = False

# update schedule
# when load from a previous model, if set to True
# only maintain the iteration number and all the other settings of the 
# schedule will be changed
_C.SOLVER.UPDATE_SCHEDULE_DURING_LOAD = False

# Number of images per batch
# This is global, so if we have 8 GPUs and IMS_PER_BATCH = 16, each GPU will
# see 2 images per batch
_C.SOLVER.IMS_PER_BATCH = 16

# ---------------------------------------------------------------------------- #
# Specific test options
# ---------------------------------------------------------------------------- #
_C.TEST = CN()
_C.TEST.EXPECTED_RESULTS = []
_C.TEST.EXPECTED_RESULTS_SIGMA_TOL = 4
# Number of images per batch
# This is global, so if we have 8 GPUs and IMS_PER_BATCH = 16, each GPU will
# see 2 images per batch
_C.TEST.IMS_PER_BATCH = 8
# Number of detections per image
_C.TEST.DETECTIONS_PER_IMG = 100

# ---------------------------------------------------------------------------- #
# Test-time augmentations for bounding box detection
# See configs/test_time_aug/e2e_mask_rcnn_R-50-FPN_1x.yaml for an example
# ---------------------------------------------------------------------------- #
_C.TEST.BBOX_AUG = CN()

# Enable test-time augmentation for bounding box detection if True
_C.TEST.BBOX_AUG.ENABLED = False

# Horizontal flip at the original scale (id transform)
_C.TEST.BBOX_AUG.H_FLIP = False

# Each scale is the pixel size of an image's shortest side
_C.TEST.BBOX_AUG.SCALES = ()

# Max pixel size of the longer side
_C.TEST.BBOX_AUG.MAX_SIZE = 4000

# Horizontal flip at each scale
_C.TEST.BBOX_AUG.SCALE_H_FLIP = False

_C.TEST.SAVE_PROPOSALS = False
# Settings for relation testing
_C.TEST.RELATION = CN()
_C.TEST.RELATION.MULTIPLE_PREDS = False
_C.TEST.RELATION.IOU_THRESHOLD = 0.5
_C.TEST.RELATION.REQUIRE_OVERLAP = True
# when predict the label of bbox, run nms on each cls
_C.TEST.RELATION.LATER_NMS_PREDICTION_THRES = 0.3
# synchronize_gather, used for sgdet, otherwise test on multi-gpu will cause out of memory
# for detection mode, need to set as false, otherwise there may have some prediction been missed
_C.TEST.RELATION.SYNC_GATHER = True

_C.TEST.ALLOW_LOAD_FROM_CACHE = False
_C.TEST.CUSTUM_EVAL = False
_C.TEST.EVAL_TOPRECALL = False #针对EEM的验证metrics
_C.TEST.CUSTUM_PATH = '.'
_C.TEST.PLOT_PREDICATE_DISTRIBUTION= False
# ---------------------------------------------------------------------------- #
# Misc options
# ---------------------------------------------------------------------------- #
_C.OUTPUT_DIR = "./checkpoints"
_C.GLOVE_DIR = "datasets/vg/stanford_spilt/glove"

_C.PATHS_CATALOG = os.path.join(os.path.dirname(__file__), "paths_catalog.py")
_C.PATHS_DATA = os.path.join(os.path.dirname(__file__), "../data/datasets")

# ---------------------------------------------------------------------------- #
# Precision options
# ---------------------------------------------------------------------------- #

# Precision of input, allowable: (float32, float16)
_C.DTYPE = "float32"

# Enable verbosity in apex.amp
_C.AMP_VERBOSE = False
_C.GLOBAL_SETTING = CN()
_C.GLOBAL_SETTING.DATASET_CHOICE = 'VG'
_C.GLOBAL_SETTING.RELATION_PREDICTOR = ' '
_C.GLOBAL_SETTING.BASIC_ENCODER = 'Hybrid-Attention'
_C.GLOBAL_SETTING.USE_BIAS = True
_C.GLOBAL_SETTING.USE_GCL = False
_C.GLOBAL_SETTING.CHOOSE_BEST_MODEL_BY_METRIC = '_mean_recall'
_C.GLOBAL_SETTING.PRINT_INTERVAL = 100

_C.GLOBAL_SETTING.GCL_SETTING = CN()
_C.GLOBAL_SETTING.GCL_SETTING.GROUP_SPLIT_MODE = 'divide4'
_C.GLOBAL_SETTING.GCL_SETTING.KNOWLEDGE_LOSS_COEFFICIENT = 1.0
_C.GLOBAL_SETTING.GCL_SETTING.KNOWLEDGE_TRANSFER_MODE = 'KL_logit_TopDown'
_C.GLOBAL_SETTING.GCL_SETTING.NO_RELATION_RESTRAIN = True
_C.GLOBAL_SETTING.GCL_SETTING.ZERO_LABEL_PADDING_MODE = 'rand_insert'
_C.GLOBAL_SETTING.GCL_SETTING.NO_RELATION_PENALTY = 0.1
