<jupyter_start><jupyter_text>Test DeepWay, a DNN able to Detect Waypoints for Autonomous Navigation in Row based CropsTest DeepWay to automatically detect waypoints from a occupancy grid map of a row based crop. The waypoints can be used to generate aa road map for a UGV. That combined with a local motion planner and data fusion localization methodologies can provide a robust and complete autonomous navigation in vineyards.<jupyter_code>%matplotlib inline
%reload_ext autoreload
%autoreload 2
# import some libraries
import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
import glob
import cv2
from tqdm.notebook import tqdm
from utils.visualization import plotData, plotHistory, plotDataRes, plotImages
from utils.tools_net import waypointProxSup, interpret, deepPathLoss, AP
from utils.tools import load_config, resizeAddCorrection
from utils.deep_way_net import build_deepway
# select a GPU and set memory growth 
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
# important paths and names
PATH_DIR = os.path.abspath('.')

TEST_DATA_PATH = os.path.join(PATH_DIR, 'dataset/test')
SATELLITE_DATA_PATH = os.path.join(PATH_DIR, 'dataset/satellite')
PATH_WEIGHTS = os.path.join(PATH_DIR, 'bin')

#name_model = 'deep_way_pretrained.h5'
name_model = 'deep_way.h5'

waypoint_file_name = 'waypoints.csv'
config_file = 'utils/config.json'
# important config
config = load_config(config_file)<jupyter_output><empty_output><jupyter_text>1.0 Import the Test Dataset<jupyter_code>def load_dataset_test(df, path_test):
    img_list = glob.glob(os.path.join(path_test, '*.png'))
    img_list = sorted(img_list,key=lambda s: int(s.split('/')[-1][3:-4])) #sort by name
    data_n = len(img_list)
    MASK_DIM = config['MASK_DIM'] 
    X = np.empty((data_n, MASK_DIM, MASK_DIM), dtype='float32')
    y = np.empty((data_n, MASK_DIM, MASK_DIM), dtype='float32')
    for index,img in tqdm(enumerate(img_list),total=data_n):
        name = img.split('/')[-1][:-4]
        mask = cv2.bitwise_not(cv2.imread(img, cv2.IMREAD_GRAYSCALE)) # open grayscale and invert 255
        points = df.loc[df['N_img'] == name].to_numpy()[:,1:].astype('uint32')
        points_x = points[:,0]
        points_y = points[:,1]
        mask_points = np.zeros((MASK_DIM,MASK_DIM))
        mask_points[points_y, points_x] = config['WAYP_VALUE']
        
        X[index,:,:] = mask
        y[index,:,:] = mask_points
    return X, resizeAddCorrection(y, config['WAYP_VALUE'], config['K'])
#img_folder = SATELLITE_DATA_PATH  #for satellite test set
img_folder = TEST_DATA_PATH       #for artificial test set

df_waypoints_test = pd.read_csv(os.path.join(img_folder, waypoint_file_name))
X_test, y_test = load_dataset_test(df_waypoints_test, img_folder)<jupyter_output><empty_output><jupyter_text>2.0 Import DeepWay<jupyter_code># load network
deepway_net = build_deepway(name_model, config['FILTERS'], config['KERNEL_SIZE'],
                            config['N'], config['MASK_DIM'])
deepway_net.load_weights(os.path.join(PATH_WEIGHTS, name_model))
name_model<jupyter_output><empty_output><jupyter_text>3.0 Test some Random Predictions<jupyter_code>index = 6 # choose a map
dist_thresh = 8 # waypoint suppression
conf_thresh = 0.3

pred = interpret(deepway_net.predict(X_test[index:index+1]),
                                 conf_thresh = conf_thresh, dist_thresh = dist_thresh,
                         waypoint_prox_sup=True, K=config['K'], MASK_DIM=config['MASK_DIM'])

plotData(X_test[index], pred[0])<jupyter_output><empty_output><jupyter_text>4.0 Plot Multiple Images<jupyter_code>index = 20
n = 3
dist_thresh = 8 # waypoint suppression
conf_thresh = 0.3

y_pred = interpret(deepway_net.predict(X_test[index:index+n]),
                                 conf_thresh = conf_thresh, dist_thresh = dist_thresh, waypoint_prox_sup=True,
                               K=config['K'], MASK_DIM=config['MASK_DIM'])

plotImages(X_test[index:index+n], y_pred, n)<jupyter_output><empty_output><jupyter_text>5.0 Compute AP<jupyter_code>DIST_RANGE_PERC = 2 # Rc
DIST_TRESH = 8 # waypoint suppression

rec_tot, prec_tot = AP(X_test, y_test, deepway_net, True, 
                       DIST_RANGE = DIST_RANGE_PERC, K=config['K'], MASK_DIM=config['MASK_DIM'], 
                       WAYP_VALUE = config['WAYP_VALUE'], dist_thresh = DIST_TRESH)
plt.scatter(rec_tot, prec_tot, c='r', s=15)
plt.plot(rec_tot, prec_tot, c='r')
plt.title(f"AP@{DIST_RANGE_PERC}: {np.mean(prec_tot):.4f}", )
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.grid()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11804670912951168, 'fraction_numerical': 0.011252653927813163, 'mean_word_length': 4.801724137931035, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '132909', 'license': 'MIT', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1659, 'n_words': 343, 'path': 'DeepWay Test.ipynb', 'repo_name': 'fsalv/DeepWay', 'size': '4710'}