-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/shannon surprise #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kasaiatsuki
wants to merge
13
commits into
feat/yolop_shortcut
Choose a base branch
from
feat/shannon_surprise
base: feat/yolop_shortcut
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3df79a6
シャノンサプライズの実装
Kasaiatsuki 14efc8d
自分の環境設定を削除
Kasaiatsuki 7089e64
シャノンサプライズの方法を変更
Kasaiatsuki de98363
パラメータの変更
Kasaiatsuki 2422c78
デプロイ
262691e
ノードが0に戻る問題を修正
Kasaiatsuki e728cf6
lambda1のフォールバックの問題を修正
Kasaiatsuki 55f89f1
リサイズ方式の不一致を修正
Kasaiatsuki 067ad62
デフォルト値を修正
Kasaiatsuki 6cf3a17
ウェイポイントの正規化範囲を修正で
Kasaiatsuki 3bba221
重みファイルの探索パスの修正
Kasaiatsuki e3fc8eb
高速化
Kasaiatsuki 9eff038
自己情報量をもちいてシャノンサプライズをする方式に変更
Kasaiatsuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| epochs: 200 | ||
| batch_size: 8 | ||
| learning_rate: 0.0002 | ||
| num_workers: 2 | ||
| weight_file: "e2e_model.pt" | ||
| epochs: 1000 | ||
| batch_size: 16 | ||
| learning_rate: 0.0001 | ||
| num_workers: 8 | ||
| weight_file: "e2e_model.pt" | ||
| split_seed: 0 | ||
| curve_surprise_weighting: | ||
| enabled: true | ||
| num_bins: 16 | ||
| smoothing: 1.0 | ||
| max_weight: 50.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from typing import Tuple | ||
|
|
||
| import cv2 | ||
| import numpy as np | ||
|
|
||
|
|
||
| PLACENET_CROP_SIZE = 288 | ||
| MODEL_INPUT_SIZE = (85, 85) | ||
|
|
||
|
|
||
| def center_square_crop(image: np.ndarray, crop_size: int = PLACENET_CROP_SIZE) -> np.ndarray: | ||
| height, width = image.shape[:2] | ||
| if height < crop_size or width < crop_size: | ||
| raise ValueError(f'Image is smaller than {crop_size}x{crop_size}: {width}x{height}') | ||
|
|
||
| top = (height - crop_size) // 2 | ||
| left = (width - crop_size) // 2 | ||
| return image[top:top + crop_size, left:left + crop_size] | ||
|
|
||
|
|
||
| def color_mask_to_binary(mask_image: np.ndarray) -> np.ndarray: | ||
| if mask_image.ndim == 2: | ||
| return (mask_image > 0).astype(np.uint8) | ||
|
|
||
| red_mask = ( | ||
| (mask_image[:, :, 2] > 200) | ||
| & (mask_image[:, :, 0] < 50) | ||
| & (mask_image[:, :, 1] < 50) | ||
| ) | ||
| bright_mask = mask_image.max(axis=2) > 127 | ||
| return (red_mask | bright_mask).astype(np.uint8) | ||
|
|
||
|
|
||
| def preprocess_lane_mask(mask: np.ndarray) -> np.ndarray: | ||
| binary_mask = color_mask_to_binary(mask) | ||
| height, width = binary_mask.shape[:2] | ||
| if height < PLACENET_CROP_SIZE or width < PLACENET_CROP_SIZE: | ||
| return cv2.resize(binary_mask, MODEL_INPUT_SIZE, interpolation=cv2.INTER_NEAREST) | ||
|
|
||
| cropped_mask = center_square_crop(binary_mask) | ||
| return cv2.resize(cropped_mask, MODEL_INPUT_SIZE, interpolation=cv2.INTER_NEAREST) | ||
|
|
||
|
|
||
| def lane_mask_to_tensor_array(mask: np.ndarray) -> np.ndarray: | ||
| return preprocess_lane_mask(mask).astype(np.float32) | ||
|
|
||
|
|
||
| def overlay_lane_mask(image_bgr: np.ndarray, processed_mask: np.ndarray) -> np.ndarray: | ||
| height, width = image_bgr.shape[:2] | ||
| if height < PLACENET_CROP_SIZE or width < PLACENET_CROP_SIZE: | ||
| cropped_image = image_bgr | ||
| else: | ||
| cropped_image = center_square_crop(image_bgr) | ||
| debug_image = cv2.resize(cropped_image, MODEL_INPUT_SIZE, interpolation=cv2.INTER_AREA) | ||
| debug_image[processed_mask == 1] = [0, 0, 255] | ||
| return debug_image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from typing import List, Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| def crop_images(image: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: | ||
| center_crop = image[:, 40:440] | ||
| right_crop = image[:, 80:480] | ||
| left_crop = image[:, 0:400] | ||
| return center_crop, right_crop, left_crop | ||
|
|
||
|
|
||
| def rotate_waypoints(waypoints: List[List[float]], angle: float) -> List[List[float]]: | ||
| cos_theta = np.cos(angle) | ||
| sin_theta = np.sin(angle) | ||
| rotation_matrix = np.array([[cos_theta, -sin_theta], | ||
| [sin_theta, cos_theta]]) | ||
|
|
||
| rotated_waypoints = [] | ||
| for waypoint in waypoints: | ||
| rotated = rotation_matrix @ np.array(waypoint) | ||
| rotated_waypoints.append(rotated.tolist()) | ||
|
|
||
| return rotated_waypoints | ||
|
|
||
|
|
||
| def augment(image: np.ndarray, waypoints: List[List[float]]) -> List[Tuple[np.ndarray, List[List[float]]]]: | ||
| center_crop, right_crop, left_crop = crop_images(image) | ||
|
|
||
| center_waypoints = waypoints | ||
| right_waypoints = rotate_waypoints(waypoints, 0.1745) | ||
| left_waypoints = rotate_waypoints(waypoints, -0.1745) | ||
|
|
||
| return [ | ||
| (center_crop, center_waypoints), | ||
| (right_crop, right_waypoints), | ||
| (left_crop, left_waypoints) | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from pathlib import Path | ||
| from typing import Optional, Tuple | ||
|
|
||
| import cv2 | ||
| import numpy as np | ||
| import torch | ||
|
|
||
|
|
||
| class YOLOPv2Processor: | ||
| def __init__(self, model_path: Path, device: torch.device, input_size: int = 640, use_fp16: bool = False): | ||
| self.device = device | ||
| self.input_shape = (input_size, input_size) | ||
| self.use_fp16 = use_fp16 and device.type == 'cuda' | ||
|
|
||
| if model_path.exists(): | ||
| self.model = torch.jit.load(str(model_path), map_location=device) | ||
| self.model.to(device) | ||
| if self.use_fp16: | ||
| self.model.half() | ||
| self.model.eval() | ||
| else: | ||
| raise FileNotFoundError(f'YOLOPv2 model not found: {model_path}') | ||
|
|
||
| def letterbox(self, img: np.ndarray, new_shape: Tuple[int, int], color: Tuple[int, int, int] = (114, 114, 114), stride: int = 32) -> Tuple[np.ndarray, float, Tuple[float, float]]: | ||
| shape = img.shape[:2] | ||
| r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) | ||
|
|
||
| new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) | ||
| dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] | ||
| dw, dh = np.mod(dw, stride) / 2, np.mod(dh, stride) / 2 | ||
|
|
||
| if shape[::-1] != new_unpad: | ||
| img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) | ||
|
|
||
| top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) | ||
| left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) | ||
|
|
||
| img = cv2.copyMakeBorder( | ||
| img, top, bottom, left, right, | ||
| cv2.BORDER_CONSTANT, value=color | ||
| ) | ||
|
|
||
| return img, r, (dw, dh) | ||
|
|
||
| def lane_line_mask(self, ll: torch.Tensor) -> np.ndarray: | ||
| if ll.shape[1] == 1: | ||
| ll_seg_mask = (ll[:, 0] > 0.5).int() | ||
| else: | ||
| ll_seg_mask = torch.argmax(ll, dim=1).int() | ||
| return ll_seg_mask.squeeze().cpu().numpy() | ||
|
|
||
| def _restore_original_size( | ||
| self, | ||
| mask: np.ndarray, | ||
| original_shape: Tuple[int, int], | ||
| ratio: float, | ||
| pad: Tuple[float, float], | ||
| ) -> np.ndarray: | ||
| original_h, original_w = original_shape | ||
| pad_left, pad_top = pad | ||
| top = max(int(round(pad_top - 0.1)), 0) | ||
| left = max(int(round(pad_left - 0.1)), 0) | ||
| unpad_h = int(round(original_h * ratio)) | ||
| unpad_w = int(round(original_w * ratio)) | ||
|
|
||
| unpadded = mask[top:top + unpad_h, left:left + unpad_w] | ||
| return cv2.resize(unpadded, (original_w, original_h), interpolation=cv2.INTER_NEAREST) | ||
|
|
||
| def process_image(self, image: np.ndarray, target_size: Optional[Tuple[int, int]] = None) -> np.ndarray: | ||
| original_shape = image.shape[:2] | ||
| img_resized, ratio, (pad_left, pad_top) = self.letterbox(image, self.input_shape) | ||
|
|
||
| img = img_resized.astype(np.float32) / 255.0 | ||
| img = torch.from_numpy(np.transpose(img, (2, 0, 1))).unsqueeze(0).to(self.device) | ||
| if self.use_fp16: | ||
| img = img.half() | ||
|
|
||
| with torch.no_grad(): | ||
| outputs = self.model(img) | ||
| [pred, anchor_grid], seg, ll = outputs | ||
|
|
||
| ll_seg_mask = self.lane_line_mask(ll) | ||
| original_mask = self._restore_original_size( | ||
| ll_seg_mask, | ||
| original_shape, | ||
| ratio, | ||
| (pad_left, pad_top), | ||
| ) | ||
|
|
||
| if target_size is None: | ||
| return original_mask | ||
|
|
||
| return cv2.resize( | ||
| original_mask, | ||
| target_size, | ||
| interpolation=cv2.INTER_NEAREST | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ubuntu20.04の環境だと使用できない可能性があると思うので、調べておいた方が良いです