def predict(net, filename): '''Run neural net on images from image_set''' file_name = PREFIX_IMAGES + filename + '.png' pred = nn.classify(file_name, net, NN_PROB_THRESH) car_preds = [] for p in pred: if p[0] == 0: car_preds.append( [0, p[1]] + [int(p[2][0]), int(p[2][1]), int(p[2][2]), int(p[2][3])]) # car_preds.append([int(p[2][0]), int(p[2][1]), int(p[2][2]), int(p[2][3])]) for p in car_preds: scale_x = 1.5385 scale_y = 3.125 p[2] = int(p[2] * scale_x) p[3] = int(p[3] * scale_y) p[4] = int(p[4] * scale_x) p[5] = int(p[5] * scale_y) # print("car_preds: ") # print(car_preds) return car_preds
def predict(net, image_set): '''Run neural net on images from image_set''' with open(image_set, "r") as f: content = f.readlines() images = [c.strip() for c in content] predictions = dict() for i in images: file_name = PREFIX_IMAGES + i + '.png' pred = nn.classify(file_name, net, NN_PROB_THRESH) boxes = [] for p in pred: if p[0] == 0: boxes += [list(p[2])] for box in boxes: # Matrix #scale_x = 1.5336 #scale_y = 2.7396 # Scenic scale_x = 1.5385 scale_y = 3.125 box[0] = int(box[0] * scale_x) box[1] = int(box[1] * scale_y) box[2] = int(box[2] * scale_x) box[3] = int(box[3] * scale_y) predictions[i] = boxes return predictions
def predict(net, image_set): '''Run neural net on images from image_set''' with open(image_set, "r") as f: content = f.readlines() images = [c.strip() for c in content] predictions = dict() for i in images: file_name = PREFIX_IMAGES + i + '.jpg' pred = nn.classify(file_name, net, NN_PROB_THRESH) car_preds = [] for p in pred: if p[0] == 0: car_preds.append([0, p[1]] + [int(p[2][0]), int(p[2][1]), int(p[2][2]), int(p[2][3])]) for p in car_preds: # Matrix scale_x = 1.5336 scale_y = 2.7396 # Scenic #scale_x = 1.5385 #scale_y = 3.125 p[2] = int(p[2]*scale_x) p[3] = int(p[3]*scale_y) p[4] = int(p[4]*scale_x) p[5] = int(p[5]*scale_y) predictions[i] = car_preds return predictions
def predict(net, image_set): '''Run neural net on images from image_set''' with open(image_set, "r") as f: content = f.readlines() images = [c.strip() for c in content] predictions = dict() for i in images: (boxes, probs, _) = nn.classify(PREFIX_IMAGES + i + '.png', net, NN_PROB_THRESH) predictions[i] = boxes return predictions
#CHECKPOINT = '/home/tommaso/scenicEx/data/scenic/checkpoints/train/model.ckpt-5500' #CHECKPOINT = '/home/tommaso/scenicEx/data/matrix/checkpoints_overlap_250/train/model.ckpt-32670' #CHECKPOINT = '/home/tommaso/scenicEx/data/matrix/checkpoints/train/model.ckpt-5250' CHECKPOINT = '/home/tommaso/squeezeDet/data/webots/train/checkpoints/train/model.ckpt-1150' FILE = '/home/tommaso/squeezeDet/data/webots/test/images/1003.png' PREFIX = '/home/tommaso/scenicEx/data/matrix/' PREFIX_LABELS = PREFIX + 'labels/' PREFIX_IMAGES = PREFIX + 'images/' image_set = '/home/tommaso/scenicEx/data/matrix/ImageSets/test_overlap.txt' net = nn.init(CHECKPOINT) pred = nn.classify(FILE, net, NN_PROB_THRESH) print(pred) #ap, recall = eval_set(net, image_set) # avg_precs = [] # avg_recs = [] # max_precs = [] # max_recs = [] # # for i in range(1,9,1): # precs = [] # recs = [] # for j in range(4950,5020,10): # checkpoint_path = '/home/tommaso/scenicEx/data/matrix/checkpoints_overlap_250/checkpoints_overlap_250_' + str(i) + '/train/model.ckpt-' + str(j) # print(checkpoint_path) # net = nn.init(checkpoint_path)
'''Compute precision and recall''' tp = sum(d != -1 for d in detects) fp = sum(d == -1 for d in detects) fn = sum(g not in detects for g in range(n_gt)) print((tp, fp, fn)) prec = tp / float(tp + fp) rec = tp / float(tp + fn) return prec, rec def precision_recall(preds, gt_boxes, iou_tresh): detects = pred_2_detect(preds, gt_boxes, iou_tresh) return prec_rec(detects, len(gt_boxes)) checkpoint = '/home/tommaso/analyzeNN/data/train_0/checkpoint/train/model.ckpt-5000' net = nn.init(checkpoint) tmp_img_file_name = "/home/tommaso/analyzeNN/data/test/images/h_m_0_3_000006.png" gt_b_1 = edge_2_cent([641, 181, 675, 209]) gt_b_2 = edge_2_cent([598, 178, 644, 214]) gt_b_3 = edge_2_cent([514, 162, 604, 236]) gt_boxes = [gt_b_1, gt_b_2, gt_b_3] (boxes, probs, cats) = nn.classify(tmp_img_file_name, net)
def main(argv): print(argv) TEST_NN = True reaction_time = 0.75 if TEST_NN else float(argv[1]) cruising_speed = 15.0 if TEST_NN else float(argv[2]) print(reaction_time) steering_constant = 35.0 * math.pow(cruising_speed, -1.442) mean = 0.55 var = 0.5 # First the car does lane keeping for about 1.5 secs (50 iters) driver.setCruisingSpeed(cruising_speed) driver.setSteeringAngle(0) driver.step() # * np.sqrt(2 * np.pi * var) x_start = mean + np.sqrt(2 * var) * erfinv(-0.99) x_end = mean + np.sqrt(2 * var) * erfinv(0.99) max_iters = int(math.ceil((x_end - x_start) / delta_t)) reaction_iters = int(math.ceil(reaction_time / delta_t)) print("Mean, var, reaction time: ", mean, var, reaction_time) d = np.inf iter = 0 k = 2 offset_center = 0 print("Mode: LANE KEEPING") #while d > k*cruising_speed: while d > 15.: #while True: if iter % DETECT_FREQ == 1: frame = get_camera_image(camera_lk, IMG_W_LK, IMG_H_LK) frame = np.array(frame.convert('RGB')) try: offset_center = process_pipeline(frame, keep_state=False) except: print('Lane assistant: Skipping frame') offset_center = 0 steer_angle = offset_center * 0.5 driver.setSteeringAngle(steer_angle) image_cones = get_camera_image(camera_cnn, IMG_W_CNN, IMG_H_CNN) # convert to a openCV2 image cv2_image = cv2.cvtColor(np.array(image_cones), cv2.COLOR_RGB2BGR) # call neural net pred = nn.classify(cv2_image, net, NN_PROB_THRESH) d = estimate_dist(pred) print("CNN estimated distance from cones: ", d) iter += 1 driver.step() # Now start the gaussian maneuver x_current = x_start for _ in range(reaction_iters): driver.setCruisingSpeed(cruising_speed) driver.setSteeringAngle(0) driver.step() driver.setCruisingSpeed(cruising_speed) driver.setSteeringAngle(normal_steering(x=x_current, mean=mean, var=var)) print("Mode: LANE CHANGE") for iter in range(max_iters + 1): driver.setCruisingSpeed(cruising_speed) if iter % 8 == 0: x_current = x_start + iter * delta_t print( x_current, steering_constant * normal_steering(x=x_current, mean=mean, var=var), x_current - mean) driver.setSteeringAngle( -steering_constant * normal_steering(x=x_current, mean=mean, var=var)) driver.step() # Now do lane keeping again print("Mode: LANE KEEPING") iter = 0 while driver.step() != -1: if iter % 8 == 0: frame = get_camera_image(camera_lk, IMG_W_LK, IMG_H_LK) frame = np.array(frame.convert('RGB')) try: offset_center = process_pipeline(frame, keep_state=False) except: print('Lane assistant: Skipping frame') offset_center = 0 steer_angle = offset_center * 0.5 driver.setSteeringAngle(steer_angle) iter += 1