def val(test_loader): test_iter = iter(test_loader) max_iter = len(data_loader) n_correct = 0 n_total = 0 for i in range(max_iter): data = test_iter.next() cpu_images = data[0] cpu_labels = data[1] [num] = cpu_labels.shape utils.loadData(image, cpu_images) preds = MODEL(image) arg_max = preds.argmax(1).cpu.numpy() labels = cpu_labels.numpy() correct = np.sum(arg_max == labels) n_correct += correct n_total += num acc = n_correct / float(n_total) return acc
def trainBatch(): data1 = train_iter1.next() data2 = train_iter2.next() cpu_images = torch.cat((data1[0], data2[0]), 0) cpu_texts1 = data1[1] + data2[1] cpu_texts2 = data1[3] + data2[3] utils.loadData(image, cpu_images) t1, l1 = converter.encode(cpu_texts1, scanned=True) utils.loadData(text1_ori, t1) utils.loadData(length_ori, l1) t2, l2 = converter.encode(cpu_texts2, scanned=True) utils.loadData(text2_ori, t2) N = len(cpu_texts1) if opt.LR is True: preds1, preds2 = MODEL(image, length_ori, text1_ori, text2_ori, cpu_texts=cpu_texts1) text1_new = text1_ori text2_new = text2_ori cost_pred1 = criterion(preds1, text1_new) / 2.0 cost_pred2 = criterion(preds2, text2_new) / 2.0 loss_pred_avg1.add(cost_pred1) loss_pred_avg2.add(cost_pred2) cost = cost_pred1 + cost_pred2 else: preds1 = MODEL(image, length_ori, text1_ori, None, cpu_texts=cpu_texts1) text1_new = text1_ori cost_pred1 = criterion(preds1, text1_new) loss_pred_avg1.add(cost_pred1) cost = cost_pred1 loss_avg.add(cost) MODEL.zero_grad() cost.backward() optimizer.step() return cost
def train_batch(): data = train_iter.next() cpu_images = data[0] cpu_labels = data[1] utils.loadData(image, cpu_images) utils.loadData(ori_label, cpu_labels) # print('ori_label.shape',ori_label.shape) preds = MODEL(image) # print('pred---', preds.shape) # print('label--', ori_label.shape) cost = criterion(preds, ori_label) # print('cost-------', cost) loss.add(cost) MODEL.zero_grad() cost.backward() optimizer.step()
def main(): input_size = 10000 embedding_size = 24 output_size = 5 learning_rate = 0.01 oov_token = '<OOV>' loss = 'sparse_categorical_crossentropy' optimizer = Adam(learning_rate=learning_rate) epochs = 1 train_val_split = 0.2 sentences, sentiments = helper.get_data('data/train.tsv') sentences = helper.remove_stopwords(sentences, 'data/stopwords') max_length = len(max(sentences, key=len)) tokenizer = helper.get_tokenizer(input_size, oov_token, sentences) padded_sentences = helper.convert_to_sequences(tokenizer, sentences, max_length) train_padded_sentences, validation_padded_sentences, train_sentiments, validation_sentiments = \ train_test_split( padded_sentences, sentiments, test_size=train_val_split, random_state=42 ) train_padded_sentences = np.array(train_padded_sentences) train_sentiments = np.array(train_sentiments) validation_padded_sentences = np.array(validation_padded_sentences) validation_sentiments = np.array(validation_sentiments) layers = [ tf.keras.layers.Embedding(input_size, embedding_size, input_length=max_length), # tf.keras.layers.LSTM(32), # tf.keras.layers.Conv1D(filters=64, kernel_size=5, activation='relu'), # tf.keras.layers.MaxPooling1D(pool_size=4), # tf.keras.layers.Dropout(0.2), tf.keras.layers.GlobalAveragePooling1D(), tf.keras.layers.Dense(units=24, activation='relu'), tf.keras.layers.Dense(units=output_size, activation='softmax') ] model = MODEL(input_size, output_size, layers, loss, optimizer, epochs) model.__train__(train_padded_sentences, train_sentiments, validation_padded_sentences, validation_sentiments) model.__plot_graph__('accuracy')
def val_beam(dataset, max_iter=9999): rotate90 = dataset.ifRotate90 data_loader = torch.utils.data.DataLoader(dataset, shuffle=False, batch_size=opt.batchSize, num_workers=1) # opt.batchSize val_iter = iter(data_loader) max_iter = min(max_iter, len(data_loader)) n_correct = 0 n_total = 0 for i in range(max_iter): data = val_iter.next() ori_cpu_images = data[0] flag_rotate90 = data[2] cpu_texts1 = data[1] cpu_texts2 = data[3] t1, l1 = converter.encode(cpu_texts1, scanned=True) t2, l2 = converter.encode(cpu_texts2, scanned=True) utils.loadData(text1_ori, t1) utils.loadData(text2_ori, t2) utils.loadData(length_ori, l1) All_preds_add5EOS1 = [] All_scores1 = [] All_preds_add5EOS2 = [] All_scores2 = [] cpu_images = ori_cpu_images utils.loadData(image, cpu_images) if opt.LR: local_preds1, local_scores1, local_preds2, local_scores2 = MODEL( image, length_ori, text1_ori, text2_ori, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_preds_add5EOS2.append(local_preds2) All_scores1.append(local_scores1) All_scores2.append(local_scores2) else: local_preds1, local_scores1 = MODEL(image, length_ori, text1_ori, None, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_scores1.append(local_scores1) length_label = (length_ori - 1).data.cpu().numpy() # %%% Left/Right Rotate %%% if rotate90 == True: PIL_imgs = [ toPIL(ori_cpu_images[i].div(2).sub(-0.5)) for i in range(ori_cpu_images.shape[0]) ] PIL_imgs_left90 = [ PIL_imgs[i].transpose(Image.ROTATE_90).resize( (opt.imgW, opt.imgH), Image.BILINEAR) if flag_rotate90[i] else PIL_imgs[i] for i in range(ori_cpu_images.shape[0]) ] PIL_imgs_right90 = [ PIL_imgs[i].transpose(Image.ROTATE_270).resize( (opt.imgW, opt.imgH), Image.BILINEAR) if flag_rotate90[i] else PIL_imgs[i] for i in range(ori_cpu_images.shape[0]) ] imgs_Tensor_left90 = [ toTensor(PIL_imgs_left90[i]) for i in range(ori_cpu_images.shape[0]) ] imgs_Tensor_right90 = [ toTensor(PIL_imgs_right90[i]) for i in range(ori_cpu_images.shape[0]) ] # Left cpu_images = torch.stack(imgs_Tensor_left90) cpu_images.sub_(0.5).div_(0.5) utils.loadData(image, cpu_images) if opt.LR: local_preds1, local_scores1, local_preds2, local_scores2, _ = MODEL( image, length_ori, text1_ori, text2_ori, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_preds_add5EOS2.append(local_preds2) All_scores1.append(local_scores1) All_scores2.append(local_scores2) else: local_preds1, local_scores1, _ = MODEL(image, length_ori, text1_ori, None, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_scores1.append(local_scores1) # Right cpu_images = torch.stack(imgs_Tensor_right90) cpu_images.sub_(0.5).div_(0.5) utils.loadData(image, cpu_images) if opt.LR: local_preds1, local_scores1, local_preds2, local_scores2, _ = MODEL( image, length_ori, text1_ori, text2_ori, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_preds_add5EOS2.append(local_preds2) All_scores1.append(local_scores1) All_scores2.append(local_scores2) else: local_preds1, local_scores1, _ = MODEL(image, length_ori, text1_ori, None, test=True, cpu_texts=cpu_texts1) All_preds_add5EOS1.append(local_preds1) All_scores1.append(local_scores1) # Start to decode preds_add5EOS1 = [] preds_score1 = [] for j in range(cpu_images.size(0)): text_begin = 0 if j == 0 else (length_ori.data[:j].sum() + j * 5) max_score = -99999 max_index = 0 for index in range(len(All_scores1)): local_score = All_scores1[index][j] if local_score > max_score: max_score = local_score max_index = index preds_add5EOS1.extend( All_preds_add5EOS1[max_index][text_begin:text_begin + int(length_ori[j].data) + 5]) preds_score1.append(max_score) preds_add5EOS1 = torch.stack(preds_add5EOS1) sim_preds_add5eos1 = converter.decode(preds_add5EOS1.data, length_ori.data + 5) if opt.LR: preds_add5EOS2 = [] preds_score2 = [] for j in range(cpu_images.size(0)): text_begin = 0 if j == 0 else (length_ori.data[:j].sum() + j * 5) max_score = -99999 max_index = 0 for index in range(len(All_scores2)): local_score = All_scores2[index][j] if local_score > max_score: max_score = local_score max_index = index preds_add5EOS2.extend( All_preds_add5EOS2[max_index][text_begin:text_begin + int(length_ori[j].data) + 5]) preds_score2.append(max_score) preds_add5EOS2 = torch.stack(preds_add5EOS2) sim_preds_add5eos2 = converter.decode(preds_add5EOS2.data, length_ori.data + 5) if opt.LR: batch_index = 0 for pred1, target1, pred2, target2 in zip(sim_preds_add5eos1, cpu_texts1, sim_preds_add5eos2, cpu_texts2): if preds_score1[batch_index] > preds_score2[batch_index]: pred = pred1 target = target1 else: pred = pred2 target = target2 pred = pred.split(opt.sep)[0] + opt.sep test_alphabet = dataset.test_alphabet.split(opt.sep) pred = ''.join( pred[i].lower() if pred[i].lower() in test_alphabet else '' for i in range(len(pred))) target = ''.join(target[i].lower() if target[i].lower() in test_alphabet else '' for i in range(len(target))) if pred.lower() == target.lower(): n_correct += 1 n_total += 1 batch_index += 1 else: for pred, target in zip(sim_preds_add5eos1, cpu_texts1): pred = pred.split(opt.sep)[0] + opt.sep test_alphabet = dataset.test_alphabet.split(opt.sep) pred = ''.join( pred[i].lower() if pred[i].lower() in test_alphabet else '' for i in range(len(pred))) target = ''.join(target[i].lower() if target[i].lower() in test_alphabet else '' for i in range(len(target))) if pred.lower() == target.lower(): n_correct += 1 n_total += 1 accuracy = n_correct / float(n_total) dataset_name = dataset.root.split('/')[-1] print(dataset_name + ' ACCURACY -----> %.1f%%, ' % (accuracy * 100.0)) return accuracy
train_dataset_2, batch_size=batchSize2, shuffle=False, sampler=dataset.randomSequentialSampler(train_dataset_2, batchSize2), num_workers=int(opt.workers)) test_dataset1 = dataset.lmdbDataset(test=True, root=opt.test_1, transform=dataset.resizeNormalize( (opt.imgW, opt.imgH))) nclass = len(opt.alphabet.split(opt.sep)) converter = utils.strLabelConverterForAttention(opt.alphabet, opt.sep) criterion = torch.nn.CrossEntropyLoss() MODEL = MODEL(opt.n_bm, nclass, dec_layer=opt.dec_layer, LR=opt.LR) # print("MODEL have {} paramerters in total".format(sum(x.numel() for x in MODEL.parameters()))) if opt.MODEL != '': print('loading pretrained model from %s' % opt.MODEL) state_dict = torch.load(opt.MODEL) MODEL_state_dict_rename = OrderedDict() for k, v in state_dict.items(): name = k.replace("module.", "") # remove `module.` MODEL_state_dict_rename[name] = v MODEL.load_state_dict(MODEL_state_dict_rename, strict=True) image = torch.FloatTensor(opt.batchSize, 1, opt.imgH, opt.imgW) text1_ori = torch.LongTensor(opt.batchSize * 5) text2_ori = torch.LongTensor(opt.batchSize * 5)
cudnn.benchmark = True if torch.cuda.is_available() and not opt.cuda: print( "WARNING: You have a CUDA device, so you should probably run with --cuda" ) test_dataset1 = dataset.lmdbDataset(test=True, root=opt.test_1, transform=dataset.resizeNormalize( (opt.imgW, opt.imgH))) nclass = len(opt.alphabet.split(opt.sep)) converter = utils.strLabelConverterForAttention(opt.alphabet, opt.sep) MODEL = MODEL(opt.n_bm, nclass, dec_layer=opt.dec_layer, LR=opt.LR) # print("MODEL have {} paramerters in total".format(sum(x.numel() for x in MODEL.parameters()))) if opt.MODEL != '': print('loading pretrained model from %s' % opt.MODEL) state_dict = torch.load(opt.MODEL) MODEL_state_dict_rename = OrderedDict() for k, v in state_dict.items(): name = k.replace("module.", "") # remove `module.` MODEL_state_dict_rename[name] = v MODEL.load_state_dict(MODEL_state_dict_rename, strict=True) image = torch.FloatTensor(opt.batchSize, 1, opt.imgH, opt.imgW) text1_ori = torch.LongTensor(opt.batchSize * 5) text2_ori = torch.LongTensor(opt.batchSize * 5)
def Load_train_data(args): # Train data train_dataset_1 = dataset.lmdbDataset( args.alphabet,root=args.train_1, transform=dataset.resizeNormalize((args.imgW, args.imgH))) assert train_dataset_1 train_dataset = train_dataset_1 if args.train_2!=None: train_dataset_2 = dataset.lmdbDataset( args.alphabet,root=args.train_2, transform=dataset.resizeNormalize((args.imgW, args.imgH))) assert train_dataset_2 train_dataset = torch.utils.data.ConcatDataset([train_dataset, train_dataset_2]) if args.train_3!=None: train_dataset_3 = dataset.lmdbDataset( args.alphabet,root=args.train_3, transform=dataset.resizeNormalize((args.imgW, args.imgH))) assert train_dataset_3 train_dataset = torch.utils.data.ConcatDataset([train_dataset, train_dataset_3]) # 该接口主要用来将自定义的数据读取接口的输出或者PyTorch已有的数据读取接口的输入按照batch size封装成Tensor # train_loader = torch.utils.data.DataLoader( # train_dataset, batch_size=args.batchSize, # shuffle=False,sampler=dataset.randomSequentialSampler(train_dataset, args.batchSize), # num_workers=int(args.workers)) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batchSize, shuffle=False, num_workers=int(args.workers)) return train_loader def Load_test_data(dataset_name): dataset = dataset.lmdbDataset( args.alphabet1,test=True,root=dataset_name, transform=dataset.resizeNormalize((args.imgW, args.imgH))) test_loader = torch.utils.data.DataLoader( dataset, shuffle=False, batch_size=args.batchSize, num_workers=int(args.workers)) return test_loader def set_random_seed(random_seed): random.seed(random_seed) np.random.seed(random_seed) torch.manual_seed(random_seed) # print(random.seed) def train_batch(): data = train_iter.next() cpu_images = data[0] cpu_labels = data[1] utils.loadData(image, cpu_images) utils.loadData(ori_label, cpu_labels) label = utils.label_convert(ori_label, nclass) ## 进行 one-hot 编码 print("label size", label.shape) preds = MODEL(image) cost_pred = criterion(preds, label) cost = cost_pred loss_avg.add(cost) MODEL.zero_grad() cost.backward() optimizer.step()
args = parse_args() print(args) set_random_seed(args.random_seed) train_loader = Load_train_data(args) if args.test_1!=None: test_loader1 = Load_train_data(args.test_1) if args.test_2!=None: test_loader2 = Load_train_data(args.test_2) if args.test_3!=None: test_loader3 = Load_train_data(args.test_3) nclass = len(args.alphabet.split(args.sep)) criterion = torch.nn.CrossEntropyLoss() MODEL = MODEL(nclass) image = torch.FloatTensor(args.batchSize, 1, args.imgH, args.imgW) ori_label = torch.IntTensor(args.batchSize) label = torch.IntTensor(args.batchSize, nclass) if args.cuda: MODEL.cuda() MODEL = torch.nn.DataParallel(MODEL, device_ids=range(args.ngpu)) image = image.cuda() ori_label = ori_label.cuda() label = label.cuda() criterion = criterion.cuda() # loss averager loss_pred = utils.averager()
import numpy as np import cv2 from models.model import MODEL from models.transformer.Constants import UNK,PAD,BOS,EOS,PAD_WORD,UNK_WORD,BOS_WORD,EOS_WORD import os,sys,pdb model_path = sys.argv[1] img_path = sys.argv[2] img_name = img_path.split('.')[0].split('/')[-1] alphabet = '0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! " \' # $ % & ( ) * + , - . / : ; < = > ? @ [ \\ ] _ ` ~' n_bm = 5 imgW = 160 imgH = 48 nclass = len(alphabet.split(' ')) MODEL = MODEL(n_bm, nclass) if torch.cuda.is_available(): MODEL = MODEL.cuda() print('loading pretrained model from %s' % model_path) state_dict = torch.load(model_path) MODEL_state_dict_rename = OrderedDict() for k, v in state_dict.items(): name = k.replace("module.", "") # remove `module.` MODEL_state_dict_rename[name] = v MODEL.load_state_dict(MODEL_state_dict_rename) for p in MODEL.parameters(): p.requires_grad = False
print(args) set_random_seed(args.random_seed) train_loader = Load_train_data(args) if args.test_1 != None: test_loader1 = Load_test_data(args, args.test_1) if args.test_2 != None: test_loader2 = Load_test_data(args, args.test_2) if args.test_3 != None: test_loader3 = Load_test_data(args, args.test_3) nclass = 2 criterion = torch.nn.CrossEntropyLoss() MODEL = MODEL() image = torch.FloatTensor(args.batchSize, 1, args.imgH, args.imgW) ori_label = torch.LongTensor(args.batchSize) if args.cuda: MODEL.cuda() # MODEL = torch.nn.DataParallel(MODEL, device_ids=range(args.ngpu)) image = image.cuda() ori_label = ori_label.cuda() criterion = criterion.cuda() # # loss averager loss = utils.averager() optimizer = torch.optim.Adadelta(filter(lambda p: p.requires_grad, MODEL.parameters()),