def __get_point_distances(self, mytank, obstacle): d1 = get_distance(Point(obstacle[0]), mytank) d2 = get_distance(Point(obstacle[1]), mytank) d3 = get_distance(Point(obstacle[2]), mytank) d4 = get_distance(Point(obstacle[3]), mytank) return [d1, d2, d3, d4]
def arrange(lines): """ Delete overlapping lines, connect incontinuous lines. Inputs: - lines: List of lines to be rearranged. Each line is represent as a numpy array [P.x, P.y, Q.x, Q.y], where P and Q are the endpoints of the line. """ flag = False while not flag: flag = True for line1 in lines: if not flag: break for line2 in lines: if line1 is line2: continue p = [[gm.Point(*line[:2]), gm.Point(*line[2:])] for line in (line1, line2)] l = [gm.determine_linear_equation(*p[i]) for i in range(2)] if gm.get_distance(l[1], p[0][0]) <= delta_e and gm.get_distance( l[1], p[0][1]) <= delta_e: ends = p[0] pl = [ gm.get_perpendicular_line(l[0], p[1][i]) for i in range(2) ] out = [ not ((gm.f(pl[i], p[0][0]) > 0) ^ (gm.f(pl[i], p[0][1]) > 0)) for i in range(2) ] if out[0] and out[1] and not ((gm.f(pl[0], p[0][0]) > 0) ^ (gm.f(pl[1], p[0][0]) > 0)): flag = False for i in range(2): if flag: break for j in range(2): if ends[i] - p[1][j] <= delta_v: ends[i] = p[1][1 - j] flag = True break else: for i in range(2): if out[i]: ends[i] = p[1][i] remove(lines, line1) remove(lines, line2) lines.append( np.array(ends[0].to_tuple() + ends[1].to_tuple())) flag = False break
def draw_nose(img, keypoints): if keypoints is None: return img if len(keypoints) == 17: keypoints = get_updated_keypoints(keypoints) center = (keypoints["nose"][0], keypoints["nose"][1]) hcenter = (keypoints["head_center"][0], keypoints["head_center"][1]) radius = int(get_distance(center, hcenter) / 2) color = (0, 0, 255) img = cv2.circle(img=img, center=center, radius=radius, color=color, thickness=-1, lineType=cv2.LINE_AA) return img
adv_acc_pos = 0 adv_fool = 0 # Post-training print('Loading best weights') model.load_weights(logdir + 'model_best_val_weights.h5') test_loss = model.evaluate(x_test, x_test, batch_size=batch_size, verbose=0) print('Test loss: {:.2f}'.format(test_loss)) # KNN test def sample_points(data_, labels_, n_): target_idxs_ = [] for l_ in np.unique(labels_): t_ = np.argwhere(labels_ == l_).reshape(-1) np.random.shuffle(t_) target_idxs_.append(t_[:n_]) target_idxs_ = np.hstack(target_idxs_) return data_[target_idxs_], labels_[target_idxs_] embeddings_train = clip(encoder.predict(x_train), radius) embeddings_test = clip(encoder.predict(x_test), radius) knn = KNeighborsClassifier(n_neighbors=5, metric=get_distance(radius)) for n_labels in [100, 600, 1000]: knn.fit(*sample_points(embeddings_train, y_train, n_labels)) knn_score = knn.score(embeddings_test, y_test) print('KNN score ({} labels): {}'.format(n_labels, knn_score))