def tower_loss(images, score_maps, geo_maps, training_masks, labels, reuse_variables=None): # Build inference graph with tf.variable_scope(tf.get_variable_scope(), reuse=reuse_variables): f_score, f_geometry = model.model(images, is_training=True) f_dat = labels model_loss = model.loss(score_maps, f_score, geo_maps, f_geometry, training_masks) #total_loss = tf.add_n([model_loss] + 0.7*sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))) total_loss = sum([model_loss]) + reg_constant * sum( tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) # add summary if reuse_variables is None: tf.summary.image('input', images) tf.summary.image('score_map', score_maps) tf.summary.image('score_map_pred', f_score * 255) tf.summary.image('geo_map_0', geo_maps[:, :, :, 0:1]) tf.summary.image('geo_map_0_pred', f_geometry[:, :, :, 0:1]) tf.summary.image('training_masks', training_masks) #tf.summary.image('weight_vis', [v for v in tf.trainable_variables() if 'resnet_v1_50' in v.name][0]) tf.summary.scalar('model_loss', model_loss) tf.summary.scalar('total_loss', total_loss) return total_loss, model_loss, f_score, f_geometry, f_dat
def i_am_testing(images): with tf.variable_scope(tf.get_variable_scope(), reuse=True): f_score, f_geometry = model.model(images, is_training=True)
def evaluateModel(model_ver, model_path, angles, imname_list, im_dir, annot_dir, results_dir, nms_thres, score_map_thresh, box_thresh, inspect_mode=False, calc_p_r=False): mask_dir = '/mnt/nfs/work1/elm/ray/evaluation/EAST_single/' print "Evaluating EAST model, separately from training process..." with tf.get_default_graph().as_default(): input_images = tf.placeholder(tf.float32, shape=[None, None, None, 4], 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=True) variable_averages = tf.train.ExponentialMovingAverage( 0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) with tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) as sess: ckpt_state = tf.train.get_checkpoint_state(model_path) model_path = os.path.join( model_path, os.path.basename(ckpt_state.model_checkpoint_path)) saver.restore(sess, model_path) count_right = 0 count_wrong = 0 count_posNotDetected = 0 for imName in imname_list: print imName labels = [] im00 = cv2.imread(im_dir + imName + ".tiff")[:, :, ::-1] w, h, _ = im00.shape im0 = im00[:, :, :] im0 = cv2.resize(im0, (h, w)) slide_window = 400 crop_size = 512 crop_center = (256, 256) num_rows, num_cols = int(np.ceil(w / slide_window)), int( np.ceil(h / slide_window)) for rot in angles: if rot == 0: box_thresh_loc = box_thresh[0] else: box_thresh_loc = box_thresh[1] print ">>> testing for angle: ", rot im = cv2.imread(im_dir + imName + ".tiff") #[:, :, ::-1] filename = mask_dir + imName + ".tiff" mask_im = cv2.imread(filename)[:, :, 0] mask_im = np.expand_dims(mask_im, axis=2) im = np.append(im, mask_im, axis=2) boxes_one_rot = [] for i in range(num_rows): images = [] for j in range(num_cols): temp = im[slide_window*i:slide_window*i+crop_size, \ slide_window*j:slide_window*j+crop_size, :] w2, h2, _ = temp.shape if w2 < crop_size or h2 < crop_size: result = np.zeros((crop_size, crop_size, 4)) result[:w2, :h2] = temp temp = result M = cv2.getRotationMatrix2D(crop_center, rot, 1.0) temp = cv2.warpAffine(temp, M, (crop_size, crop_size)) images.append(temp) score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: images}) for j in range(num_cols): boxes = detect(score_map=score[j], geo_map=geometry[j], score_map_thresh=score_map_thresh, box_thresh=box_thresh_loc, nms_thres=nms_thres) if boxes is not None: boxes = boxes[:, :8].reshape((-1, 4, 2)) for box in boxes: M_inv = cv2.getRotationMatrix2D( crop_center, -1 * rot, 1) box[0] = M_inv.dot( np.array((box[0, 0], box[0, 1]) + (1, ))) box[1] = M_inv.dot( np.array((box[1, 0], box[1, 1]) + (1, ))) box[2] = M_inv.dot( np.array((box[2, 0], box[2, 1]) + (1, ))) box[3] = M_inv.dot( np.array((box[3, 0], box[3, 1]) + (1, ))) box[0, 0] = box[0, 0] + j * slide_window box[0, 1] = box[0, 1] + i * slide_window box[1, 0] = box[1, 0] + j * slide_window box[1, 1] = box[1, 1] + i * slide_window box[2, 0] = box[2, 0] + j * slide_window box[2, 1] = box[2, 1] + i * slide_window box[3, 0] = box[3, 0] + j * slide_window box[3, 1] = box[3, 1] + i * slide_window # setting up the actual orientation expected box_f = copy.copy(box) box_f[0, 0] = box[3, 0] box_f[0, 1] = box[3, 1] box_f[1, 0] = box[2, 0] box_f[1, 1] = box[2, 1] box_f[2, 0] = box[1, 0] box_f[2, 1] = box[1, 1] box_f[3, 0] = box[0, 0] box_f[3, 1] = box[0, 1] boxes_one_rot.append(box_f) boxes_single_rot = np.zeros((len(boxes_one_rot), 9)) boxes_single_rot[:, :8] = np.array(boxes_one_rot).reshape( (-1, 8)) boxes_single_rot[:, 8] = 1 labels += boxes_single_rot.tolist() if inspect_mode == True: boxes_single_rot = lanms.merge_quadrangle_n9( boxes_single_rot, lanms_thresh).astype('int16') for box in boxes_single_rot: pts = np.array(box[:8], dtype=np.int32).reshape( (4, 2)) cv2.polylines(im0[-w / 2, -h / 2, ::-1], [pts], True, color=(255, 255, 0), thickness=4) cv2.imwrite( results_dir + imName + model_ver + str(int(rot)) + ".tiff", im[-w / 2, -w / 2, ::-1]) np.savetxt(results_dir + imName + model_ver + str(int(rot)) + ".txt", boxes_single_rot, '%5.1f', delimiter=",") ### Plot out and save the text detections for each image boxes = lanms.merge_quadrangle_n9(np.array(labels), nms_thres) if not os.path.isdir('Data/final_detections_npy/'): os.mkdir('Data/final_detections_npy/') if not os.path.isdir('Data/final_detections_npy/' + model_ver): os.mkdir('Data/final_detections_npy/' + model_ver) np.save( 'Data/final_detections_npy/' + model_ver + '/' + imName + '.npy', boxes) #for box in boxes: # pts = np.array(box[:8], dtype=np.int32).reshape((4,2)) # cv2.polylines(im00[:, :, ::-1], [pts], True, color=(255, 0, 0), thickness=4) #annotation = np.load("/mnt/nfs/work1/elm/ray/new_char_anots_ncs/" + "j" + "/" + imName + ".npy").item() #for index in range(len(annotation)): # vertices = annotation[index]["vertices"] # vertices = [np.array(vertices, dtype=np.int32)] # cv2.polylines(im00[:, :, ::-1], vertices, True, color=(0, 0, 255), thickness=2) #cv2.imwrite(results_dir+imName+model_ver+".tiff", im00[:, :, ::-1]) #np.savetxt(results_dir+imName+model_ver+".txt", boxes, delimiter=",") ### Compute the TP, FP, FN info for each image if calc_p_r: count_right_cache = 0 boxes = boxes[:, :8].reshape((-1, 4, 2)) annotation = np.load(annot_dir + imName + ".npy").item() num_true_pos = len(annotation) for box in boxes: 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 k = 0 idx = 0 count_wrong += 1 while (idx < num_true_pos): if k in annotation: proposed_label = annotation[k]['vertices'] if len(proposed_label) == 4: x3, y3, x2, y2, x1, y1, x0, y0 = proposed_label[0][0], proposed_label[0][1], proposed_label[1][0], proposed_label[1][1], \ proposed_label[2][0], proposed_label[2][1], proposed_label[3][0], proposed_label[3][1] if (checkIOU(box, [[x0, y0], [x1, y1], [x2, y2], [x3, y3]]) == True): count_right_cache += 1 count_wrong -= 1 break idx += 1 k += 1 count_posNotDetected += num_true_pos - count_right_cache count_right += count_right_cache precision = (float)(count_right) / (float)( count_right + count_wrong) # TP / TP + FP recall = (float)(count_right) / (float)( count_right + count_posNotDetected) # TP / TP + FN fscore = 2 * (precision * recall) / (precision + recall) print "Precision, recall, fscore: " + str( precision) + ", " + str(recall) + ", " + str(fscore)