def main(params): net = getattr(resnet, params['model'])() net.load_state_dict( torch.load(os.path.join(params['model_root'], params['model'] + '.pth'))) my_resnet = myResnet(net) my_resnet.cuda() my_resnet.eval() imgs = json.load(open(params['input_json'], 'r')) imgs = imgs['images'] N = len([i for i in imgs if i['filepath'] == 'val2014']) import pdb pdb.set_trace() seed(123) # make reproducible dir_fc = params['output_dir'] + '_fc' dir_att = params['output_dir'] + '_att' if not os.path.isdir(dir_fc): os.mkdir(dir_fc) if not os.path.isdir(dir_att): os.mkdir(dir_att) with h5py.File(os.path.join(dir_fc, 'feats_fc.h5')) as file_fc,\ h5py.File(os.path.join(dir_att, 'feats_att.h5')) as file_att: for i, img in enumerate(imgs): if img['filepath'] == 'val2014': # load the image I = skimage.io.imread( os.path.join(params['images_root'], img['filepath'], img['filename'])) # handle grayscale input images if len(I.shape) == 2: I = I[:, :, np.newaxis] I = np.concatenate((I, I, I), axis=2) I = I.astype('float32') / 255.0 I = torch.from_numpy(I.transpose([2, 0, 1])).cuda() with torch.no_grad(): I = Variable(preprocess(I)) tmp_fc, tmp_att = my_resnet(I, params['att_size']) # write to hdf5 d_set_fc = file_fc.create_dataset(str(img['cocoid']), (2048, ), dtype="float") d_set_att = file_att.create_dataset( str(img['cocoid']), (params['att_size'], params['att_size'], 2048), dtype="float") d_set_fc[...] = tmp_fc.cpu().float().numpy() d_set_att[...] = tmp_att.cpu().float().numpy() if i % 1000 == 0: print('processing %d/%d (%.2f%% done)' % (i, N, i * 100.0 / N)) file_fc.close() file_att.close()
def main(params): net = getattr(resnet, params['model'])() net.load_state_dict( torch.load(os.path.join(params['model_root'], params['model'] + '.pth'))) torch.cuda.empty_cache() gc.collect() my_resnet = myResnet(net) my_resnet.cuda() my_resnet.eval() imgs = json.load(open(params['input_json'], 'r')) imgs = imgs['images'] N = len(imgs) seed(123) # make reproducible dir_fc = params['output_dir'] + '_fc' dir_att = params['output_dir'] + '_att' if not os.path.isdir(dir_fc): os.mkdir(dir_fc) if not os.path.isdir(dir_att): os.mkdir(dir_att) for i, img in enumerate(imgs): gc.collect() torch.cuda.empty_cache() # load the image I = skimage.io.imread( os.path.join(params['images_root'], img['filepath'], img['filename'])) # handle grayscale input images if len(I.shape) == 2: I = I[:, :, np.newaxis] I = np.concatenate((I, I, I), axis=2) I = I.astype('float32') / 255.0 I = torch.from_numpy(I.transpose([2, 0, 1])).cuda() I = preprocess(I) with torch.no_grad(): tmp_fc, tmp_att = my_resnet(I, params['att_size']) # write to pkl np.save(os.path.join(dir_fc, str(img['imgid'])), tmp_fc.data.cpu().float().numpy()) np.savez_compressed(os.path.join(dir_att, str(img['imgid'])), feat=tmp_att.data.cpu().float().numpy()) del tmp_fc, tmp_att, I if i % 100 == 0: torch.cuda.empty_cache() if i % 1000 == 0: print('processing %d/%d (%.2f%% done)' % (i, N, i * 100.0 / N)) print('wrote ', params['output_dir'])
from torch.autograd import Variable import skimage.io from torchvision import transforms as trn preprocess = trn.Compose([ #trn.ToPILImage(), #trn.Scale(256), #trn.ToTensor(), trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) from resnet_utils import myResnet resnet = models.resnet101() resnet.load_state_dict(torch.load('/home-nfs/rluo/rluo/model/resnet101-5d3b4d8f.pth')) my_resnet = myResnet(resnet) my_resnet.cuda() my_resnet.eval() def build_vocab(imgs, params): count_thr = params['word_count_threshold'] # count up the number of words counts = {} for img in imgs: for sent in img['sentences']: for w in sent['tokens']: counts[w] = counts.get(w, 0) + 1 cw = sorted([(count,w) for w,count in counts.iteritems()], reverse=True) print 'top words and their counts:' print '\n'.join(map(str,cw[:20]))
th.start() #启动线程 if os.path.isfile(词数词典路径) and os.path.isfile(数_词表路径): 词_数表, 数_词表 = 读出引索(词数词典路径, 数_词表路径) with open(词数词典路径, encoding='utf8') as f: 词数词典 = json.load(f) with open(操作查询路径, encoding='utf8') as f: 操作查询词典 = json.load(f) 方向表 = ['上移', '下移', '左移', '右移', '左上移', '左下移', '右上移', '右下移'] 设备 = MyMNTDevice(_DEVICE_ID) device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu") mod = torchvision.models.resnet101( pretrained=True).eval().cuda(device).requires_grad_(False) resnet101 = myResnet(mod) config = TransformerConfig() model = get_model(config, 130, 模型名称) model = model.cuda(device).requires_grad_(False) while True: if AI打开: 图片路径 = 训练数据保存目录 + '/{}/'.format(str(int(time.time()))) os.mkdir(图片路径) 记录文件 = open(图片路径 + '_操作数据.json', 'w+') 图片张量 = torch.Tensor(0)
def main(params): net = models.resnet101(pretrained=True) #net = getattr(resnet, params['model'])() #net.load_state_dict(torch.load(os.path.join(params['model_root'],params['model']+'.pth'))) my_resnet = myResnet(net) #my_resnet.cuda() my_resnet.eval() imgs = json.load(open(params['input_json'], 'r')) imgs = imgs['images'] N = len(imgs) #get rid of images not exist if params['iscmplt'] == 0: new_img = [] for i, img in enumerate(imgs): if len(new_img) == 100: break k = os.path.join('images', img['filename']) try: I = skimage.io.imread(k) new_img.append(imgs[i]) except: print("none") imgs = new_img print("end selecting") seed(123) # make reproducible dir_fc = params['output_dir'] + '_fc' dir_att = params['output_dir'] + '_att' if not os.path.isdir(dir_fc): os.mkdir(dir_fc) if not os.path.isdir(dir_att): os.mkdir(dir_att) for i, img in enumerate(imgs): print(i) # load the image #I = skimage.io.imread(os.path.join(params['images_root'], img['filepath'], img['filename'])) I = skimage.io.imread( os.path.join(params['images_root'], img['filename'])) # handle grayscale input images if len(I.shape) == 2: I = I[:, :, np.newaxis] I = np.concatenate((I, I, I), axis=2) I = I.astype('float32') / 255.0 I = torch.from_numpy(I.transpose([2, 0, 1])) #I = torch.from_numpy(I.transpose([2,0,1])).cuda() I = Variable(preprocess(I), volatile=True) tmp_fc, tmp_att = my_resnet(I, params['att_size']) # write to pkl np.save(os.path.join(dir_fc, str(img['cocoid'])), tmp_fc.data.cpu().float().numpy()) np.savez_compressed(os.path.join(dir_att, str(img['cocoid'])), feat=tmp_att.data.cpu().float().numpy()) if i % 1000 == 0: print('processing %d/%d (%.2f%% done)' % (i, N, i * 100.0 / N)) # if i==4: # break print('wrote ', params['output_dir'])
import torch import torchvision import numpy as np import os import json from PIL import Image from resnet_utils import myResnet 操作记录='../训练数据样本' if not os.path.exists(操作记录): os.makedirs(操作记录) device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu") resnet101=torchvision.models.resnet101(pretrained=True).eval() resnet101=myResnet(resnet101).cuda(device).requires_grad_(False) 词数词典路径="./json/词_数表.json" with open(词数词典路径, encoding='utf8') as f: 词数词典=json.load(f) for root, dirs, files in os.walk(操作记录): if len(dirs)>0: break for 号 in dirs: 路径json = 操作记录+'/' + 号 + '/_操作数据.json' numpy数组路径= 操作记录+'/' + 号 + '/图片_操作预处理数据2.npz' if os.path.isfile(numpy数组路径): continue 图片张量 = torch.Tensor(0)
def main(): global AI打开 global 操作列 加三技能 = '6' 加二技能 = '5' 加一技能 = '4' 购买 = 'f1' 词数词典路径 = "./json/词_数表.json" 数_词表路径 = "./json/数_词表.json" 操作查询路径 = "./json/名称_操作.json" 操作词典 = {"图片号": "0", "移动操作": "无移动", "动作操作": "无动作"} th = threading.Thread(target=start_listen, ) th.start() # 启动线程 if os.path.isfile(词数词典路径) and os.path.isfile(数_词表路径): 词_数表, 数_词表 = 读出引索(词数词典路径, 数_词表路径) with open(词数词典路径, encoding='utf8') as f: 词数词典 = json.load(f) with open(操作查询路径, encoding='utf8') as f: 操作查询词典 = json.load(f) 方向表 = ['上移', '下移', '左移', '右移', '左上移', '左下移', '右上移', '右下移'] 设备 = MyMNTDevice(_DEVICE_ID) device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu") # mod = torchvision.models.resnet101(pretrained=True).eval().cuda(device).requires_grad_(False) # mod = torchvision.models.resnet101(pretrained=True).eval().cpu().requires_grad_(False) # mod = torchvision.models.resnet50(pretrained=True).eval().cpu().requires_grad_(False) # mod = torchvision.models.resnet34(pretrained=True).eval().cpu().requires_grad_(False) mod = torchvision.models.resnet18( pretrained=True).eval().cpu().requires_grad_(False) resnet101 = myResnet(mod) config = TransformerConfig() model = get_model(config, 130, 模型名称) # model = model.cuda(device).requires_grad_(False) model = model.cpu().requires_grad_(False) 抽样np = 0 if AI打开: 图片张量 = torch.Tensor(0) 操作张量 = torch.Tensor(0) # 伪词序列 = torch.from_numpy(np.ones((1, 60)).astype(np.int64)).cuda(device).unsqueeze(0) 伪词序列 = torch.from_numpy(np.ones( (1, 60)).astype(np.int64)).cpu().unsqueeze(0) 操作序列 = np.ones((1, )) 操作序列[0] = 128 计数 = 0 time_start = time.time() 旧指令 = '移动停' for i in range(1000000): # logger.info("++++001+++++") if AI打开 == False: break try: imgA = 取图(窗口名称) except: AI打开 = False print('取图失败') break # logger.info("+++++002++++") 计时开始 = time.time() # preprocess 图片张量, 操作序列 = preprocess(图片张量, imgA, resnet101, 操作序列, 抽样np) pre_process = time.time() logger.info("pre_process : {} ms ".format(pre_process - 计时开始)) # transform model # 操作张量 = torch.from_numpy(操作序列.astype(np.int64)).cuda(device) 操作张量 = torch.from_numpy(操作序列.astype(np.int64)).cpu() src_mask, trg_mask = create_masks(操作张量.unsqueeze(0), 操作张量.unsqueeze(0), device) 输出_实际_A = model(图片张量.unsqueeze(0), 操作张量.unsqueeze(0), trg_mask) # logger.info("+++++003++++") LI = 操作张量.contiguous().view(-1) # LA=输出_实际_A.view(-1, 输出_实际_A.size(-1)) if 计数 % 20 == 0 and 计数 != 0: print("jineng + zhuangbei ") 设备.发送(购买) 设备.发送(加三技能) 设备.发送(加二技能) 设备.发送(加一技能) 设备.发送('移动停') logger.warning("{} {}".format(旧指令, '周期')) # print(旧指令, '周期') # time.sleep(0.02) 设备.发送(旧指令) # logger.info("++++004+++++") if 计数 % 1 == 0: time_end = time.time() 输出_实际_A = F.softmax(输出_实际_A, dim=-1) 输出_实际_A = 输出_实际_A[:, -1, :] 抽样 = torch.multinomial(输出_实际_A, num_samples=1) 抽样np = 抽样.cpu().numpy() 指令 = 数_词表[str(抽样np[0, -1])] 指令集 = 指令.split('_') # 操作词典 = {"图片号": "0", "移动操作": "无移动", "动作操作": "无动作"} 操作词典['图片号'] = str(i) 方向结果 = 处理方向() # logger.info("++++005+++++") logger.info("方向结果:{} 操作列:{} 攻击态:{}".format( 方向结果, len(操作列), 攻击态)) # deal with output 操作列, output_suc = output(方向结果, 操作列, 操作词典, 指令集, 旧指令, 设备, imgA, i) if output_suc == 0: AI打开 = False break # logging # logger.info("++++008+++++") 用时1 = 0.22 - (time.time() - 计时开始) if 用时1 > 0: logger.info("++++sleep+++++") time.sleep(用时1) logger.info("+++++++++") 用时 = time_end - time_start print("用时{} 第{}张 延时{}".format(用时, i, 用时1), 'A键按下', A键按下, 'W键按下', W键按下, 'S键按下', S键按下, 'D键按下', D键按下, '旧指令', 旧指令, 'AI打开', AI打开, '操作列', 操作列) 计数 = 计数 + 1 # logger.info("++++009+++++") 记录文件.close() time.sleep(1) print('AI打开', AI打开)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--data_dir", default='../data', type=str) parser.add_argument("--image_dir", default='../image', type=str) parser.add_argument( "--bert_model", default="bert-base-uncased", type=str, help="Bert pre-trained model selected in the list: bert-base-uncased, " "bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, " "bert-base-multilingual-cased, bert-base-chinese.") parser.add_argument( "--output_dir", default=None, type=str, required=True, help= "The output directory where the model predictions and checkpoints will be written." ) parser.add_argument( "--max_seq_length", default=77, type=int, help= "The maximum total input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, and sequences shorter \n" "than this will be padded.") parser.add_argument( "--max_hashtag_length", default=12, type=int, help= "The maximum total hashtag input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, and sequences shorter \n" "than this will be padded.") parser.add_argument("--do_train", action='store_true', help="Whether to run training.") parser.add_argument("--do_test", action='store_true', help="Whether to run on the test set.") parser.add_argument("--train_batch_size", default=32, type=int, help="Total batch size for training.") parser.add_argument("--eval_batch_size", default=16, type=int, help="Total batch size for eval.") parser.add_argument("--learning_rate", default=5e-5, type=float, help="The initial learning rate for Adam.") parser.add_argument("--num_train_epochs", default=8.0, type=float, help="Total number of training epochs to perform.") parser.add_argument( "--warmup_proportion", default=0.1, type=float, help= "Proportion of training to perform linear learning rate warmup for. " "E.g., 0.1 = 10%% of training.") parser.add_argument('--seed', type=int, default=42, help="random seed for initialization") parser.add_argument( '--model_select', default='MsdBERT', help='model select' ) # select from MsdBERT, ResNetOnly, BertOnly, Res_BERT args = parser.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") n_gpu = torch.cuda.device_count() logger.info("device: {} n_gpu: {}".format(device, n_gpu)) logger.info("************** Using: " + torch.cuda.get_device_name(0) + " ******************") # set seed val np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) random.seed(args.seed) if n_gpu > 0: torch.cuda.manual_seed_all(args.seed) if not args.do_train and not args.do_test: raise ValueError( "At least one of 'do_train' or 'do_test' must be True.") if os.path.exists(args.output_dir) and os.listdir( args.output_dir) and args.do_train: raise ValueError( "Output directory ({}) already exists and is not empty.".format( args.output_dir)) os.makedirs(args.output_dir, exist_ok=True) processor = Processer(args.data_dir, args.image_dir, args.model_select, args.max_seq_length, args.max_hashtag_length) label_list = processor.get_labels() tokenizer = BertTokenizer.from_pretrained(args.bert_model, do_lower_case=True) train_examples = None num_train_steps = None eval_examples = None if args.do_train: train_examples = processor.get_train_examples() eval_examples = processor.get_eval_examples() num_train_steps = int((len(train_examples) * args.num_train_epochs) / args.train_batch_size) if args.model_select == 'BertOnly': model = BertOnly() elif args.model_select == 'ResNetOnly': model = ResNetOnly() elif args.model_select == 'Res_BERT': model = Res_BERT() elif args.model_select == 'MsdBERT': model = MsdBERT() else: raise ValueError("A model must be given.") model.to(device) net = torchvision.models.resnet152(pretrained=True) encoder = myResnet(net).to(device) if n_gpu > 1: model = torch.nn.DataParallel(model) encoder = torch.nn.DataParallel(encoder) param_optimizer = list(model.named_parameters()) no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] optimizer_grouped_parameters = [{ 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01 }, { 'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0 }, { 'params': encoder.parameters() }] t_total = num_train_steps optimizer = BertAdam(optimizer_grouped_parameters, lr=args.learning_rate, warmup=args.warmup_proportion, t_total=t_total) output_model_file = os.path.join(args.output_dir, "pytorch_model.bin") output_encoder_file = os.path.join(args.output_dir, "pytorch_encoder.bin") train_loss = 0 if args.do_train: train_features = processor.convert_mm_examples_to_features( train_examples, label_list, tokenizer) eval_features = processor.convert_mm_examples_to_features( eval_examples, label_list, tokenizer) train_input_ids, train_input_mask, train_added_input_mask, train_img_feats, \ train_hashtag_input_ids, train_hashtag_input_mask, train_label_ids = train_features train_data = TensorDataset(train_input_ids, train_input_mask, train_added_input_mask, train_img_feats, \ train_hashtag_input_ids, train_hashtag_input_mask, train_label_ids) train_dataloader = DataLoader(train_data, sampler=RandomSampler(train_data), batch_size=args.train_batch_size) eval_input_ids, eval_input_mask, eval_added_input_mask, eval_img_feats, \ eval_hashtag_input_ids, eval_hashtag_input_mask, eval_label_ids = eval_features eval_data = TensorDataset(eval_input_ids, eval_input_mask, eval_added_input_mask, eval_img_feats, \ eval_hashtag_input_ids, eval_hashtag_input_mask, eval_label_ids) eval_dataloader = DataLoader(eval_data, sampler=SequentialSampler(eval_data), batch_size=args.eval_batch_size) max_acc = 0.0 logger.info("*************** Running training ***************") for train_idx in trange(int(args.num_train_epochs), desc="Epoch"): logger.info("********** Epoch: " + str(train_idx + 1) + " **********") logger.info(" Num examples = %d", len(train_examples)) logger.info(" Batch size = %d", args.train_batch_size) logger.info(" Num steps = %d", num_train_steps) model.train() encoder.train() tr_loss = 0 nb_tr_steps = 0 for step, batch in enumerate( tqdm(train_dataloader, desc="Iteration")): batch = tuple(t.to(device) for t in batch) train_input_ids, train_input_mask, train_added_input_mask, train_img_feats, \ train_hashtag_input_ids, train_hashtag_input_mask, train_label_ids = batch imgs_f, img_mean, train_img_att = encoder(train_img_feats) loss = model(train_input_ids, train_img_att, train_input_mask, train_added_input_mask, train_hashtag_input_ids, train_hashtag_input_mask, train_label_ids) if n_gpu > 1: loss = loss.mean() loss.backward() tr_loss += loss.item() nb_tr_steps += 1 optimizer.step() optimizer.zero_grad() logger.info("***** Running evaluation on Dev Set*****") logger.info(" Num examples = %d", len(eval_examples)) logger.info(" Batch size = %d", args.eval_batch_size) model.eval() encoder.eval() eval_loss, eval_accuracy = 0, 0 nb_eval_steps, nb_eval_examples = 0, 0 true_label_list = [] pred_label_list = [] for batch in eval_dataloader: batch = tuple(t.to(device) for t in batch) eval_input_ids, eval_input_mask, eval_added_input_mask, eval_img_feats, \ eval_hashtag_input_ids, eval_hashtag_input_mask, eval_label_ids = batch imgs_f, img_mean, eval_img_att = encoder(eval_img_feats) with torch.no_grad(): tmp_eval_loss = model(eval_input_ids, eval_img_att, eval_input_mask, eval_added_input_mask, eval_hashtag_input_ids, eval_hashtag_input_mask, eval_label_ids) logits = model(eval_input_ids, eval_img_att, eval_input_mask, eval_added_input_mask, eval_hashtag_input_ids, eval_hashtag_input_mask) logits = logits.detach().cpu().numpy() label_ids = eval_label_ids.to('cpu').numpy() true_label_list.append(label_ids) pred_label_list.append(logits) tmp_eval_accuracy = accuracy(logits, label_ids) eval_loss += tmp_eval_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += eval_input_ids.size(0) nb_eval_steps += 1 eval_loss = eval_loss / nb_eval_steps eval_accuracy = eval_accuracy / nb_eval_examples train_loss = tr_loss / nb_tr_steps if args.do_train else None true_label = np.concatenate(true_label_list) pred_outputs = np.concatenate(pred_label_list) precision, recall, F_score = macro_f1(true_label, pred_outputs) result = { 'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'precision': precision, 'recall': recall, 'f_score': F_score, 'train_loss': train_loss } logger.info("***** Dev Eval results *****") for key in sorted(result.keys()): logger.info(" %s = %s", key, str(result[key])) if eval_accuracy > max_acc: torch.save(model.state_dict(), output_model_file) torch.save(encoder.state_dict(), output_encoder_file) logger.info("better model") max_acc = eval_accuracy if args.do_test: model.load_state_dict(torch.load(output_model_file)) encoder.load_state_dict(torch.load(output_encoder_file)) model.to(device) encoder.to(device) model.eval() encoder.eval() test_examples = processor.get_test_examples() logger.info("***** Running evaluation on Test Set*****") logger.info(" Num examples = %d", len(test_examples)) logger.info(" Batch size = %d", args.eval_batch_size) test_features = processor.convert_mm_examples_to_features( test_examples, label_list, tokenizer) test_input_ids, test_input_mask, test_added_input_mask, test_img_feats, \ test_hashtag_input_ids, test_hashtag_input_mask, test_label_ids = test_features test_data = TensorDataset(test_input_ids, test_input_mask, test_added_input_mask, test_img_feats, \ test_hashtag_input_ids, test_hashtag_input_mask, test_label_ids) test_dataloader = DataLoader(test_data, sampler=SequentialSampler(test_data), batch_size=args.eval_batch_size) eval_loss, eval_accuracy = 0, 0 nb_eval_steps, nb_eval_examples = 0, 0 true_label_list = [] pred_label_list = [] for batch in test_dataloader: batch = tuple(t.to(device) for t in batch) test_input_ids, test_input_mask, test_added_input_mask, test_img_feats, \ test_hashtag_input_ids, test_hashtag_input_mask, test_label_ids = batch imgs_f, img_mean, test_img_att = encoder(test_img_feats) with torch.no_grad(): tmp_eval_loss = model(test_input_ids,test_img_att, test_input_mask, test_added_input_mask, \ test_hashtag_input_ids, test_hashtag_input_mask, test_label_ids) logits = model(test_input_ids, test_img_att, test_input_mask, test_added_input_mask, \ test_hashtag_input_ids, test_hashtag_input_mask) logits = logits.detach().cpu().numpy() label_ids = test_label_ids.to('cpu').numpy() true_label_list.append(label_ids) pred_label_list.append(logits) tmp_eval_accuracy = accuracy(logits, label_ids) eval_loss += tmp_eval_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += test_input_ids.size(0) nb_eval_steps += 1 eval_loss = eval_loss / nb_eval_steps eval_accuracy = eval_accuracy / nb_eval_examples loss = train_loss if args.do_train else None true_label = np.concatenate(true_label_list) pred_outputs = np.concatenate(pred_label_list) precision, recall, F_score = macro_f1(true_label, pred_outputs) result = { 'test_loss': eval_loss, 'test_accuracy': eval_accuracy, 'precision': precision, 'recall': recall, 'f_score': F_score, 'train_loss': loss } pred_label = np.argmax(pred_outputs, axis=-1) fout_p = open(os.path.join(args.output_dir, "pred.txt"), 'w') fout_t = open(os.path.join(args.output_dir, "true.txt"), 'w') for i in range(len(pred_label)): attstr = str(pred_label[i]) fout_p.write(attstr + '\n') for i in range(len(true_label)): attstr = str(true_label[i]) fout_t.write(attstr + '\n') fout_p.close() fout_t.close() output_eval_file = os.path.join(args.output_dir, "eval_results.txt") with open(output_eval_file, "w") as writer: logger.info("***** Test Eval results *****") for key in sorted(result.keys()): logger.info(" %s = %s", key, str(result[key])) writer.write("%s = %s\n" % (key, str(result[key])))
import numpy as np import os import json from PIL import Image from resnet_utils import myResnet os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' 操作记录 = '../训练数据样本' if not os.path.exists(操作记录): os.makedirs(操作记录) device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu") resnet101 = torchvision.models.resnet101(pretrained=True).eval() # resnet101=myResnet(resnet101).cuda(device).requires_grad_(False) resnet101 = myResnet(resnet101).cpu().requires_grad_(False) 词数词典路径 = "./json/词_数表.json" with open(词数词典路径, encoding='utf8') as f: 词数词典 = json.load(f) for root, dirs, files in os.walk(操作记录): if len(dirs) > 0: break for 号 in dirs: 路径json = 操作记录 + '/' + 号 + '/_操作数据.json' numpy数组路径 = 操作记录 + '/' + 号 + '/图片_操作预处理数据.npz' if os.path.isfile(numpy数组路径): continue 图片张量 = torch.Tensor(0)