From dce4a1a2c30080968a47f7c72755282787154344 Mon Sep 17 00:00:00 2001 From: Daniel Saavedra Date: Tue, 17 Mar 2020 14:20:58 -0300 Subject: [PATCH] Panel disconected --- Result_ssd7_panel/Panel_Detector.ipynb | 4 +- Result_yolo3_panel/.gitignore | 4 + config_full_yolo_panel_infer.json | 49 ++++++++ .../utils/__pycache__/__init__.cpython-36.pyc | Bin 172 -> 183 bytes .../utils/__pycache__/bbox.cpython-36.pyc | Bin 2650 -> 2661 bytes .../utils/__pycache__/colors.cpython-36.pyc | Bin 1721 -> 1732 bytes .../utils/__pycache__/utils.cpython-36.pyc | Bin 9086 -> 9097 bytes panel_disconnect.py | 41 +++++++ predict_yolo3.py | 4 +- predict_yolo3_disconnect.py | 114 +++++++++++++++++- 10 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 config_full_yolo_panel_infer.json create mode 100644 panel_disconnect.py diff --git a/Result_ssd7_panel/Panel_Detector.ipynb b/Result_ssd7_panel/Panel_Detector.ipynb index feee2d8..1e34fa5 100644 --- a/Result_ssd7_panel/Panel_Detector.ipynb +++ b/Result_ssd7_panel/Panel_Detector.ipynb @@ -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.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':color, 'alpha':1.0})\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", " \n", " boxes_area_score[str(num)] = {'xmin': xmin, 'xmax': xmax, 'ymin': ymin, 'ymax': ymax, 'score' : score}\n", @@ -2037,7 +2037,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.6.5" } }, "nbformat": 4, diff --git a/Result_yolo3_panel/.gitignore b/Result_yolo3_panel/.gitignore index 75fc3d2..08a4b76 100644 --- a/Result_yolo3_panel/.gitignore +++ b/Result_yolo3_panel/.gitignore @@ -1,5 +1,9 @@ result_otros/ log_experimento_panel_gpu/ +Result_Prueba/ +Prueba/ +Otros_2/ +Otros/ *.jpg *.h5 diff --git a/config_full_yolo_panel_infer.json b/config_full_yolo_panel_infer.json new file mode 100644 index 0000000..ad43da3 --- /dev/null +++ b/config_full_yolo_panel_infer.json @@ -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 + } +} diff --git a/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/__init__.cpython-36.pyc index 9e1ed72b4b05d4ed9fa3ae4ee2b9afb703aa4205..a8f73ec6d5f75c39b9ac35be4e847509e7e31ebe 100644 GIT binary patch delta 67 zcmZ3(xSf&1n3tDJImk9{B8Rztihf3ZZmNDtPH|#lS!zmAqP|ONadt_5fqqbGUP)p~ WQGQ;kUUH6ZVy13MYH`WLWKRIXY8Ji# delta 56 zcmdnaxQ3C#n3tDpmW^ceL=JP6Nd1ib+*JLP9Nm=E;_QGP^n3tDJImk9Hnwx>)@x+Ou8J9^nZxY=v67lCAMv6Q4%ltfMTK1t%xw zq=GdS=})fYIL8<@*@JVXjR(*aO~w#Su3M~mrMU%_MM5A8nDUE@EIP4Zs$O!AZepfxNosM)=JhOGOaK}I7n1-0 delta 59 zcmX@YyOWp0n3tDpmW^ceMvi6{m2mxx{M=OilpNiZ)Z*-t`~rO!C>@lVSCW`gl%JQn Jc^3;8696m~6Ttug diff --git a/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc b/keras-yolo3-master/utils/__pycache__/utils.cpython-36.pyc index b818292e7800337f486f60336d7069deac1c4a61..fe277b7d4a34fa977c9b10437ea0af171d5d3a46 100644 GIT binary patch delta 1132 zcmYk5&rcIU6vsPlX)CoLQmR-5X`AXIP_^}U6n`No{zMX!MO~-cfo^Pf$(t<{4|>*{ z&c(!o2TvZg@elB(i3#^jG@eY1iJrZn?`;*`WIyxf&6_vheQ$Q{+{4LOF_GxZ&K$q} zDmKUpyij%OqEM+x&X+_5c;T#&cU@<(Fe7Z2SHQ8wm{}X;)~G9_+nQ!C;!IaRHl~sB z#4z$;;tBG7qJ&&a)TZ?~w{6GeuI1QL-;8O2F9>51glS3N*FDc`58m<@R zwpn#R!a)jI{|-H}09M7Q{u!4szHF48CS0Jf)9PJnFPl<7Q)M=%Zf^NCe+k_S_~P?< z94_JjjBo{^MS;}7NrV?Nm-c%R)Mech{ug>-?5l)P3XFLjRvkO^PY0Z%ofEW^8M=DA zbvDd#^7#8URqWr+40W|%W98Pp{{8F$X@DSs&i@)gXr1k&{mT0F)M(u=FXhoU;a#X$ zrfWF1QRc2$g*ghCBupc;UO0e*7x8t;|Afs_h+j172%HIEjo~brcT_1omh$3$J}Mes zpNW#WzBiKtv>?52c z6cJj~V;pXvrK{C}0vl1k1_s$kYjE&b@Dz36ZyWq{anrF)?g|^kPt-dX(8&@^1!fE9--sxQahbqrSM(zW1Hz(8FcAav?PRUqDx% dG6Q4dG+l2{yI<5xC=;@T5dulZvxc*?zX4S`8X*7x delta 1044 zcmYk4OK%cU6vw%gSD{dmmfDbjAR(Q`mfF&XR&AP?)<4+W{M@MYjS&o?D`02zk4FUuzwgh?|%vW z;?DrP1GQ`1Cu*8*il(A#20tz~Whz$We4WaYVpzG}vRE@9qZ^8;G-ZPaOxpN=?lL5u zLY{pHOhVOPfkoiO-Zx~a{k``xAw%p;aK?4sX0y=?Ob>>m+|$bI<+350+}+9>l30_o zo}Ggf+YK(0&GygWLy{T7MO%n|L<%7w900*XS1_IhaF15ZOEuAeHg-c3U zr%$kx0-Fj)`$kaahDNJmU8dc*GR3Ol6d7eN!UH4cFwtoeg{}%!O8M&kxSe%D{i5EY zS(L4?&*3q$#T)}gqOr=r-|}5ZSKx)`+Z??E10{$8KyXvsn=S)*m((2RF6cseAY1># z9dJ?x9YdjqI~5h|Kk9?t#FcBflH@u2cw(J2SbXp-q3rgcK$`7mgX828?oRCp+%PUCmW#s)Gp7J);?cA72=?rPtr9> zG-VCmjuQ!#U|sIquF6#AT}DF{T=WX&ud>(iAu`QA#UsEU@j}pQ$|0&^i 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 \ No newline at end of file diff --git a/predict_yolo3.py b/predict_yolo3.py index aff26b6..c0e9de2 100755 --- a/predict_yolo3.py +++ b/predict_yolo3.py @@ -16,7 +16,7 @@ import numpy as np def _main_(args): - + config_path = args.conf input_path = args.input output_path = args.output @@ -30,7 +30,7 @@ def _main_(args): # Set some parameter ############################### 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 diff --git a/predict_yolo3_disconnect.py b/predict_yolo3_disconnect.py index a2680e0..167f14e 100755 --- a/predict_yolo3_disconnect.py +++ b/predict_yolo3_disconnect.py @@ -15,8 +15,112 @@ from tqdm import tqdm 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 input_path = args.input output_path = args.output @@ -122,6 +226,7 @@ def _main_(args): times = [] for image_path in image_paths: + image = cv2.imread(image_path) print(image_path) start = time.time() @@ -130,10 +235,9 @@ def _main_(args): print('Elapsed time = {}'.format(time.time() - start)) times.append(time.time() - start) # draw bounding boxes on the image using labels - for box in boxes: - - draw_boxes(image, boxes, config['model']['labels'], obj_thresh) - + I = image.copy() + draw_boxes(I, boxes, config['model']['labels'], obj_thresh) + # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))