def __init__(self, config, resume, model, optimizer, loss_function): self.n_gpu = torch.cuda.device_count() self.device = self._prepare_device(self.n_gpu, cudnn_deterministic=config["cudnn_deterministic"]) self.model = model.to(self.device) if self.n_gpu > 1: self.model = torch.nn.DataParallel(self.model, device_ids=list(range(self.n_gpu))) self.optimizer = optimizer self.loss_function = loss_function # Trainer self.epochs = config["trainer"]["epochs"] self.save_checkpoint_interval = config["trainer"]["save_checkpoint_interval"] self.validation_config = config["trainer"]["validation"] self.validation_interval = self.validation_config["interval"] self.find_max = self.validation_config["find_max"] self.validation_custom_config = self.validation_config["custom"] self.start_epoch = 1 self.best_score = -np.inf if self.find_max else np.inf self.root_dir = Path(config["root_dir"]) / config["experiment_name"] self.checkpoints_dir = self.root_dir / "checkpoints" self.logs_dir = self.root_dir / "logs" prepare_empty_dir([self.checkpoints_dir, self.logs_dir], resume=resume) self.writer = visualization.writer(self.logs_dir.as_posix()) self.writer.add_text( tag="Configuration", text_string=f"<pre> \n{json5.dumps(config, indent=4, sort_keys=False)} \n</pre>", global_step=1 ) if resume: self._resume_checkpoint() print("Configurations are as follows: ") print(json5.dumps(config, indent=2, sort_keys=False)) with open((self.root_dir / f"{time.strftime('%Y-%m-%d-%H-%M-%S')}.json").as_posix(), "w") as handle: json5.dump(config, handle, indent=2, sort_keys=False) self._print_networks([self.model])
def main(): argv = sys.argv print(curLine(), "argv:", argv) host_name = sys.argv[2] if len(argv) == 3: arg_groups = params.parse(sys.argv[1], host_name, mode="train") test_score_sum = 0.0 max_test_score = 0.0 experiment_times = 0 eval_score_list = [] best_experiment_times = None for args, config in arg_groups: if not os.path.exists(args.summary_dir): os.makedirs(args.summary_dir) args.pretrained_embeddings = os.path.join( "/home/%s/Word2Vector/Chinese" % host_name, args.pretrained_embeddings) # print(curLine(), "args.data_dir:%s, args.output_dir:%s" % (args.data_dir, args.output_dir)) trainer = Trainer(args) states, best_eval_score = trainer.train(experiment_times) eval_score_list.append(best_eval_score) test_score_sum += best_eval_score if max_test_score < best_eval_score: max_test_score = best_eval_score best_experiment_times = experiment_times experiment_times += 1 print( curLine(), "experiment_times=%d/%d, best_experiment_times=%d, ave_test_score=%f, max_test_score=%f" % (experiment_times, len(arg_groups), best_experiment_times, test_score_sum / experiment_times, max_test_score)) with open('%s/log.jsonl' % args.output_dir, 'a') as f: f.write( json5.dumps({ 'data': os.path.basename(args.data_dir), 'params': config, 'state': states, })) f.write('\n') print(curLine(), "eval_score_list:", eval_score_list, eval_score_list.index(max_test_score), "\n") else: print(curLine(), 'Usage: "python train.py configs/xxx.json5 host_name"')
def cmd_decode(args, schema): decode_schema = build_decode_schema(schema) encoded = open(args.filename, "rb").read() tree, length = decode(decode_schema, "configuration", BitStream(encoded)) if args.nums: decoded = "".join("%s\n" % l for l in make_nums(schema, tree, "configuration")) extension = "nums" else: decoded = json5.dumps(tree, indent=2) extension = "json5" if args.stdout: sys.stdout.write(decoded) else: open("%s.%s" % (os.path.splitext(args.filename)[0], extension), "w").write(decoded)
def test(domain): rnn.eval() f.eval() if not os.path.exists(os.path.join('./', 'test_results')): os.mkdir(os.path.join('./', 'test_results')) results = {} for video in tqdm(test_set.videos()): feature = test_set[video].to( device) # feature: tensor, (n_seq, seq_len, feature_dim) scores = f(rnn(feature)) # scores: tensor, (n_seq, 1) scores = scores.cpu().detach().numpy().tolist() scores = [i[0] for i in scores] results[video] = scores with open(os.path.join('./test_results', '{}.json'.format(domain)), 'w') as file: file.write(js.dumps(results))
def __init__(self, config, checkpoint_path, output_dir): checkpoint_path = Path(checkpoint_path).expanduser().absolute() output_root_dir = Path(output_dir).expanduser().absolute() self.device = prepare_device(torch.cuda.device_count()) self.enhanced_dir = output_root_dir / "enhanced" prepare_empty_dir([self.enhanced_dir]) self.dataloader = self._load_dataloader(config["dataset"]) self.model = self._load_model(config["model"], checkpoint_path, self.device) self.inference_config = config["inference"] print("Configurations are as follows: ") print(json5.dumps(config, indent=2, sort_keys=False)) with open((output_root_dir / f"{time.strftime('%Y-%m-%d-%H-%M-%S')}.json").as_posix(), "w") as handle: json5.dump(config, handle, indent=2, sort_keys=False)
def main(): argv = sys.argv if len(argv) == 2: arg_groups = params.parse(sys.argv[1]) for args, config in arg_groups: trainer = Trainer(args) states = trainer.train() with open('models/log.jsonl', 'a') as f: f.write( json5.dumps({ 'data': os.path.basename(args.data_dir), 'params': config, 'state': states, })) f.write('\n') elif len(argv) == 3 and '--dry' in argv: argv.remove('--dry') arg_groups = params.parse(sys.argv[1]) pprint([args.__dict__ for args, _ in arg_groups]) else: print('Usage: "python train.py configs/xxx.json5"')
async def test_patch_unicode(jp_fetch, labserverapp): id = '@jupyterlab/unicode-extension:plugin' settings = dict(comment=big_unicode_string[::-1]) payload = dict(raw=json5.dumps(settings)) r = await jp_fetch('lab', 'api', 'settings', id, method='PUT', body=json.dumps(payload)) assert r.code == 204 r = await jp_fetch( 'lab', 'api', 'settings', id, method='GET', ) data = json.loads(r.body.decode()) assert data["settings"]["comment"] == big_unicode_string[::-1]
async def test_patch_unicode(jp_fetch, labserverapp): id = "@jupyterlab/unicode-extension:plugin" settings = dict(comment=big_unicode_string[::-1]) payload = dict(raw=json5.dumps(settings)) r = await jp_fetch("lab", "api", "settings", id, method="PUT", body=json.dumps(payload)) validate_request(r) r = await jp_fetch( "lab", "api", "settings", id, method="GET", ) validate_request(r) data = json.loads(r.body.decode()) assert data["settings"]["comment"] == big_unicode_string[::-1]
def test_skip_keys(self): self.assertRaises(TypeError, json5.dumps, {"foo": 1, (1, 2): 2}) self.assertEqual(json5.dumps({ "foo": 1, (1, 2): 2 }, skipkeys=True), '{foo: 1}')
def test_ensure_ascii(self): self.check(u'\u00fc', '"\\u00fc"') self.assertEqual(json5.dumps(u'\u00fc', ensure_ascii=False), u'"\u00fc"')
def test_quote_keys(self): self.assertEqual(json5.dumps({"foo": 1}, quote_keys=True), '{"foo": 1}')
async def test_patch_bad_data(jp_fetch, labserverapp): with pytest.raises(tornado.httpclient.HTTPClientError) as e: settings = dict(keyMap=10) payload = dict(raw=json5.dumps(settings)) await jp_fetch("foo", method="PUT", body=json.dumps(payload)) assert expected_http_error(e, 404)
def check(self, obj, s): self.assertEqual(json5.dumps(obj), s)
def application_json(self): result = dict() result['modules'] = self.application.values() result['has_html'] = self.has_html return json.dumps(result)
if content0["url"] == "1": resultStr = str(buffer, encoding='utf-8') print('resultStr: {0}'.format(resultStr)) else: sessionid = dataBundle.getString("sid", "tts") with open(sessionid + ".pcm", 'ab+') as tts: tts.write(buffer) elif evetType == pyaiui.AIUIConstant.EVENT_AUDIO: pass pyaiui.aiui_init_cae_engine("USB_AC108_V2.0_4") # 创建语音交互代理,生命周期要在全局 agent = pyaiui.IAIUIAgent.createAgent(json5.dumps(cfg, quote_keys=True), OnEvent) # 一下三行是一个标准的消息发送格式,创建->发送->销毁 start_msg = pyaiui.IAIUIMessage.create(pyaiui.AIUIConstant.CMD_START) agent.sendMessage(start_msg) start_msg.destroy() wakup_msg = pyaiui.IAIUIMessage.create(pyaiui.AIUIConstant.CMD_WAKEUP) agent.sendMessage(wakup_msg) wakup_msg.destroy() # with open(TEST_AUDIO_PATH, 'rb') as audio: # while True: # buf = audio.read(1280) # # if not buf:
def senddingding(): op = request.json.get('op') content = request.json.get('content') send_ding(op, content, "监控") return json5.dumps({'code': '0'})
def test_patch_wrong_id(self): with assert_http_error(404): self.settings_api.put('foo', dict(raw=json5.dumps(dict())))
#informacion en tipo JSON data_py = \ { "Nombre": "Luis", #string "Apellido": "Lopez", #string "Edad": 17, #int "Hijos": #array [ "Andres", #string "Carlos", #string "Fernando" #string ], "Trabaja": True, #bool "Trabajo": #dictionary { "Puesto": "Programador", #string "Antiguedad": 5 #int } } """ de Python a JSON """ data_json = json.dumps(data_py) #enviar data_json a js """ de JSON a Python""" data_py = json.loads(data_json) print(data_py["Nombre"]) #accediendo a un campo
def test_empty_key(self): self.assertEqual(json5.dumps({'': 'value'}), '{"": "value"}')
def print_stack(s, indent=4): stack = s remove_parents(stack) return json5.dumps(stack, indent=indent)
import json5 x = { "name": "Viktor", "age": 30, "married": True, "divorced": False, "children": ("Anna","Bogdan"), "pets": None, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } print(json5.dumps(x))
def test_non_string_keys(self): self.assertEqual(json5.dumps({False: 'a', 1: 'b', 2.0: 'c', None: 'd'}), '{"false": "a", "1": "b", "2.0": "c", "null": "d"}')
def test_sort_keys(self): od = OrderedDict() od['foo'] = 1 od['bar'] = 2 self.assertEqual(json5.dumps(od, sort_keys=True), '{bar: 2, foo: 1}')
#import libraries import csv import json5 #file paths csvfilepath = "data.csv" jsonfilepath = 'data.json' # read csv and add it to a dictionary with open(csvfilepath) as csvfile: csvReader = csv.reader(csvfile) next(csvReader) data = [] for csvRow in csvReader: data.append({ "FileId": csvRow[0], "Number": csvRow[1], "ClassId": csvRow[2] }) # write data to a json file with open(jsonfilepath, "w") as jsonfile: jsonfile.write(json5.dumps(data, indent=5))
def check(self, obj, s): self.assertEqual(json5.dumps(obj, compact=True), s)
async def test_patch_wrong_id(jp_fetch, labserverapp): with pytest.raises(tornado.httpclient.HTTPClientError) as e: await jp_fetch("foo", method="PUT", body=json.dumps(dict(raw=json5.dumps(dict())))) assert expected_http_error(e, 404)
def test_patch(self): id = '@jupyterlab/shortcuts-extension:plugin' assert self.settings_api.put( id, dict(raw=json5.dumps(dict()))).status_code == 204
#!/usr/local/bin/python3 import sys import json5 print(end='') lines = sys.stdin.readlines() inputStr = ''.join(str(s) for s in lines) toIndent = len(lines[1]) - len(lines[1].lstrip()) startIndex = inputStr.find('{') endIndex = inputStr.rfind('}') + 1 extractedStr = inputStr[startIndex:endIndex] leftTruncatedString = inputStr[0:startIndex] rigthTruncatedString = inputStr[endIndex:len(inputStr) + 1] json5Output = json5.loads(extractedStr) jsonOutput = json5.dumps(json5Output, sort_keys=True, indent=toIndent) formattedJSONOutput = jsonOutput if (extractedStr.find("'") != -1): formattedJSONOutput = jsonOutput.replace('"', "'") print(leftTruncatedString + formattedJSONOutput + rigthTruncatedString)
def test_patch_invalid_payload_format(self): id = '@jupyterlab/codemirror-extension:commands' settings = dict(keyMap=10) payload = dict(foo=json5.dumps(settings)) with assert_http_error(400): self.settings_api.put(id, payload)
import xmltodict import json import json5 from typing import Dict circuit: Dict[str, any] = { 'name': 'Fuji International Speedway', 'kana': '富士スピードウェイ', 'international': True, 'age': 54, 'km': 4.563, 'course': ['本コース', 'ショート', 'ドリフト', 'ジムカーナ', 'カート'] } print(circuit) print(xmltodict.unparse({'サーキット': circuit}, pretty=True)) print(json.dumps(circuit, ensure_ascii=False, indent=4)) print(json5.dumps(circuit, ensure_ascii=False, indent=4))