def main():
    args = get_parameter()
    args.weights_dir = os.path.join(args.weights_dir, args.model)
    utils.check_folder(args.weights_dir)
    if os.path.exists(args.log_dir):
        utils.setup_logging(os.path.join(args.log_dir, 'tools.txt'), resume=True)
    config = dict()
    for i in args.keyword:
        config[i] = True
    if ('export_onnx' in config.keys()):
        export_onnx(args)
    if ('inference' in config.keys()):
        inference(args)
    if ('verbose' in config.keys()):
        if torch.cuda.is_available():
            checkpoint = torch.load(args.old)
        else:
            checkpoint = torch.load(args.old, map_location='cpu')
        if ('state_dict' in checkpoint):
            checkpoint = checkpoint['state_dict']
        if ('model' in checkpoint):
            checkpoint = checkpoint['model']
        for (name, value) in checkpoint.items():
            if ((('quant_activation' in name) or ('quant_weight' in name)) and (name.split('.')[(- 1)] in args.verbose_list)):
                print(name, value.shape, value.requires_grad)
                print(value.data)
            elif ('all' in args.verbose_list):
                if ('num_batches_tracked' not in name):
                    if isinstance(value, torch.Tensor):
                        print(name, value.shape, value.requires_grad)
                    elif (isinstance(value, int) or isinstance(value, float) or isinstance(value, str)):
                        print(name, value, type(value))
                    else:
                        print(name, type(value))
    if (('load' in config.keys()) or ('save' in config.keys())):
        model_name = args.model
        if (model_name in models.model_zoo):
            (model, args) = models.get_model(args)
        else:
            print(('model(%s) not support, available models: %r' % (model_name, models.model_zoo)))
            return
        if utils.check_file(args.old):
            raw = ('raw' in config.keys())
            if torch.cuda.is_available():
                checkpoint = torch.load(args.old)
            else:
                checkpoint = torch.load(args.old, map_location='cpu')
            try:
                utils.load_state_dict(model, (checkpoint.get('state_dict', None) if (not raw) else checkpoint), verbose=False)
            except RuntimeError:
                print('Loading pretrained model failed')
            print('Loading pretrained model OK')
            if (('save' in config.keys()) and (args.new != '')):
                torch.save(model.state_dict(), args.new)
                print(('Save pretrained model into %s' % args.new))
        else:
            print(('file not exist %s' % args.old))
    if ('update' in config.keys()):
        mapping_from = []
        mapping_to = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
        if os.path.isfile(args.mapping_to):
            with open(args.mapping_to) as f:
                mapping_to = f.readlines()
                f.close()
        mapping_from = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
        mapping_from = [i for i in mapping_from if ((len(i) > 0) and (i[0] != '#'))]
        mapping_to = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_to]
        mapping_to = [i for i in mapping_to if ((len(i) > 0) and (i[0] != '#'))]
        if ((len(mapping_to) != len(mapping_from)) or (len(mapping_to) == 0) or (len(mapping_from) == 0)):
            mapping = None
            logging.info('no valid mapping')
        else:
            mapping = dict()
            for (i, k) in enumerate(mapping_from):
                if (('{' in k) and ('}' in k) and ('{' in mapping_to[i]) and ('}' in mapping_to[i])):
                    item = k.split('{')
                    for v in item[1].strip('}').split(','):
                        v = v.strip()
                        mapping[(item[0] + v)] = (mapping_to[i].split('{')[0] + v)
                else:
                    mapping[k] = mapping_to[i]
        raw = ('raw' in config.keys())
        if (not os.path.isfile(args.old)):
            args.old = args.pretrained
        utils.import_state_dict(args.old, args.new, mapping, raw, raw_prefix=args.case)
    if ('det-load' in config.keys()):
        from third_party.checkpoint import DetectionCheckpointer
        model_name = args.model
        if (model_name in models.model_zoo):
            (model, args) = models.get_model(args)
        else:
            print(('model(%s) not support, available models: %r' % (model_name, models.model_zoo)))
            return
        split = os.path.split(args.old)
        checkpointer = DetectionCheckpointer(model, split[0], save_to_disk=True)
        checkpointer.resume_or_load(args.old, resume=True)
        checkpointer.save(split[1])
    if ('swap' in config.keys()):
        mapping_from = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
            mapping_from = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
            mapping_from = [i for i in mapping_from if ((len(i) > 0) and (i[0] != '#'))]
            lists = args.verbose_list
            for i in lists:
                item = i.split('/')
                interval = int(item[0])
                index = item[1].split('-')
                index = [int(x) for x in index]
                if (((len(mapping_from) % interval) == 0) and (len(index) <= interval)):
                    mapping_to = mapping_from.copy()
                    for (j, k) in enumerate(index):
                        k = (k % interval)
                        mapping_to[j::interval] = mapping_from[k::interval]
            mapping_to = [(i + '\n') for i in mapping_to]
            with open((args.mapping_from + '-swap'), 'w') as f:
                f.writelines(mapping_to)
                f.close()
    if ('sort' in config.keys()):
        mapping_from = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
            mapping_from.sort()
            with open((args.mapping_from + '-sort'), 'w') as f:
                f.writelines(mapping_from)
                f.close()
    if (('verify-data' in config.keys()) or ('verify-image' in config.keys())):
        if ('verify-image' in config.keys()):
            lists = args.verbose_list
        else:
            with open(os.path.join(args.root, 'train.txt')) as f:
                lists = f.readlines()
                f.close()
        from PIL import Image
        from threading import Thread
        print(('going to check %d files' % len(lists)))

        def check(lists, start, end, index):
            for (i, item) in enumerate(lists[start:end]):
                try:
                    items = item.split()
                    if (len(items) >= 1):
                        path = items[0].strip().strip('\n')
                    else:
                        print(('skip line %s' % i))
                        continue
                    path = os.path.join(args.root, os.path.join('train', path))
                    imgs = Image.open(path)
                    imgs.resize((256, 256))
                    if (index == 0):
                        print(i, end='\r', file=sys.stderr)
                except (RuntimeError, IOError):
                    print(('\nError when read image %s' % path))
            print('\nFinish checking', index)
        num = min(len(lists), 20)
        for i in range(num):
            start = ((len(lists) // num) * i)
            end = min((start + (len(lists) // num)), len(lists))
            th = Thread(target=check, args=(lists, start, end, i))
            th.start()
def main():
    args = get_parameter()
    args.weights_dir = os.path.join(args.weights_dir, args.model)
    utils.check_folder(args.weights_dir)
    if os.path.exists(args.log_dir):
        utils.setup_logging(os.path.join(args.log_dir, 'tools.txt'), resume=True)
    config = dict()
    for i in args.keyword:
        config[i] = True
    if ('export_onnx' in config.keys()):
        export_onnx(args)
    if ('inference' in config.keys()):
        inference(args)
    if ('verbose' in config.keys()):
        if torch.cuda.is_available():
            checkpoint = torch.load(args.old)
        else:
            checkpoint = torch.load(args.old, map_location='cpu')
        if ('state_dict' in checkpoint):
            checkpoint = checkpoint['state_dict']
        if ('model' in checkpoint):
            checkpoint = checkpoint['model']
        for (name, value) in checkpoint.items():
            if ((('quant_activation' in name) or ('quant_weight' in name)) and (name.split('.')[(- 1)] in args.verbose_list)):
                print(name, value.shape, value.requires_grad)
                print(value.data)
            elif ('all' in args.verbose_list):
                if ('num_batches_tracked' not in name):
                    if isinstance(value, torch.Tensor):
                        print(name, value.shape, value.requires_grad)
                    elif (isinstance(value, int) or isinstance(value, float) or isinstance(value, str)):
                        print(name, value, type(value))
                    else:
                        print(name, type(value))
    if (('load' in config.keys()) or ('save' in config.keys())):
        model_name = args.model
        if (model_name in models.model_zoo):
            (model, args) = models.get_model(args)
        else:
            print(('model(%s) not support, available models: %r' % (model_name, models.model_zoo)))
            return
        if utils.check_file(args.old):
            raw = ('raw' in config.keys())
            if torch.cuda.is_available():
                checkpoint = torch.load(args.old)
            else:
                checkpoint = torch.load(args.old, map_location='cpu')
            try:
                utils.load_state_dict(model, (checkpoint.get('state_dict', None) if (not raw) else checkpoint), verbose=False)
            except RuntimeError:
                print('Loading pretrained model failed')
            print('Loading pretrained model OK')
            if (('save' in config.keys()) and (args.new != )):
                torch.save(model.state_dict(), args.new)
                print(('Save pretrained model into %s' % args.new))
        else:
            print(('file not exist %s' % args.old))
    if ('update' in config.keys()):
        mapping_from = []
        mapping_to = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
        if os.path.isfile(args.mapping_to):
            with open(args.mapping_to) as f:
                mapping_to = f.readlines()
                f.close()
        mapping_from = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
        mapping_from = [i for i in mapping_from if ((len(i) > 0) and (i[0] != '#'))]
        mapping_to = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_to]
        mapping_to = [i for i in mapping_to if ((len(i) > 0) and (i[0] != '#'))]
        if ((len(mapping_to) != len(mapping_from)) or (len(mapping_to) == 0) or (len(mapping_from) == 0)):
            mapping = None
            logging.info('no valid mapping')
        else:
            mapping = dict()
            for (i, k) in enumerate(mapping_from):
                if (('{' in k) and ('}' in k) and ('{' in mapping_to[i]) and ('}' in mapping_to[i])):
                    item = k.split('{')
                    for v in item[1].strip('}').split(','):
                        v = v.strip()
                        mapping[(item[0] + v)] = (mapping_to[i].split('{')[0] + v)
                else:
                    mapping[k] = mapping_to[i]
        raw = ('raw' in config.keys())
        if (not os.path.isfile(args.old)):
            args.old = args.pretrained
        utils.import_state_dict(args.old, args.new, mapping, raw, raw_prefix=args.case)
    if ('det-load' in config.keys()):
        from third_party.checkpoint import DetectionCheckpointer
        model_name = args.model
        if (model_name in models.model_zoo):
            (model, args) = models.get_model(args)
        else:
            print(('model(%s) not support, available models: %r' % (model_name, models.model_zoo)))
            return
        split = os.path.split(args.old)
        checkpointer = DetectionCheckpointer(model, split[0], save_to_disk=True)
        checkpointer.resume_or_load(args.old, resume=True)
        checkpointer.save(split[1])
    if ('swap' in config.keys()):
        mapping_from = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
            mapping_from = [i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
            mapping_from = [i for i in mapping_from if ((len(i) > 0) and (i[0] != '#'))]
            lists = args.verbose_list
            for i in lists:
                item = i.split('/')
                interval = int(item[0])
                index = item[1].split('-')
                index = [int(x) for x in index]
                if (((len(mapping_from) % interval) == 0) and (len(index) <= interval)):
                    mapping_to = mapping_from.copy()
                    for (j, k) in enumerate(index):
                        k = (k % interval)
                        mapping_to[j::interval] = mapping_from[k::interval]
            mapping_to = [(i + '\n') for i in mapping_to]
            with open((args.mapping_from + '-swap'), 'w') as f:
                f.writelines(mapping_to)
                f.close()
    if ('sort' in config.keys()):
        mapping_from = []
        if os.path.isfile(args.mapping_from):
            with open(args.mapping_from) as f:
                mapping_from = f.readlines()
                f.close()
            mapping_from.sort()
            with open((args.mapping_from + '-sort'), 'w') as f:
                f.writelines(mapping_from)
                f.close()
    if (('verify-data' in config.keys()) or ('verify-image' in config.keys())):
        if ('verify-image' in config.keys()):
            lists = args.verbose_list
        else:
            with open(os.path.join(args.root, 'train.txt')) as f:
                lists = f.readlines()
                f.close()
        from PIL import Image
        from threading import Thread
        print(('going to check %d files' % len(lists)))

        def check(lists, start, end, index):
            for (i, item) in enumerate(lists[start:end]):
                try:
                    items = item.split()
                    if (len(items) >= 1):
                        path = items[0].strip().strip('\n')
                    else:
                        print(('skip line %s' % i))
                        continue
                    path = os.path.join(args.root, os.path.join('train', path))
                    imgs = Image.open(path)
                    imgs.resize((256, 256))
                    if (index == 0):
                        print(i, end='\r', file=sys.stderr)
                except (RuntimeError, IOError):
                    print(('\nError when read image %s' % path))
            print('\nFinish checking', index)
        num = min(len(lists), 20)
        for i in range(num):
            start = ((len(lists) // num) * i)
            end = min((start + (len(lists) // num)), len(lists))
            th = Thread(target=check, args=(lists, start, end, i))
            th.start()