本文整理汇总了Python中easydict.EasyDict方法的典型用法代码示例。如果您正苦于以下问题:Python easydict.EasyDict方法的具体用法?Python easydict.EasyDict怎么用?Python easydict.EasyDict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类easydict
的用法示例。
在下文中一共展示了easydict.EasyDict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_config_from_json
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_config_from_json(json_file):
"""
Get the config from a json file
:param json_file: the path of the config file
:return: config(namespace), config(dictionary)
"""
# parse the configurations from the config json file provided
with open(json_file, 'r') as config_file:
try:
config_dict = json.load(config_file)
# EasyDict allows to access dict values as attributes (works recursively).
config = EasyDict(config_dict)
return config, config_dict
except ValueError:
print("INVALID JSON file format.. Please provide a good json file")
exit(-1)
示例2: get_config
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_config(config_file, exp_dir=None):
""" Construct and snapshot hyper parameters """
config = edict(yaml.load(open(config_file, 'r')))
# create hyper parameters
config.run_id = str(os.getpid())
config.exp_name = '_'.join([
config.model.name, config.dataset.name,
time.strftime('%Y-%b-%d-%H-%M-%S'), config.run_id
])
if exp_dir is not None:
config.exp_dir = exp_dir
config.save_dir = os.path.join(config.exp_dir, config.exp_name)
# snapshot hyperparameters
mkdir(config.exp_dir)
mkdir(config.save_dir)
save_name = os.path.join(config.save_dir, 'config.yaml')
yaml.dump(edict2dict(config), open(save_name, 'w'), default_flow_style=False)
return config
示例3: get_dataset_celeb
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_dataset_celeb(input_dir):
clean_list_file = input_dir+"_clean_list.txt"
ret = []
dir2label = {}
for line in open(clean_list_file, 'r'):
line = line.strip()
if not line.startswith('./m.'):
continue
line = line[2:]
vec = line.split('/')
assert len(vec)==2
if vec[0] in dir2label:
label = dir2label[vec[0]]
else:
label = len(dir2label)
dir2label[vec[0]] = label
fimage = edict()
fimage.id = line
fimage.classname = str(label)
fimage.image_path = os.path.join(input_dir, fimage.id)
ret.append(fimage)
return ret
示例4: get_dataset_facescrub
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_dataset_facescrub(input_dir):
ret = []
label = 0
person_names = []
for person_name in os.listdir(input_dir):
person_names.append(person_name)
person_names = sorted(person_names)
for person_name in person_names:
subdir = os.path.join(input_dir, person_name)
if not os.path.isdir(subdir):
continue
for _img in os.listdir(subdir):
fimage = edict()
fimage.id = os.path.join(person_name, _img)
fimage.classname = str(label)
fimage.image_path = os.path.join(subdir, _img)
fimage.landmark = None
fimage.bbox = None
ret.append(fimage)
label += 1
return ret
示例5: update_config
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def update_config(config_file):
exp_config = None
with open(config_file) as f:
exp_config = edict(yaml.load(f))
for k, v in exp_config.items():
if k in config:
if isinstance(v, dict):
if k == 'TRAIN':
if 'BBOX_WEIGHTS' in v:
v['BBOX_WEIGHTS'] = np.array(v['BBOX_WEIGHTS'])
elif k == 'network':
if 'PIXEL_MEANS' in v:
v['PIXEL_MEANS'] = np.array(v['PIXEL_MEANS'])
for vk, vv in v.items():
config[k][vk] = vv
else:
if k == 'SCALES':
config[k][0] = (tuple(v))
else:
config[k] = v
else:
raise ValueError("key must exist in config.py")
示例6: get_config_from_json
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_config_from_json(json_file):
"""
Get the config from a json file
Input:
- json_file: json configuration file
Return:
- config: namespace
- config_dict: dictionary
"""
# parse the configurations from the config json file provided
with open(json_file, 'r') as config_file:
config_dict = json.load(config_file)
# convert the dictionary to a namespace using bunch lib
config = EasyDict(config_dict)
return config, config_dict
示例7: get_config_from_yaml
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def get_config_from_yaml(yaml_file):
"""
Get the config from yaml file
Input:
- yaml_file: yaml configuration file
Return:
- config: namespace
- config_dict: dictionary
"""
with open(yaml_file) as fp:
config_dict = yaml.load(fp)
# convert the dictionary to a namespace using bunch lib
config = EasyDict(config_dict)
return config, config_dict
示例8: _merge_a_into_b
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def _merge_a_into_b(a, b):
"""Merge config dictionary a into config dictionary b, clobbering the
options in b whenever they are also specified in a.
"""
if type(a) is not edict:
return
for k, v in a.items():
# a must specify keys that are in b
if k not in b:
raise KeyError('{} is not a valid config key'.format(k))
# the types must match, too
old_type = type(b[k])
if old_type is not type(v):
if isinstance(b[k], np.ndarray):
v = np.array(v, dtype=b[k].dtype)
else:
raise ValueError(('Type mismatch ({} vs. {}) '
'for config key: {}').format(type(b[k]),
type(v), k))
# recursively merge dicts
if type(v) is edict:
try:
_merge_a_into_b(a[k], b[k])
except:
print(('Error under config key: {}'.format(k)))
raise
else:
b[k] = v
开发者ID:Sunarker,项目名称:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代码行数:33,代码来源:config.py
示例9: cfg_from_file
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def cfg_from_file(filename):
"""Load a config file and merge it into the default options."""
import yaml
with open(filename, 'r') as f:
yaml_cfg = edict(yaml.load(f))
_merge_a_into_b(yaml_cfg, __C)
开发者ID:Sunarker,项目名称:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代码行数:9,代码来源:config.py
示例10: process_config
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def process_config(json_file):
"""
Get the json file
Processing it with EasyDict to be accessible as attributes
then editing the path of the experiments folder
creating some important directories in the experiment folder
Then setup the logging in the whole program
Then return the config
:param json_file: the path of the config file
:return: config object(namespace)
"""
config, _ = get_config_from_json(json_file)
print(" THE Configuration of your experiment ..")
pprint(config)
# making sure that you have provided the exp_name.
try:
print(" *************************************** ")
print("The experiment name is {}".format(config.exp_name))
print(" *************************************** ")
except AttributeError:
print("ERROR!!..Please provide the exp_name in json file..")
exit(-1)
# create some important directories to be used for that experiment.
config.summary_dir = os.path.join("experiments", config.exp_name, "summaries/")
config.checkpoint_dir = os.path.join("experiments", config.exp_name, "checkpoints/")
config.out_dir = os.path.join("experiments", config.exp_name, "out/")
config.log_dir = os.path.join("experiments", config.exp_name, "logs/")
create_dirs([config.summary_dir, config.checkpoint_dir, config.out_dir, config.log_dir])
# setup logging in the project
setup_logging(config.log_dir)
logging.getLogger().info("Hi, This is root.")
logging.getLogger().info("After the configurations are successfully processed and dirs are created.")
logging.getLogger().info("The pipeline of the project will begin now.")
return config
示例11: main
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def main():
config = json.load(open('../../configs/dcgan_exp_0.json'))
config = edict(config)
inp = torch.autograd.Variable(torch.randn(config.batch_size, config.g_input_size, 1, 1))
print (inp.shape)
netD = Generator(config)
out = netD(inp)
print (out.shape)
示例12: main
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def main():
config = json.load(open('../../configs/dcgan_exp_0.json'))
config = edict(config)
inp = torch.autograd.Variable(torch.randn(config.batch_size, config.input_channels, config.image_size, config.image_size))
print (inp.shape)
netD = Discriminator(config)
out = netD(inp)
print (out)
示例13: pad_collate
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def pad_collate(data):
"""Creates mini-batch tensors from the list of tuples (src_seq, trg_seq).
"""
# separate source and target sequences
batch = edict()
batch["qas"], batch["qas_mask"] = pad_sequences_2d([d["qas"] for d in data], dtype=torch.long)
batch["qas_bert"], _ = pad_sequences_2d([d["qas_bert"] for d in data], dtype=torch.float)
batch["sub"], batch["sub_mask"] = pad_sequences_2d([d["sub"] for d in data], dtype=torch.long)
batch["sub_bert"], _ = pad_sequences_2d([d["sub_bert"] for d in data], dtype=torch.float)
batch["vid_name"] = [d["vid_name"] for d in data]
batch["qid"] = [d["qid"] for d in data]
batch["target"] = torch.tensor([d["target"] for d in data], dtype=torch.long)
batch["vcpt"], batch["vcpt_mask"] = pad_sequences_2d([d["vcpt"] for d in data], dtype=torch.long)
batch["vid"], batch["vid_mask"] = pad_sequences_2d([d["vfeat"] for d in data], dtype=torch.float)
# no need to pad these two, since we will break down to instances anyway
batch["att_labels"] = [d["att_labels"] for d in data] # a list, each will be (num_img, num_words)
batch["anno_st_idx"] = [d["anno_st_idx"] for d in data] # list(int)
if data[0]["ts_label"] is None:
batch["ts_label"] = None
elif isinstance(data[0]["ts_label"], list): # (st_ed, ce)
batch["ts_label"] = dict(
st=torch.LongTensor([d["ts_label"][0] for d in data]),
ed=torch.LongTensor([d["ts_label"][1] for d in data]),
)
batch["ts_label_mask"] = make_mask_from_length([len(d["image_indices"]) for d in data])
elif isinstance(data[0]["ts_label"], torch.Tensor): # (st_ed, bce) or frm
batch["ts_label"], batch["ts_label_mask"] = pad_sequences_1d([d["ts_label"] for d in data], dtype=torch.float)
else:
raise NotImplementedError
batch["ts"] = [d["ts"] for d in data]
batch["image_indices"] = [d["image_indices"] for d in data]
batch["q_l"] = [d["q_l"] for d in data]
batch["boxes"] = [d["boxes"] for d in data]
batch["object_labels"] = [d["object_labels"] for d in data]
return batch
示例14: edict2dict
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def edict2dict(edict_obj):
dict_obj = {}
for key, vals in edict_obj.items():
if isinstance(vals, edict):
dict_obj[key] = edict2dict(vals)
else:
dict_obj[key] = vals
return dict_obj
示例15: __init__
# 需要导入模块: import easydict [as 别名]
# 或者: from easydict import EasyDict [as 别名]
def __init__(self, args):
self.args = args
model = edict()
self.threshold = args.threshold
self.det_minsize = 50
self.det_threshold = [0.4,0.6,0.6]
self.det_factor = 0.9
_vec = args.image_size.split(',')
assert len(_vec)==2
image_size = (int(_vec[0]), int(_vec[1]))
self.image_size = image_size
_vec = args.model.split(',')
assert len(_vec)==2
prefix = _vec[0]
epoch = int(_vec[1])
print('loading',prefix, epoch)
ctx = mx.gpu(args.gpu)
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
all_layers = sym.get_internals()
sym = all_layers['fc1_output']
model = mx.mod.Module(symbol=sym, context=ctx, label_names = None)
#model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))])
model.set_params(arg_params, aux_params)
self.model = model
mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
detector = MtcnnDetector(model_folder=mtcnn_path, ctx=ctx, num_worker=1, accurate_landmark = True, threshold=[0.0,0.0,0.2])
self.detector = detector