Panel disconected

This commit is contained in:
Daniel Saavedra
2020-03-17 14:20:58 -03:00
parent 337be0d7c7
commit dce4a1a2c3
10 changed files with 207 additions and 9 deletions

View File

@@ -1695,7 +1695,7 @@
" current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color=color, fill=False, linewidth=2))\n", " current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color=color, fill=False, linewidth=2))\n",
" current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':color, 'alpha':1.0})\n", " current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':color, 'alpha':1.0})\n",
" \n", " \n",
" I = orig_images[0]\n", " I = orig_images[0].copy()\n",
" score = np.sum(I[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / ((ymax-ymin)*(xmax-xmin))\n", " score = np.sum(I[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / ((ymax-ymin)*(xmax-xmin))\n",
" \n", " \n",
" boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score}\n", " boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score}\n",
@@ -2037,7 +2037,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.8" "version": "3.6.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -1,5 +1,9 @@
result_otros/ result_otros/
log_experimento_panel_gpu/ log_experimento_panel_gpu/
Result_Prueba/
Prueba/
Otros_2/
Otros/
*.jpg *.jpg
*.h5 *.h5

View File

@@ -0,0 +1,49 @@
{
"model" : {
"min_input_size": 400,
"max_input_size": 400,
"anchors": [5,7, 10,14, 15, 15, 26,32, 45,119, 54,18, 94,59, 109,183, 200,21],
"labels": ["panel"],
"backend": "keras-yolo3-master/full_yolo_backend.h5"
},
"train": {
"train_image_folder": "Train&Test_A/Train/images/",
"train_annot_folder": "Train&Test_A/Train/anns/",
"cache_name": "Result_yolo3_panel/train_panel.pkl",
"train_times": 1,
"batch_size": 2,
"learning_rate": 1e-3,
"nb_epochs": 500,
"warmup_epochs": 15,
"ignore_thresh": 0.5,
"gpus": "0,1",
"grid_scales": [1,1,1],
"obj_scale": 5,
"noobj_scale": 1,
"xywh_scale": 1,
"class_scale": 1,
"tensorboard_dir": "Result_yolo3_panel/log_experimento_panel_gpu",
"saved_weights_name": "Result_yolo3_panel/yolo3_full_panel.h5",
"debug": true
},
"valid": {
"valid_image_folder": "Train&Test_A/Test/images/",
"valid_annot_folder": "Train&Test_A/Test/anns/",
"cache_name": "Result_yolo3_panel/val_panel.pkl",
"valid_times": 1
},
"test": {
"test_image_folder": "Train&Test_A/Test/images/",
"test_annot_folder": "Train&Test_A/Test/anns/",
"cache_name": "Result_yolo3_panel/test_panel.pkl",
"test_times": 1
}
}

41
panel_disconnect.py Normal file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 17 13:55:42 2020
@author: dlsaavedra
"""
import numpy as np
def disconnect(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0):
new_boxes = []
for num, box in enumerate(boxes):
xmin = box.xmin + merge
xmax = box.xmax - merge
ymin = box.ymin + merge
ymax = box.ymax - merge
if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh:
area = (ymax - ymin)*(xmax - xmin)
z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
if area > area_min:
box.score = z_score
new_boxes.append(box)
#boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area}
mean_score = np.mean([box.score for box in new_boxes])
sd_score = np.std([box.score for box in new_boxes])
new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2]
for box in new_boxes:
z_score = (box.score - mean_score)/sd_score
box.classes[0] = min((z_score-2)*0.5+ 0.5, 1)
return new_boxes

View File

@@ -16,7 +16,7 @@ import numpy as np
def _main_(args): def _main_(args):
config_path = args.conf config_path = args.conf
input_path = args.input input_path = args.input
output_path = args.output output_path = args.output
@@ -30,7 +30,7 @@ def _main_(args):
# Set some parameter # Set some parameter
############################### ###############################
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.8, 0.3 obj_thresh, nms_thresh = 0.5, 0.3
############################### ###############################
# Load the model # Load the model

View File

@@ -15,8 +15,112 @@ from tqdm import tqdm
import numpy as np import numpy as np
def _main_(args): def disconnect(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0):
new_boxes = []
for num, box in enumerate(boxes):
xmin = box.xmin + merge
xmax = box.xmax - merge
ymin = box.ymin + merge
ymax = box.ymax - merge
if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh:
area = (ymax - ymin)*(xmax - xmin)
z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
if area > area_min:
box.score = z_score
new_boxes.append(box)
#boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area}
mean_score = np.mean([box.score for box in new_boxes])
sd_score = np.std([box.score for box in new_boxes])
new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2]
for box in new_boxes:
z_score = (box.score - mean_score)/sd_score
box.classes[0] = min((z_score-2)*0.5+ 0.5, 1)
return new_boxes
def disconnect_plot(image, boxes, obj_thresh = 0.5, area_min = 400, merge = 0):
new_boxes = []
for num, box in enumerate(boxes):
xmin = box.xmin + merge
xmax = box.xmax - merge
ymin = box.ymin + merge
ymax = box.ymax - merge
if xmin > 0 and ymin > 0 and xmax < image.shape[1] and ymax < image.shape[0] and box.classes[0] > obj_thresh:
area = (ymax - ymin)*(xmax - xmin)
z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
if area > area_min:
box.score = z_score
new_boxes.append(box)
#boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score, 'area' : area}
mean_score = np.mean([box.score for box in new_boxes])
sd_score = np.std([box.score for box in new_boxes])
normal_score = ([box.score for box in new_boxes] - mean_score)/sd_score
# plt.figure()
# _ = plt.hist(normal_score, bins='auto') # arguments are passed to np.histogram
# plt.title("Histogram with 'auto' bins")
# plt.show()
#
# plt.figure()
# mean = np.mean([boxes_area_score[i]['area'] for i in boxes_area_score])
# sd = np.std([boxes_area_score[i]['area'] for i in boxes_area_score])
# normal = ([boxes_area_score[i]['area'] for i in boxes_area_score] - mean)/sd
# _ = plt.hist(normal, bins='auto') # arguments are passed to np.histogram
# plt.title("Histogram with 'auto' bins")
# plt.show()
new_boxes = [box for box in new_boxes if (box.score - mean_score)/sd_score > 2]
for box in new_boxes:
z_score = (box.score - mean_score)/sd_score
box.classes[0] = min((z_score-2)*0.5+ 0.5, 1)
colors = plt.cm.brg(np.linspace(0, 1, 21)).tolist()
plt.figure(figsize=(10,6))
plt.imshow(I,cmap = 'gray')
current_axis = plt.gca()
for box in new_boxes:
color = colors[2]
#boxes_area_score[key]['score_norm'] = (boxes_area_score[key]['score'] - mean) / sd
#z_score = (box.score - mean_score) / sd_score
#z_score = (boxes_area_score[key]['area'] )
### Escribe el z-score
#if z_score > 1:
current_axis.text((box.xmin + box.xmax)/2,
(box.ymin+ box.ymax)/2,
'%.2f' % box.classes[0], size='x-large',
color='white', bbox={'facecolor':color, 'alpha':1.0})
return new_boxes
def _main_(args):
config_path = args.conf config_path = args.conf
input_path = args.input input_path = args.input
output_path = args.output output_path = args.output
@@ -122,6 +226,7 @@ def _main_(args):
times = [] times = []
for image_path in image_paths: for image_path in image_paths:
image = cv2.imread(image_path) image = cv2.imread(image_path)
print(image_path) print(image_path)
start = time.time() start = time.time()
@@ -130,10 +235,9 @@ def _main_(args):
print('Elapsed time = {}'.format(time.time() - start)) print('Elapsed time = {}'.format(time.time() - start))
times.append(time.time() - start) times.append(time.time() - start)
# draw bounding boxes on the image using labels # draw bounding boxes on the image using labels
for box in boxes: I = image.copy()
draw_boxes(I, boxes, config['model']['labels'], obj_thresh)
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)
# write the image with bounding boxes to file # write the image with bounding boxes to file
cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))