def convert(options): # Create a model to classify single image x_single = tf.placeholder( tf.float32, [1, train.IMAGE_SIZE, train.IMAGE_SIZE, train.IMAGE_CHANNEL_NUM], name="input_single") y_single = train.inference(x_single) output_single = tf.identity(tf.nn.softmax(y_single, axis=1), name="output_single") with tf.Session() as sess: saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(options.model_dir) if ckpt and ckpt.model_checkpoint_path: print("Restoring %s" % ckpt.model_checkpoint_path) # Restore the model trained by train.py saver.restore(sess, ckpt.model_checkpoint_path) graph_def = tf.get_default_graph().as_graph_def() # Freeze the graph output_graph = graph_util.convert_variables_to_constants( sess, graph_def, ["output_single"]) # The input type and shape of the converted model is inferred from the input_tensors argument tflite_model = tf.contrib.lite.toco_convert( output_graph, input_tensors=[x_single], output_tensors=[output_single]) with open(options.output_file, "wb") as f: f.write(tflite_model) else: print("Checkpoint not found")
def predict_proba(self, data, return_sequences=False, return_df=True): """ @param data: data object for inference @param return_sequences: whether to return outputs from all time steps (many-to-many prediction) shape = b x s or just the last one (many-to-one prediction) shape = b """ loader = self.get_inference_loader(data) if not hasattr(self, 'model'): print("initializing new model...") if feature_embed_dims is not None: self.feature_embed_dims = feature_embed_dims self.process_feature_embed_dims(data) self.prep_model_params(data) self.model = tcn_model.TCN_Deposit(self.params) inference_formatter = train.inference(self, self.model, loader, return_sequences) if return_df: result = inference_formatter.get_inference(data['features_meta'], return_sequences) else: result = inference_formatter.get_inference(None, return_sequences) return result
def reg(img): label = None acc = 0.0 img0 = cv2.resize(img, (64, 64)) img2 = tf.cast(img0, tf.float32) img3 = tf.reshape(img2, (1, 64, 64, 3)) logit = train.inference(img3) qq = tf.nn.softmax(logit) maxa = tf.argmax(logit, 1) q = qq[0][maxa[0]] variable_averages = tf.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: tf.local_variables_initializer().run() init_op = tf.global_variables_initializer() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] for i in range(1): ss, dd = sess.run([maxa, q]) label = ss acc = dd return label, acc
def replyToMessage(self, message=None): print('dismissing notification') # Try to dismiss notification try: # Looping through buttons of page buttons = self.driver.find_elements_by_tag_name('button') for button in buttons: print(button.text) if button.text == 'Not Now': print('aaa') button.click() break except Exception as e: logging.error(e) if message: top_n = 1 for i in range(top_n): sentence = inference(message, top_n) sentence = ' '.join(sentence) print(sentence) responses.append(sentence) else: sentence = choice(self.greetings) self.__randomSleep__() self.driver.find_elements_by_xpath("*//textarea")[0].send_keys(sentence) self.__randomSleep__() buttons = self.driver.find_elements_by_css_selector( self.selectors['send']) buttons[len(buttons)-1].click() self.__randomSleep__()
def interence(): try: result = train.inference(int(ui.textEdit_5.toPlainText())) ax = plt.subplot(122) ax.bar(labels, result) plt.show() except ValueError: pass
def eval(): with tf.Graph().as_default(): images, labels = inputs(FLAGS.eval_data, FLAGS.img_size, FLAGS.batch_size) tf.summary.image('eval_input', images, 20) # inference model logits, _ = inference(images, FLAGS.num_classes) # Calculate predictions top_k_op = tf.nn.in_top_k(logits, labels, 1, name="accuracy") # Restore the moving average version of the learned variables for eval. # ema = tf.train.ExponentialMovingAverage(decay=0.9999) # saver = tf.train.Saver(ema.variables_to_restore) # summary_op = tf.merge_all_summaries() # graph_def = tf.get_default_graph().as_graph_def() # summary_writer = tf.train.SummaryWriter(FLAGS.log_dir, graph_def=graph_def) checkpoint_file = os.path.join(FLAGS.checkpoint_dir, 'model.ckpt-' + FLAGS.ckpt_step) saver = tf.train.Saver() with tf.Session() as sess: # ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) # if ckpt and ckpt.model_checkpoint_path: # # restore from checkpoint # saver.restore(sess, checkpoint_file) # global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] # print ('global_step = %d' % global_step) # else: # print('No checkpoint file found') # return saver.restore(sess, checkpoint_file) # Start the queue runners. coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size)) true_count = 0 # Counts the number of correct predictions. total_sample_count = num_iter * FLAGS.batch_size step = 0 while step < num_iter and not coord.should_stop(): predictions = sess.run([top_k_op]) true_count += np.sum(predictions) print(step, true_count) step += 1 # Compute precisions precision = true_count / total_sample_count print('precision = %.4f' % (precision)) coord.request_stop() coord.join(threads)
def run(imgName): graph = tf.Graph() with graph.as_default(): imgData = cv2.imread(imgName, 0) input = tf.convert_to_tensor(imgData, dtype=tf.float32) logist = train.inference(input) prediction = tf.nn.softmax(logist) saver = tf.train.Saver() with tf.Session(graph=graph) as sess: saver.restore(sess, checkPointPath) predictionVal = sess.run(prediction) top_1 = predictionVal.argsort()[0][::-1][0] print("The prediction val is: ", predictionVal) print("The predict class is: {}".format(top_1))
def translate_seq2seq(source): source = source.rstrip() source = " ".join(source) result = inference(config, source) print(result) return " ".join(result)
def pretrain(): opt = parser.parse_args() pretrain_cfg = PretrainConfig.load_from_json(os.path.join(opt.workspace_path, opt.pretrain_cfg_file)) model_cfg = ModelConfig.load_from_json(os.path.join(opt.workspace_path, opt.model_cfg_file)) img_file = os.path.join(opt.input_path, opt.img_file) corpus_file = os.path.join(opt.input_path, opt.corpus_file) eval_corpus_file = os.path.join(opt.input_path, opt.eval_corpus_file) vocab_file = os.path.join(opt.input_path, opt.vocab_file) video_type_map_file = os.path.join(opt.input_path, opt.video_type_map_file) merges_file = os.path.join(opt.input_path, opt.merges_file) preprocess_dir = os.path.join(opt.workspace_path, opt.preprocess_dir) if not os.path.exists(preprocess_dir): os.mkdir(preprocess_dir) save_dir = os.path.join(opt.workspace_path, opt.save_dir) if not os.path.exists(save_dir): os.mkdir(save_dir) log_dir = os.path.join(opt.workspace_path, opt.log_dir) if not os.path.exists(log_dir): os.mkdir(log_dir) if opt.model_file is not None: model_file = os.path.join(save_dir, opt.model_file) else: model_file = None log_filename = "{}log.txt".format("" if not opt.eval else "eval_") log_filename = os.path.join(log_dir,log_filename) logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO, handlers=[logging.FileHandler(os.path.join(opt.log_dir, log_filename)), logging.StreamHandler()]) logger = logging.getLogger(__name__) logger.info(opt) tokenizer = MyBartTokenizer(vocab_file, merges_file) dev_data = Dataset(vocab_file, eval_corpus_file, img_file, video_type_map_file, preprocess_dir, model_cfg, pretrain_cfg, imgs=None, is_training=False, type ='pretrain') dev_data.load_dataset(tokenizer) dev_data.load_dataloader() if opt.eval is False: train_data = Dataset(vocab_file, corpus_file, img_file, video_type_map_file, preprocess_dir, model_cfg, pretrain_cfg, imgs=dev_data.imgs, is_training=True, type ='pretrain') train_data.load_dataset(tokenizer) train_data.load_dataloader() model = MyPLVCG(model_cfg, dev_data.video_type_weight, type="pretrain") if opt.eval is False: #Train if model_file is not None: model.load_state_dict(torch.load(model_file)) optimizer = AdamW(filter(lambda p: p.requires_grad,model.parameters()), lr=pretrain_cfg.lr, eps=pretrain_cfg.adam_epsilon) train(pretrain_cfg, logger, save_dir, model, train_data, dev_data, optimizer, type = 'pretrain') else: #Evaluation checkpoint = os.path.join(save_dir, 'best-model.pt') logger.info("Loading checkpoint from {}".format(checkpoint)) model.load_state_dict(torch.load(checkpoint)) if torch.cuda.is_available(): model.to(torch.device("cuda")) model.eval() with(torch.no_grad()): total_loss, predictions, predictions_type, logits = inference(pretrain_cfg, model, dev_data, type) print_results(save_dir, dev_data, 0, total_loss, predictions, predictions_type, dev_data.comments, dev_data.contexts, dev_data.video_types)
''' img=cv.imread("2_1.png") img=cv.cvtColor(img,cv.COLOR_BGR2GRAY) print(img.shape) img=img.reshape(-1,28,28,1) print(img.shape) ''' index=122 img=np.reshape(mnist.test.images[index],[28,28]) plt.imshow(img,cmap=plt.cm.gray) plt.show() img=np.reshape(mnist.test.images[index],[1,28,28,1]) y_output=train.inference(tf.cast(img,tf.float32),False,None) prediction = tf.argmax(input=y_output, axis=1) variable_average=tf.train.ExponentialMovingAverage(decay=0.99) saver=tf.train.Saver(variable_average.variables_to_restore()) with tf.Session() as sess: init_op=tf.global_variables_initializer() sess.run(init_op) saver.restore(sess, "E:/python_study/maching learning/Project/zerotoone/mnist-gpu/model/mnist_model-29001") print(sess.run(prediction))
#!/usr/bin/env python import numpy as np import tensorflow as tf import imageio import PIL from train import inference if __name__ == "__main__": weightdir = "./weight/" pb_path = "./weight/model2.pb" x = tf.placeholder(tf.float64, [None, 3], name='input') x = tf.cast(x, tf.float32) y = inference(x, 3, 10, 1) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=5) ckpt = tf.train.get_checkpoint_state(weightdir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored from ", ckpt.model_checkpoint_path) # save pb constant_graph = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, ["output"]) with tf.gfile.FastGFile(pb_path, mode='wb') as f: f.write(constant_graph.SerializeToString()) print("%d ops in the final graph." % len(constant_graph.node))
def classification(): opt = parser.parse_args() classification_cfg = ClassificationConfig.load_from_json( os.path.join(opt.workspace_path, opt.classification_cfg_file)) model_cfg = ModelConfig.load_from_json( os.path.join(opt.workspace_path, opt.model_cfg_file)) img_file = os.path.join(opt.input_path, opt.img_file) corpus_file = os.path.join(opt.input_path, opt.corpus_file) eval_corpus_file = os.path.join(opt.input_path, opt.eval_corpus_file) vocab_file = os.path.join(opt.input_path, opt.vocab_file) merges_file = os.path.join(opt.input_path, opt.merges_file) preprocess_dir = os.path.join(opt.workspace_path, opt.preprocess_dir) video_type_map_file = os.path.join(opt.input_path, opt.video_type_map_file) save_dir = os.path.join(opt.workspace_path, opt.save_dir) log_dir = os.path.join(opt.workspace_path, opt.log_dir) if opt.model_file is not None: model_file = os.path.join(save_dir, opt.model_file) else: model_file = None pretrain_file = os.path.join(opt.workspace_path, opt.pretrain_file) log_filename = "{}log.txt".format("" if not opt.eval else "eval_") log_filename = os.path.join(log_dir, log_filename) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO, handlers=[ logging.FileHandler(os.path.join(opt.log_dir, log_filename)), logging.StreamHandler() ]) logger = logging.getLogger(__name__) logger.info(opt) tokenizer = MyBartTokenizer(vocab_file, merges_file) dev_data = Dataset(vocab_file, eval_corpus_file, img_file, video_type_map_file, preprocess_dir, model_cfg, classification_cfg, imgs=None, is_training=False, type='fine_tuning') dev_data.load_classification_dataset(tokenizer) dev_data.load_dataloader() if opt.eval is False: train_data = Dataset(vocab_file, corpus_file, img_file, video_type_map_file, preprocess_dir, model_cfg, classification_cfg, imgs=dev_data.imgs, is_training=True, type='fine_tuning') train_data.load_classification_dataset(tokenizer) train_data.load_dataloader() model = MyClassificationPLVCG(model_cfg, classification_cfg.negative_num, type="classification") if not opt.without_pretrain: model_dict = model.state_dict() print("Loading pretrain file...") pretrained_dict = torch.load(pretrain_file) pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) gt = np.array(([1] + [0] * (5 - 1)) * (100 // 5)) if opt.eval is False: #Train if model_file is not None: model.load_state_dict(torch.load(model_file)) optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=classification_cfg.lr, eps=classification_cfg.adam_epsilon) train(classification_cfg, logger, save_dir, model, train_data, dev_data, optimizer, type='classification') else: #Evaluation checkpoint = os.path.join(save_dir, 'best-model.pt') model = MyClassificationPLVCG(model_cfg, classification_cfg.negative_num) logger.info("Loading checkpoint from {}".format(checkpoint)) model.load_state_dict(torch.load(checkpoint)) if torch.cuda.is_available(): model.to(torch.device("cuda")) model.eval() with (torch.no_grad()): total_loss, predictions, logits = inference( classification_cfg, model, dev_data, 'classification') for i in range(10): print(0.3 + i * 0.05) predictions = logit_pred(logits, 0.3 + i * 0.05) precision, recall, f1 = metrics(predictions, classification_cfg.negative_num) print("precision:%f \t recall:%f \t f1:%f" % (precision, recall, f1)) rids = dev_data.dataset.rids print_classification_res(save_dir, dev_data, 0, total_loss, predictions, dev_data.comments, dev_data.contexts, rids, logits, classification_cfg.negative_num)
import tensorflow as tf import numpy as np import os import train import time import cv2 MOVING_AVERAGE_DECAY = 0.99 EVAL_INTERVAL_SECS = 10 image_size = 128 img = cv2.imread("qqq.jpg") img0 = cv2.resize(img, (128, 128)) img2 = tf.cast(img0, tf.float32) img3 = tf.reshape(img2, (1, 128, 128, 3)) y = train.inference(img3) qq = tf.nn.softmax(y) maxa = tf.argmax(y, 1) q = qq[0][maxa[0]] variable_averages = tf.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: tf.local_variables_initializer().run() init_op = tf.global_variables_initializer() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
def evaluation(img_path, ckpt_path): tf.reset_default_graph() f = open(img_path, 'r') img = cv2.imread(img_path, cv2.IMREAD_COLOR) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face = faceCascade.detectMultiScale(gray, 1.1, 3) if len(face) > 0: for rect in face: random_str = str(random.random()) cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0, 255), thickness=2) face_detect_img_path = 'D:/PycharmProjects/myface/eval_images/' + random_str + '.jpg' cv2.imwrite(face_detect_img_path, img) x = rect[0] y = rect[1] w = rect[2] h = rect[3] cv2.imwrite('D:/PycharmProjects/myface/eval_images/' + random_str + '.jpg', img[y:y+h, x:x+w]) target_image_path = 'D:/PycharmProjects/myface/eval_images/' + random_str + '.jpg' else: print('image:No Face') return f.close() f = open(target_image_path, 'r') image = [] img = cv2.imread(target_image_path) img = cv2.resize(img, (28, 28)) image.append(img.flatten().astype(np.float32)/255.0) image = np.asarray(image) logits = train.inference(image, 1.0) sess = tf.InteractiveSession() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) if ckpt_path: saver.restore(sess, ckpt_path) softmax = logits.eval() result = softmax[0] rates = [round(n * 100.0, 1) for n in result] humans = [] for index, rate in enumerate(rates): name = HUMAN_NAMES[index] humans.append({ 'label': index, 'name': name, 'rate': rate }) rank = sorted(humans, key=lambda x: x['rate'], reverse=True) print(img_path) print(rank) return [rank, os.path.basename(img_path), random_str + '.jpg']
def main(): parser = argparse.ArgumentParser(description='seq2seq') parser.add_argument( '--model', default='seq2seq', type=str, help= 'which model you are going to train, now including [seq2seq, pmi_seq2seq]' ) parser.add_argument( '--attn', default=None, type=str, help='which attention method to use, including [dot, general, concat]') parser.add_argument('--gpu', default=-1, type=int, help='which GPU to use, -1 means using CPU') parser.add_argument('--save', action="store_true", help='whether to save model or not') parser.add_argument('--bs', default=64, type=int, help='batch size') parser.add_argument('--emb_dim', default=300, type=int, help='embedding dim') parser.add_argument('--enc_hid_dim', default=300, type=int, help='hidden dim of lstm') parser.add_argument('--dec_hid_dim', default=300, type=int, help='hidden dim of lstm') parser.add_argument('--birnn', action='store_true', help='whether to use bidirectional rnn, default False') parser.add_argument('--n_layers', default=1, type=int, help='layer num of encoder and decoder') parser.add_argument('--dropout', default=0.5, type=float, help='dropout ratio') parser.add_argument('--n_epochs', default=30, type=int, help='num of train epoch') parser.add_argument('--min_freq', default=1, type=int, help='minimal occur times for vocabulary') parser.add_argument('--clip', default=None, type=float, help='grad clip') parser.add_argument('--maxlen', default=None, type=int, help='max length of text') parser.add_argument('--dataset_dir_path', default=None, type=str, help='path to directory where data file is saved') parser.add_argument('--tokenizer', default='spacy_en', type=str, help='which tokenizer to use for the dataset') parser.add_argument('--train_file', default=None, type=str, help='train file name') parser.add_argument('--valid_file', default=None, type=str, help='valid file name') parser.add_argument('--test_file', default=None, type=str, help='test file name') parser.add_argument('--save_dir', default='models', type=str, help='save dir') parser.add_argument('--vocab_file', default=None, type=str, help='predefined vocab file') parser.add_argument( '--num_workers', default=0, type=int, help= 'how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process.' ) parser.add_argument('--l2', default=0, type=float, help='l2 regularization') parser.add_argument('--lr', default=1e-4, type=float, help='learning rate') parser.add_argument( '--teaching_rate', default=1, type=float, help='teaching_rate is probability to use teacher forcing') parser.add_argument('--pretrained_embed_file', default=None, type=str, help='torchtext vector name') parser.add_argument('--warmup', default=0, type=int, help='warmup steps, 0 means not using NoamOpt') parser.add_argument('--cell_type', default='LSTM', type=str, help='cell type of encoder/decoder, LSTM or GRU') parser.add_argument( '--comment', default='', type=str, help='comment, will be used as prefix of save directory') parser.add_argument('--smoothing', default=0.0, type=float, help='smoothing rate of computing kl div loss') parser.add_argument('--max_vocab_size', default=None, type=int, help='max size of vocab') parser.add_argument('--serialize', action='store_true', help='whether to serialize examples and vocab') parser.add_argument('--use_serialized', action='store_true', help='whether to use serialized dataset') parser.add_argument('--model_path', default=None, type=str, help='restore model to continue training') parser.add_argument('--global_step', default=0, type=int, help='global step for continuing training') parser.add_argument('--inference', action='store_true', help='inference mode') parser.add_argument('--seed', default=20020206, type=int, help='random seed') parser.add_argument( '--ln', action='store_true', help= 'whether to use layernorm, if model is pmi_seq2seq, use conditional layernorm as default' ) parser.add_argument( '--patience', default=None, type=int, help= "stop when {patience} continued epochs giving no improved performance" ) args, unparsed = parser.parse_known_args() setup_random_seed(args.seed) writer = None if args.save: tz_sh = tz.gettz('Asia/Shanghai') save_dir = os.path.join( args.save_dir, args.comment + 'run' + str(datetime.now(tz=tz_sh)).replace( ":", "-").split(".")[0].replace(" ", '.')) if args.model_path: save_dir = os.path.split(args.model_path)[0] args.save_dir = save_dir if not os.path.exists(save_dir): os.mkdir(save_dir) with open(os.path.join(save_dir, 'args.txt'), 'w') as f: json.dump(args.__dict__, f, indent=2) writer = SummaryWriter(os.path.join(save_dir, 'summary')) device = torch.device(args.gpu if ( torch.cuda.is_available() and args.gpu >= 0) else 'cpu') args.device = device if args.tokenizer == 'spacy_en': dataset = seq2seq_dataset(args) elif args.tokenizer == 'jieba': from data.dataset import jieba_tokenize dataset = seq2seq_dataset(args, tokenizer=jieba_tokenize) elif args.tokenizer == 'bert': tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') dataset = seq2seq_dataset(args, tokenizer=tokenizer.tokenize) elif args.tokenizer == 'whitespace': from data.dataset import whitespace_tokenize dataset = seq2seq_dataset(args, tokenizer=whitespace_tokenize) # dataset = load_iwslt(args) SRC = dataset['fields']['src'] TGT = dataset['fields']['tgt'] EMB_DIM = args.emb_dim ENC_HID_DIM = args.enc_hid_dim DEC_HID_DIM = args.dec_hid_dim N_LAYERS = args.n_layers ENC_DROPOUT = args.dropout DEC_DROPOUT = args.dropout SRC_PAD_IDX = SRC.vocab.stoi[SRC.pad_token] TGT_PAD_IDX = TGT.vocab.stoi[TGT.pad_token] N_EPOCHS = args.n_epochs CLIP = args.clip src_embedding = Embedding(len(SRC.vocab), EMB_DIM, padding_idx=SRC_PAD_IDX, dropout=ENC_DROPOUT) tgt_embedding = Embedding(len(TGT.vocab), EMB_DIM, padding_idx=TGT_PAD_IDX, dropout=DEC_DROPOUT) if args.pretrained_embed_file: # 权重在词汇表vocab的vectors属性中 src_pretrained_vectors = SRC.vocab.vectors tgt_pretrained_vectors = TGT.vocab.vectors # 指定嵌入矩阵的初始权重 src_embedding.lut.weight.data.copy_(src_pretrained_vectors) tgt_embedding.lut.weight.data.copy_(tgt_pretrained_vectors) print("pretrained vectors loaded successfully!") enc = RNNBaseEncoder(args.cell_type, EMB_DIM, ENC_HID_DIM, N_LAYERS, bidirectional=args.birnn, dropout=ENC_DROPOUT, layernorm=args.ln) if args.attn is not None: dec = LuongAttnRNNDecoder(args.cell_type, EMB_DIM, ENC_HID_DIM, DEC_HID_DIM, attn_method=args.attn, num_layers=N_LAYERS, dropout=DEC_DROPOUT) else: dec = RNNBaseDecoder(args.cell_type, EMB_DIM, DEC_HID_DIM, num_layers=N_LAYERS, dropout=DEC_DROPOUT) generator = Generator(DEC_HID_DIM, len(TGT.vocab)) if args.ln: if args.model == 'seq2seq': layernorm = LayerNorm(feature=ENC_HID_DIM) elif args.model == 'pmi_seq2seq': layernorm = LayerNorm(feature=DEC_HID_DIM, conditional=True, condition_size=len(TGT.vocab), condition_hidden_size=DEC_HID_DIM, condition_activation="ReLU") else: raise ValueError(args.model, "is not a legal model name!") else: layernorm = None if args.model == 'seq2seq': model = RNNBaseSeq2Seq(enc, dec, src_embedding, tgt_embedding, generator).to(device) train_pmi = None elif args.model == 'pmi_seq2seq': # 默认pmi_hid_dim = ENC_HID_DIM, 因此dec_hid_dim必须是enc_hid_dim的两倍! model = RNNBasePMISeq2Seq(ENC_HID_DIM, enc, dec, src_embedding, tgt_embedding, generator, layernorm).to(device) from scipy import sparse train_pmi = sparse.load_npz( os.path.join(args.dataset_dir_path, "train_sparse_pmi_matrix.npz")) # valid_pmi = sparse.load_npz(os.path.join(args.dataset_dir_path, "valid_sparse_pmi_matrix.npz")) # 好像用不上valid和test pmi,不然算标签泄漏了? # test_pmi = sparse.load_npz(os.path.join(args.dataset_dir_path, "test_sparse_pmi_matrix.npz")) if args.model_path is not None: logger.info(f"Restore model from {args.model_path}...") # model.load_state_dict(torch.load(args.model_path, map_location={'cuda:0': 'cuda:' + str(args.gpu)})) model = torch.load(args.model_path, map_location={'cuda:0': 'cuda:' + str(args.gpu)}) model.to(args.device) print(model) weight = torch.ones(len(TGT.vocab), device=args.device) weight[TGT_PAD_IDX] = 0 criterion = nn.NLLLoss(reduction='sum', ignore_index=TGT_PAD_IDX, weight=weight) # criterion = LabelSmoothing(args, len(TGT.vocab), padding_idx=TGT_PAD_IDX, smoothing=args.smoothing) if args.inference: try: assert args.model_path is not None except AssertionError: logger.error( "If you want to do inference, you must offer a trained model's path!" ) finally: inference(args, model, dataset['valid_iterator'], fields=dataset['fields'], mode='valid', pmi=train_pmi) inference(args, model, dataset['test_iterator'], fields=dataset['fields'], mode='test', pmi=train_pmi) return 0 print(f'The model has {count_parameters(model):,} trainable parameters') optimizer = AdamOptimizer(model.parameters(), lr=args.lr, weight_decay=args.l2, max_grad_norm=args.clip) # optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2) if args.warmup > 0: optimizer = NoamOptimWrapper(args.hid_dim, 1, args.warmup, optimizer) if args.global_step > 0: logger.info(f'Global step start from {args.global_step}') optimizer._step = args.global_step # TODO 取消hard-code式保存最佳指标 best_global_step = 0 best_valid_loss = float('inf') best_test_loss = float('inf') global_step = optimizer._step patience = args.patience if args.patience else float('inf') no_improve = 0 for epoch in range(N_EPOCHS): start_time = time.time() train_metrics = train(args, model, dataset['train_iterator'], optimizer, criterion, fields=dataset['fields'], writer=writer, pmi=train_pmi) global_step += len(dataset['train_iterator']) args.global_step = global_step valid_metrics = evaluate(args, model, dataset['valid_iterator'], criterion, fields=dataset['fields'], pmi=train_pmi) test_metrics = evaluate(args, model, dataset['test_iterator'], criterion, fields=dataset['fields'], pmi=train_pmi) end_time = time.time() epoch_mins, epoch_secs = epoch_time(start_time, end_time) print( f'Epoch: {epoch + 1:02} | Global step: {global_step} | Time: {epoch_mins}m {epoch_secs}s' ) for metrics, mode in zip([train_metrics, valid_metrics, test_metrics], ['Train', 'Valid', 'Test']): print_metrics(metrics, mode=mode) # TODO 优化存储logfile,取消hard-code模式 if args.save: write_metrics_to_writer(valid_metrics, writer, global_step, mode='Valid') write_metrics_to_writer(test_metrics, writer, global_step, mode='Test') best_valid_loss = valid_metrics['epoch_loss'] if valid_metrics[ 'epoch_loss'] < best_valid_loss else best_valid_loss best_test_loss = test_metrics['epoch_loss'] if test_metrics[ 'epoch_loss'] < best_test_loss else best_test_loss best_global_step = global_step if valid_metrics[ 'epoch_loss'] == best_valid_loss else best_global_step if best_global_step == global_step: torch.save( model, os.path.join(save_dir, f'model_global_step-{global_step}.pt')) # torch.save(model.state_dict(), os.path.join(save_dir, f'model_global_step-{global_step}.pt')) no_improve = 0 else: no_improve += 1 with open( os.path.join(save_dir, f'log_global_step-{global_step}.txt'), 'w') as log_file: valid_metrics['Best Global Step'] = best_global_step valid_metrics['Best Loss'] = best_valid_loss test_metrics['Best Loss'] = best_test_loss test_metrics['Best PPL'] = math.exp(best_test_loss) inference(args, model, dataset['valid_iterator'], fields=dataset['fields'], mode='valid', pmi=train_pmi) inference(args, model, dataset['test_iterator'], fields=dataset['fields'], mode='test', pmi=train_pmi) valid_path_hyp = os.path.join(args.save_dir, 'responses-valid.txt') test_path_hyp = os.path.join(args.save_dir, 'responses-test.txt') valid_path_ref = os.path.join(args.save_dir, 'answers-valid.txt') test_path_ref = os.path.join(args.save_dir, 'answers-test.txt') other_valid_metrics = calc_metrics(path_refs=valid_path_ref, path_hyp=valid_path_hyp) other_test_metrics = calc_metrics(path_refs=test_path_ref, path_hyp=test_path_hyp) valid_metrics.update(other_valid_metrics) test_metrics.update(other_test_metrics) os.remove(os.path.join(args.save_dir, 'posts-valid.txt')) os.remove(os.path.join(args.save_dir, 'posts-test.txt')) os.remove(valid_path_hyp) os.remove(valid_path_ref) os.remove(test_path_hyp) os.remove(test_path_ref) for metric, performance in valid_metrics.items(): log_file.write(f'Valid {metric}: {performance}\n') for metric, performance in test_metrics.items(): log_file.write(f'Test {metric}: {performance}\n') if no_improve >= patience: break
# go forward car_controls.throttle = INITIAL_THROTTLE car_controls.steering = 0 client.setCarControls(car_controls) # Load saved training params as ordinary NumPy W, b = pickle.load(open('params.pkl', 'rb')) with tf.Graph().as_default(): # Placeholder for an image x = tf.placeholder('float', [None, IMGSIZE]) # Our inference engine, intialized with weights we just loaded output = inference(x, IMGSIZE, 2, W, b) # TensorFlow initialization boilerplate sess = tf.Session() init_op = tf.global_variables_initializer() sess.run(init_op) # Once the brakes come on, we need to keep them on for a while before exiting; otherwise, # the vehicle will resume moving. brakingCount = 0 # Loop until we detect a collision while True: # Get RGBA camera images from the car responses = client.simGetImages(
channels = features['channels'] decoded_image = tf.decode_raw(image, tf.uint8) decoded_image = tf.reshape(decoded_image, [64, 64, 3]) # image_size = 130 min_after_dequeue = 100 capacity = 1000 + 3 * batch_size image_batch, label_batch = tf.train.shuffle_batch( [decoded_image, label], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue) image_batch = tf.cast(image_batch, tf.float32) y = train.inference(image_batch) correct_prediction = tf.equal(tf.argmax(y, 1), label_batch) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: tf.local_variables_initializer().run() init_op = tf.global_variables_initializer() sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) ckpt = tf.train.get_checkpoint_state(train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path:
# 引数チェック if len(sys.argv) != 2: sys.stderr.write("usage: label_face.py <filename>\n") sys.exit(-1) # 動画を開いてファイル名を取得 input = cv2.VideoCapture(sys.argv[1]) basename, ext = os.path.splitext(os.path.basename(sys.argv[1])) # placeholder用意 images_placeholder = tf.placeholder( tf.float32, shape=[None, IMAGE_HEIGHT_PX * IMAGE_WIDTH_PX * IMAGE_COLOR_CHANNELS]) keep_prob = tf.placeholder(tf.float32) # モデル生成 logits = inference(images_placeholder, keep_prob) # TenforFlowセッション開始 sess = tf.InteractiveSession() # 変数初期化 sess.run(tf.global_variables_initializer()) # モデルの読み込み saver = tf.train.Saver() saver.restore(sess, './model/model.ckpt') # ラベル label = ['Shiki', 'Shuko', 'Kanade', 'Frederica', 'Mika'] width = input.get(cv2.CAP_PROP_FRAME_WIDTH) height = input.get(cv2.CAP_PROP_FRAME_HEIGHT) fps = input.get(cv2.CAP_PROP_FPS)
import sys from train import inference, model_ import tensorlayer as tl """ Trained with 50 epochs and a batch of 32 """ # When file is ran if __name__ == "__main__": # Trying to load model to get response try: load_weights = tl.files.load_npz(name='model.npz') tl.files.assign_weights(load_weights, model_) except Exception as e: print(e) print('AI not connected') # Creating while loop while True: text = input("> ") if text == 'exit': break top_n = 1 for i in range(top_n): sentence = inference(text, top_n) print(" >", ' '.join(sentence))
MOVING_AVERAGE_DECAY = 0.99 EVAL_INTERVAL_SECS = 10 c = 0 cap = cv2.VideoCapture('data/bend/daria_bend.avi') while (cap.isOpened()): c += 1 ret, frame = cap.read() if (ret == 0): break img0 = cv2.resize(frame, (128, 128)) with tf.Graph().as_default() as g: img2 = tf.cast(img0, tf.float32) img3 = tf.reshape(img2, (1, 128, 128, 3)) logit = train.inference(img3) qq = tf.nn.softmax(logit) maxa = tf.argmax(logit, 1) q = qq[0][maxa[0]] variable_averages = tf.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: tf.local_variables_initializer().run() init_op = tf.global_variables_initializer() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split(