#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) Megvii, Inc. and its affiliates.

import os

from yolox.exp import Exp as MyExp


class Exp(MyExp):
    def __init__(self):
        super(Exp, self).__init__()
        self.data_dir = "/home/kitraining/To_Annotate"  # Where images are located
        self.annotations_dir = "./backend/1/custom_exp_1"  # Where annotation JSONs are located
        self.train_ann = "coco_project_1_train.json"
        self.val_ann = "coco_project_1_valid.json"
        self.test_ann = "coco_project_1_test.json"
        self.num_classes = 2
        # Disable train2017 subdirectory - our images are directly in data_dir
        self.name = ""
        # Set data workers for training
        self.data_num_workers = 8
        self.depth = 1.0
        self.width = 1.0
        self.input_size = (640.0, 640.0)
        self.mosaic_scale = (0.1, 2.0)
        self.test_size = (640.0, 640.0)
        self.enable_mixup = True
        self.max_epoch = 300
        self.warmup_epochs = 5
        self.warmup_lr = 0.0
        self.scheduler = "yoloxwarmcos"
        self.no_aug_epochs = 15
        self.min_lr_ratio = 0.05
        self.ema = True
        self.weight_decay = 0.0005
        self.momentum = 0.9
        self.print_interval = 10
        self.eval_interval = 10
        self.test_conf = 0.01
        self.nms_thre = 0.65
        self.mosaic_prob = 1.0
        self.mixup_prob = 1.0
        self.hsv_prob = 1.0
        self.flip_prob = 0.5
        self.degrees = 10.0
        self.translate = 0.1
        self.shear = 2.0
        self.mixup_scale = (0.5, 1.5)
        self.activation = "silu"
        self.random_size = (10, 20)

    def get_dataset(self, cache=False, cache_type="ram"):
        """Override to use name parameter for images directory"""
        from yolox.data import COCODataset
        
        # COCODataset constructs image paths as: os.path.join(data_dir, name, file_name)
        # YOLOX adds "annotations/" to data_dir automatically, so we pass annotations_dir directly
        # Use empty string for name since we have absolute paths in JSON
        return COCODataset(
            data_dir=self.annotations_dir,
            json_file=self.train_ann,
            name="",
            img_size=self.input_size,
            preproc=self.preproc if hasattr(self, 'preproc') else None,
            cache=cache,
            cache_type=cache_type,
        )
    
    def get_eval_dataset(self, **kwargs):
        """Override eval dataset using name parameter"""
        from yolox.data import COCODataset
        
        testdev = kwargs.get("testdev", False)
        legacy = kwargs.get("legacy", False)
        
        return COCODataset(
            data_dir=self.annotations_dir,
            json_file=self.val_ann if not testdev else self.test_ann,
            name="",
            img_size=self.test_size,
            preproc=None,  # No preprocessing for evaluation
        )
        self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0]
