# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import

import numpy as np

from alpharotate.utils.pretrain_zoo import PretrainModelZoo
from configs._base_.models.retinanet_r50_fpn import *
from configs._base_.datasets.dota_detection import *
from configs._base_.schedules.schedule_1x import *

# schedule
BATCH_SIZE = 1
GPU_GROUP = "0,1,2"
NUM_GPU = len(GPU_GROUP.strip().split(','))
LR = 1e-3
SAVE_WEIGHTS_INTE = 11725 * 2
DECAY_EPOCH = [8, 11, 20]
MAX_EPOCH = 12
WARM_EPOCH = 1 / 16.
DECAY_STEP = np.array(DECAY_EPOCH, np.int32) * SAVE_WEIGHTS_INTE
MAX_ITERATION = SAVE_WEIGHTS_INTE * MAX_EPOCH
WARM_SETP = int(WARM_EPOCH * SAVE_WEIGHTS_INTE)

# dataset
DATASET_NAME = 'DIOR-R'
CLASS_NUM = 20

# model
# backbone
pretrain_zoo = PretrainModelZoo()
PRETRAINED_CKPT = pretrain_zoo.pretrain_weight_path(NET_NAME, ROOT_PATH)
TRAINED_CKPT = os.path.join(ROOT_PATH, 'output/trained_weights')

# bbox head
NUM_SUBNET_CONV = 4
LEVEL = ['P3', 'P4', 'P5', 'P6', 'P7']
BASE_ANCHOR_SIZE_LIST = [32, 64, 128, 256, 512]
ANCHOR_STRIDE = [8, 16, 32, 64, 128]
ANCHOR_SCALES = [2 ** 0, 2 ** (1.0 / 3.0), 2 ** (2.0 / 3.0)]
ANCHOR_RATIOS = [1, 1 / 2, 2.]

# loss
CLS_WEIGHT = 1.0
REG_WEIGHT = 2.0
REG_LOSS_MODE = 2  # GWD loss

GWD_TAU = 2.0
GWD_FUNC = tf.sqrt   # 0: sqrt  1: log

VERSION = 'RetinaNet_DIOR_R_GWD_2x_20211025'

"""
RetinaNet-H + gwd
FLOPs: 844274473;    Trainable params: 32553441

cls : trainstation|| Recall: 0.5579567779960707 || Precison: 0.05186267348429511|| AP: 0.3790245357146766
F1:0.4669655144213633 P:0.5555555555555556 R:0.4027504911591356
cls : vehicle|| Recall: 0.28566066066066065 || Precison: 0.14913380888923727|| AP: 0.2613276786331748
F1:0.3592172743769671 P:0.6824295922656578 R:0.24376876876876877
cls : stadium|| Recall: 0.8467261904761905 || Precison: 0.07834228280324934|| AP: 0.6810510075110445
F1:0.7031459776094414 P:0.7940074906367042 R:0.6309523809523809
cls : overpass|| Recall: 0.5235690235690236 || Precison: 0.05315330712698684|| AP: 0.3527429284882417
F1:0.46359011585701687 P:0.6211121583411876 R:0.36980920314253646
cls : baseballfield|| Recall: 0.749563191613279 || Precison: 0.2865731462925852|| AP: 0.6943189881143922
F1:0.7506495329000139 P:0.9364388332607749 R:0.6263832265579499
cls : Expressway-Service-area|| Recall: 0.7585253456221198 || Precison: 0.05712104386451971|| AP: 0.640328706204132
F1:0.7034864873457429 P:0.8093525179856115 R:0.6221198156682027
cls : chimney|| Recall: 0.7866149369544132 || Precison: 0.14657509488523404|| AP: 0.7257945306725796
F1:0.8275433019954207 P:0.9579081632653061 R:0.7284190106692532
cls : golffield|| Recall: 0.8591304347826086 || Precison: 0.0836437521164917|| AP: 0.7224020840177585
F1:0.7563449864879924 P:0.8237704918032787 R:0.6991304347826087
cls : ship|| Recall: 0.6685613596316717 || Precison: 0.3799279680863091|| AP: 0.5834326158043945
F1:0.6682286379662189 P:0.7848407008396273 R:0.5817938952992667
cls : bridge|| Recall: 0.33294708381614524 || Precison: 0.04482579303172127|| AP: 0.1698649907665968
F1:0.2762413694348751 P:0.4002932551319648 R:0.21089223638470453
cls : airport|| Recall: 0.3888888888888889 || Precison: 0.019500075289865984|| AP: 0.20551279261971053
F1:0.30307582559646185 P:0.3401869158878505 R:0.2732732732732733
cls : windmill|| Recall: 0.6781187458305536 || Precison: 0.14756478188284822|| AP: 0.542335825425252
F1:0.6360429813813846 P:0.7545787545787546 R:0.5496997998665777
cls : tenniscourt|| Recall: 0.8624540378591856 || Precison: 0.37669521770164166|| AP: 0.8081120012983397
F1:0.8590358318823728 P:0.9257394587791064 R:0.8013073675609423
cls : storagetank|| Recall: 0.4954839262017893 || Precison: 0.42498898516669115|| AP: 0.4414936491630534
F1:0.5772355547083314 P:0.831053901850362 R:0.44218997474423183
cls : airplane|| Recall: 0.6363857769118363 || Precison: 0.3823809175385966|| AP: 0.5922014406624769
F1:0.6748235624556933 P:0.8719135802469136 R:0.5504140282513394
cls : basketballcourt|| Recall: 0.8802423112767941 || Precison: 0.18355844913030803|| AP: 0.8084508472696832
F1:0.8739696681700668 P:0.9366009589770911 R:0.8191985088536813
cls : groundtrackfield|| Recall: 0.9007957559681697 || Precison: 0.16352080123266563|| AP: 0.7632743161358586
F1:0.7863380249952894 P:0.7907725321888412 R:0.7819628647214855
cls : harbor|| Recall: 0.46602254428341383 || Precison: 0.018431007909921155|| AP: 0.17101625602149292
F1:0.28477612467081004 P:0.35302525011910435 R:0.23864734299516907
cls : Expressway-toll-station|| Recall: 0.5755813953488372 || Precison: 0.045971674019038775|| AP: 0.5318138052920292
F1:0.6390931743217857 P:0.9042553191489362 R:0.4941860465116279
cls : dam|| Recall: 0.49814126394052044 || Precison: 0.023055746730901584|| AP: 0.20179367979166893
F1:0.31048762100223887 P:0.3661616161616162 R:0.2695167286245353
mAP is : 0.5138146339803277
"""
