show_offset.py
Go to the documentation of this file.
00001 # --------------------------------------------------------
00002 # Deformable Convolutional Networks
00003 # Copyright (c) 2017 Microsoft
00004 # Licensed under The Apache-2.0 License [see LICENSE for details]
00005 # Written by Guodong Zhang
00006 # --------------------------------------------------------
00007 
00008 import matplotlib.pyplot as plt
00009 import numpy as np
00010 
00011 def show_boxes_simple(bbox, color='r', lw=2):
00012     rect = plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0],
00013                           bbox[3] - bbox[1], fill=False, edgecolor=color, linewidth=lw)
00014     plt.gca().add_patch(rect)
00015 
00016 def kernel_inv_map(vis_attr, target_point, map_h, map_w):
00017     pos_shift = [vis_attr['dilation'] * 0 - vis_attr['pad'],
00018                  vis_attr['dilation'] * 1 - vis_attr['pad'],
00019                  vis_attr['dilation'] * 2 - vis_attr['pad']]
00020     source_point = []
00021     for idx in range(vis_attr['filter_size']**2):
00022         cur_source_point = np.array([target_point[0] + pos_shift[idx / 3],
00023                                      target_point[1] + pos_shift[idx % 3]])
00024         if cur_source_point[0] < 0 or cur_source_point[1] < 0 \
00025                 or cur_source_point[0] > map_h - 1 or cur_source_point[1] > map_w - 1:
00026             continue
00027         source_point.append(cur_source_point.astype('f'))
00028     return source_point
00029 
00030 def offset_inv_map(source_points, offset):
00031     for idx, _ in enumerate(source_points):
00032         source_points[idx][0] += offset[2*idx]
00033         source_points[idx][1] += offset[2*idx + 1]
00034     return source_points
00035 
00036 def get_bottom_position(vis_attr, top_points, all_offset):
00037     map_h = all_offset[0].shape[2]
00038     map_w = all_offset[0].shape[3]
00039 
00040     for level in range(vis_attr['plot_level']):
00041         source_points = []
00042         for idx, cur_top_point in enumerate(top_points):
00043             cur_top_point = np.round(cur_top_point)
00044             if cur_top_point[0] < 0 or cur_top_point[1] < 0 \
00045                 or cur_top_point[0] > map_h-1 or cur_top_point[1] > map_w-1:
00046                 continue
00047             cur_source_point = kernel_inv_map(vis_attr, cur_top_point, map_h, map_w)
00048             cur_offset = np.squeeze(all_offset[level][:, :, int(cur_top_point[0]), int(cur_top_point[1])])
00049             cur_source_point = offset_inv_map(cur_source_point, cur_offset)
00050             source_points = source_points + cur_source_point
00051         top_points = source_points
00052     return source_points
00053 
00054 def plot_according_to_point(vis_attr, im, source_points, map_h, map_w, color=[255,0,0]):
00055     plot_area = vis_attr['plot_area']
00056     for idx, cur_source_point in enumerate(source_points):
00057         y = np.round((cur_source_point[0] + 0.5) * im.shape[0] / map_h).astype('i')
00058         x = np.round((cur_source_point[1] + 0.5) * im.shape[1] / map_w).astype('i')
00059 
00060         if x < 0 or y < 0 or x > im.shape[1]-1 or y > im.shape[0]-1:
00061             continue
00062         y = min(y, im.shape[0] - vis_attr['plot_area'] - 1)
00063         x = min(x, im.shape[1] - vis_attr['plot_area'] - 1)
00064         y = max(y, vis_attr['plot_area'])
00065         x = max(x, vis_attr['plot_area'])
00066         im[y-plot_area:y+plot_area+1, x-plot_area:x+plot_area+1, :] = np.tile(
00067             np.reshape(color, (1, 1, 3)), (2*plot_area+1, 2*plot_area+1, 1)
00068         )
00069     return im
00070 
00071 
00072 
00073 def show_dpsroi_offset(im, boxes, offset, classes, trans_std=0.1):
00074     plt.cla
00075     for idx, bbox in enumerate(boxes):
00076         plt.figure(idx+1)
00077         plt.axis("off")
00078         plt.imshow(im)
00079 
00080         offset_w = np.squeeze(offset[idx, classes[idx]*2, :, :]) * trans_std
00081         offset_h = np.squeeze(offset[idx, classes[idx]*2+1, :, :]) * trans_std
00082         x1 = int(bbox[0])
00083         y1 = int(bbox[1])
00084         x2 = int(bbox[2])
00085         y2 = int(bbox[3])
00086         roi_width = x2-x1+1
00087         roi_height = y2-y1+1
00088         part_size = offset_w.shape[0]
00089         bin_size_w = roi_width / part_size
00090         bin_size_h = roi_height / part_size
00091         show_boxes_simple(bbox, color='b')
00092         for ih in range(part_size):
00093             for iw in range(part_size):
00094                 sub_box = np.array([x1+iw*bin_size_w, y1+ih*bin_size_h,
00095                                     x1+(iw+1)*bin_size_w, y1+(ih+1)*bin_size_h])
00096                 sub_offset = offset_h[ih, iw] * np.array([0, 1, 0, 1]) * roi_height \
00097                              + offset_w[ih, iw] * np.array([1, 0, 1, 0]) * roi_width
00098                 sub_box = sub_box + sub_offset
00099                 show_boxes_simple(sub_box)
00100         plt.show()
00101 
00102 def show_dconv_offset(im, all_offset, step=[2, 2], filter_size=3,
00103                       dilation=2, pad=2, plot_area=2, plot_level=3):
00104     vis_attr = {'filter_size': filter_size, 'dilation': dilation, 'pad': pad,
00105                 'plot_area': plot_area, 'plot_level': plot_level}
00106 
00107     map_h = all_offset[0].shape[2]
00108     map_w = all_offset[0].shape[3]
00109 
00110     step_h = step[0]
00111     step_w = step[1]
00112     start_h = np.round(step_h / 2)
00113     start_w = np.round(step_w / 2)
00114 
00115     plt.figure()
00116     for im_h in range(start_h, map_h, step_h):
00117         for im_w in range(start_w, map_w, step_w):
00118             target_point = np.array([im_h, im_w])
00119             source_y = np.round(target_point[0] * im.shape[0] / map_h)
00120             source_x = np.round(target_point[1] * im.shape[1] / map_w)
00121             if source_y < plot_area or source_x < plot_area \
00122                     or source_y >= im.shape[0] - plot_area or source_x >= im.shape[1] - plot_area:
00123                 continue
00124 
00125             cur_im = np.copy(im)
00126             source_points = get_bottom_position(vis_attr, [target_point], all_offset)
00127             cur_im = plot_according_to_point(vis_attr, cur_im, source_points, map_h, map_w)
00128             cur_im[source_y-plot_area:source_y+plot_area+1, source_x-plot_area:source_x+plot_area+1, :] = \
00129                 np.tile(np.reshape([0, 255, 0], (1, 1, 3)), (2*plot_area+1, 2*plot_area+1, 1))
00130 
00131 
00132             plt.axis("off")
00133             plt.imshow(cur_im)
00134             plt.show(block=False)
00135             plt.pause(0.01)
00136             plt.clf()


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30