def proposed_test_each(op, data, size): acc_list = [] auc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] line_str = op(path, img, mask, size) line_str_norm = gray_norm(line_str, mask) bin = find_best_thresh(line_str_norm, ground, mask)[1] temp_acc = [] for optic_thresh in range(1, 255): optic = cached_optic_norm(path, img, mask, size) optic = cv2.threshold(optic, optic_thresh, 255, cv2.THRESH_BINARY)[1] optic = cv2.erode(optic, np.ones((3, 3), np.uint8), iterations=1) bin_subtract = bin.copy() bin_subtract[optic == 255] = 0 acc = accuracy(ground, bin_subtract) temp_acc.append(acc) best_optic_thresh = np.argmax(temp_acc) + 1 optic = cached_optic_norm(path, img, mask, size) optic = cv2.threshold(optic, best_optic_thresh, 255, cv2.THRESH_BINARY)[1] optic = cv2.erode(optic, np.ones((3, 3), np.uint8), iterations=1) temp_acc = [] for proposed_thresh in range(1, 255): min_window = proposed_norm(path, img, mask, size) min_window = cv2.threshold(min_window, proposed_thresh, 255, cv2.THRESH_BINARY)[1] bin_subtract = bin.copy() bin_subtract[optic == 255] = 0 bin_subtract[min_window == 255] = 0 acc = accuracy(ground, bin_subtract) temp_acc.append(acc) best_acc = np.max(temp_acc) acc_list.append(best_acc) return np.mean(acc_list)
def optic_train(op, thresh, data, size): avg_acc_list = [] for optic_thresh in range(1, 255): acc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] optic = cached_optic_norm(path, img, mask, size) optic = cv2.threshold(optic, optic_thresh, 255, cv2.THRESH_BINARY)[1] optic = cv2.erode(optic, np.ones((3, 3), np.uint8), iterations=1) line_str = op(path, img, mask, size) line_str[optic == 255] = line_str[mask == 255].min() line_str = gray_norm(line_str, mask) bin = cv2.threshold(line_str, thresh, 255, cv2.THRESH_BINARY)[1] bin_fov = bin[mask == 255] ground_fov = ground[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) avg_acc = np.mean(acc_list) avg_acc_list.append(avg_acc) best = np.argmax(avg_acc_list) return best + 1, avg_acc_list[best]
def proposed_test(op, data, thresh, optic_thresh, proposed_thresh): size = 15 acc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] line_str = op(path, img, mask, size) line_str = gray_norm(line_str, mask) bin = cv2.threshold(line_str, thresh, 255, cv2.THRESH_BINARY)[1] optic = cached_optic_norm(path, img, mask, size) optic = cv2.threshold(optic, optic_thresh, 255, cv2.THRESH_BINARY)[1] optic = cv2.erode(optic, np.ones((3, 3), np.uint8), iterations=1) bin[optic == 255] = 0 min_window = proposed_norm(path, img, mask, size) min_window = cv2.threshold(min_window, proposed_thresh, 255, cv2.THRESH_BINARY)[1] bin[min_window == 255] = 0 bin_fov = bin[mask == 255] ground_fov = ground[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) return np.mean(acc_list)
def line_with_training(): train_data = DriveDatasetLoader('D:/Datasets/DRIVE', 10).load_train() test_data = DriveDatasetLoader('D:/Datasets/DRIVE', 10).load_test() # op = cached_single_norm op = cached_multi_norm size = 15 timer = Timer() green('line_with_training') timer.start('Train') thresh, train_acc = find_best_acc(op, train_data, size) train_auc = calc_auc(train_data, op, size) timer.stop() timer.start('Test') test_acc = accuracy(op, test_data, thresh, size) test_auc = calc_auc(test_data, op, size) timer.stop() green(f'Threshold: {thresh}') green(f'Train average accuracy: {train_acc}') green(f'Train average AUC: {train_auc}') green(f'Test average accuracy: {test_acc}') green(f'Test average AUC: {test_auc}')
def basic_test(op, data, thresh, size): acc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] line_str = op(path, img, mask, size) bin = cv2.threshold(line_str, thresh, 255, cv2.THRESH_BINARY)[1] bin_fov = bin[mask == 255] ground_fov = ground[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) return np.mean(acc_list)
def find_best_thresh(line_str, ground, mask): line_str = gray_norm(line_str, mask) ground_fov = ground[mask == 255] acc_list = [] img_list = [] for t in range(1, 255): bin = cv2.threshold(line_str, t, 255, cv2.THRESH_BINARY)[1] bin_fov = bin[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) img_list.append(bin) best = np.argmax(acc_list) return best + 1, img_list[best], acc_list[best]
def proposed_train(op, thresh, optic_thresh, data, size): avg_acc_list = [] for proposed_thresh in range(1, 255): acc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] line_str = op(path, img, mask, size) line_str = gray_norm(line_str, mask) bin = cv2.threshold(line_str, thresh, 255, cv2.THRESH_BINARY)[1] optic = cached_optic_norm(path, img, mask, size) optic = cv2.threshold(optic, optic_thresh, 255, cv2.THRESH_BINARY)[1] optic = cv2.erode(optic, np.ones((3, 3), np.uint8), iterations=1) bin[optic == 255] = 0 min_window = proposed_norm(path, img, mask, size) min_window = cv2.threshold(min_window, proposed_thresh, 255, cv2.THRESH_BINARY)[1] bin[min_window == 255] = 0 bin_fov = bin[mask == 255] ground_fov = ground[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) # cv2.imshow('Before proposed', bin) # cv2.imshow('Line', line_str) # cv2.imshow('Min-window', min_window) # cv2.imshow('Proposed', bin) # cv2.waitKey(0) # cv2.destroyAllWindows() # return avg_acc = np.mean(acc_list) avg_acc_list.append(avg_acc) best = np.argmax(avg_acc_list) return best + 1, avg_acc_list[best]
def basic_train(op, data, size): avg_acc_list = [] for thresh in range(1, 255): acc_list = [] for path, img, mask, ground in data: img = 255 - img[:, :, 1] line_str = op(path, img, mask, size) bin = cv2.threshold(line_str, thresh, 255, cv2.THRESH_BINARY)[1] bin_fov = bin[mask == 255] ground_fov = ground[mask == 255] acc = accuracy(bin_fov, ground_fov) acc_list.append(acc) avg_acc = np.mean(acc_list) avg_acc_list.append(avg_acc) best = np.argmax(avg_acc_list) return best + 1, avg_acc_list[best]