def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) count = 0 _t = {'im_detect': Timer(), 'misc': Timer()} keys, scores, bboxes, hdet, odet = [], [], [], [], [] for i in range(80): keys.append([]) scores.append([]) bboxes.append([]) hdet.append([]) odet.append([]) for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) im_detect(sess, net, image_id, Test_RCNN, keys, scores, bboxes, hdet, odet) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 pickle.dump(keys, open(output_dir + 'keys.pkl', 'wb')) pickle.dump(scores, open(output_dir + 'scores.pkl', 'wb')) pickle.dump(bboxes, open(output_dir + 'bboxes.pkl', 'wb')) pickle.dump(hdet, open(output_dir + 'hdet.pkl', 'wb')) pickle.dump(odet, open(output_dir + 'odet.pkl', 'wb'))
def test_net(sess, net, Test_RCNN, output_dir, object_thres, human_thres): np.random.seed(cfg.RNG_SEED) detection = {} count = 0 # timers _t = {'im_detect': Timer(), 'misc': Timer()} # glob.iglob函数获取一个可遍历对象,使用它可以逐个获取匹配的文件路径名 for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) #if image_id in all_remaining: # im_detect_remaining(sess, net, image_id, Test_RCNN, object_thres, human_thres, detection) # print('dealing with remaining image') #else: # im_detect(sess, net, image_id, Test_RCNN, object_thres, human_thres, detection) im_detect(sess, net, image_id, Test_RCNN, object_thres, human_thres, detection) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 # 把 detection的结果存入pickle文件中 pickle.dump(detection, open(output_dir, "wb"))
def test_net(sess, net, Test_RCNN, prior_mask, Action_dic_inv, output_dir, object_thres, human_thres, prior_flag): np.random.seed(cfg.RNG_SEED) detection = [] count = 0 # timers _t = {'im_detect': Timer(), 'misc': Timer()} for line in open(cfg.DATA_DIR + '/' + '/v-coco/data/splits/vcoco_test.ids', 'r'): _t['im_detect'].tic() image_id = int(line.rstrip()) im_detect(sess, net, image_id, Test_RCNN, prior_mask, Action_dic_inv, object_thres, human_thres, prior_flag, detection) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 4946, _t['im_detect'].average_time)) count += 1 pickle.dump(detection, open(output_dir, "wb"))
def test_net(sess, net, Test_RCNN, prior_mask, Action_dic_inv, img_dir, output_dir, object_thres, human_thres, prior_flag): np.random.seed(cfg.RNG_SEED) detection = [] count = 0 # timers _t = {'im_detect': Timer(), 'misc': Timer()} for im_file in glob.glob(img_dir + "*.png"): _t['im_detect'].tic() image_id = im_file.split('/')[-1] im_detect(sess, net, img_dir, image_id, Test_RCNN, prior_mask, Action_dic_inv, object_thres, human_thres, prior_flag, detection) _t['im_detect'].toc() print('im_detect: {:d} {:.3f}s'.format(count + 1, _t['im_detect'].average_time)) count += 1 pickle.dump(detection, open(output_dir, "wb"))
def test_net(sess, net, Test_RCNN, prior_mask, Action_dic_inv, image_file, output_dir, object_thres, human_thres, prior_flag): np.random.seed(cfg.RNG_SEED) detection = [] count = 0 # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} # for im_file in glob.glob(img_dir + "*.png"): #png _t['im_detect'].tic() # image_id = im_file.split('/')[-1] im_detect(sess, net, image_file, Test_RCNN, prior_mask, Action_dic_inv, object_thres, human_thres, prior_flag, detection) # print('detection: ', detection) _t['im_detect'].toc() print('im_detect: {:.3f}s'.format( _t['im_detect'].average_time)) # count += 1 return detection
def test_net(sess, net, Test_RCNN, output_dir, object_thres, human_thres): np.random.seed(cfg.RNG_SEED) detection = {} count = 0 # timers _t = {'im_detect': Timer(), 'misc': Timer()} for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) im_detect(sess, net, image_id, Test_RCNN, object_thres, human_thres, detection) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 pickle.dump(detection, open(output_dir, "wb"))
def test_net_data_api1(sess, net, output_dir, h_box, o_box, o_cls, h_score, o_score, image_id): detection = {} verb_to_HO_matrix, obj_to_HO_matrix = get_convert_matrix() # prediction_HO = net.test_image_HO(sess, im_orig, blobs) # timers ones = np.ones([1, 600], np.float32) _t = {'im_detect': Timer(), 'misc': Timer()} last_img_id = -1 count = 0 fuse_res = tf.constant(0) obj_scores = tf.constant(0) objid = tf.constant(0) obj_scores = tf.constant(0) _t['im_detect'].tic() while True: _t['im_detect'].tic() from tensorflow.python.framework.errors_impl import InvalidArgumentError try: pH, pO, pSp, pVerbs, pSpHO, pFuse, f_obj_score, f_obj_cls, _h_box, _o_box, _o_cls, _h_score, _o_score, _image_id = sess.run([ net.predictions["cls_prob_H"] if 'cls_prob_H' in net.predictions else h_box, net.predictions["cls_prob_O"] if 'cls_prob_O' in net.predictions else h_box, net.predictions["cls_prob_sp"] if 'cls_prob_sp' in net.predictions else h_box, net.predictions["cls_prob_hoi"] if 'cls_prob_hoi' in net.predictions else h_box, net.predictions["cls_prob_spverbs"] if 'cls_prob_spverbs' in net.predictions else h_box, fuse_res if 'cls_prob_sp' in net.predictions else h_box, obj_scores, objid, h_box, o_box, o_cls, h_score, o_score, image_id]) # print(pFuse.shape, f_obj_score.shape, f_obj_cls.shape) except InvalidArgumentError as e: # cls_prob_HO = np.zeros(shape=[blobs['sp'].shape[0], self.num_classes]) raise e except tf.errors.OutOfRangeError: print('END') break # if net.model_name.__contains__('cosine'): # temp = [[_h_box[i], _o_box[i], _o_cls[i], 0, _h_score[i], _o_score[i], pH[i], pO[i], pSp[i], pVerbs[i], pSpHO[i], pFuse[i], f_obj_score[i], f_obj_cls[i]] for i in range(len(_h_box))] # else: temp = [[_h_box[i], _o_box[i], _o_cls[i], 0, _h_score[i], _o_score[i], pH[i], pO[i], pSp[i], pVerbs[i], pSpHO[i]] for i in range(len(_h_box))] # detection[_image_id] = temp if _image_id in detection: detection[_image_id].extend(temp) else: detection[_image_id] = temp _t['im_detect'].toc() count += 1 print('\rmodel: {} im_detect: {:d}/{:d} {:d}, {:.3f}s'.format(net.model_name, count, 10566, _image_id, _t['im_detect'].average_time), end='', flush=True) # TODO remove # pickle.dump(detection, open('test_new.pkl', 'wb')) pickle.dump(detection, open(output_dir, "wb")) del detection import gc gc.collect()
def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) keys, scores_HO, bboxes, hdet, odet = [], [], [], [], [] for i in range(80): keys.append([]) scores_HO.append([]) bboxes.append([]) hdet.append([]) odet.append([]) count = 0 _t = {'im_detect': Timer(), 'misc': Timer()} for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) im_detect(sess, net, image_id, Test_RCNN, keys, scores_HO, bboxes, hdet, odet) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 for i in range(80): scores_HO[i] = np.concatenate(scores_HO[i], axis=0) bboxes[i] = np.concatenate(bboxes[i], axis=0) keys[i] = np.array(keys[i]) hdet[i] = np.array(hdet[i]) odet[i] = np.array(odet[i]) map, mrec = get_map(keys, scores_HO, bboxes, hdet, odet) if not os.path.exists(output_dir): os.mkdir(output_dir) pickle.dump({ 'ap': map, 'rec': mrec }, open(output_dir + 'detail_HO.pkl', 'wb')) with open(output_dir + 'result_HO.txt', 'w') as f: f.write('total ap: %.4f rec: %.4f \n' % (float(np.mean(map)), float(np.mean(mrec)))) f.write( 'rare ap: %.4f rec: %.4f \n' % (float(np.mean(map[cnt < 10])), float(np.mean(mrec[cnt < 10])))) f.write( 'non-rare ap: %.4f rec: %.4f \n' % (float(np.mean(map[cnt >= 10])), float(np.mean(mrec[cnt >= 10]))))
def test_net_data_fcl(sess, net, output_dir, h_box, o_box, o_cls, h_score, o_score, image_id): detection = {} # prediction_HO = net.test_image_HO(sess, im_orig, blobs) # timers _t = {'im_detect': Timer(), 'misc': Timer()} last_img_id = -1 count = 0 _t['im_detect'].tic() while True: _t['im_detect'].tic() from tensorflow.python.framework.errors_impl import InvalidArgumentError try: pH, pO, pSp, pVerbs, _h_box, _o_box, _o_cls, _h_score, _o_score, _image_id = sess.run([ net.predictions["cls_prob_H"] if 'cls_prob_H' in net.predictions else h_box, # from previous work net.predictions["cls_prob_O"] if 'cls_prob_O' in net.predictions else h_box, net.predictions["cls_prob_sp"] if 'cls_prob_sp' in net.predictions else h_box, net.predictions["cls_prob_verbs"] if 'cls_prob_verbs' in net.predictions else h_box, h_box, o_box, o_cls, h_score, o_score, image_id]) except InvalidArgumentError as e: # cls_prob_HO = np.zeros(shape=[blobs['sp'].shape[0], self.num_classes]) if net.model_name.__contains__('lamb'): print('InvalidArgumentError', sess.run([image_id])) continue else: raise e except tf.errors.OutOfRangeError: print('END') break # additional predictions are for ablated study. temp = [[_h_box[i], _o_box[i], _o_cls[i], 0, _h_score[i], _o_score[i], pH[i], pO[i], pSp[i], pVerbs[i], 0] for i in range(len(_h_box))] # detection[_image_id] = temp if _image_id in detection: detection[_image_id].extend(temp) else: detection[_image_id] = temp _t['im_detect'].toc() count += 1 print('\rmodel: {} im_detect: {:d}/{:d} {:d}, {:.3f}s'.format(net.model_name, count, 10566, _image_id, _t['im_detect'].average_time), end='', flush=True) if net.model_name.__contains__('_debugrl_') or 'DEBUG_NET' in os.environ: if count >= 1: print(temp) break pickle.dump(detection, open(output_dir, "wb")) del detection import gc gc.collect()
def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) bboxes, binary, keys = [], [], [] binary_score_all = {} for i in range(80): keys.append([]) binary.append([]) bboxes.append([]) count = 0 _t = {'im_detect': Timer(), 'misc': Timer()} for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) im_detect(sess, net, image_id, Test_RCNN, keys, binary, bboxes, binary_score_all) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 if not os.path.exists(output_dir): os.mkdir(output_dir) map, mrec = Generate_NIS_map(bboxes, binary, keys, output_dir) pickle.dump({ 'ap': map, 'rec': mrec }, open(output_dir + '/eval_nis.pkl', 'wb')) pickle.dump(binary_score_all, open(output_dir + '/binary_score_all.pkl', 'wb')) modes = ['pos_', 'neg_', 'pos-neg_'] for i in range(3): with open(output_dir + '/' + modes[i] + 'result.txt', 'w') as f: f.write('%14s, ap: %.4f rec: %.4f \n' % ('Summary_' + modes[i], float(np.mean( map[i])), float(np.mean(mrec[i])))) for j in range(80): f.write('%14s, ap: %.4f rec: %.4f \n' % (obj_name[j], float(map[i, j]), float(mrec[i, j]))) print("%14s, mAP: %.4f, mRec: %.4f" % (modes[i][:-1], float(np.mean(map[i])), float(np.mean(mrec[i]))))
def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) detection = {} count = 0 _t = {'im_detect' : Timer(), 'misc' : Timer()} keys, scores_P, scores_A, scores_L, bboxes, hdet, odet = [], [], [], [], [], [], [] for i in range(80): keys.append([]) scores_P.append([]) scores_A.append([]) scores_L.append([]) bboxes.append([]) hdet.append([]) odet.append([]) for line in glob.iglob(cfg.DATA_DIR + '/' + 'hico_20160224_det/images/test2015/*.jpg'): _t['im_detect'].tic() image_id = int(line[-9:-4]) im_detect(sess, net, image_id, Test_RCNN, keys, scores_P, scores_A, scores_L, bboxes, hdet, odet) # im_detect(sess, net, image_id, Test_RCNN, keys, scores, bboxes, hdet, odet) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format(count + 1, 9658, _t['im_detect'].average_time)) count += 1 for i in range(80): scores_P[i] = np.concatenate(scores_P[i], axis=0) scores_A[i] = np.concatenate(scores_A[i], axis=0) scores_L[i] = np.concatenate(scores_L[i], axis=0) bboxes[i] = np.concatenate(bboxes[i], axis=0) keys[i] = np.array(keys[i]) hdet[i] = np.array(hdet[i]) odet[i] = np.array(odet[i]) if not os.path.exists(output_dir): os.mkdir(output_dir) pickle.dump(scores_P, open(os.path.join(output_dir, 'scores_P.pkl'), 'wb')) pickle.dump(scores_A, open(os.path.join(output_dir, 'scores_A.pkl'), 'wb')) pickle.dump(scores_L, open(os.path.join(output_dir, 'scores_L.pkl'), 'wb')) pickle.dump(bboxes, open(os.path.join(output_dir, 'bboxes.pkl'), 'wb')) pickle.dump(keys, open(os.path.join(output_dir, 'keys.pkl'), 'wb')) pickle.dump(hdet, open(os.path.join(output_dir, 'hdet.pkl'), 'wb')) pickle.dump(odet, open(os.path.join(output_dir, 'odet.pkl'), 'wb'))
def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) detection = {} count = 0 _t = {'im_detect': Timer(), 'misc': Timer()} for key in Test_RCNN.keys(): _t['im_detect'].tic() im_detect(sess, net, key, Test_RCNN, detection) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, 9658, _t['im_detect'].average_time)) count += 1 pickle.dump(detection, open(output_dir, "wb"))
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) sess.run(tf.global_variables_initializer()) if cfg.TRAIN_MODULE_CONTINUE == 1: self.from_previous_ckpt(sess) else: if cfg.TRAIN_INIT_WEIGHT == 1: self.from_snapshot(sess) elif cfg.TRAIN_INIT_WEIGHT == 2: self.from_previous_ckpt(sess) elif cfg.TRAIN_INIT_WEIGHT == 3: self.from_best_trained_model(sess) sess.graph.finalize() timer = Timer() Data_length = len(self.Trainval_GT) keys = self.Trainval_GT.keys() idx = range(Data_length) if cfg.TRAIN_MODULE_CONTINUE == 2: iter = 0 elif cfg.TRAIN_MODULE_CONTINUE == 1: path_iter = self.pretrained_model.split('.ckpt')[0] iter_num = path_iter.split('_')[-1] iter = int(iter_num) + 1 while iter < max_iters + 1: timer.tic() if iter % Data_length == 0: np.random.shuffle(idx) image_id = keys[idx[iter % Data_length]] blobs = Get_Next_Instance_HO_Neg_HICO_3D(self.Trainval_GT, self.Trainval_N, image_id, self.Pos_augment, self.Neg_select) if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): total_loss, summary = self.net.train_step_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: total_loss = self.net.train_step(sess, blobs, lr.eval(), train_op) del blobs timer.toc() if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %6u, total loss: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time)) if (iter % cfg.TRAIN.SNAPSHOT_ITERS == 0 and iter != 0) or (iter == 10): self.snapshot(sess, iter) iter += 1 self.writer.close()
def test_net(sess, net, Test_RCNN, output_dir): np.random.seed(cfg.RNG_SEED) detection = [] count = 0 _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(len(Test_RCNN)): _t['im_detect'].tic() im_detect(sess, net, i, Test_RCNN, detection) _t['im_detect'].toc() print('im_detect: {:d}/{:d} {:.3f}s'.format( count + 1, len(Test_RCNN), _t['im_detect'].average_time)) count += 1 with open(output_dir, 'w') as csvfile: writer = csv.writer(csvfile) writer.writerows(detection)
def train_model_tf(self, sess, max_iters): lr, train_op = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() # Data_length = len(self.Trainval_GT) iter = self.get_init_step() while iter < max_iters + 1: timer.tic() blobs = {} from tensorflow.python.framework.errors_impl import InvalidArgumentError try: if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, image_id, summary = self.net.train_step_tfr_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, image_id = self.net.train_step_tfr( sess, blobs, lr.eval(), train_op) except InvalidArgumentError as e: print('InvalidArgumentError') image_id = -1 total_loss = 0 if self.net.model_name.__contains__('lamb'): print('InvalidArgumentError', image_id) else: raise e timer.toc() # print(image_id) # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: if type(image_id) == tuple: image_id = image_id[0] out_str = 'iter: %d / %d, im_id: %u, total loss: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time) print(out_str, end='\r', flush=True) # Snapshotting if (iter % cfg.TRAIN.SNAPSHOT_ITERS == 0 and iter != 0) or (iter == 10): # self.net.test_ self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() Data_length = len(self.Trainval_GT) iter = 0 while iter < max_iters + 1: timer.tic() if self.iCAN_Early_flag == 1: blobs = Get_Next_Instance_HO_Neg(self.Trainval_GT, self.Trainval_N, iter, self.Pos_augment, self.Neg_select, Data_length) if self.iCAN_Early_flag == 0: # Pos + spNeg (factorized model only) blobs = Get_Next_Instance_HO_spNeg(self.Trainval_GT, self.Trainval_N, iter, self.Pos_augment, self.Neg_select, Data_length) if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary loss_cls_H, loss_cls_HO, total_loss, summary = self.net.train_step_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary loss_cls_H, loss_cls_HO, total_loss = self.net.train_step( sess, blobs, lr.eval(), train_op) timer.toc() # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %u, total loss: %.6f, loss_cls_H: %.6f, loss_cls_HO: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, self.Trainval_GT[iter%Data_length][0], total_loss, loss_cls_H, loss_cls_HO, lr.eval(), timer.average_time)) # Snapshotting if (iter % cfg.TRAIN.SNAPSHOT_ITERS == 0 and iter != 0) or (iter == 10): self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): print('train: ', self.net.model_name) lr, train_op, train_op_g, D_loss, g_loss = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() # Data_length = len(self.Trainval_GT) iter = 0 self.train_model_stepwise_inner(D_loss, g_loss, iter, lr, max_iters, sess, timer, train_op, train_op_g) self.writer.close()
def train_model(self, sess, max_iters): lr, train_op, t_loss = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() import logging logging.basicConfig( filename='/home/zhou9878/{}.log'.format(self.net.model_name), level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) iter = self.get_init_step() while iter < max_iters + 1: timer.tic() if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary # total_loss, image_id, summary = self.net.train_step_tfr_with_summary(sess, blobs, lr, train_op) total_loss, summary, image_id, _ = sess.run( [t_loss, self.net.summary_op, self.net.image_id, train_op]) # total_loss, summary = self.net.train_step_with_summary(sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, image_id, _ = sess.run( [t_loss, self.net.image_id, train_op]) # total_loss, image_id = self.net.train_step_tfr(sess, blobs, lr, train_op) timer.toc() # print(image_id) # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: if type(image_id) == tuple or (type(image_id) != np.int32 and len(image_id) > 1): image_id = image_id[0] # print('iter: {:d} / {:d}, im_id: {:d}, total loss: {:.6f}, lr: {:f}, speed: {:.3f} s/iter'.format( # iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time), end='\n', flush=True) logger.info( 'iter: {:d} / {:d}, im_id: {:d}, total loss: {:.6f}, lr: {:f}, speed: {:.3f} s/iter' .format(iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time)) # print('\rmodel: {} im_detect: {:d}/{:d} {:d}, {:.3f}s'.format(net.model_name, count, 15765, _image_id, # _t['im_detect'].average_time), end='', # flush=True) # Snapshotting self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op, D_loss, train_op_g, g_loss = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() # Data_length = len(self.Trainval_GT) iter = self.get_init_step() self.train_model_stepwise_inner(D_loss, g_loss, iter, lr, max_iters, sess, timer, train_op, train_op_g) # elif self.net.model_name.__contains__('epic3'): # self.train_model_epic3_inner(D_loss, g_loss, iter, lr, max_iters, sess, timer, train_op, train_op_g) self.writer.close()
def train_model(self, sess, max_iters): if 'CUDA_VISIBLE_DEVICES' not in os.environ or len( os.environ['CUDA_VISIBLE_DEVICES'].split(',')) == 1: lr, train_op, t_loss = self.construct_graph2(sess) else: lr, train_op, t_loss = self.construct_graph2(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() import logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # Data_length = len(self.Trainval_GT) iter = self.get_init_step() while iter < max_iters + 1: timer.tic() blobs = {} from tensorflow.python.framework.errors_impl import InvalidArgumentError if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, summary, image_id, _ = sess.run( [t_loss, self.net.summary_op, self.net.image_id, train_op]) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, image_id, _ = sess.run( [t_loss, self.net.image_id, train_op]) timer.toc() # print(image_id) # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: if type(image_id) == tuple: image_id = image_id[0] logger.info( 'iter: {:d} / {:d}, im_id: {:d}, total loss: {:.6f}, lr: {:f}, speed: {:.3f} s/iter' .format(iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time)) # Snapshotting self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op, t_loss = self.construct_graph2(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() # Data_length = len(self.Trainval_GT) iter = self.get_init_step() while iter < max_iters + 1: timer.tic() if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary # total_loss, image_id, summary = self.net.train_step_tfr_with_summary(sess, blobs, lr, train_op) total_loss, summary, image_id, _ = sess.run( [t_loss, self.net.summary_op, self.net.image_id, train_op]) # total_loss, summary = self.net.train_step_with_summary(sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, image_id, _ = sess.run( [t_loss, self.net.image_id, train_op]) # total_loss, image_id = self.net.train_step_tfr(sess, blobs, lr, train_op) timer.toc() # print(image_id) # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: if type(image_id) == tuple: image_id = image_id[0] print( 'iter: {:d} / {:d}, im_id: {:d}, total loss: {:.6f}, lr: {:f}, speed: {:.3f} s/iter' .format(iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time), end='\r', flush=True) # print('\rmodel: {} im_detect: {:d}/{:d} {:d}, {:.3f}s'.format(net.model_name, count, 15765, _image_id, # _t['im_detect'].average_time), end='', # flush=True) # Snapshotting self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) self.from_snapshot(sess) sess.graph.finalize() timer = Timer() # Data_length = len(self.Trainval_GT) iter = 0 import logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) while iter < max_iters + 1: timer.tic() blobs = {} if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, image_id, summary = self.net.train_step_tfr_with_summary( sess, blobs, lr, train_op) # total_loss, summary = self.net.train_step_with_summary(sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, image_id = self.net.train_step_tfr( sess, blobs, lr, train_op) timer.toc() # print(image_id) # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: if type(image_id) == tuple: image_id = image_id[0] logger.info( 'iter: {:d} / {:d}, im_id: {:d}, total loss: {:.6f}, lr: {:f}, speed: {:.3f} s/iter' .format(iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time)) # Snapshotting t_iter = iter self.snapshot(sess, t_iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) if cfg.TRAIN_MODULE_CONTINUE == 1: self.from_previous_ckpt(sess) else: if cfg.TRAIN_INIT_WEIGHT == 2: # load all params self.from_best_trained_model(sess) elif cfg.TRAIN_INIT_WEIGHT == 1: self.from_snapshot(sess) # load from snapshot elif cfg.TRAIN_INIT_WEIGHT == 3: # load all params, initial from our best, including pvp self.from_previous_ckpt(sess) else: raise NotImplemented sess.graph.finalize() timer = Timer() Data_length = len(self.Trainval_GT) keys = self.Trainval_GT.keys() path_iter = self.pretrained_model.split('.ckpt')[0] iter_num = path_iter.split('_')[-1] if cfg.TRAIN_MODULE_CONTINUE == 2: iter = 0 elif cfg.TRAIN_MODULE_CONTINUE == 1: iter = int(iter_num) + 1 while iter < max_iters + 1: timer.tic() image_id = keys[iter % Data_length] blobs = Get_Next_Instance_Verb_AVA_transfer( self.Trainval_GT, image_id, self.Pos_augment) if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, summary = self.net.train_step_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss = self.net.train_step(sess, blobs, lr.eval(), train_op) del blobs timer.toc() # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %s, total loss: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, self.Trainval_GT[image_id][0][0], total_loss, lr.eval(), timer.average_time)) # Snapshotting if (iter % cfg.TRAIN.SNAPSHOT_ITERS == 0 and iter != 0) or (iter == 10): self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) if cfg.TRAIN_MODULE_CONTINUE == 1: self.from_previous_ckpt(sess) else: if cfg.TRAIN_INIT_WEIGHT == 2: self.from_best_trained_model(sess) if cfg.TRAIN_INIT_WEIGHT == 1: self.from_snapshot(sess) if cfg.TRAIN_INIT_WEIGHT == 3: # load all paras including D, initial from our best self.from_best_trained_model(sess) sess.graph.finalize() timer = Timer() path_iter = self.pretrained_model.split('.ckpt')[0] iter_num = path_iter.split('_')[-1] if cfg.TRAIN_MODULE_CONTINUE == 2: iter = 0 if cfg.TRAIN_MODULE_CONTINUE == 1: iter = int(iter_num) Data_length = len(self.Trainval_GT) idx = range(Data_length) np.random.shuffle(idx) while iter < max_iters + 1: timer.tic() if iter % Data_length == 0: np.random.shuffle(idx) image_id = idx[iter % Data_length] blobs = Get_Next_Instance_HO_Neg_HICO_pose_pattern_version2(self.Trainval_GT, self.Trainval_N, image_id, self.Pos_augment, self.Neg_select, Data_length) if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, summary = self.net.train_step_with_summary(sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss = self.net.train_step(sess, blobs, lr.eval(), train_op) timer.toc() # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %u, total loss: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, image_id, total_loss, lr.eval(), timer.average_time)) # Snapshotting if (iter % cfg.TRAIN.SNAPSHOT_ITERS * 5 == 0 and iter != 0) or (iter == 10): self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): lr, train_op = self.construct_graph(sess) if cfg.TRAIN_MODULE_CONTINUE == 1: self.from_previous_ckpt(sess) else: if cfg.TRAIN_INIT_WEIGHT == 2: self.from_best_trained_model(sess) if cfg.TRAIN_INIT_WEIGHT == 1: self.from_snapshot(sess) sess.graph.finalize() timer = Timer() Data_length = len(self.Trainval_GT) path_iter = self.pretrained_model.split('.ckpt')[0] iter_num = path_iter.split('_')[-1] if cfg.TRAIN_MODULE_CONTINUE == 2: iter = 0 if cfg.TRAIN_MODULE_CONTINUE == 1: iter = int(iter_num) while iter < max_iters + 1: timer.tic() if self.Early_flag == 1: blobs = Get_Next_Instance_HO_Neg_pose_pattern_version2( self.Trainval_GT, self.Trainval_N, iter, self.Pos_augment, self.Neg_select, Data_length) if self.Early_flag == 0: # Pos + spNeg (factorized model only) blobs = Get_Next_Instance_HO_spNeg_pose_pattern_version2( self.Trainval_GT, self.Trainval_N, iter, self.Pos_augment, self.Neg_select, Data_length) if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): loss_cls_H, loss_cls_HO, total_loss, summary = self.net.train_step_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: loss_cls_H, loss_cls_HO, total_loss = self.net.train_step( sess, blobs, lr.eval(), train_op) timer.toc() # Display training information if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %u, total loss: %.6f, loss_cls_H: %.6f, loss_cls_HO: %.6f, lr: %f, speed: %.3f s/iter' % \ (iter, max_iters, self.Trainval_GT[iter%Data_length][0][0], total_loss, loss_cls_H, loss_cls_HO, lr.eval(), timer.average_time)) # Snapshotting if (iter % cfg.TRAIN.SNAPSHOT_ITERS == 0 and iter != 0) or (iter == 10): self.snapshot(sess, iter) iter += 1 self.writer.close()
def train_model(self, sess, max_iters): timer = Timer() Data_length = len(self.Trainval_GT) lr, train_op = self.construct_graph(sess) # 加载初始的模型参数 if cfg.TRAIN_MODULE_CONTINUE == 1: # continue training self.from_previous_ckpt(sess) else: # from iter 0 ,默认是这个 # Initializing weight: 1--from faster RCNN 2--from previous best 3--from our model with d if cfg.TRAIN_INIT_WEIGHT == 2: self.from_best_trained_model(sess) elif cfg.TRAIN_INIT_WEIGHT == 1: self.from_snapshot(sess) elif cfg.TRAIN_INIT_WEIGHT == 3: # load all paras including D, initial from our best self.from_previous_ckpt(sess) # 将图变为只读(read-only),新的操作就不能够添加到图里了 sess.graph.finalize() # 获取模型当前的iter值 if cfg.TRAIN_MODULE_CONTINUE == 2: # from iter 0 ,默认是这个 iter = 0 elif cfg.TRAIN_MODULE_CONTINUE == 1: # from iter_ckpt path_iter = self.pretrained_model.split('.ckpt')[0] iter_num = path_iter.split('_')[-1] iter = int(iter_num) cur_min = 10 # 执行max_iters次梯度迭代 while iter < max_iters + 1: timer.tic() # 获取增强后的一张图片的信息 blobs = Get_Next_Instance_HO_Neg_HICO_pose_pattern_version2( self.Trainval_GT, self.Trainval_N, iter, self.Pos_augment, self.Neg_select, Data_length) # 执行一次梯度下降 # train_step_with_summary传入lr是为了记录lr的summary if (iter % cfg.TRAIN.SUMMARY_INTERVAL == 0) or (iter < 20): # Compute the graph with summary total_loss, base_loss, binary_loss, part_loss, summary = self.net.train_step_with_summary( sess, blobs, lr.eval(), train_op) self.writer.add_summary(summary, float(iter)) else: # Compute the graph without summary total_loss, base_loss, binary_loss, part_loss = self.net.train_step( sess, blobs, lr.eval(), train_op) timer.toc() # 打印训练信息 if iter % (cfg.TRAIN.DISPLAY) == 0: print('iter: %d / %d, im_id: %u, lr: %f, speed: %.3f s/iter\ntotal loss: %.6f\nbase loss: %.6f\nbinary loss: %.6f\npart loss: %.6f' % \ (iter, max_iters, self.Trainval_GT[iter%Data_length][0][0], lr.eval(), timer.average_time, total_loss, base_loss, binary_loss, part_loss)) # 保存模型 if (iter % cfg.TRAIN.SNAPSHOT_ITERS * 5 == 0 and iter != 0) or ( iter == 10) or (iter > 1000 and total_loss < cur_min - 0.0001): if (iter > 1000 and total_loss < cur_min - 0.0001): cur_min = total_loss self.snapshot(sess, iter, total_loss, base_loss, binary_loss, part_loss) # 更新迭代器 iter += 1 self.writer.close()
augment_type=-1, large_neg_for_ho=large_neg_for_ho) net.set_ph(image, image_id, num_pos, Human_augmented, Object_augmented, action_HO, sp) net.init_verbs_objs_cls() net.create_architecture(False) saver = tf.train.Saver() saver.restore(sess, weight) print('Pre-trained weights loaded.') detection = {} # prediction_HO = net.test_image_HO(sess, im_orig, blobs) # timers _t = {'im_detect': Timer(), 'misc': Timer()} last_img_id = -1 count = 0 img_id_list = [] O_list = [] V_list = [] A_list = [] result = {} result['img_id_list'] = [] result['O_list'] = [] result['V_list'] = [] result['A_list'] = [] # result['fake_O_list'] = [] # result['fake_O_all_list'] = [] while True: _t['im_detect'].tic()