Commit 955339d7 authored by Oksana Belyaeva's avatar Oksana Belyaeva
Browse files

added module eval model

parent b8d0385e
This diff is collapsed.
from typing import List
import numpy as np
import os
import tensorflow as tf
import cv2
import json
import utils.label_map_util as label_map_util
import utils.visualization_utils as vis_util
from pycocotools.coco import COCO
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
def init_coco_format(image_id: int, category_id: int, bbox: List[float], score: float):
"""
Results must be written to file in JSON format.
one dict per instance:
[{
"image_id" : int,
"category_id" : int,
"bbox : [x,y,width,height],
"score" : float,
}]
:param all_boxes:
:return:
"""
return {
"image_id": image_id,
"category_id": category_id,
"bbox": bbox, # [x,y,width,height]
"score": score
}
def predict_test_from_coco_dataset(data_dir: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/images/test',
coco_ann_file_path: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/test.json',
output_coco_predict_file: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/predict_test.json'):
coco = COCO(coco_ann_file_path)
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
catIds = coco.getCatIds(catNms=['text', 'title', 'table', 'figure'])
imgIds = coco.getImgIds()
detection_graph = tf.Graph()
# setting parameters
PATH_TO_CKPT = 'object_detection_graph_pln_4cl/frozen_inference_graph.pb'
PATH_TO_LABELS = 'configs/label_map_4cl.pbtxt'
NUM_CLASSES = 4
PATH_TO_TEST_RES_IMAGES_DIR = "/home/ox/work/pablaynet/test_res_4cl"
#'/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/images/test_res' # 'images/lep_res'#tz/gost-23118-99_res'#'tz/GOST_31937-2011_res'#'PubLayNet/images/examples_res'
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
coco_output = []
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # return [ymin, xmin, ymax, xmax]
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for image_id in imgIds:
img_name = coco.loadImgs(image_id)[0]['file_name']
height = coco.loadImgs(image_id)[0]['height']
width = coco.loadImgs(image_id)[0]['width']
image = cv2.imread(os.path.join(data_dir, img_name))
image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
scores = [s for s in scores[0] if s > 0.6]
num = len(scores)
boxes = boxes[0][:num] # network return [ymin, xmin, ymax, xmax]
classes = classes[0][:num]
for i in range(0, int(num)):
bbox = boxes[i]
boxes[i] = [float(min(bbox[0] * height, height)),
float(min(bbox[1] * width, width)),
float(min((bbox[2]) * height, height)),
float(min((bbox[3]) * width, width))]
# Visualization of the results of a detection.
res = vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
boxes,
classes.astype(np.int32),
scores,
category_index,
use_normalized_coordinates=False,
line_thickness=2)
cv2.imwrite(PATH_TO_TEST_RES_IMAGES_DIR + "/" + img_name,
cv2.cvtColor(res, cv2.COLOR_RGB2BGR))
annIds = coco.getAnnIds(imgIds=image_id)
anns = coco.loadAnns(annIds)
# write into file coco object detection output
for i in range(0, int(num)):
bbox = boxes[i]
# converting from [ymin, xmin, ymax, xmax] to [xmin, ymin, width, height]
bbox = [float(bbox[1]), float(bbox[0]), float(bbox[3] - bbox[1]), float(bbox[2] - bbox[0])]
coco_output.append(init_coco_format(image_id, int(classes[i]), bbox, float(scores[i])))
with open(os.path.join(output_coco_predict_file), 'w') as output_json_file:
json.dump(coco_output, output_json_file)
def evaluate(annotation_gt: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/test.json',
annotation_predict: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/predict_test.json'):
from pycocotools.cocoeval import COCOeval
pylab.rcParams["figure.figsize"] = (10.0, 8.0)
# inizialization
annType = ['segm', 'bbox', 'keypoints']
annType = annType[1] # specify type here
cocoGt = COCO(annotation_gt)
cocoDt = cocoGt.loadRes(annotation_predict)
# running evaluation
cocoEval = COCOeval(cocoGt, cocoDt, annType)
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
if __name__ == '__main__':
predict_test_from_coco_dataset(data_dir='/home/ox/PycharmProjects/TF_ObjectDetection_API/PubLayNet/images/test',
coco_ann_file_path='/home/ox/PycharmProjects/TF_ObjectDetection_API/PubLayNet/jsons/test_4cl.json',
output_coco_predict_file='/home/ox/PycharmProjects/TF_ObjectDetection_API/PubLayNet/jsons/predict_test.json')
evaluate(annotation_gt='/home/ox/PycharmProjects/TF_ObjectDetection_API/PubLayNet/jsons/test_4cl.json',
annotation_predict='/home/ox/PycharmProjects/TF_ObjectDetection_API/PubLayNet/jsons/predict_test.json')
\ No newline at end of file
model_checkpoint_path: "model.ckpt"
all_model_checkpoint_paths: "model.ckpt"
model {
faster_rcnn {
num_classes: 3
image_resizer {
keep_aspect_ratio_resizer {
min_dimension: 300
max_dimension: 4024
}
}
feature_extractor {
type: "faster_rcnn_resnet101"
first_stage_features_stride: 16
}
first_stage_anchor_generator {
grid_anchor_generator {
height_stride: 16
width_stride: 16
scales: 0.25
scales: 0.5
scales: 1.0
scales: 2.0
aspect_ratios: 0.5
aspect_ratios: 1.0
aspect_ratios: 2.0
}
}
first_stage_box_predictor_conv_hyperparams {
op: CONV
regularizer {
l2_regularizer {
weight: 0.0
}
}
initializer {
truncated_normal_initializer {
stddev: 0.009999999776482582
}
}
}
first_stage_nms_score_threshold: 0.0
first_stage_nms_iou_threshold: 0.699999988079071
first_stage_max_proposals: 300
first_stage_localization_loss_weight: 2.0
first_stage_objectness_loss_weight: 1.0
initial_crop_size: 14
maxpool_kernel_size: 2
maxpool_stride: 2
second_stage_box_predictor {
mask_rcnn_box_predictor {
fc_hyperparams {
op: FC
regularizer {
l2_regularizer {
weight: 0.0
}
}
initializer {
variance_scaling_initializer {
factor: 1.0
uniform: true
mode: FAN_AVG
}
}
}
use_dropout: false
dropout_keep_probability: 1.0
}
}
second_stage_post_processing {
batch_non_max_suppression {
score_threshold: 0.0
iou_threshold: 0.6000000238418579
max_detections_per_class: 100
max_total_detections: 300
}
score_converter: SOFTMAX
}
second_stage_localization_loss_weight: 2.0
second_stage_classification_loss_weight: 1.0
}
}
train_config {
batch_size: 1
data_augmentation_options {
random_horizontal_flip {
}
}
optimizer {
momentum_optimizer {
learning_rate {
manual_step_learning_rate {
initial_learning_rate: 0.003000000026077032
schedule {
step: 1
learning_rate: 9.999999747378752e-05
}
schedule {
step: 900000
learning_rate: 2.9999999242136255e-05
}
schedule {
step: 1200000
learning_rate: 3.000000106112566e-06
}
}
}
momentum_optimizer_value: 0.8999999761581421
}
use_moving_average: false
}
gradient_clipping_by_norm: 10.0
fine_tune_checkpoint: "pretrain_models/faster_rcnn_resnet101_publaynet/model.ckpt-105527"
from_detection_checkpoint: true
}
train_input_reader {
label_map_path: "configs/label_map_3cl.pbtxt"
tf_record_input_reader {
input_path: "tfrecord_data/train-0_3cl.record"
}
}
eval_config {
num_examples: 8000
max_evals: 10
use_moving_averages: false
}
eval_input_reader {
label_map_path: "configs/label_map_3cl.pbtxt"
shuffle: false
num_readers: 1
tf_record_input_reader {
input_path: "tfrecord_data/train-0_3cl.record"
}
}
from typing import List
import numpy as np
import os
import tensorflow as tf
from matplotlib import pyplot as plt
from PIL import Image
import cv2
import json
import utils.label_map_util as label_map_util
import utils.visualization_utils as vis_util
from pycocotools.coco import COCO
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
def init_coco_format(image_id: int, category_id: int, bbox: List[float], score: float):
"""
Results must be written to file in JSON format.
one dict per instance:
[{
"image_id" : int,
"category_id" : int,
"bbox : [x,y,width,height],
"score" : float,
}]
:param all_boxes:
:return:
"""
return {
"image_id": image_id,
"category_id": category_id,
"bbox": bbox, #[x,y,width,height],
"score": score
}
def test():
......@@ -102,132 +73,5 @@ def test():
cv2.imwrite(PATH_TO_TEST_RES_IMAGES_DIR + "/" + image_path.split('/')[-1], cv2.cvtColor(res, cv2.COLOR_RGB2BGR))
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def predict_test_from_coco_dataset(data_dir: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/images/test',
coco_ann_file_path: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/test.json',
output_coco_predict_file: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/predict_test.json'):
import skimage.io as io
coco = COCO(coco_ann_file_path)
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
catIds = coco.getCatIds(catNms=['text', 'table', 'figure'])
imgIds = coco.getImgIds()
detection_graph = tf.Graph()
PATH_TO_CKPT = 'object_detection_graph/frozen_inference_graph.pb'
PATH_TO_LABELS = 'configs/label_map_3cl.pbtxt'
NUM_CLASSES = 3
PATH_TO_TEST_RES_IMAGES_DIR = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/images/test_res' # 'images/lep_res'#tz/gost-23118-99_res'#'tz/GOST_31937-2011_res'#'PubLayNet/images/examples_res'
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
coco_output = []
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for image_id in imgIds:
img_name = coco.loadImgs(image_id)[0]['file_name']
width = coco.loadImgs(image_id)[0]['height']
height = coco.loadImgs(image_id)[0]['width']
image = cv2.imread(os.path.join(data_dir, img_name))
image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#cv2.imshow("from_coco", image)
#cv2.waitKey()
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
scores = [s for s in scores[0] if s > 0.6]
num = len(scores)
boxes = boxes[0][:num]
classes = classes[0][:num]
for i in range(0, int(num)):
bbox = boxes[i]
boxes[i] = [float(min(bbox[0] * width, width)),
float(min(bbox[1] * height, height)),
float(min((bbox[2]) * width, width)),
float(min((bbox[3]) * height, height))]
# Visualization of the results of a detection.
res = vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=False,
line_thickness=2)
cv2.imwrite(PATH_TO_TEST_RES_IMAGES_DIR + "/" + img_name,
cv2.cvtColor(res, cv2.COLOR_RGB2BGR))
annIds = coco.getAnnIds(imgIds=image_id)
anns = coco.loadAnns(annIds)
# write into file coco object detection output
for i in range(0, int(num)):
bbox = boxes[i]
bbox = [float(bbox[1]), float(bbox[0]), float(bbox[3] - bbox[1]), float(bbox[2] - bbox[0])]
coco_output.append(init_coco_format(image_id, int(classes[i]), bbox, float(scores[i])))
with open(os.path.join(output_coco_predict_file), 'w') as output_json_file:
json.dump(coco_output, output_json_file)
def evaluate(annotation_gt: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/test.json',
annotation_predict: str = '/home/ox/PycharmProjects/TF_ObjectDetection_API/CLAWDATA/jsons/predict_test.json'):
from pycocotools.cocoeval import COCOeval
pylab.rcParams["figure.figsize"] = (10.0, 8.0)
# inizialization
annType = ['segm', 'bbox', 'keypoints']
annType = annType[1] # specify type here
cocoGt = COCO(annotation_gt)
cocoDt = cocoGt.loadRes(annotation_predict)
# running evaluation
cocoEval = COCOeval(cocoGt, cocoDt, annType)
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
if __name__ == '__main__':
#test()
predict_test_from_coco_dataset()
evaluate()
\ No newline at end of file
test()
......@@ -6,6 +6,7 @@ import pandas as pd
from config_project import CUR_DATA_DIR
import os
from typing import Dict
from utils_convert_draw_format.ConverterCLAW import ConverterCLAW
def convert_one_image(filename: str, height: int, width: int, annotations: Dict, samples: Dict) -> List:
......@@ -60,6 +61,7 @@ def create_csv_annotation(json_path: str, path_images: str, out_csv: str):
if __name__ == "__main__":
create_csv_annotation(json_path=os.path.join(CUR_DATA_DIR, 'jsons/train.json'),
path_images=os.path.join(CUR_DATA_DIR, "images/train-1/"),
out_csv=os.path.join(CUR_DATA_DIR, "csv/train-1.csv"))
\ No newline at end of file
converter = ConverterCLAW()
create_csv_annotation(json_path=os.path.join(converter.WORK_DATA_DIR, 'jsons/train.json'),
path_images=os.path.join(converter.WORK_DATA_DIR, "images/train-1/"),
out_csv=os.path.join(converter.WORK_DATA_DIR, "csv/train-1.csv"))
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment