def group_l2_data(): print('group_l2_data') train_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/l2_train' test_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/l2_test' utils.remake_dir(train_dir) utils.remake_dir(test_dir) data_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/l2_data' images = fnmatch.filter(os.listdir(data_dir), '*.png') train_count = 0 test_count = 0 for idx, imgname in enumerate(images): fullname = os.path.join(data_dir, imgname) if idx < len(images) * 0.1: shutil.copy(fullname, os.path.join(test_dir, imgname)) shutil.copy(fullname[:-3] + 'out', os.path.join(test_dir, imgname[:-3] + 'out')) test_count += 1 else: shutil.copy(fullname, os.path.join(train_dir, imgname)) shutil.copy(fullname[:-3] + 'out', os.path.join(train_dir, imgname[:-3] + 'out')) train_count += 1 print(train_count, test_count)
def test_sampling(): print('test_sampling') target_dir = 'fig_rst' utils.remake_dir('fig_rst') sample_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/seg_sampling' files = os.listdir(sample_dir) fig = utils.init_figure_with_idx(1, figsize=[4, 4]) for idx, tname in enumerate(files): if not tname.endswith('.png'): continue img_name = os.path.join(sample_dir, tname) img_org = misc.imread(img_name) img = misc.imresize(img_org, [144, 144, 3]) gt_file = open(img_name[:-3] + 'out', 'r') gt = np.load(gt_file) gt_file.close() fig.clf() draw_results_plot(img, gt) utils.save_fig_in_dir(fig, dirname=target_dir, filename=tname)
def gen_l2_data(): print('gen_l2_data') target_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/l2_data' utils.remake_dir(target_dir) sample_dir = '/mnt/hard-ext/yomkiru/Data/Strawberries/seg_sampling' files = os.listdir(sample_dir) rot_options = np.linspace(0, 360, 36, endpoint=False) flip_options = [False, True] for idx, tname in enumerate(files): if not tname.endswith('.png'): continue img_name = os.path.join(sample_dir, tname) img_org = misc.imread(img_name) img = misc.imresize(img_org, [144, 144, 3]) gt_file = open(img_name[:-3] + 'out', 'r') gt = np.load(gt_file) gt_file.close() for t_flip in flip_options: for t_rot in rot_options: t_gt = copy.deepcopy(gt) t_img = copy.deepcopy(img) if t_flip: t_img = t_img[:, ::-1] t_gt[0] = (t_img.shape[1] - t_gt[0]) t_img = utils.rotate_in_degrees(t_img, -t_rot) rotate_point(t_gt, math.radians(t_rot), t_img.shape) outfile_name = '%s_%d_%s' % (tname[:-4], t_rot, 'F' if t_flip else 'N') outfile_name = os.path.join(target_dir, outfile_name) misc.imsave(outfile_name + '.png', t_img) outfile = open(outfile_name + '.out', 'w') np.save(outfile, t_gt) outfile.close() print('created: %s' % outfile_name)
test_img = misc.imread('img_sample/test_1.png') rmax, cmax, _ = test_img.shape rsize, csize = 110, 110 box_list = list() img_list_1 = list() for ri in range(0, rmax - rsize + 20, 20): for ci in range(0, cmax - csize + 20, 20): t_img = test_img[ri:ri + rsize, ci:ci + csize] img_list_1.append(t_img) box_list.append(SlidingBox(ri, ci)) rst_1 = layer1.run_network(img_list_1) rst_dir_l1 = 'l1_rst' utils.remake_dir(rst_dir_l1) fig1 = utils.init_figure_with_idx(0, figsize=[10, 6]) sp1 = utils.imshow_in_subplot(1, 1, 1, test_img) img_list_2 = list() box_list_2 = list() for idx in range(len(img_list_1)): if rst_1[idx][0] > rst_1[idx][1]: # good img_list_2.append(img_list_1[idx]) box_list_2.append(box_list[idx]) sp1.add_patch( patches.Rectangle((box_list[idx].ci, box_list[idx].ri), csize, rsize, linewidth=1,
def group_data(l1_g_dir, l1_ng_dir, l1_train_dir, l1_test_dir): print('group_data') print(l1_g_dir, l1_ng_dir, l1_train_dir, l1_test_dir) utils.remake_dir(l1_train_dir) utils.remake_dir(os.path.join(l1_train_dir, 'g')) utils.remake_dir(os.path.join(l1_train_dir, 'ng')) utils.remake_dir(l1_test_dir) utils.remake_dir(os.path.join(l1_test_dir, 'g')) utils.remake_dir(os.path.join(l1_test_dir, 'ng')) g_files = os.listdir(l1_g_dir) ng_files = os.listdir(l1_ng_dir) for fidx, gf_name in enumerate(g_files): if fidx < len(g_files) * 0.1: shutil.copy(os.path.join(l1_g_dir, gf_name), os.path.join(l1_test_dir, 'g', gf_name)) else: shutil.copy(os.path.join(l1_g_dir, gf_name), os.path.join(l1_train_dir, 'g', gf_name)) for fidx, ngf_name in enumerate(ng_files): if fidx < len(ng_files) * 0.1: shutil.copy(os.path.join(l1_ng_dir, ngf_name), os.path.join(l1_test_dir, 'ng', ngf_name)) else: shutil.copy(os.path.join(l1_ng_dir, ngf_name), os.path.join(l1_train_dir, 'ng', ngf_name))