def printoutput1(print_data): model=torch.load("./BestNet",map_location="cpu") if (use_gpu): model=model.to(device) unloader = transforms.ToPILImage() k=0; hists=[] ''' for sample in print_data.get_loader(): data=sample['image'] target=torch.cat(tuple(sample['label']),1) target=target.argmax(dim=1,keepdim=False) ''' for data,target in print_data.get_loader(): if (use_gpu): data=data.to(device) target=target.to(device) output=model(data) output2=output.cpu().clone(); output2 = torch.softmax(output2, dim=1).argmax(dim=1, keepdim=False) image2=target.cpu().clone() image2 = torch.softmax(image2, dim=1).argmax(dim=1, keepdim=False) for i in range(batch_size): k1=k%print_data.get_len(); k2=k//print_data.get_len(); path=output_path+'/'+print_data.get_namelist()[k1]+'_'+str(k2); if not os.path.exists(path): os.makedirs(path); image=data[i].cpu().clone(); image =unloader(image) image.save(path+'/'+print_data.get_namelist()[k1]+'.jpg',quality=100); image=output[i].cpu().clone(); image = torch.softmax(image, dim=0).argmax(dim=0, keepdim=False) #image=target[i].cpu().clone(); image=image.unsqueeze(dim=0); image=torch.zeros(9,64,64).scatter_(0, image, 255) for j in range(9): image3=unloader(np.uint8(image[j].numpy())) image3.save(path+'/'+print_data.get_namelist()[k1]+'lbl0'+str(j)+'.jpg',quality=100); k+=1 if (k>=print_data.get_len()):break #if (k>=print_data.get_len()):break hist = np.bincount(9 * image2.reshape([-1]) + output2.reshape([-1]),minlength=81).reshape(9, 9) hists.append(hist); if (k>=print_data.get_len()):break #if (k>=print_data.get_len()):break hists_sum=np.sum(np.stack(hists, axis=0), axis=0) tp=0; tpfn=0; tpfp=0; f1score=0.0; for i in range(1,9): tp+=hists_sum[i][i].sum() tpfn+=hists_sum[i,:].sum() tpfp+=hists_sum[:,i].sum() f1score=2*tp/(tpfn+tpfp) print('Printoutput F1 Score: {:.4f}\n'.format(f1score)) print("printoutput1 Finish");
def train(epoch): for batch_idx,sample in enumerate(train_data.get_loader()): ''' for i in range(batch_size): image=sample['image'][i].cpu().clone(); image=transforms.ToPILImage()(image).convert('RGB') plt.imshow(image); plt.show(block=True); ''' if (use_gpu): sample['image']=sample['image'].to(device) for i in range(len(sample['label'])): sample['label'][i]=sample['label'][i].to(device) optimizer.zero_grad() output=model(sample['image']) labels=sample['label'].argmax(dim=1,keepdim=False) ''' f = open("out.txt", "w") for i in range(64): for j in range(64): print(int(labels[0][i][j]),end=' ',file=f); print("",file=f); input('pause'); ''' if (use_gpu): labels=labels.to(device); loss=fun.cross_entropy(output,labels) if (batch_idx%250==0): print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(sample['image']), len(train_data.get_loader().dataset), 100. * batch_idx / len(train_data.get_loader()),loss.data)) loss.backward() optimizer.step()
def after_input(): categories = postgresql.select_category() conn = psycopg2.connect( host= 'yakpago-db-instance.ce3s5gihh4zd.ap-northeast-2.rds.amazonaws.com', dbname='pharmacy', user='******', password='******', port='5432') # db에 접속 cur = conn.cursor() cur.execute( "select pharmacy_name, pharmacy_address, pharmacy_tel ,longitude, latitude from pharmacy;" ) ex = cur.fetchmany(22914) if request.method == 'GET': return render_template('main.html', data=ex, categories=categories) elif request.method == 'POST': input_form = request.form result = model.model(input_form) print(result) result = postgresql.select_item_names(result['item_seq'].tolist()) # 머신러닝 모델 실행 후 추천 약품의 item_seq(약품 코드)와 item_name(약품명)을 딕셔너리 형태로 임시로 저장해 놓는 파일 생성. with open('static\\results.txt', 'w+t', encoding='utf-8') as f: json.dump(result, f, indent="\t", ensure_ascii=False) return render_template('main.html', data=ex, categories=categories, scroll='Result')
def recognition( frame, face_location_, name_index, emotion, is_detected, ): model = md.model() model.load_weights('model/model.h5') # 아는 while True: if is_detected.value == 1: start = time.time() rgb = frame.array.astype(np.uint8) bgr = rgb[:, :, ::-1] face_location = face_location_.array.astype(np.int16) face_reco( rgb, face_location, name_index, ) face_emo( rgb, face_location, model, emotion, ) # 여기 왜 비동기 처리가 안되는가 else: time.sleep(0.03) pass
def detect_face(img): """ Detects faces on an image :param img: instance of PIL.Image :return: True - if there are any face on image False - if there are none """ detect = model.model(img) if detect is not None: return True return False
def run(self, s): res = self.validate() if res['status']: tpd, op = res['results'][0], res['results'][1:] output = model(tpd, op) self.parameters = format_results(output, tpd) self.buiildTables() self.showImages() self.compute.setDisabled(True) self.reset.setEnabled(True) self.report.setEnabled(True) else: self.msgBox(res['err'])
def __init__(self, modelDir=None): # set GPU fraction gpuOptions = tf.GPUOptions( per_process_gpu_memory_fraction=cfg.GpuMemory) # allow tf to allocate device automatically conf = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpuOptions) self.sess = tf.Session(config=conf) #get result self.images, self.bn, self.outs, self.labels, self.bBoxes = model.model( self.sess) boxNum = self.labels.get_shape().as_list()[1] #calculate loss self.pos, self.neg, self.groundTruthLabels, self.groundTruthBBoxes, self.totalLoss, self.classLoss,\ self.bBoxesLoss = loss.loss(self.labels, self.bBoxes, boxNum) outShapes = [t.get_shape().as_list() for t in self.outs] cfg.outShapes = outShapes #generate default box cfg.defaults = addSSD.defaultBox(outShapes) #initialize train parameters with tf.variable_scope("optimizer"): self.global_step = tf.Variable(0) self.learningRate = tf.placeholder(tf.float32, shape=[]) self.optimizer = tf.train.AdamOptimizer(1e-3).minimize( self.totalLoss, global_step=self.global_step) newVars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="optimizer") self.sess.run(tf.variables_initializer(newVars)) if modelDir is None: modelDir = FLAGS.modelDir checkPoint = tf.train.get_checkpoint_state(modelDir) self.saver = tf.train.Saver() #if have checkPoint, restore checkPoint if checkPoint and checkPoint.model_checkpoint_path: self.saver.restore(self.sess, checkPoint.model_checkpoint_path) print("restored %s" % checkPoint.model_checkpoint_path)
def test(config, logger): print('load test data') if os.path.exists('maps.npy'): vocab, type_dict, label_dict = np.load('maps.npy') else: raise FileNotFoundError('can not find maps.npy') for k, v in config.items(): logger.info('config {}: {}'.format(k, v)) config['vocab'] = vocab config['type_dict'] = type_dict config['label_dict'] = label_dict config['word_num'] = len(vocab) config['type_num'] = len(type_dict) config['label_num'] = len(label_dict) config['position_num'] = config['pos_max'] test_datas = load_data_file('datas/test.json', zero=config['zero'], lower=config['lower']) test_data_loader = data_loader(test_datas, config) badcase = test_data_loader.load() logger.info('test datas: {}'.format(len(test_data_loader))) logger.info('test badcase: {}'.format(badcase)) with tf.Graph().as_default() as g: test_model = model(config, 'test') test_model.build(g) logger.info('***TEST***') test_score, test_accs = test_model.run_evaule(test_data_loader) parser_score(0, '', test_score, test_accs, logger, 'test') logger.info('******\n')
def __init__(self, modelDir = None): # set GPU fraction gpuOptions = tf.GPUOptions(per_process_gpu_memory_fraction = cfg.GpuMemory) # allow tf to allocate device automatically conf = tf.ConfigProto(allow_soft_placement = True, gpu_options = gpuOptions) self.sess = tf.Session(config = conf) #get result self.images, self.bn, self.outs, self.labels, self.bBoxes = model.model(self.sess) boxNum = self.labels.get_shape().as_list()[1] #calculate loss self.pos, self.neg, self.groundTruthLabels, self.groundTruthBBoxes, self.totalLoss, self.classLoss,\ self.bBoxesLoss = loss.loss(self.labels, self.bBoxes, boxNum) outShapes = [t.get_shape().as_list() for t in self.outs] cfg.outShapes = outShapes #generate default box cfg.defaults = addSSD.defaultBox(outShapes) #initialize train parameters with tf.variable_scope("optimizer"): self.global_step = tf.Variable(0) self.learningRate = tf.placeholder(tf.float32, shape=[]) self.optimizer = tf.train.AdamOptimizer(1e-3).minimize(self.totalLoss, global_step=self.global_step) newVars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="optimizer") self.sess.run(tf.variables_initializer(newVars)) if modelDir is None: modelDir = FLAGS.modelDir checkPoint = tf.train.get_checkpoint_state(modelDir) self.saver = tf.train.Saver() #if have checkPoint, restore checkPoint if checkPoint and checkPoint.model_checkpoint_path: self.saver.restore(self.sess, checkPoint.model_checkpoint_path) print("restored %s" % checkPoint.model_checkpoint_path)
def do_run(space): model_dir = '/home/alex/research/malware/models/' l1 = space['l1'] l2 = space['l2'] mwr = space['mwr'] epochs = space['epochs'] hard = space['hard'] distil_temp = space['distil'] if 'distil' in space else 1.0 restriction = space['restriction'] if 'restriction' in space else 'weights' two_outs = space['two_outs'] if 'two_outs' in space else False m = model(l1=l1, l2=l2, hard_constraint=hard, distil_temp=distil_temp, restriction=restriction, two_outs=two_outs) samples_per_epoch = int(train_mal / mwr) optimizer = keras.optimizers.Adam() m.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy', 'matthews_correlation']) name = "regnonegsoftmax3.l1-{}.l2-{}.mwr-{}-epoch-{}".format( l1, l2, mwr, epochs) if restriction and restriction != 'weights': name = name + "-" + restriction if distil_temp != 1.0: name = name + "-distil-{}".format(distil_temp) if hard: name = name + "-hard" print("Begining trial " + name) model_path = model_dir + name + ".hdf5" #saver = keras.callbacks.ModelCheckpoint(model_dir+name+'.weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto') callbacks = [] #[saver] if os.path.isfile(model_path): history = None m.load_weights(model_path) else: split = True if split: history = m.fit_generator( train_gen(mwr=mwr, distil_temp=distil_temp), samples_per_epoch=samples_per_epoch, nb_epoch=epochs, pickle_safe=False, #nb_worker=4, #validation_data=(x_test, y_test), callbacks=callbacks) else: history = m.fit( x_train, y_train, batch_size=1000, #train_gen(), samples_per_epoch=samples_per_epoch, nb_epoch=epochs, #validation_data=(x_test, y_test), callbacks=callbacks) history = history.history m.save_weights(model_path) y_pred = m.predict(x_test) y_truth = y_test y_round = np.copy(y_pred) y_round[y_round < 0.5] = 0 y_round[y_round >= 0.5] = 1 confusion = confusion_matrix(y_truth, y_round) print(confusion) fpr = float(confusion[0][1]) / (confusion[0][0] + confusion[0][1]) * 100 fnr = float(confusion[1][0]) / (confusion[1][0] + confusion[1][1]) * 100 print("FPR: {}% FNR: {}%".format(fpr, fnr)) try_on = model(l1=0.0, l2=0.0, hard_constraint=False, distil_temp=100) try_on.load_weights( "models/regnonegsoftmax3.l1-0.0.l2-0.0.mwr-0.3-epoch-10-distil-100.hdf5" ) max_samples = 45 adv_res = adversarial_eval(as_dense(m), max_samples=max_samples, try_on=as_dense(try_on)) print(adv_res) K.clear_session() return { 'loss': 1, 'confusion': confusion, 'adversarial': adv_res, 'history': history, 'status': STATUS_OK }
def test(): global bestloss,bestf1 test_loss=0 correct=0 hists=[] for data,target in val_data.get_loader(): if (use_gpu): data=data.to(device) target=target.to(device) #data,target=Variable(data),Variable(target) output=model(data) target=torch.softmax(target, dim=1).argmax(dim=1, keepdim=False); if (use_gpu): test_loss+=fun.cross_entropy(output,target,size_average=False).to(device).data else: test_loss+=fun.cross_entropy(output,target,size_average=False).data image=output.cpu().clone() image = torch.softmax(image, dim=1).argmax(dim=1, keepdim=False) output=target.cpu().clone(); hist = np.bincount(9 * output.reshape([-1]) + image.reshape([-1]),minlength=81).reshape(9, 9) hists.append(hist); ''' for sample in test_data.get_loader(): if (use_gpu): sample['image']=sample['image'].to(device) for i in range(len(sample['label'])): sample['label'][i]=sample['label'][i].to(device) data=Variable(sample['image']) target=torch.cat(tuple(sample['label']),1) target=target.argmax(dim=1,keepdim=False) target=Variable(target) output=model(data) if (use_gpu): test_loss+=fun.cross_entropy(output,target,size_average=False).to(device).data else: test_loss+=fun.cross_entropy(output,target,size_average=False).data image=output.cpu().clone() image = torch.softmax(image, dim=1).argmax(dim=1, keepdim=False) output=target.cpu().clone(); hist = np.bincount(9 * output.reshape([-1]) + image.reshape([-1]),minlength=81).reshape(9, 9) hists.append(hist); ''' hists_sum=np.sum(np.stack(hists, axis=0), axis=0) tp=0; tpfn=0; tpfp=0; f1score=0.0; for i in range(1,9): tp+=hists_sum[i][i].sum() tpfn+=hists_sum[i,:].sum() tpfp+=hists_sum[:,i].sum() f1score=2*tp/(tpfn+tpfp) test_loss/=len(test_data.get_loader().dataset) print('\nTest set: {} Cases,Average loss: {:.4f}\n'.format( len(test_data.get_loader().dataset),test_loss)) print("stage1 tp=",tp) print("stage1 tpfp=",tpfp) print("stage1 tpfn=",tpfn) print('\nTest set: {} Cases,F1 Score: {:.4f}\n'.format( len(test_data.get_loader().dataset),f1score)) loss_list.append(test_loss.data.cpu().numpy()); f1_list.append(f1score); if (f1score>bestf1): bestf1=f1score print("Best data Stage1 Updata\n"); torch.save(model,"./BestNet") '''
def main(): #checkpoint_path = '/home/dragonx/Documents/VideoText2018/EAST-master/weights/east_icdar2015_resnet_v1_50_rbox/' filename = '/media/dragonx/752d26ef-8f47-416d-b311-66c6dfabf4a3/Video Detection/ICDAR/train/Video_16_3_2.mp4' cap = cv2.VideoCapture(filename) parser = argparse.ArgumentParser() parser.add_argument('--checkpoint-path', default=checkpoint_path) args = parser.parse_args() if not os.path.exists(checkpoint_path): raise RuntimeError('Checkpoint `{}` not found'.format(checkpoint_path)) # read images until it is completed index = 0 logger.info('loading model') gpu_options = tf.GPUOptions(allow_growth=True) input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry = model.model(input_images, is_training=False) variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) # restore the model from weights sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) ckpt_state = tf.train.get_checkpoint_state(checkpoint_path) model_path = os.path.join( checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path)) logger.info('Restore from {}'.format(model_path)) saver.restore(sess, model_path) # get infos for video written frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. out = cv2.VideoWriter('EAST_testDemo1.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height)) while (cap.isOpened()): ret, frame = cap.read() index = index + 1 if ret == True: cv2.imshow('Frame', frame) print('Processing %d frame with ' % (index), frame.shape) ######### Use EAST text detector ########### start_time = time.time() img = frame rtparams = collections.OrderedDict() rtparams['start_time'] = datetime.datetime.now().isoformat() rtparams['image_size'] = '{}x{}'.format(img.shape[1], img.shape[0]) timer = collections.OrderedDict([('net', 0), ('restore', 0), ('nms', 0)]) im_resized, (ratio_h, ratio_w) = resize_image(img) rtparams['working_size'] = '{}x{}'.format(im_resized.shape[1], im_resized.shape[0]) start = time.time() score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: [im_resized[:, :, ::-1]]}) timer['net'] = time.time() - start boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer) logger.info('net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format( timer['net'] * 1000, timer['restore'] * 1000, timer['nms'] * 1000)) if boxes is not None: scores = boxes[:, 8].reshape(-1) boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h duration = time.time() - start_time timer['overall'] = duration logger.info('[timing] {}'.format(duration)) text_lines = [] if boxes is not None: text_lines = [] for box, score in zip(boxes, scores): box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: continue tl = collections.OrderedDict( zip(['x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3'], map(float, box.flatten()))) tl['score'] = float(score) text_lines.append(tl) ret = { 'text_lines': text_lines, 'rtparams': rtparams, 'timing': timer, } new_img = draw_illu(img.copy(), ret) cv2.imshow('Annotated Frame with EAST', new_img) out.write(new_img) # Quit when Q is pressed if cv2.waitKey(25) & 0xFF == ord('q'): break time.sleep(.100) else: break cap.release() out.release() cv2.destroyAllWindows()
def main(): #>>>>>>>>>>>>>>>>>>>>>define data/model path>>>>>>>>>>>>>>>>>>>>># #checkpoint_path = '/home/dragonx/Documents/VideoText2018/EAST-master/weights/east_icdar2015_resnet_v1_50_rbox/' #checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/LSTM_east/20180908-124306' checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/east/' idname1 = '20180921-135717' idname2 = 'model.ckpt-56092' test_data_path = '/media/dragonx/DataLight/ICDAR2015/test/' save_path = '/media/dragonx/DataLight/ICDAR2015/test_results1/' filename = '/media/dragonx/DataLight/ICDAR2013/test/Video_6_3_2.mp4' idx = 0 # initial frame number #>>>>>>>>>>>>>>>>>>>>>>Sort test video>>>>>>>>>>>>>>>>>>>>>>>>>>># video_set = [] for root, dirs, files in os.walk(test_data_path): for file in files: if file.endswith('.mp4'): video_set.append(os.path.splitext(file)[0]) index = range(0, len(video_set)) # parser for running outside # parser = argparse.ArgumentParser() # parser.add_argument('--checkpoint-path', default=checkpoint_path) # args = parser.parse_args() if not os.path.exists(checkpoint_path): raise RuntimeError('Checkpoint `{}` not found'.format(checkpoint_path)) logger.info('loading model') #>>>>>>>>>>>>>>>>>>>>>>> Loading Model >>>>>>>>>>>>>>>>>>>>>>>>># gpu_options = tf.GPUOptions(allow_growth=True) input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry, _ = model.model(input_images, is_training=False) variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) #>>>>>>>>>>>>>>>>>>>>>>>> restore the model from weights>>>>>>>># sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) model_path = checkpoint_path + idname1 + '/' + idname2 logger.info('Restore from {}'.format(model_path)) saver.restore(sess, model_path) #>>>>>>>>>>>>>>>>>>>>>>Start evaluation>>>>>>>>>>>>>>>>>>>>>>>>># P_test = [] R_test = [] f1_test = [] for k in index: P_video = [] R_video = [] f1_video = [] video_save = save_path + video_set[k] + idname1 + '_' + idname2 + '.avi' t_start = time.time() # sort up all the paths xml_solo_path = test_data_path + video_set[k] raw_video_path = test_data_path + video_set[k] + '.mp4' cap = cv2.VideoCapture(raw_video_path) frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) cnt_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) out = cv2.VideoWriter(video_save, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height)) # 1. load both polys and tags; 2. generate geo maps(the format of polys and tags need to match) # polys_array_list, tags_array_list, id_list_list, frame_num = load_annotations_solo(xml_solo_path, \ # 1, cnt_frame, frame_width, frame_height) #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>loop over frames>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> for m in range(cnt_frame): ret, frame = cap.read() # text_polys, text_tags = load_annoataion(txt_fn) # text_polys, text_tags = polys_array_list[m], tags_array_list[m] # text_polys, text_tags = check_and_validate_polys(text_polys, text_tags, (frame_height, frame_width)) # # im, text_polys, text_tags = crop_area(im, text_polys, text_tags, crop_background=False) # if text_polys.shape[0] == 0: # continue if ret == True: # print('Processing %d frame with '%(m), frame.shape) start_time = time.time() img = frame rtparams = collections.OrderedDict() rtparams['start_time'] = datetime.datetime.now().isoformat() rtparams['image_size'] = '{}x{}'.format( img.shape[1], img.shape[0]) timer = collections.OrderedDict([('net', 0), ('restore', 0), ('nms', 0)]) # im_resized, (ratio_h, ratio_w) = resize_image(img) im_resized = cv2.resize(frame, (int(512), int(512))) ratio_h, ratio_w = 512 / frame_height, 512 / frame_width rtparams['working_size'] = '{}x{}'.format( im_resized.shape[1], im_resized.shape[0]) start = time.time() score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: [im_resized[:, :, ::-1]]}) timer['net'] = time.time() - start boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer) logger.info( 'net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format( timer['net'] * 1000, timer['restore'] * 1000, timer['nms'] * 1000)) if boxes is not None: scores = boxes[:, 8].reshape(-1) boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h duration = time.time() - start_time timer['overall'] = duration logger.info('[timing] {}'.format(duration)) text_lines = [] if boxes is not None: text_lines = [] for box, score in zip(boxes, scores): box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: continue tl = collections.OrderedDict( zip([ 'x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3' ], map(float, box.flatten()))) tl['score'] = float(score) text_lines.append(tl) pred = { 'text_lines': text_lines, 'rtparams': rtparams, 'timing': timer, } new_img = draw_illu(img.copy(), pred) # out.write(new_img) #>>>>>>>>>>>>>>>>>>>>>>>>Evaluation>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # targets = text_polys # precision, recall, f1 = eval_single_frame(targets, pred) precision, recall, f1 = 0, 0, 0 P_video.append(precision) R_video.append(recall) f1_video.append(f1) # new_img1 = draw_illu_gt(new_img.copy(), targets, precision, recall, f1) out.write(new_img) # if m == 0: # fig1 = plt.figure(figsize=(20, 10)) # fig1.add_subplot(1, 2, 1) # plt.imshow(new_img ) # plt.title("Text Detection with fine-tuned EAST") # fig1.add_subplot(1, 2, 2) # plt.imshow(new_img1) # plt.title('Text Detection Results Comparison') # plt.show() if cv2.waitKey(25) & 0xFF == ord('q'): break # time.sleep(.100) else: break # evaluation on ret and gt P_test.append(np.array(P_video, dtype=np.float32)) R_test.append(np.array(R_video, dtype=np.float32)) f1_test.append(np.array(f1_video, dtype=np.float32)) print(P_video) print(R_video) print(f1_video) print("testing results are P:{}, R:{}, F1:{} on ".format( sum(P_video) / cnt_frame, sum(R_video) / cnt_frame, sum(f1_video) / cnt_frame) + video_set[k]) cap.release() out.release() cv2.destroyAllWindows() print('here is the precision') for item in P_test: print(np.mean(item)) print('here is the recall') for item in R_test: print(np.mean(item)) print('here is the f-score') for item in f1_test: print(np.mean(item)) print(video_set)
def evaluation(opt): device = torch.device('cuda' if opt.cuda else 'cpu') alpha = opt.alpha beta = opt.beta gamma = opt.gamma cycle_num = opt.cycle_num #load test model #test_model = FSRCNN(opt.upscale) test_model = model(opt.upscale) state_dict = load_state_dict(opt.model_path) test_model.load_state_dict(state_dict) test_model = test_model.to(device) test_model.eval() #load baseline baseline_model = SRCNN(opt.upscale) baseline_dict = load_state_dict(opt.baseline_path) baseline_model.load_state_dict(baseline_dict) baseline_model = baseline_model.to(device) baseline_model.eval() #load dataset dataset = TestDataset(opt.HR_path, opt.LR_path) dataloader = DataLoader(dataset, batch_size=opt.batch_size, shuffle=True) crop_boarder = opt.upscale print("dd") baseline_psnr, baseline_ssim = sr_forward_psnr(dataloader, baseline_model, device, crop_boarder) print("baseline_psnr:"+ str(baseline_psnr)) print("baseline_ssim:"+ str(baseline_ssim)) test_psnr, test_ssim = sr_forward_psnr(dataloader, test_model, device, crop_boarder) print("test_psnr:"+ str(test_psnr)) print("test_ssim:"+ str(test_ssim)) baseline_times = 0.0 test_times = 0.0 # test_ssim = 0.0 # test_psnr = 0.0 # baseline_ssim = 0.0 # baseline_psnr = 0.0 for index in range(cycle_num): baseline_time = sr_forward_time(dataloader, baseline_model, device) baseline_times += baseline_time print("baseline time"+str(baseline_times)) for index in range(cycle_num): test_time = sr_forward_time(dataloader, test_model, device) test_times += test_time print("test time"+str(test_times)) score = alpha * (test_psnr-baseline_psnr) + beta * (test_ssim-baseline_ssim) + gamma * min(baseline_times/test_times, 4) print('psnr: {:.4f}'.format(alpha * (test_psnr-baseline_psnr))) print('ssim: {:.4f}'.format(beta * (test_ssim-baseline_ssim))) print('time: {:.4f}'.format(gamma * min((baseline_times/test_times), 4))) print('score: {:.4f}'.format(score)) print('avarage score: {:.4f}'.format(score)) #calc FLOPs width = 360 height = 240 flops, params = profile(test_model, input_size=(1, 3, height, width)) print('test_model{} x {}, flops: {:.4f} GFLOPs, params: {}'.format(height, width, flops/(1e9), params))
def train(config, logger): logger.info('------load train data------') train_datas = load_data_file('datas/train.json', zero=config['zero'], lower=config['lower']) config = build_maps(train_datas, config, logger) train_data_loader = data_loader(train_datas, config) data_count, badcase, ignore, pos_max = train_data_loader.load( config['use_small']) logger.info('---Data count---') for label, num in data_count.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Badcase count---') for label, num in badcase.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Ignore count---') for label, num in ignore.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('--------------------------\n') logger.info('------load val data------') val_datas = load_data_file('datas/dev.json', zero=config['zero'], lower=config['lower']) val_data_loader = data_loader(val_datas, config) data_count, badcase, ignore, _ = val_data_loader.load(config['use_small']) logger.info('---Data count---') for label, num in data_count.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Badcase count---') for label, num in badcase.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Ignore count---') for label, num in ignore.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('--------------------------\n') logger.info('------load test data------') test_datas = load_data_file('datas/test.json', zero=config['zero'], lower=config['lower']) test_data_loader = data_loader(test_datas, config) data_count, badcase, ignore, _ = test_data_loader.load(config['use_small']) logger.info('---Data count---') for label, num in data_count.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Badcase count---') for label, num in badcase.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('---Ignore count---') for label, num in ignore.items(): logger.info('{:<50}\t{:>}'.format(label, num)) logger.info('--------------------------\n') config['position_num'] = pos_max print(config['position_num'], 'position_num') trustable_pattern = None if config['bootstrap'] or config['redistribution']: logger.info('init patterns') pattern_dict = {} for data in train_data_loader.dataset: pattern = data[-1] label = data[-2] if label not in pattern_dict: pattern_dict[label] = {} if pattern not in pattern_dict[label]: pattern_dict[label][pattern] = 0 pattern_dict[label][pattern] += 1 trustable_pattern = {} for k, v in pattern_dict.items(): sel_num = int(config['init_patterns_ratio'] * len(v)) sel_num = config['init_patterns_max'] if sel_num > config[ 'init_patterns_max'] else sel_num v = heapq.nlargest(sel_num, v.items(), key=lambda x: x[1]) trustable_pattern[k] = set([v1 for v1, v2 in v]) with tf.Graph().as_default() as g: train_model = model(config, 'train', trustable_pattern) train_model.build(g) with tf.Graph().as_default() as g: val_model = model(config, 'val') val_model.build(g) # for not boostrap or firt loop of bootstrap best_score = [-1., 0, None, None] loop = config['first_loop_epoch'] if config['bootstrap'] else config[ 'epoch'] logger.info('not bootstrap or first loop of bootstrap:{}'.format(loop)) for epoch in range(loop): logger.info('***TRAIN: {}***'.format(epoch)) if config['attention_regularization']: attentions, kls, patterns, labels = train_model.run_train( train_data_loader) else: patterns, labels = train_model.run_train(train_data_loader) logger.info('***VAL***') cur_score, accs = val_model.run_evaule(val_data_loader) best_score, is_new = parser_score(epoch, best_score, cur_score, accs, logger) if is_new: logger.info('***TEST***') test_score, test_accs = val_model.run_evaule(test_data_loader) parser_score(epoch, '', test_score, test_accs, logger, 'test') logger.info('******\n') # for other bootstrap loop if config['bootstrap']: logger.info('for other bootstrap loop') # update patterns pattern_condidates = {} for kl, pattern, label in zip(kls, patterns, labels): pattern_score = 1. / (1. + kl) if pattern_score > config[ 'pattern_threshold'] and pattern not in trustable_pattern: if label not in pattern_condidates: pattern_condidates[label] = [] pattern_condidates[label].append((pattern, pattern_score)) print('for update patterns') for label in config['label_dict'].keys(): if label not in pattern_condidates: continue num = len(pattern_condidates[label]) if len(pattern_condidates[label]) > config['pattern_max'] \ else config['pattern_max'] pattern_condidates_label = heapq.nlargest( num, pattern_condidates[label], key=lambda x: x[1]) trustable_pattern[label].update(pattern_condidates_label) train_model.update_trustable_pattern(trustable_pattern) for epoch in range(config['epoch'] - config['first_loop_epoch']): logger.info('***TRAIN: {}***'.format(epoch)) if config['attention_regularization']: attentions, kls, patterns, labels = train_model.run_train( train_data_loader) else: patterns, labels = train_model.run_train(train_data_loader) logger.info('***VAL***') cur_score, accs = val_model.run_evaule(val_data_loader) best_score, is_new = parser_score(epoch, best_score, cur_score, accs, logger) if is_new: logger.info('***TEST***') test_score, test_accs = val_model.run_evaule(test_data_loader) parser_score(epoch, '', test_score, test_accs, logger, 'test') logger.info('******\n') # update patterns pattern_condidates = {} for kl, pattern, label in zip(kls, patterns, labels): pattern_score = 1 / (1 + kl) if pattern_score > config[ 'pattern_threshold'] and pattern not in trustable_pattern: if label not in pattern_condidates: pattern_condidates[label] = [] pattern_condidates[label].append((pattern, pattern_score)) print('for update patterns') for label in config['label_dict'].keys(): if label not in pattern_condidates: continue num = len(pattern_condidates[label]) if len(pattern_condidates[label]) > config['pattern_max'] \ else config['pattern_max'] pattern_condidates_label = heapq.nlargest( num, pattern_condidates[label], key=lambda x: x[1]) trustable_pattern[label].update(pattern_condidates_label) train_model.update_trustable_pattern(trustable_pattern)
import tensorflow as tf import tensorflow.keras as keras import numpy as np import pandas as pd import matplotlib.pyplot as plt import itertools, functools import math import time import importlib import os, sys from tqdm import tqdm import model.model as model mo = model.model() mo.load_weights(model.MODEL_WEIGHTS) # data setup. stc.mpic column is the state complexity of mpi collective (gather, scatter) routines. input_columns = [ 'stc.code', 'stc.mpi', 'stc.mpic', 'problem.n', 'problem.nb', 'distr.procs', 'distr.hosts' ] output_columns = ['result.time'] def data_preproc(df): for key in ['problem.n', 'problem.nb', 'stc.code', 'stc.mpi', 'stc.mpic']: if key in df: df[key] = np.log(df[key] + 0.001)
from fastapi.middleware.cors import CORSMiddleware from starlette.responses import StreamingResponse from fastapi import FastAPI, File, UploadFile from model.model import model import io m = model() app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=['*'], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get('/') async def index(): return "Wichianmas" @app.post('/detect') async def detect(file: UploadFile = File(...), data="false"): contents = await file.read() result = m.detect(contents) return StreamingResponse(io.BytesIO(result), media_type='image/jpg')
def main(pos: Tuple[float, float], tomtom_key: str, lookup_range: float, timeout: int, list_roads: bool, road: int, file_json: str, road_provider_name: str = "default_road_provider", model_name: str = "default_model"): controller = Controller(provider=road_provider(road_provider_name, tomtom_key), c_model=model.model(model_name)) for i in range(3): try: print("Road Query...") roads = controller.query_roads(pos) except OverpassGatewayTimeout: sys.stderr.write( "WARNING: Couldn't list roads. Server load too high\n") print("Sleeping for 60s") time.sleep(60) continue except OverpassTooManyRequests: sys.stderr.write( "WARNING: Couldn't list roads. Too many requests\n") print("Sleeping for 60s") time.sleep(60) continue break if road is None: print("No roads found.") exit() if list_roads: for i in range(len(roads)): print("road num:", i + 1, " - name:", roads[i]) else: if len(roads) > 1: sys.stderr.write("WARNING: multiple roads detected\n") for i in range(len(roads)): print("road num:", i + 1, " - name:", roads[i]) for i in range(3): try: print("Result Query...") result_str: str = controller.get_result( roads[max(0, min(road, len(roads) - 1))], lookup_range / 2, pos, timeout) if file_json != "": if not os.path.exists("json"): os.makedirs("json") with open("json/" + file_json, "w") as json_out: json_out.write(result_str) result: dict = json.loads(result_str) road: dict = result.get("road") print("road:", road.get("name")) print( "speed:", sum([ fragment.get("speed") for fragment in road.get("fragments") ]) / len(road.get("fragments"))) print( "extra lateral clearance:", sum([ fragment.get("extra_lateral_clearance") for fragment in road.get("fragments") ]) / len(road.get("fragments"))) print( "bendiness:", sum([ fragment.get("bendiness") for fragment in road.get("fragments") ]) / len(road.get("fragments"))) print( "width:", sum([ fragment.get("width") for fragment in road.get("fragments") ]) / len(road.get("fragments"))) print( "length:", sum([ fragment.get("length") for fragment in road.get("fragments") ])) print("intersections:", road.get("intersections")) print("cars per day: ", result.get("average_daily_traffic")) except OverpassGatewayTimeout: sys.stderr.write( "WARNING: Couldn't get results. Server load too high\n") print("Sleeping for 60s") time.sleep(60) continue except OverpassTooManyRequests: sys.stderr.write( "WARNING: Couldn't get results. Too many requests\n") print("Sleeping for 60s") time.sleep(60) continue except TimeoutException: sys.stderr.write( "ERROR: Road provider timeout. Consider increasing timeout or reducing road length\n" ) break
from data.load import train_gen, x_test, y_test, train_good, train_mal from model.model import model import keras import keras.backend as K import numpy as np import os model_dir = '/home/alex/research/malware/models/' for mwr in [0.3, 0.4, 0.5]: for temp in [10, 33, 66, 100, 150, 300]: m = model(distil_temp=temp) optimizer = keras.optimizers.Adam() m.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy', 'matthews_correlation']) epochs = 10 model_path = model_dir + "regnonegsoftmax3.l1-0.l2-0.mwr-{}-epoch-{}-distil-train-{}.hdf5".format(mwr, epochs, temp) samples_per_epoch = int(train_mal / mwr) print ("Looking for {}".format(model_path)) if os.path.isfile(model_path): m.load_weights(model_path) else:
import skimage.color as sc from data.mydataloader import Dataset import time import cv2 import os cuda = 1 device = torch.device('cuda' if cuda else 'cpu') def tensor_to_np(tensor): img = tensor.mul(255).byte() img = img.cpu().numpy().transpose((1, 2, 0)) return img # load model model = model.model().to(device) model_dict = utils.load_state_dict("./pretrained-model/model.pth") model.load_state_dict(model_dict, strict=True) # load model testset =Dataset("./datasets/same/HR", "./datasets/same/LR") testing_data_loader = DataLoader(dataset=testset, num_workers=0, batch_size=1, shuffle=False) model.eval() avg_psnr, avg_ssim = 0, 0 start = time.time() for _,batch in enumerate(testing_data_loader):
def generate_text(start_word, tokenizer, model, max_len=100): output = tokenizer.texts_to_sequences([start_word])[0] endtoken = tokenizer.word_index['endtoken'] for i in range(max_len): token_list = pad_sequences([output], maxlen=max_len, padding='pre') predicted = np.argmax(model.predict(token_list)[0][2:]) + 2 if predicted == endtoken: return tokenizer.sequences_to_texts([output]) output.append(predicted) return tokenizer.sequences_to_texts([output]) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( '--token', default='/home/angps/Documents/Quotes_generation/data/tokenizer.pickle' ) parser.add_argument( '--model', default='/home/angps/Documents/Quotes_generation/model_logs/model.hdf5' ) parser.add_argument('--startword', required=True, type=str) args = parser.parse_args() with open(args.token, 'rb') as f: tokenizer = pickle.load(f) model = model(batch_size=1) model.load_weights(args.model) print(generate_text(args.startword, tokenizer, model))
def main(): #>>>>>>>>>>>>>>>>>>>>>define data/model path>>>>>>>>>>>>>>>>>>>>># #checkpoint_path = '/home/dragonx/Documents/VideoText2018/EAST-master/weights/east_icdar2015_resnet_v1_50_rbox/' #checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/LSTM_east/20180908-124306' checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/east/' idname1 = '20180921-135717' idname2 = 'model.ckpt-56092' test_data_path = '/media/dragonx/DataLight/ICDAR2015/test/' save_path = '/media/dragonx/DataLight/ICDAR2015/test_results/' video_set = [] for root, dirs, files in os.walk(test_data_path): for file in files: if file.endswith('.mp4'): video_set.append(os.path.splitext(file)[0]) index = range(0, len(video_set)) if not os.path.exists(checkpoint_path): raise RuntimeError( 'Checkpoint `{}` not found'.format(checkpoint_path)) if not os.path.exists(checkpoint_path): raise RuntimeError( 'Checkpoint `{}` not found'.format(checkpoint_path)) # read images until it is completed logger.info('loading model') #>>>>>>>>>>>>>>>>>>>>>>> Loading Model >>>>>>>>>>>>>>>>>>>>>>>>># gpu_options = tf.GPUOptions(allow_growth=True) input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry, _ = model.model(input_images, is_training=False) variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) # restore the model from weights sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # model_path= tf.train.latest_checkpoint(checkpoint_path) # ckpt_state = tf.train.get_checkpoint_state(checkpoint_path) # model_path = os.path.join(checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path)) model_path = checkpoint_path + '/' + idname logger.info('Restore from {}'.format(model_path)) saver.restore(sess, model_path) # get infos for video written frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. video_save = '/media/dragonx/DataLight/ICDAR2015/test_results/EAST_'+ idname + '.avi' out = cv2.VideoWriter(video_save, cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height)) while(cap.isOpened()): ret, frame = cap.read() index = index+1 if ret == True: cv2.imshow('Frame', frame) print('Processing %d frame with '%(index), frame.shape) ######### Use EAST text detector ########### start_time = time.time() img = frame rtparams = collections.OrderedDict() rtparams['start_time'] = datetime.datetime.now().isoformat() rtparams['image_size'] = '{}x{}'.format(img.shape[1], img.shape[0]) timer = collections.OrderedDict([ ('net', 0), ('restore', 0), ('nms', 0) ]) im_resized, (ratio_h, ratio_w) = resize_image(img) rtparams['working_size'] = '{}x{}'.format( im_resized.shape[1], im_resized.shape[0]) start = time.time() score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: [im_resized[:,:,::-1]]}) timer['net'] = time.time() - start boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer) logger.info('net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format( timer['net']*1000, timer['restore']*1000, timer['nms']*1000)) if boxes is not None: scores = boxes[:,8].reshape(-1) boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h duration = time.time() - start_time timer['overall'] = duration logger.info('[timing] {}'.format(duration)) text_lines = [] if boxes is not None: text_lines = [] for box, score in zip(boxes, scores): box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3]-box[0]) < 5: continue tl = collections.OrderedDict(zip( ['x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3'], map(float, box.flatten()))) tl['score'] = float(score) text_lines.append(tl) ret = { 'text_lines': text_lines, 'rtparams': rtparams, 'timing': timer, } new_img = draw_illu(img.copy(), ret) cv2.imshow('Annotated Frame with EAST', new_img) out.write(new_img) fig1 = plt.figure(figsize=(20, 10)) fig1.add_subplot(1, 2, 1) plt.imshow((np.squeeze(score)*255).astype(np.uint8)) plt.title("Score Map") fig1.add_subplot(1, 2, 2) plt.imshow(geometry[0, :,:,1]) plt.title('Geometry map') plt.show() # Quit when Q is pressedplt.title("Text Detection with fine-tuned EAST") if cv2.waitKey(25) & 0xFF == ord('q'): break time.sleep(.100) else: break cap.release() out.release() cv2.destroyAllWindows()
fl = np.load(DIREC + typ + '_quotes_maxlen100.npy',allow_pickle=True) while True: in_quotes = [] out_quotes = [] for i in range(batch_size): num = np.random.choice(len(fl)-1) quotes = fl[num] idx = np.random.choice(len(quotes)) in_quote, out_quote = quotes[:idx], quotes[idx] in_quote = pad_sequences([in_quote],maxlen = maxlen).flatten() out_quote = to_categorical(out_quote,num_classes = vocab_size) in_quotes = in_quotes + [in_quote] out_quotes = out_quotes + [out_quote] yield np.reshape(in_quotes,(batch_size,maxlen)),np.reshape(out_quotes,(batch_size,vocab_size)) model = model() model.compile(optimizer='adam', loss='categorical_crossentropy') optimizer = tf.keras.optimizers.Adam() base_lr = 0.001 train_gen = data_gen('train',BATCH) test_gen = data_gen('val',BATCH) checkpoint_path = DIREC + "model_logs/model.hdf5" checkpoint_dir = os.path.dirname(checkpoint_path) cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True, save_best_only=True, verbose=1)
def __init__(self): self.model = model() self.View = View()
def main(): #>>>>>>>>>>>>>>>>>>>>>define data/model path>>>>>>>>>>>>>>>>>>>>># #checkpoint_path = '/home/dragonx/Documents/VideoText2018/EAST-master/weights/east_icdar2015_resnet_v1_50_rbox/' #checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/LSTM_east/20180908-124306' checkpoint_path = '/media/dragonx/DataStorage/ARC/EAST/checkpoints/east/' idname1 = '20180921-173054' idname2 = 'model.ckpt-56092' test_data_path = '/media/dragonx/DataLight/ICDAR2013/test/' save_path = '/media/dragonx/DataLight/ICDAR2013/test_results1/' filename = '/media/dragonx/DataLight/ICDAR2013/test/Video_6_3_2.mp4' idx = 0 # initial frame number if platform.uname()[1] != 'dragonx-H97N-WIFI': print("Now it knows it's in a remote cluster") checkpoint_path = '/work/cascades/lxiaol9/ARC/EAST/checkpoints/east/' idname1 = '20180921-173054' idname2 = 'model.ckpt-56092' test_data_path = '/work/cascades/lxiaol9/ARC/EAST/data/ICDAR2013/test/' save_path = '/work/cascades/lxiaol9/ARC/EAST/data/ICDAR2013/test_results1/' #>>>>>>>>>>>>>>>>>>>>>>Sort test video>>>>>>>>>>>>>>>>>>>>>>>>>>># video_set = [] for root, dirs, files in os.walk(test_data_path): for file in files: if file.endswith('.mp4'): video_set.append(os.path.splitext(file)[0]) index = range(1, 6) # parser for running outside # parser = argparse.ArgumentParser() # parser.add_argument('--checkpoint-path', default=checkpoint_path) # args = parser.parse_args() if not os.path.exists(checkpoint_path): raise RuntimeError('Checkpoint `{}` not found'.format(checkpoint_path)) logger.info('loading model') #>>>>>>>>>>>>>>>>>>>>>>> Loading Model >>>>>>>>>>>>>>>>>>>>>>>>># gpu_options = tf.GPUOptions(allow_growth=True) input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry, _ = model.model(input_images, is_training=False) variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) #>>>>>>>>>>>>>>>>>>>>>>>> restore the model from weights>>>>>>>># sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) model_path = checkpoint_path + idname1 + '/' + idname2 logger.info('Restore from {}'.format(model_path)) saver.restore(sess, model_path) #>>>>>>>>>>>>>>>>>>>>>> construct KF filter model here>>>>>>>>>># # tracker = KalmanRBOXTracker() #>>>>>>>>>>>>>>>>>>>>>>Start evaluation>>>>>>>>>>>>>>>>>>>>>>>>># P_test = [] R_test = [] f1_test = [] for k in index: P_video = [] R_video = [] f1_video = [] video_save = save_path + video_set[ k] + idname1 + '_' + idname2 + '_tracking.avi' file_txt = save_path + video_set[k] + '.txt' file1 = open(file_txt, "w+") file1.close() t_start = time.time() # sort up all the paths xml_solo_path = test_data_path + video_set[k] raw_video_path = test_data_path + video_set[k] + '.mp4' cap = cv2.VideoCapture(raw_video_path) frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) cnt_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) out = cv2.VideoWriter(video_save, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height)) # 1. load both polys and tags; 2. generate geo maps(the format of polys and tags need to match) # polys_array_list, tags_array_list, id_list_list, frame_num = load_annotations_solo(xml_solo_path, \ # 1, cnt_frame, frame_width, frame_height) #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>loop over frames>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # we will initialize a tracker object for every # mot_tracker = motion_bayestrack() for m in range(cnt_frame): ret, frame = cap.read() # text_polys, text_tags = load_annoataion(txt_fn) # text_polys, text_tags = polys_array_list[m], tags_array_list[m] # text_polys, text_tags = check_and_validate_polys(text_polys, text_tags, (frame_height, frame_width)) # # im, text_polys, text_tags = crop_area(im, text_polys, text_tags, crop_background=False) # if text_polys.shape[0] == 0: # continue if ret == True: # print('Processing %d frame with '%(m), frame.shape) start_time = time.time() img = frame rtparams = collections.OrderedDict() rtparams['start_time'] = datetime.datetime.now().isoformat() rtparams['image_size'] = '{}x{}'.format( img.shape[1], img.shape[0]) timer = collections.OrderedDict([('net', 0), ('restore', 0), ('nms', 0)]) # im_resized, (ratio_h, ratio_w) = resize_image(img) im_resized = cv2.resize(frame, (int(512), int(512))) ratio_h, ratio_w = 512 / frame_height, 512 / frame_width rtparams['working_size'] = '{}x{}'.format( im_resized.shape[1], im_resized.shape[0]) start = time.time() score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: [im_resized[:, :, ::-1]]}) timer['net'] = time.time() - start boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer) logger.info( 'net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format( timer['net'] * 1000, timer['restore'] * 1000, timer['nms'] * 1000)) # we will store the text boxes here: if boxes is not None: with open(file_txt, "a+") as f: print("Writing frame {:d}".format(m)) for box in boxes: f.write( "{:d},".format(m) + '-1,' + ','.join(["{:2.3f}".format(x) for x in box]) + ',-1,-1,-1\n') scores = boxes[:, 8].reshape(-1) boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Motion model for every box>>>>>>>>>>>>>>># # predict search region, # Kalman Filter Updates # if(display): # plt.ion() # fig = plt.figure() # ax1 = fig.add_subplot(111, aspect='equal') # fn = 'mot_benchmark/%s/%s/img1/%06d.jpg'%(phase,seq,frame) # im =io.imread(fn) # ax1.imshow(im) # plt.title(seq+' Tracked Targets') # start_time = time.time() # trackers = mot_tracker.update(boxes, scores) # cycle_time = time.time() - start_time # total_time += cycle_time # for d in trackers: # print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1'%(frame,d[4],d[0],d[1],d[2]-d[0],d[3]-d[1]),file=out_file) # if(display): # d = d.astype(np.int32) # ax1.add_patch(patches.Rectangle((d[0],d[1]),d[2]-d[0],d[3]-d[1],fill=False,lw=3,ec=colours[d[4]%32,:])) # ax1.set_adjustable('box-forced') # # if(display): # fig.canvas.flush_events() # plt.draw() # ax1.cla() #>>>>>>>>>>>>>>>>>>>>>>>>> KF end>>>>>>>>>>>>>>>>>>>>>>>>>>>># duration = time.time() - start_time timer['overall'] = duration logger.info('[timing] {}'.format(duration)) text_lines = [] if boxes is not None: text_lines = [] for box, score in zip(boxes, scores): box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: continue tl = collections.OrderedDict( zip([ 'x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3' ], map(float, box.flatten()))) tl['score'] = float(score) text_lines.append(tl) pred = { 'text_lines': text_lines, 'rtparams': rtparams, 'timing': timer, } new_img = draw_illu(img.copy(), pred) # out.write(new_img) #>>>>>>>>>>>>>>>>>>>>>>>>Evaluation>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # targets = text_polys # precision, recall, f1 = eval_single_frame(targets, pred) precision, recall, f1 = 0, 0, 0 P_video.append(precision) R_video.append(recall) f1_video.append(f1) # new_img1 = draw_illu_gt(new_img.copy(), targets, precision, recall, f1) out.write(new_img) # if m == 0: # fig1 = plt.figure(figsize=(20, 10)) # fig1.add_subplot(1, 2, 1) # plt.imshow(new_img ) # plt.title("Text Detection with fine-tuned EAST") # fig1.add_subplot(1, 2, 2) # plt.imshow(new_img1) # plt.title('Text Detection Results Comparison') # plt.show() if cv2.waitKey(25) & 0xFF == ord('q'): break # time.sleep(.100) else: break # evaluation on ret and gt P_test.append(np.array(P_video, dtype=np.float32)) R_test.append(np.array(R_video, dtype=np.float32)) f1_test.append(np.array(f1_video, dtype=np.float32)) print(P_video) print(R_video) print(f1_video) print("testing results are P:{}, R:{}, F1:{} on ".format( sum(P_video) / cnt_frame, sum(R_video) / cnt_frame, sum(f1_video) / cnt_frame) + video_set[k]) cap.release() out.release() cv2.destroyAllWindows() print('here is the precision') for item in P_test: print(np.mean(item)) print('here is the recall') for item in R_test: print(np.mean(item)) print('here is the f-score') for item in f1_test: print(np.mean(item)) print(video_set)