# a = snaps[0] # nans_imgshow(a) # convert to greyscale greysnaps = nanrgb2grey(snaps) testimgs = nanrgb2grey(testimgs) testimg = testimgs[35] save_image('image-analysis/train.png', img) save_image('image-analysis/test.png', testimg) h1 = datamae['heading'][35] print('heading by mae:', h1) save_image('image-analysis/test-rotated-mae.png', rotate(h1, testimg)) heat = np.abs(testimgs[35] - greysnaps[127]) save_image('image-analysis/mae-heat.png', heat) h2 = datacc['heading'][35] print('heading by cc:', h2) save_image('image-analysis/test-rotated-cc.png', rotate(h2, testimg)) # plt.imshow(heat, cmap='hot', interpolation='nearest') # plt.show() index_mae = datamae['best_index'][35]
def log_error_points(route, traj, nav, thresh=0.5, route_id=1, target_path=None): if target_path: logs_path = os.path.join(target_path, 'route' + str(route_id)) else: logs_path = 'route' + str(route_id) os.makedirs(logs_path, exist_ok=True) # the antworld agent if not route.get('qimgs'): agent = antworld2.Agent() # get xy coords traj_xy = np.column_stack((traj['x'], traj['y'])) route_xy = np.column_stack((route['x'], route['y'])) rsims_matrices = nav.get_rsims_log() index_log = nav.get_index_log() degrees = nav.degrees # Loop through every test point for i in range(len(traj['heading'])): dist = np.squeeze( cdist(np.expand_dims(traj_xy[i], axis=0), route_xy, 'euclidean')) min_dist_i = np.argmin(dist) min_dist = dist[min_dist_i] route_match_i = index_log[i] if min_dist > thresh: point_path = os.path.join(logs_path, str(i)) os.mkdir(point_path) # Save best match window (or non) images if traj.get('window_log'): w = traj.get('window_log') for wi in range(w[0], w[1]): imgfname = route['filename'][wi] cv.imwrite(os.path.join(point_path, imgfname), route['imgs'][wi]) else: # If perfect memory is used imgfname = route['filename'][route_match_i] cv.imwrite(os.path.join(point_path, imgfname), route['imgs'][route_match_i]) # save the minimum distance image imgfname = 'img{}-mindist.png'.format(min_dist_i) cv.imwrite(os.path.join(point_path, imgfname), route['imgs'][min_dist_i]) # Save the query image rotated to the extractred direction h = traj['heading'][i] if route.get('qimgs'): rimg = rotate(h, route['qimgs'][i]) imgfname = 'rotated-grid-h' + str(h) + '.png' cv.imwrite(os.path.join(point_path, imgfname), rimg) imgfname = 'test-grid.png' cv.imwrite(os.path.join(point_path, imgfname), route['qimgs'][i]) else: #TODO: Rotating the image to the recovered heading is nto enough here as it is also # dependent on the heading of the previous lo9cation or in other word the location where the # test image was sampled. img = agent.get_img(traj_xy[i], h) # rimg = rotate(h, img) imgfname = str(h) + '.png' cv.imwrite(os.path.join(point_path, imgfname), img) # Save ridf rsim = rsims_matrices[i][route_match_i] fig = plt.figure() plt.plot(degrees, rsim) fig.savefig(os.path.join(point_path, 'rsim.png')) plt.close(fig) # Save window or full memory rsims heatmap fig = plt.figure() plt.imshow(rsims_matrices[i], cmap='hot') fig.savefig(os.path.join(point_path, 'heat.png')) plt.close(fig) path = os.path.join(point_path, 'map.png') plot_route_errors(route, traj, route_i=route_match_i, error_i=i, path=path)
route_id = 1 path = 'new-antworld/exp1/route' + str(route_id) + '/' route = load_route_naw(path, route_id=1, imgs=True) imgs = route['imgs'] im = cv.resize(imgs[0], (256, 35)) im = im.astype(np.int32) imm = ma.masked_array(im, mask) # plt.imshow(rotate(90, imm), cmap='gray') # plt.show() # Original image sims = rmf(im, im, d_range=(-180, 180)) imr = rotate(90, im) print(np.mean(np.abs(im - imr))) # Masked image masked_sims = rmf(imm, imm, d_range=(-180, 180)) print(np.ma.mean(imm - rotate(90, imm))) # Nan's image imn = imm.astype(np.float64) imn = imn.filled(np.nan) #nans_imgshow(rotate(90, imn)) print(np.nanmean(imn - rotate(90, imn))) nan_sims = rmf(imn, imn, matcher=nanmae, d_range=(-180, 180)) sims = np.stack([sims, masked_sims, nan_sims], axis=0) plot_multiline(sims, labels=['original', 'masked', 'nan-masked'])
imgs = [] for imgfile in filenames: img = cv.imread(os.path.join(data_path, imgfile)) imgs.append(cv.cvtColor(img, cv.COLOR_BGR2RGB)) imgs = rgb02nan(imgs) imgs = nanrgb2grey(imgs) # get the headings of eahc of the images sims = rmf(imgs[0], imgs, matcher=nanmae) headings = np.argmin(sims, axis=1) # Rotational similarity between the first image and itself rsims = [] for h in headings: rim = rotate(h, imgs[0]) rsims.append(nanmae(imgs[0], rim)) plt.scatter(headings, rsims, label='in-silica') minsims = np.min(sims, axis=1) plt.scatter(headings, minsims, label='actual rotation') # linking lines # for i, sim in enumerate(minsims): # plt.plot([headings[i], headings[i]], [minsims[i], rsims[i]]) plt.legend() plt.show() # masked = np.ma.masked_invalid(imgs[0]) # path = os.path.join(fwd, 'odk-mask.pickle')
# alternative way to convert to greyscale greysnaps2 = nanrbg2greyweighted(snaps) # a = greysnaps2[0] # nans_imgshow(a) sims = rmf(testimgs[35], greysnaps[127], matcher=nanmae, d_range=(-180, 180)) save_image('test.png', testimgs[35]) save_image('train.png', greysnaps[127]) index = np.argmin(sims) deg_range = (-180, 180) degrees = np.arange(*deg_range) h = int(degrees[index]) test_rotated = rotate(h, testimgs[35]) save_image('test rotated.png', test_rotated) plot_multiline(sims, xlabel='Degrees', ylabel='Image Difference') # plot_3d(sims, show=True) heat = np.abs(testimgs[35] - greysnaps[127]) plt.imshow(heat, cmap='hot', interpolation='nearest') plt.show() deg_range = (-90, 90) degrees = np.arange(*deg_range) mindiff = [] best_index = [] heading = [] data = {'best_index': [], 'mindiff': [], 'heading': [], 'rsims': []}
img = route.get_imgs()[0] deg_range = (-180, 180) degrees = np.arange(*deg_range) sims = [] for i, r in enumerate(degrees): fig = plt.figure(figsize=(10, 7)) plt.suptitle('deg={}'.format(r)) rows = 3 cols = 1 ax = fig.add_subplot(rows, cols, 1) ax.set_title('train') plt.imshow(img, cmap='gray') plt.axis('off') ax = fig.add_subplot(rows, cols, 2) ax.set_title('test') plt.imshow(rotate(r, img), cmap='gray') plt.axis('off') ax = fig.add_subplot(rows, cols, 3) ax.set_title('RMF') sims.append(mae(img, rotate(r, img))) plt.plot(degrees[:i + 1], sims) plt.xlim(-180, 180) fig.savefig('{}.png'.format(r + 180)) plt.close(fig)