Spaces:
Sleeping
Sleeping
Initial commit
Browse filesFiles changed (44)
- .gitattributes+1
- .gitignoreAdded+28
- LICENSEAdded+21
- app.pyAdded+938
- example.pyAdded+67
- examples/cartoon_horse.mp4Added+3
- examples/gradio_examples/house.mp4Added+3
- examples/gradio_examples/man_walking_long.mp4Added+3
- examples/gradio_examples/parkour.mp4Added+3
- examples/gradio_examples/valley.mp4Added+3
- examples/parkour_long.mp4Added+3
- examples/skating.mp4Added+3
- examples/skiing.mp4Added+3
- pi3/models/dinov2/__init__.pyAdded+6
- pi3/models/dinov2/hub/__init__.pyAdded+4
- pi3/models/dinov2/hub/backbones.pyAdded+156
- pi3/models/dinov2/hub/utils.pyAdded+39
- pi3/models/dinov2/layers/__init__.pyAdded+11
- pi3/models/dinov2/layers/attention.pyAdded+89
- pi3/models/dinov2/layers/block.pyAdded+259
- pi3/models/dinov2/layers/dino_head.pyAdded+58
- pi3/models/dinov2/layers/drop_path.pyAdded+34
- pi3/models/dinov2/layers/layer_scale.pyAdded+27
- pi3/models/dinov2/layers/mlp.pyAdded+40
- pi3/models/dinov2/layers/patch_embed.pyAdded+88
- pi3/models/dinov2/layers/swiglu_ffn.pyAdded+72
- pi3/models/dinov2/models/__init__.pyAdded+43
- pi3/models/dinov2/models/vision_transformer.pyAdded+404
- pi3/models/dinov2/utils/__init__.pyAdded+4
- pi3/models/dinov2/utils/cluster.pyAdded+95
- pi3/models/dinov2/utils/config.pyAdded+72
- pi3/models/dinov2/utils/dtype.pyAdded+37
- pi3/models/dinov2/utils/param_groups.pyAdded+103
- pi3/models/dinov2/utils/utils.pyAdded+95
- pi3/models/layers/attention.pyAdded+369
- pi3/models/layers/block.pyAdded+406
- pi3/models/layers/camera_head.pyAdded+93
- pi3/models/layers/pos_embed.pyAdded+174
- pi3/models/layers/transformer_head.pyAdded+81
- pi3/models/pi3.pyAdded+216
- pi3/utils/basic.pyAdded+223
- pi3/utils/debug.pyAdded+63
- pi3/utils/geometry.pyAdded+375
- requirements.txtAdded+13
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.mp4 filter=lfs diff=lfs merge=lfs -text
12345678910111213141516171819202122232425262728*.pyc*.egg-info*.pt*.pth*.zip*.tar.gz*.render*
**/__pycache__/
.vscodeactivate.shlogsoutputsdatackptsvis
*.ply*.npy*.png*.log*.jpg*.out*.pt
resize_demo_imgs.pyimg_dir_to_video.py123456789101112131415161718192021MIT License
Copyright (c) 2025 The Pi3 Authors
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
import osimport cv2import torchimport numpy as npimport gradio as grimport sysimport shutilfrom datetime import datetimeimport globimport gcimport time
from pi3.utils.geometry import se3_inverse, homogenize_points, depth_edgefrom pi3.models.pi3 import Pi3from pi3.utils.basic import load_images_as_tensor
import trimeshimport matplotlibfrom scipy.spatial.transform import Rotation
"""Gradio utils"""
def predictions_to_glb( predictions, conf_thres=50.0, filter_by_frames="all", show_cam=True,) -> trimesh.Scene: """ Converts VGGT predictions to a 3D scene represented as a GLB file.
Args: predictions (dict): Dictionary containing model predictions with keys: - world_points: 3D point coordinates (S, H, W, 3) - world_points_conf: Confidence scores (S, H, W) - images: Input images (S, H, W, 3) - extrinsic: Camera extrinsic matrices (S, 3, 4) conf_thres (float): Percentage of low-confidence points to filter out (default: 50.0) filter_by_frames (str): Frame filter specification (default: "all") show_cam (bool): Include camera visualization (default: True)
Returns: trimesh.Scene: Processed 3D scene containing point cloud and cameras
Raises: ValueError: If input predictions structure is invalid """ if not isinstance(predictions, dict): raise ValueError("predictions must be a dictionary")
if conf_thres is None: conf_thres = 10
print("Building GLB scene") selected_frame_idx = None if filter_by_frames != "all" and filter_by_frames != "All": try: # Extract the index part before the colon selected_frame_idx = int(filter_by_frames.split(":")[0]) except (ValueError, IndexError): pass
pred_world_points = predictions["points"] pred_world_points_conf = predictions.get("conf", np.ones_like(pred_world_points[..., 0]))
# Get images from predictions images = predictions["images"] # Use extrinsic matrices instead of pred_extrinsic_list camera_poses = predictions["camera_poses"]
if selected_frame_idx is not None: pred_world_points = pred_world_points[selected_frame_idx][None] pred_world_points_conf = pred_world_points_conf[selected_frame_idx][None] images = images[selected_frame_idx][None] camera_poses = camera_poses[selected_frame_idx][None]
vertices_3d = pred_world_points.reshape(-1, 3) # Handle different image formats - check if images need transposing if images.ndim == 4 and images.shape[1] == 3: # NCHW format colors_rgb = np.transpose(images, (0, 2, 3, 1)) else: # Assume already in NHWC format colors_rgb = images colors_rgb = (colors_rgb.reshape(-1, 3) * 255).astype(np.uint8)
conf = pred_world_points_conf.reshape(-1) # Convert percentage threshold to actual confidence value if conf_thres == 0.0: conf_threshold = 0.0 else: # conf_threshold = np.percentile(conf, conf_thres) conf_threshold = conf_thres / 100
conf_mask = (conf >= conf_threshold) & (conf > 1e-5)
vertices_3d = vertices_3d[conf_mask] colors_rgb = colors_rgb[conf_mask]
if vertices_3d is None or np.asarray(vertices_3d).size == 0: vertices_3d = np.array([[1, 0, 0]]) colors_rgb = np.array([[255, 255, 255]]) scene_scale = 1 else: # Calculate the 5th and 95th percentiles along each axis lower_percentile = np.percentile(vertices_3d, 5, axis=0) upper_percentile = np.percentile(vertices_3d, 95, axis=0)
# Calculate the diagonal length of the percentile bounding box scene_scale = np.linalg.norm(upper_percentile - lower_percentile)
colormap = matplotlib.colormaps.get_cmap("gist_rainbow")
# Initialize a 3D scene scene_3d = trimesh.Scene()
# Add point cloud data to the scene point_cloud_data = trimesh.PointCloud(vertices=vertices_3d, colors=colors_rgb)
scene_3d.add_geometry(point_cloud_data)
# Prepare 4x4 matrices for camera extrinsics num_cameras = len(camera_poses)
if show_cam: # Add camera models to the scene for i in range(num_cameras): camera_to_world = camera_poses[i] rgba_color = colormap(i / num_cameras) current_color = tuple(int(255 * x) for x in rgba_color[:3])
# integrate_camera_into_scene(scene_3d, camera_to_world, current_color, scene_scale) integrate_camera_into_scene(scene_3d, camera_to_world, current_color, 1.) # fixed camera size
# Rotate scene for better visualize align_rotation = np.eye(4) align_rotation[:3, :3] = Rotation.from_euler("y", 100, degrees=True).as_matrix() # plane rotate align_rotation[:3, :3] = align_rotation[:3, :3] @ Rotation.from_euler("x", 155, degrees=True).as_matrix() # roll scene_3d.apply_transform(align_rotation)
print("GLB Scene built") return scene_3d
def integrate_camera_into_scene(scene: trimesh.Scene, transform: np.ndarray, face_colors: tuple, scene_scale: float): """ Integrates a fake camera mesh into the 3D scene.
Args: scene (trimesh.Scene): The 3D scene to add the camera model. transform (np.ndarray): Transformation matrix for camera positioning. face_colors (tuple): Color of the camera face. scene_scale (float): Scale of the scene. """
cam_width = scene_scale * 0.05 cam_height = scene_scale * 0.1
# Create cone shape for camera rot_45_degree = np.eye(4) rot_45_degree[:3, :3] = Rotation.from_euler("z", 45, degrees=True).as_matrix() rot_45_degree[2, 3] = -cam_height
opengl_transform = get_opengl_conversion_matrix() # Combine transformations complete_transform = transform @ opengl_transform @ rot_45_degree camera_cone_shape = trimesh.creation.cone(cam_width, cam_height, sections=4)
# Generate mesh for the camera slight_rotation = np.eye(4) slight_rotation[:3, :3] = Rotation.from_euler("z", 2, degrees=True).as_matrix()
vertices_combined = np.concatenate( [ camera_cone_shape.vertices, 0.95 * camera_cone_shape.vertices, transform_points(slight_rotation, camera_cone_shape.vertices), ] ) vertices_transformed = transform_points(complete_transform, vertices_combined)
mesh_faces = compute_camera_faces(camera_cone_shape)
# Add the camera mesh to the scene camera_mesh = trimesh.Trimesh(vertices=vertices_transformed, faces=mesh_faces) camera_mesh.visual.face_colors[:, :3] = face_colors scene.add_geometry(camera_mesh)
def get_opengl_conversion_matrix() -> np.ndarray: """ Constructs and returns the OpenGL conversion matrix.
Returns: numpy.ndarray: A 4x4 OpenGL conversion matrix. """ # Create an identity matrix matrix = np.identity(4)
# Flip the y and z axes matrix[1, 1] = -1 matrix[2, 2] = -1
return matrix
def transform_points(transformation: np.ndarray, points: np.ndarray, dim: int = None) -> np.ndarray: """ Applies a 4x4 transformation to a set of points.
Args: transformation (np.ndarray): Transformation matrix. points (np.ndarray): Points to be transformed. dim (int, optional): Dimension for reshaping the result.
Returns: np.ndarray: Transformed points. """ points = np.asarray(points) initial_shape = points.shape[:-1] dim = dim or points.shape[-1]
# Apply transformation transformation = transformation.swapaxes(-1, -2) # Transpose the transformation matrix points = points @ transformation[..., :-1, :] + transformation[..., -1:, :]
# Reshape the result result = points[..., :dim].reshape(*initial_shape, dim) return result
def compute_camera_faces(cone_shape: trimesh.Trimesh) -> np.ndarray: """ Computes the faces for the camera mesh.
Args: cone_shape (trimesh.Trimesh): The shape of the camera cone.
Returns: np.ndarray: Array of faces for the camera mesh. """ # Create pseudo cameras faces_list = [] num_vertices_cone = len(cone_shape.vertices)
for face in cone_shape.faces: if 0 in face: continue v1, v2, v3 = face v1_offset, v2_offset, v3_offset = face + num_vertices_cone v1_offset_2, v2_offset_2, v3_offset_2 = face + 2 * num_vertices_cone
faces_list.extend( [ (v1, v2, v2_offset), (v1, v1_offset, v3), (v3_offset, v2, v3), (v1, v2, v2_offset_2), (v1, v1_offset_2, v3), (v3_offset_2, v2, v3), ] )
faces_list += [(v3, v2, v1) for v1, v2, v3 in faces_list] return np.array(faces_list)
# -------------------------------------------------------------------------# 1) Core model inference# -------------------------------------------------------------------------def run_model(target_dir, model) -> dict: print(f"Processing images from {target_dir}")
# Device check device = "cuda" if torch.cuda.is_available() else "cpu" if not torch.cuda.is_available(): raise ValueError("CUDA is not available. Check your environment.")
# Move model to device model = model.to(device) model.eval()
# Load and preprocess images image_names = glob.glob(os.path.join(target_dir, "images", "*")) image_names = sorted(image_names) print(f"Found {len(image_names)} images") if len(image_names) == 0: raise ValueError("No images found. Check your upload.")
# interval = 10 if target_dir.endswith('.mp4') else 1 interval = 1 imgs = load_images_as_tensor(os.path.join(target_dir, "images"), interval=interval).to(device) # (N, 3, H, W)
# 3. Infer print("Running model inference...") dtype = torch.float16 with torch.no_grad(): with torch.amp.autocast('cuda', dtype=dtype): predictions = model(imgs[None]) # Add batch dimension predictions['images'] = imgs[None].permute(0, 1, 3, 4, 2) predictions['conf'] = torch.sigmoid(predictions['conf']) edge = depth_edge(predictions['local_points'][..., 2], rtol=0.03) predictions['conf'][edge] = 0.0 del predictions['local_points']
# # transform to first camera coordinate # predictions['points'] = torch.einsum('bij, bnhwj -> bnhwi', se3_inverse(predictions['camera_poses'][:, 0]), homogenize_points(predictions['points']))[..., :3] # predictions['camera_poses'] = torch.einsum('bij, bnjk -> bnik', se3_inverse(predictions['camera_poses'][:, 0]), predictions['camera_poses'])
# Convert tensors to numpy for key in predictions.keys(): if isinstance(predictions[key], torch.Tensor): predictions[key] = predictions[key].cpu().numpy().squeeze(0) # remove batch dimension
# Clean up torch.cuda.empty_cache() return predictions
# -------------------------------------------------------------------------# 2) Handle uploaded video/images --> produce target_dir + images# -------------------------------------------------------------------------def handle_uploads(input_video, input_images, interval=-1): """ Create a new 'target_dir' + 'images' subfolder, and place user-uploaded images or extracted frames from video into it. Return (target_dir, image_paths). """ start_time = time.time() gc.collect() torch.cuda.empty_cache()
# Create a unique folder name timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f") target_dir = f"input_images_{timestamp}" target_dir_images = os.path.join(target_dir, "images")
# Clean up if somehow that folder already exists if os.path.exists(target_dir): shutil.rmtree(target_dir) os.makedirs(target_dir, exist_ok=True) os.makedirs(target_dir_images, exist_ok=True)
image_paths = []
# --- Handle images --- if input_images is not None: if interval is not None and interval > 0: input_images = input_images[::interval]
for file_data in input_images: if isinstance(file_data, dict) and "name" in file_data: file_path = file_data["name"] else: file_path = file_data dst_path = os.path.join(target_dir_images, os.path.basename(file_path)) shutil.copy(file_path, dst_path) image_paths.append(dst_path) # --- Handle video --- if input_video is not None: if isinstance(input_video, dict) and "name" in input_video: video_path = input_video["name"] else: video_path = input_video
vs = cv2.VideoCapture(video_path) fps = vs.get(cv2.CAP_PROP_FPS) if interval is not None and interval > 0: frame_interval = interval else: frame_interval = int(fps * 1) # 1 frame/sec
count = 0 video_frame_num = 0 while True: gotit, frame = vs.read() if not gotit: break count += 1 if count % frame_interval == 0: image_path = os.path.join(target_dir_images, f"{video_frame_num:06}.png") cv2.imwrite(image_path, frame) image_paths.append(image_path) video_frame_num += 1
# Sort final images for gallery image_paths = sorted(image_paths)
end_time = time.time() print(f"Files copied to {target_dir_images}; took {end_time - start_time:.3f} seconds") return target_dir, image_paths
# -------------------------------------------------------------------------# 3) Update gallery on upload# -------------------------------------------------------------------------def update_gallery_on_upload(input_video, input_images, interval=-1): """ Whenever user uploads or changes files, immediately handle them and show in the gallery. Return (target_dir, image_paths). If nothing is uploaded, returns "None" and empty list. """ if not input_video and not input_images: return None, None, None, None target_dir, image_paths = handle_uploads(input_video, input_images, interval=interval) return None, target_dir, image_paths, "Upload complete. Click 'Reconstruct' to begin 3D processing."
# -------------------------------------------------------------------------# 4) Reconstruction: uses the target_dir plus any viz parameters# -------------------------------------------------------------------------def gradio_demo( target_dir, conf_thres=3.0, frame_filter="All", show_cam=True,): """ Perform reconstruction using the already-created target_dir/images. """ if not os.path.isdir(target_dir) or target_dir == "None": return None, "No valid target directory found. Please upload first.", None, None
start_time = time.time() gc.collect() torch.cuda.empty_cache()
# Prepare frame_filter dropdown target_dir_images = os.path.join(target_dir, "images") all_files = sorted(os.listdir(target_dir_images)) if os.path.isdir(target_dir_images) else [] all_files = [f"{i}: {filename}" for i, filename in enumerate(all_files)] frame_filter_choices = ["All"] + all_files
print("Running run_model...") with torch.no_grad(): predictions = run_model(target_dir, model)
# Save predictions prediction_save_path = os.path.join(target_dir, "predictions.npz") np.savez(prediction_save_path, **predictions)
# Handle None frame_filter if frame_filter is None: frame_filter = "All"
# Build a GLB file name glbfile = os.path.join( target_dir, f"glbscene_{conf_thres}_{frame_filter.replace('.', '_').replace(':', '').replace(' ', '_')}_cam{show_cam}.glb", )
# Convert predictions to GLB glbscene = predictions_to_glb( predictions, conf_thres=conf_thres, filter_by_frames=frame_filter, show_cam=show_cam, ) glbscene.export(file_obj=glbfile)
# Cleanup del predictions gc.collect() torch.cuda.empty_cache()
end_time = time.time() print(f"Total time: {end_time - start_time:.2f} seconds (including IO)") log_msg = f"Reconstruction Success ({len(all_files)} frames). Waiting for visualization."
return glbfile, log_msg, gr.Dropdown(choices=frame_filter_choices, value=frame_filter, interactive=True)
# -------------------------------------------------------------------------# 5) Helper functions for UI resets + re-visualization# -------------------------------------------------------------------------def clear_fields(): """ Clears the 3D viewer, the stored target_dir, and empties the gallery. """ return None
def update_log(): """ Display a quick log message while waiting. """ return "Loading and Reconstructing..."
def update_visualization( target_dir, conf_thres, frame_filter, show_cam, is_example): """ Reload saved predictions from npz, create (or reuse) the GLB for new parameters, and return it for the 3D viewer. If is_example == "True", skip. """
# If it's an example click, skip as requested if is_example == "True": return None, "No reconstruction available. Please click the Reconstruct button first."
if not target_dir or target_dir == "None" or not os.path.isdir(target_dir): return None, "No reconstruction available. Please click the Reconstruct button first."
predictions_path = os.path.join(target_dir, "predictions.npz") if not os.path.exists(predictions_path): return None, f"No reconstruction available at {predictions_path}. Please run 'Reconstruct' first."
key_list = [ "images", "points", "conf", "camera_poses", ]
loaded = np.load(predictions_path) predictions = {key: np.array(loaded[key]) for key in key_list}
glbfile = os.path.join( target_dir, f"glbscene_{conf_thres}_{frame_filter.replace('.', '_').replace(':', '').replace(' ', '_')}_cam{show_cam}.glb", )
if not os.path.exists(glbfile): glbscene = predictions_to_glb( predictions, conf_thres=conf_thres, filter_by_frames=frame_filter, show_cam=show_cam, ) glbscene.export(file_obj=glbfile)
return glbfile, "Updating Visualization"
# -------------------------------------------------------------------------# Example images# -------------------------------------------------------------------------
house = "examples/gradio_examples/house.mp4"man_walking_long = "examples/gradio_examples/man_walking_long.mp4"parkour = "examples/gradio_examples/parkour.mp4"valley = "examples/gradio_examples/valley.mp4"cartoon_horse = "examples/cartoon_horse.mp4"parkour_long = "examples/parkour_long.mp4"skating = "examples/skating.mp4"skiing = "examples/skiing.mp4"
# -------------------------------------------------------------------------# 6) Build Gradio UI# -------------------------------------------------------------------------
if __name__ == '__main__':
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Initializing and loading Pi3 model...")
model = Pi3() # _URL = "https://huggingface.co/yyfz233/Pi3/resolve/main/model.safetensors" # model.load_state_dict(torch.hub.load_state_dict_from_url(_URL)) model.load_state_dict(torch.load('ckpts/pi3.pt', weights_only=False, map_location=device))
model.eval() model = model.to(device)
theme = gr.themes.Ocean() theme.set( checkbox_label_background_fill_selected="*button_primary_background_fill", checkbox_label_text_color_selected="*button_primary_text_color", )
with gr.Blocks( theme=theme, css=""" /* --- Google 字体导入 (科技感字体) --- */ @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Rajdhani:wght@400;500;700&display=swap');
/* --- 动画关键帧 --- */ /* 背景动态星云效果 */ @keyframes gradient-animation { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } }
/* 标题和状态文字的霓虹灯光效 */ @keyframes text-glow { 0%, 100% { text-shadow: 0 0 10px #0ea5e9, 0 0 20px #0ea5e9, 0 0 30px #4f46e5, 0 0 40px #4f46e5; } 50% { text-shadow: 0 0 5px #0ea5e9, 0 0 10px #0ea5e9, 0 0 15px #4f46e5, 0 0 20px #4f46e5; } }
/* 卡片边框呼吸光晕 */ @keyframes border-glow { 0% { border-color: rgba(79, 70, 229, 0.5); box-shadow: 0 0 15px rgba(79, 70, 229, 0.3); } 50% { border-color: rgba(14, 165, 233, 0.8); box-shadow: 0 0 25px rgba(14, 165, 233, 0.5); } 100% { border-color: rgba(79, 70, 229, 0.5); box-shadow: 0 0 15px rgba(79, 70, 229, 0.3); } }
/* --- 全局样式:宇宙黑暗主题 --- */ .gradio-container { font-family: 'Rajdhani', sans-serif; background: linear-gradient(-45deg, #020617, #111827, #082f49, #4f46e5); background-size: 400% 400%; animation: gradient-animation 20s ease infinite; color: #9ca3af; }
/* --- 全局文字颜色修复 (解决Light Mode问题) --- */ /* 1. 修复全局、标签和输入框内的文字颜色 */ .gradio-container, .gr-label label, .gr-input, input, textarea, .gr-check-radio label { color: #d1d5db !important; /* 设置一个柔和的浅灰色 */ }
/* 2. 修复 Examples 表头 (这是您问题的核心) */ thead th { color: white !important; background-color: #1f2937 !important; /* 同时给表头一个背景色,视觉效果更好 */ }
/* 3. 修复 Examples 表格内容文字 */ tbody td { color: #d1d5db !important; } /* --- 状态信息 & 输出标题样式 (custom-log) ✨ --- */ .custom-log * { font-family: 'Orbitron', sans-serif; font-size: 24px !important; font-weight: 700 !important; text-align: center !important; color: transparent !important; background-image: linear-gradient(120deg, #93c5fd, #6ee7b7, #fde047); background-size: 300% 300%; -webkit-background-clip: text; background-clip: text; animation: gradient-animation 8s ease-in-out infinite, text-glow 3s ease-in-out infinite; padding: 10px 0; } /* --- UI 卡片/分组样式 (玻璃拟态) 💎 --- */ .gr-block.gr-group { background-color: rgba(17, 24, 39, 0.6); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border: 1px solid rgba(55, 65, 81, 0.5); border-radius: 16px; box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37); transition: all 0.3s ease; /* 应用边框呼吸光晕动画 */ animation: border-glow 5s infinite alternate; } .gr-block.gr-group:hover { box-shadow: 0 0 25px rgba(14, 165, 233, 0.4); border-color: rgba(14, 165, 233, 0.6); } /* --- 酷炫按钮样式 🚀 --- */ .gr-button { background: linear-gradient(to right, #4f46e5, #7c3aed, #0ea5e9) !important; background-size: 200% auto !important; color: white !important; font-weight: bold !important; border: none !important; border-radius: 10px !important; box-shadow: 0 4px 15px 0 rgba(79, 70, 229, 0.5) !important; transition: all 0.4s ease-in-out !important; font-family: 'Orbitron', sans-serif !important; text-transform: uppercase; letter-spacing: 1px; } .gr-button:hover { background-position: right center !important; box-shadow: 0 4px 20px 0 rgba(14, 165, 233, 0.6) !important; transform: translateY(-3px) scale(1.02); } .gr-button.primary { /* 主按钮增加呼吸光晕动画 */ animation: border-glow 3s infinite alternate; } """, ) as demo: # Instead of gr.State, we use a hidden Textbox: is_example = gr.Textbox(label="is_example", visible=False, value="None") num_images = gr.Textbox(label="num_images", visible=False, value="None") target_dir_output = gr.Textbox(label="Target Dir", visible=False, value="None")
gr.HTML( """ <style> /* --- 介绍文字区专属样式 --- */ .intro-content { font-size: 17px !important; line-height: 1.7; color: #C0C0C0 !important; } /* 额外为 p 标签添加规则,确保覆盖 */ .intro-content p { color: #C0C0C0 !important; } .intro-content h1 { font-family: 'Orbitron', sans-serif; font-size: 2.8em !important; font-weight: 900; text-align: center; color: #C0C0C0 !important; animation: text-glow 4s ease-in-out infinite; margin-bottom: 0px; } .intro-content .pi-symbol { display: inline-block; color: transparent; background-image: linear-gradient(120deg, #38bdf8, #818cf8, #c084fc); -webkit-background-clip: text; background-clip: text; text-shadow: 0 0 15px rgba(129, 140, 248, 0.5); } .intro-content .subtitle { text-align: center; font-size: 1.1em; margin-bottom: 2rem; } .intro-content a.themed-link { color: #C0C0C0 !important; text-decoration: none; font-weight: 700; transition: all 0.3s ease; } .intro-content a.themed-link:hover { color: #EAEAEA !important; text-shadow: 0 0 8px rgba(234, 234, 234, 0.7); } .intro-content h3 { font-family: 'Orbitron', sans-serif; color: #C0C0C0 !important; text-transform: uppercase; letter-spacing: 2px; border-bottom: 1px solid #374151; padding-bottom: 8px; margin-top: 25px; } .intro-content ol { list-style: none; padding-left: 0; counter-reset: step-counter; } .intro-content ol li { counter-increment: step-counter; margin-bottom: 15px; padding-left: 45px; position: relative; color: #C0C0C0 !important; /* 确保列表项文字也是银白色 */ } /* 自定义酷炫列表数字 */ .intro-content ol li::before { content: counter(step-counter); position: absolute; left: 0; top: 0; width: 30px; height: 30px; background: linear-gradient(135deg, #1e3a8a, #4f46e5); border-radius: 50%; color: white; font-weight: 700; font-family: 'Orbitron', sans-serif; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 10px rgba(79, 70, 229, 0.5); } .intro-content strong { color: #C0C0C0 !important; font-weight: 700; } .intro-content .performance-note { background-color: rgba(14, 165, 233, 0.1); border-left: 4px solid #0ea5e9; padding: 15px; border-radius: 8px; margin-top: 20px; } /* 确保提示框内的文字也生效 */ .intro-content .performance-note p { color: #C0C0C0 !important; }
</style> <div class="intro-content"> <h1>🌌 <span class="pi-symbol">π³</span>: Scalable Permutation-Equivariant Visual Geometry Learning</h1> <p class="subtitle"> <a class="themed-link" href="">🐙 GitHub Repository</a> | <a class="themed-link" href="#">🚀 Project Page</a> </p> <p>Transform your videos or image collections into detailed 3D models. The <strong class="pi-symbol">π³</strong> model processes your visual data to generate a rich 3D point cloud and calculate the corresponding camera perspectives.</p> <h3>How to Use:</h3> <ol> <li><strong>Provide Your Media:</strong> Upload a video or image set. You can specify a sampling interval below. By default, videos are sampled at 1 frame per second, and for image sets, every image is used (interval of 1). Your inputs will be displayed in the "Preview" gallery.</li> <li><strong>Generate the 3D Model:</strong> Press the "Reconstruct" button to initiate the process.</li> <li><strong>Explore and Refine Your Model:</strong> The generated 3D model will appear in the viewer on the right. Interact with it by rotating, panning, and zooming. You can also download the model as a GLB file. For further refinement, use the options below the viewer to adjust point confidence, filter by frame, or toggle camera visibility.</li> </ol> <div class="performance-note"> <p><strong>A Quick Note on Performance:</strong> The core processing by <strong class="pi-symbol">π³</strong> is incredibly fast, typically finishing in under a second. However, rendering the final 3D point cloud can take longer, depending on the complexity of the scene and the capabilities of the rendering engine.</p> </div> </div> """ )
with gr.Row(): with gr.Column(scale=1): with gr.Group(): gr.Markdown("### 1. Upload Media") input_video = gr.Video(label="Upload Video", interactive=True) input_images = gr.File(file_count="multiple", label="Or Upload Images", interactive=True) interval = gr.Number(None, label='Frame/Image Interval', info="Sampling interval. Video default: 1 FPS. Image default: 1 (all images).") image_gallery = gr.Gallery( label="Image Preview", columns=4, height="300px", show_download_button=True, object_fit="contain", preview=True, )
with gr.Column(scale=2): gr.Markdown("### 2. View Reconstruction") log_output = gr.Markdown("Please upload media and click Reconstruct.", elem_classes=["custom-log"]) reconstruction_output = gr.Model3D(height=480, zoom_speed=0.5, pan_speed=0.5, label="3D Output") with gr.Row(): submit_btn = gr.Button("Reconstruct", scale=3, variant="primary") clear_btn = gr.ClearButton( scale=1 ) with gr.Group(): gr.Markdown("### 3. Adjust Visualization") with gr.Row(): conf_thres = gr.Slider(minimum=0, maximum=100, value=20, step=0.1, label="Confidence Threshold (%)") show_cam = gr.Checkbox(label="Show Cameras", value=True) frame_filter = gr.Dropdown(choices=["All"], value="All", label="Show Points from Frame")
# Set clear button targets clear_btn.add([input_video, input_images, reconstruction_output, log_output, target_dir_output, image_gallery, interval])
# ---------------------- Examples section ---------------------- examples = [ [skating, None, 10, 20, True], [parkour_long, None, 20, 10, True], [cartoon_horse, None, 10, 20, True], [skiing, None, 30, 70, True], [man_walking_long, None, 1, 50, True], [house, None, 1, 20, True], [parkour, None, 1, 20, True], [valley, None, 1, 20, True], ]
def example_pipeline( input_video, input_images, interval, conf_thres, show_cam, ): """ 1) Copy example images to new target_dir 2) Reconstruct 3) Return model3D + logs + new_dir + updated dropdown + gallery We do NOT return is_example. It's just an input. """ target_dir, image_paths = handle_uploads(input_video, input_images, interval) # Always use "All" for frame_filter in examples frame_filter = "All" glbfile, log_msg, dropdown = gradio_demo( target_dir, conf_thres, frame_filter, show_cam ) return glbfile, log_msg, target_dir, dropdown, image_paths
gr.Markdown("Click any row to load an example.", elem_classes=["example-log"])
gr.Examples( examples=examples, inputs=[ input_video, input_images, interval, conf_thres, show_cam, ], outputs=[reconstruction_output, log_output, target_dir_output, frame_filter, image_gallery], fn=example_pipeline, cache_examples=False, examples_per_page=50, run_on_click=False, )
# ------------------------------------------------------------------------- # "Reconstruct" button logic: # - Clear fields # - Update log # - gradio_demo(...) with the existing target_dir # - Then set is_example = "False" # ------------------------------------------------------------------------- submit_btn.click(fn=clear_fields, inputs=[], outputs=[reconstruction_output]).then( fn=update_log, inputs=[], outputs=[log_output] ).then( fn=gradio_demo, inputs=[ target_dir_output, conf_thres, frame_filter, show_cam, ], outputs=[reconstruction_output, log_output, frame_filter], ).then( fn=lambda: "False", inputs=[], outputs=[is_example] # set is_example to "False" )
# ------------------------------------------------------------------------- # Real-time Visualization Updates # ------------------------------------------------------------------------- conf_thres.change( update_visualization, [ target_dir_output, conf_thres, frame_filter, show_cam, is_example, ], [reconstruction_output, log_output], ) frame_filter.change( update_visualization, [ target_dir_output, conf_thres, frame_filter, show_cam, is_example, ], [reconstruction_output, log_output], ) show_cam.change( update_visualization, [ target_dir_output, conf_thres, frame_filter, show_cam, is_example, ], [reconstruction_output, log_output], )
# ------------------------------------------------------------------------- # Auto-update gallery whenever user uploads or changes their files # ------------------------------------------------------------------------- input_video.change( fn=update_gallery_on_upload, inputs=[input_video, input_images, interval], outputs=[reconstruction_output, target_dir_output, image_gallery, log_output], ) input_images.change( fn=update_gallery_on_upload, inputs=[input_video, input_images, interval], outputs=[reconstruction_output, target_dir_output, image_gallery, log_output], )
demo.queue(max_size=20).launch(show_error=True, share=True, server_port=10001, server_name='0.0.0.0')
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import torchimport argparsefrom pi3.utils.basic import load_images_as_tensor, write_plyfrom pi3.utils.geometry import depth_edgefrom pi3.models.pi3 import Pi3
if __name__ == '__main__': # --- Argument Parsing --- parser = argparse.ArgumentParser(description="Run inference with the Pi3 model.") parser.add_argument("--data_path", type=str, default='examples/parkour', help="Path to the input image directory or a video file.") parser.add_argument("--save_path", type=str, default='examples/parkour.ply', help="Path to save the output .ply file.") parser.add_argument("--interval", type=int, default=-1, help="Interval to sample image. Default: 1 for images dir, 10 for video") parser.add_argument("--ckpt", type=str, default=None, help="Path to the model checkpoint file. Default: None") parser.add_argument("--device", type=str, default='cuda', help="Device to run inference on ('cuda' or 'cpu'). Default: 'cuda'") args = parser.parse_args() if args.interval < 0: args.interval = 10 if args.data_path.endswith('.mp4') else 1 print(f'Sampling interval: {args.interval}')
# from pi3.utils.debug import setup_debug # setup_debug()
# 1. Prepare model print(f"Loading model...") device = torch.device(args.device) if args.ckpt is not None: model = Pi3().to(device).eval() if args.ckpt.endswith('.safetensors'): from safetensors.torch import load_file weight = load_file(args.ckpt) else: weight = torch.load(args.ckpt, map_location=device, weights_only=False) model.load_state_dict(weight) else: model = Pi3.from_pretrained("yyfz233/Pi3").to(device).eval() # or # model = Pi3().to(device).eval() # _URL = "https://huggingface.co/yyfz233/Pi3/resolve/main/model.safetensors" # model.load_state_dict(torch.hub.load_state_dict_from_url(_URL))
# 2. Prepare input data # The load_images_as_tensor function will print the loading path imgs = load_images_as_tensor(args.data_path, interval=args.interval).to(device) # (N, 3, H, W)
# 3. Infer print("Running model inference...") with torch.no_grad(): with torch.amp.autocast('cuda', dtype=torch.bfloat16): res = model(imgs[None]) # Add batch dimension
# 4. process mask masks = torch.sigmoid(res['conf'][..., 0]) > 0.1 non_edge = ~depth_edge(res['local_points'][..., 2], rtol=0.03) masks = torch.logical_and(masks, non_edge)[0]
# 5. Save points print(f"Saving point cloud to: {args.save_path}") write_ply(res['points'][0][masks].cpu(), imgs.permute(0, 2, 3, 1)[masks], args.save_path) print("Done.")123version https://git-lfs.github.com/spec/v1oid sha256:4edbaec5cc3e747f73f9c01e3a3bef298dc17bb9d2d06d9a9dcd34074f69a73asize 2314425123version https://git-lfs.github.com/spec/v1oid sha256:a77b5e44ac7db0e8d1393ef727ef840580bbd3b61fab9b33f6e1df208a7f804fsize 412888123version https://git-lfs.github.com/spec/v1oid sha256:f2a445a27bff4eced24cf44168b7a79e1b486d8df74cf6ca2800097e254b237bsize 952503123version https://git-lfs.github.com/spec/v1oid sha256:1618c992c2f87fe019e90e489457cec0b0e2bcbc8e27768cd36a08fdf7e923dbsize 413960123version https://git-lfs.github.com/spec/v1oid sha256:01a0b3d4eafc9d012b59f120e408a909ea4f9ab0e2f01f144d24e34af245e129size 710743123version https://git-lfs.github.com/spec/v1oid sha256:a27bde28b0a0466e20bbc66a69fcde1c2625c34ee0465fb7cb5d4b4c2fd973fasize 5316123123version https://git-lfs.github.com/spec/v1oid sha256:fe64b74f942806e413515fdb79f7825a19ed4e77affb61652dcb554d7b54e05dsize 1438490123version https://git-lfs.github.com/spec/v1oid sha256:65e87d6fdda621f91a5e57e2b90641a6dcf28cbada9b890db8a17fe94a9958e1size 7960567123456# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
__version__ = "0.0.1"1234# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
from enum import Enumfrom typing import Union
import torch
from .utils import _DINOV2_BASE_URL, _make_dinov2_model_name
class Weights(Enum): LVD142M = "LVD142M"
def _make_dinov2_model( *, arch_name: str = "vit_large", img_size: int = 518, patch_size: int = 14, init_values: float = 1.0, ffn_layer: str = "mlp", block_chunks: int = 0, num_register_tokens: int = 0, interpolate_antialias: bool = False, interpolate_offset: float = 0.1, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs,): from ..models import vision_transformer as vits
if isinstance(weights, str): try: weights = Weights[weights] except KeyError: raise AssertionError(f"Unsupported weights: {weights}")
model_base_name = _make_dinov2_model_name(arch_name, patch_size) vit_kwargs = dict( img_size=img_size, patch_size=patch_size, init_values=init_values, ffn_layer=ffn_layer, block_chunks=block_chunks, num_register_tokens=num_register_tokens, interpolate_antialias=interpolate_antialias, interpolate_offset=interpolate_offset, ) vit_kwargs.update(**kwargs) model = vits.__dict__[arch_name](**vit_kwargs)
if pretrained: model_full_name = _make_dinov2_model_name(arch_name, patch_size, num_register_tokens) url = _DINOV2_BASE_URL + f"/{model_base_name}/{model_full_name}_pretrain.pth" state_dict = torch.hub.load_state_dict_from_url(url, map_location="cpu") model.load_state_dict(state_dict, strict=True)
return model
def dinov2_vits14(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-S/14 model (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model(arch_name="vit_small", pretrained=pretrained, weights=weights, **kwargs)
def dinov2_vitb14(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-B/14 model (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model(arch_name="vit_base", pretrained=pretrained, weights=weights, **kwargs)
def dinov2_vitl14(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-L/14 model (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model(arch_name="vit_large", pretrained=pretrained, weights=weights, **kwargs)
def dinov2_vitg14(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-g/14 model (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model( arch_name="vit_giant2", ffn_layer="swiglufused", weights=weights, pretrained=pretrained, **kwargs, )
def dinov2_vits14_reg(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-S/14 model with registers (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model( arch_name="vit_small", pretrained=pretrained, weights=weights, num_register_tokens=4, interpolate_antialias=True, interpolate_offset=0.0, **kwargs, )
def dinov2_vitb14_reg(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-B/14 model with registers (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model( arch_name="vit_base", pretrained=pretrained, weights=weights, num_register_tokens=4, interpolate_antialias=True, interpolate_offset=0.0, **kwargs, )
def dinov2_vitl14_reg(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-L/14 model with registers (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model( arch_name="vit_large", pretrained=pretrained, weights=weights, num_register_tokens=4, interpolate_antialias=True, interpolate_offset=0.0, **kwargs, )
def dinov2_vitg14_reg(*, pretrained: bool = True, weights: Union[Weights, str] = Weights.LVD142M, **kwargs): """ DINOv2 ViT-g/14 model with registers (optionally) pretrained on the LVD-142M dataset. """ return _make_dinov2_model( arch_name="vit_giant2", ffn_layer="swiglufused", weights=weights, pretrained=pretrained, num_register_tokens=4, interpolate_antialias=True, interpolate_offset=0.0, **kwargs, )123456789101112131415161718192021222324252627282930313233343536373839# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import itertoolsimport math
import torchimport torch.nn as nnimport torch.nn.functional as F
_DINOV2_BASE_URL = "https://dl.fbaipublicfiles.com/dinov2"
def _make_dinov2_model_name(arch_name: str, patch_size: int, num_register_tokens: int = 0) -> str: compact_arch_name = arch_name.replace("_", "")[:4] registers_suffix = f"_reg{num_register_tokens}" if num_register_tokens else "" return f"dinov2_{compact_arch_name}{patch_size}{registers_suffix}"
class CenterPadding(nn.Module): def __init__(self, multiple): super().__init__() self.multiple = multiple
def _get_pad(self, size): new_size = math.ceil(size / self.multiple) * self.multiple pad_size = new_size - size pad_size_left = pad_size // 2 pad_size_right = pad_size - pad_size_left return pad_size_left, pad_size_right
@torch.inference_mode() def forward(self, x): pads = list(itertools.chain.from_iterable(self._get_pad(m) for m in x.shape[:1:-1])) output = F.pad(x, pads) return output1234567891011# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
from .dino_head import DINOHeadfrom .mlp import Mlpfrom .patch_embed import PatchEmbedfrom .swiglu_ffn import SwiGLUFFN, SwiGLUFFNFusedfrom .block import NestedTensorBlockfrom .attention import MemEffAttention1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py
import loggingimport osimport warnings
from torch import Tensorfrom torch import nn
logger = logging.getLogger("dinov2")
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is Nonetry: if XFORMERS_ENABLED: from xformers.ops import memory_efficient_attention, unbind
XFORMERS_AVAILABLE = True # warnings.warn("xFormers is available (Attention)") else: # warnings.warn("xFormers is disabled (Attention)") raise ImportErrorexcept ImportError: XFORMERS_AVAILABLE = False # warnings.warn("xFormers is not available (Attention)")
class Attention(nn.Module): def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, proj_bias: bool = True, attn_drop: float = 0.0, proj_drop: float = 0.0, ) -> None: super().__init__() self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim**-0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim, bias=proj_bias) self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x: Tensor, attn_bias=None) -> Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0] * self.scale, qkv[1], qkv[2] attn = q @ k.transpose(-2, -1)
attn = attn.softmax(dim=-1) attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x
class MemEffAttention(Attention): def forward(self, x: Tensor, attn_bias=None) -> Tensor: if not XFORMERS_AVAILABLE: if attn_bias is not None: raise AssertionError("xFormers is required for using nested tensors") return super().forward(x)
B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads)
q, k, v = unbind(qkv, 2)
x = memory_efficient_attention(q, k, v, attn_bias=attn_bias) x = x.reshape([B, N, C])
x = self.proj(x) x = self.proj_drop(x) return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/patch_embed.py
import loggingimport osfrom typing import Callable, List, Any, Tuple, Dictimport warnings
import torchfrom torch import nn, Tensor
from .attention import Attention, MemEffAttentionfrom .drop_path import DropPathfrom .layer_scale import LayerScalefrom .mlp import Mlp
logger = logging.getLogger("dinov2")
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is Nonetry: if XFORMERS_ENABLED: from xformers.ops import fmha, scaled_index_add, index_select_cat
XFORMERS_AVAILABLE = True # warnings.warn("xFormers is available (Block)") else: # warnings.warn("xFormers is disabled (Block)") raise ImportErrorexcept ImportError: XFORMERS_AVAILABLE = False # warnings.warn("xFormers is not available (Block)")
class Block(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_ratio: float = 4.0, qkv_bias: bool = False, proj_bias: bool = True, ffn_bias: bool = True, drop: float = 0.0, attn_drop: float = 0.0, init_values=None, drop_path: float = 0.0, act_layer: Callable[..., nn.Module] = nn.GELU, norm_layer: Callable[..., nn.Module] = nn.LayerNorm, attn_class: Callable[..., nn.Module] = Attention, ffn_layer: Callable[..., nn.Module] = Mlp, ) -> None: super().__init__() # print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}") self.norm1 = norm_layer(dim) self.attn = attn_class( dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias, attn_drop=attn_drop, proj_drop=drop, ) self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = ffn_layer( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, bias=ffn_bias, ) self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.sample_drop_ratio = drop_path
def forward(self, x: Tensor) -> Tensor: def attn_residual_func(x: Tensor) -> Tensor: return self.ls1(self.attn(self.norm1(x)))
def ffn_residual_func(x: Tensor) -> Tensor: return self.ls2(self.mlp(self.norm2(x)))
if self.training and self.sample_drop_ratio > 0.1: # the overhead is compensated only for a drop path rate larger than 0.1 x = drop_add_residual_stochastic_depth( x, residual_func=attn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) x = drop_add_residual_stochastic_depth( x, residual_func=ffn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) elif self.training and self.sample_drop_ratio > 0.0: x = x + self.drop_path1(attn_residual_func(x)) x = x + self.drop_path1(ffn_residual_func(x)) # FIXME: drop_path2 else: x = x + attn_residual_func(x) x = x + ffn_residual_func(x) return x
def drop_add_residual_stochastic_depth( x: Tensor, residual_func: Callable[[Tensor], Tensor], sample_drop_ratio: float = 0.0,) -> Tensor: # 1) extract subset using permutation b, n, d = x.shape sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1) brange = (torch.randperm(b, device=x.device))[:sample_subset_size] x_subset = x[brange]
# 2) apply residual_func to get residual residual = residual_func(x_subset)
x_flat = x.flatten(1) residual = residual.flatten(1)
residual_scale_factor = b / sample_subset_size
# 3) add the residual x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor) return x_plus_residual.view_as(x)
def get_branges_scales(x, sample_drop_ratio=0.0): b, n, d = x.shape sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1) brange = (torch.randperm(b, device=x.device))[:sample_subset_size] residual_scale_factor = b / sample_subset_size return brange, residual_scale_factor
def add_residual(x, brange, residual, residual_scale_factor, scaling_vector=None): if scaling_vector is None: x_flat = x.flatten(1) residual = residual.flatten(1) x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor) else: x_plus_residual = scaled_index_add( x, brange, residual.to(dtype=x.dtype), scaling=scaling_vector, alpha=residual_scale_factor ) return x_plus_residual
attn_bias_cache: Dict[Tuple, Any] = {}
def get_attn_bias_and_cat(x_list, branges=None): """ this will perform the index select, cat the tensors, and provide the attn_bias from cache """ batch_sizes = [b.shape[0] for b in branges] if branges is not None else [x.shape[0] for x in x_list] all_shapes = tuple((b, x.shape[1]) for b, x in zip(batch_sizes, x_list)) if all_shapes not in attn_bias_cache.keys(): seqlens = [] for b, x in zip(batch_sizes, x_list): for _ in range(b): seqlens.append(x.shape[1]) attn_bias = fmha.BlockDiagonalMask.from_seqlens(seqlens) attn_bias._batch_sizes = batch_sizes attn_bias_cache[all_shapes] = attn_bias
if branges is not None: cat_tensors = index_select_cat([x.flatten(1) for x in x_list], branges).view(1, -1, x_list[0].shape[-1]) else: tensors_bs1 = tuple(x.reshape([1, -1, *x.shape[2:]]) for x in x_list) cat_tensors = torch.cat(tensors_bs1, dim=1)
return attn_bias_cache[all_shapes], cat_tensors
def drop_add_residual_stochastic_depth_list( x_list: List[Tensor], residual_func: Callable[[Tensor, Any], Tensor], sample_drop_ratio: float = 0.0, scaling_vector=None,) -> Tensor: # 1) generate random set of indices for dropping samples in the batch branges_scales = [get_branges_scales(x, sample_drop_ratio=sample_drop_ratio) for x in x_list] branges = [s[0] for s in branges_scales] residual_scale_factors = [s[1] for s in branges_scales]
# 2) get attention bias and index+concat the tensors attn_bias, x_cat = get_attn_bias_and_cat(x_list, branges)
# 3) apply residual_func to get residual, and split the result residual_list = attn_bias.split(residual_func(x_cat, attn_bias=attn_bias)) # type: ignore
outputs = [] for x, brange, residual, residual_scale_factor in zip(x_list, branges, residual_list, residual_scale_factors): outputs.append(add_residual(x, brange, residual, residual_scale_factor, scaling_vector).view_as(x)) return outputs
class NestedTensorBlock(Block): def forward_nested(self, x_list: List[Tensor]) -> List[Tensor]: """ x_list contains a list of tensors to nest together and run """ assert isinstance(self.attn, MemEffAttention)
if self.training and self.sample_drop_ratio > 0.0:
def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.attn(self.norm1(x), attn_bias=attn_bias)
def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.mlp(self.norm2(x))
x_list = drop_add_residual_stochastic_depth_list( x_list, residual_func=attn_residual_func, sample_drop_ratio=self.sample_drop_ratio, scaling_vector=self.ls1.gamma if isinstance(self.ls1, LayerScale) else None, ) x_list = drop_add_residual_stochastic_depth_list( x_list, residual_func=ffn_residual_func, sample_drop_ratio=self.sample_drop_ratio, scaling_vector=self.ls2.gamma if isinstance(self.ls1, LayerScale) else None, ) return x_list else:
def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.ls1(self.attn(self.norm1(x), attn_bias=attn_bias))
def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.ls2(self.mlp(self.norm2(x)))
attn_bias, x = get_attn_bias_and_cat(x_list) x = x + attn_residual_func(x, attn_bias=attn_bias) x = x + ffn_residual_func(x) return attn_bias.split(x)
def forward(self, x_or_x_list): if isinstance(x_or_x_list, Tensor): return super().forward(x_or_x_list) elif isinstance(x_or_x_list, list): if not XFORMERS_AVAILABLE: raise AssertionError("xFormers is required for using nested tensors") return self.forward_nested(x_or_x_list) else: raise AssertionError12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import torchimport torch.nn as nnfrom torch.nn.init import trunc_normal_from torch.nn.utils import weight_norm
class DINOHead(nn.Module): def __init__( self, in_dim, out_dim, use_bn=False, nlayers=3, hidden_dim=2048, bottleneck_dim=256, mlp_bias=True, ): super().__init__() nlayers = max(nlayers, 1) self.mlp = _build_mlp(nlayers, in_dim, bottleneck_dim, hidden_dim=hidden_dim, use_bn=use_bn, bias=mlp_bias) self.apply(self._init_weights) self.last_layer = weight_norm(nn.Linear(bottleneck_dim, out_dim, bias=False)) self.last_layer.weight_g.data.fill_(1)
def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=0.02) if isinstance(m, nn.Linear) and m.bias is not None: nn.init.constant_(m.bias, 0)
def forward(self, x): x = self.mlp(x) eps = 1e-6 if x.dtype == torch.float16 else 1e-12 x = nn.functional.normalize(x, dim=-1, p=2, eps=eps) x = self.last_layer(x) return x
def _build_mlp(nlayers, in_dim, bottleneck_dim, hidden_dim=None, use_bn=False, bias=True): if nlayers == 1: return nn.Linear(in_dim, bottleneck_dim, bias=bias) else: layers = [nn.Linear(in_dim, hidden_dim, bias=bias)] if use_bn: layers.append(nn.BatchNorm1d(hidden_dim)) layers.append(nn.GELU()) for _ in range(nlayers - 2): layers.append(nn.Linear(hidden_dim, hidden_dim, bias=bias)) if use_bn: layers.append(nn.BatchNorm1d(hidden_dim)) layers.append(nn.GELU()) layers.append(nn.Linear(hidden_dim, bottleneck_dim, bias=bias)) return nn.Sequential(*layers)12345678910111213141516171819202122232425262728293031323334# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/drop.py
from torch import nn
def drop_path(x, drop_prob: float = 0.0, training: bool = False): if drop_prob == 0.0 or not training: return x keep_prob = 1 - drop_prob shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets random_tensor = x.new_empty(shape).bernoulli_(keep_prob) if keep_prob > 0.0: random_tensor.div_(keep_prob) output = x * random_tensor return output
class DropPath(nn.Module): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
def __init__(self, drop_prob=None): super(DropPath, self).__init__() self.drop_prob = drop_prob
def forward(self, x): return drop_path(x, self.drop_prob, self.training)123456789101112131415161718192021222324252627# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# Modified from: https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.py#L103-L110
from typing import Union
import torchfrom torch import Tensorfrom torch import nn
class LayerScale(nn.Module): def __init__( self, dim: int, init_values: Union[float, Tensor] = 1e-5, inplace: bool = False, ) -> None: super().__init__() self.inplace = inplace self.gamma = nn.Parameter(init_values * torch.ones(dim))
def forward(self, x: Tensor) -> Tensor: return x.mul_(self.gamma) if self.inplace else x * self.gamma12345678910111213141516171819202122232425262728293031323334353637383940# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/mlp.py
from typing import Callable, Optional
from torch import Tensor, nn
class Mlp(nn.Module): def __init__( self, in_features: int, hidden_features: Optional[int] = None, out_features: Optional[int] = None, act_layer: Callable[..., nn.Module] = nn.GELU, drop: float = 0.0, bias: bool = True, ) -> None: super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features, bias=bias) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features, bias=bias) self.drop = nn.Dropout(drop)
def forward(self, x: Tensor) -> Tensor: x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/patch_embed.py
from typing import Callable, Optional, Tuple, Union
from torch import Tensorimport torch.nn as nn
def make_2tuple(x): if isinstance(x, tuple): assert len(x) == 2 return x
assert isinstance(x, int) return (x, x)
class PatchEmbed(nn.Module): """ 2D image to patch embedding: (B,C,H,W) -> (B,N,D)
Args: img_size: Image size. patch_size: Patch token size. in_chans: Number of input image channels. embed_dim: Number of linear projection output channels. norm_layer: Normalization layer. """
def __init__( self, img_size: Union[int, Tuple[int, int]] = 224, patch_size: Union[int, Tuple[int, int]] = 16, in_chans: int = 3, embed_dim: int = 768, norm_layer: Optional[Callable] = None, flatten_embedding: bool = True, ) -> None: super().__init__()
image_HW = make_2tuple(img_size) patch_HW = make_2tuple(patch_size) patch_grid_size = ( image_HW[0] // patch_HW[0], image_HW[1] // patch_HW[1], )
self.img_size = image_HW self.patch_size = patch_HW self.patches_resolution = patch_grid_size self.num_patches = patch_grid_size[0] * patch_grid_size[1]
self.in_chans = in_chans self.embed_dim = embed_dim
self.flatten_embedding = flatten_embedding
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_HW, stride=patch_HW) self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
def forward(self, x: Tensor) -> Tensor: _, _, H, W = x.shape patch_H, patch_W = self.patch_size
assert H % patch_H == 0, f"Input image height {H} is not a multiple of patch height {patch_H}" assert W % patch_W == 0, f"Input image width {W} is not a multiple of patch width: {patch_W}"
x = self.proj(x) # B C H W H, W = x.size(2), x.size(3) x = x.flatten(2).transpose(1, 2) # B HW C x = self.norm(x) if not self.flatten_embedding: x = x.reshape(-1, H, W, self.embed_dim) # B H W C return x
def flops(self) -> float: Ho, Wo = self.patches_resolution flops = Ho * Wo * self.embed_dim * self.in_chans * (self.patch_size[0] * self.patch_size[1]) if self.norm is not None: flops += Ho * Wo * self.embed_dim return flops123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import osfrom typing import Callable, Optionalimport warnings
from torch import Tensor, nnimport torch.nn.functional as F
class SwiGLUFFN(nn.Module): def __init__( self, in_features: int, hidden_features: Optional[int] = None, out_features: Optional[int] = None, act_layer: Callable[..., nn.Module] = None, drop: float = 0.0, bias: bool = True, ) -> None: super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.w12 = nn.Linear(in_features, 2 * hidden_features, bias=bias) self.w3 = nn.Linear(hidden_features, out_features, bias=bias)
def forward(self, x: Tensor) -> Tensor: x12 = self.w12(x) x1, x2 = x12.chunk(2, dim=-1) hidden = F.silu(x1) * x2 return self.w3(hidden)
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is Nonetry: if XFORMERS_ENABLED: from xformers.ops import SwiGLU
XFORMERS_AVAILABLE = True # warnings.warn("xFormers is available (SwiGLU)") else: # warnings.warn("xFormers is disabled (SwiGLU)") raise ImportErrorexcept ImportError: SwiGLU = SwiGLUFFN XFORMERS_AVAILABLE = False
# warnings.warn("xFormers is not available (SwiGLU)")
class SwiGLUFFNFused(SwiGLU): def __init__( self, in_features: int, hidden_features: Optional[int] = None, out_features: Optional[int] = None, act_layer: Callable[..., nn.Module] = None, drop: float = 0.0, bias: bool = True, ) -> None: out_features = out_features or in_features hidden_features = hidden_features or in_features hidden_features = (int(hidden_features * 2 / 3) + 7) // 8 * 8 super().__init__( in_features=in_features, hidden_features=hidden_features, out_features=out_features, bias=bias, )12345678910111213141516171819202122232425262728293031323334353637383940414243# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import logging
from . import vision_transformer as vits
logger = logging.getLogger("dinov2")
def build_model(args, only_teacher=False, img_size=224): args.arch = args.arch.removesuffix("_memeff") if "vit" in args.arch: vit_kwargs = dict( img_size=img_size, patch_size=args.patch_size, init_values=args.layerscale, ffn_layer=args.ffn_layer, block_chunks=args.block_chunks, qkv_bias=args.qkv_bias, proj_bias=args.proj_bias, ffn_bias=args.ffn_bias, num_register_tokens=args.num_register_tokens, interpolate_offset=args.interpolate_offset, interpolate_antialias=args.interpolate_antialias, ) teacher = vits.__dict__[args.arch](**vit_kwargs) if only_teacher: return teacher, teacher.embed_dim student = vits.__dict__[args.arch]( **vit_kwargs, drop_path_rate=args.drop_path_rate, drop_path_uniform=args.drop_path_uniform, ) embed_dim = student.embed_dim return student, teacher, embed_dim
def build_model_from_cfg(cfg, only_teacher=False): return build_model(cfg.student, only_teacher=only_teacher, img_size=cfg.crops.global_crops_size)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/main/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py
from functools import partialimport mathimport loggingfrom typing import Sequence, Tuple, Union, Callable
import torchimport torch.nn as nnfrom torch.utils.checkpoint import checkpointfrom torch.nn.init import trunc_normal_
from ..layers import Mlp, PatchEmbed, SwiGLUFFNFused, MemEffAttention, NestedTensorBlock as Blockfrom ...layers.attention import FlashAttention
# logger = logging.getLogger("dinov2")
def named_apply(fn: Callable, module: nn.Module, name="", depth_first=True, include_root=False) -> nn.Module: if not depth_first and include_root: fn(module=module, name=name) for child_name, child_module in module.named_children(): child_name = ".".join((name, child_name)) if name else child_name named_apply(fn=fn, module=child_module, name=child_name, depth_first=depth_first, include_root=True) if depth_first and include_root: fn(module=module, name=name) return module
class BlockChunk(nn.ModuleList): def forward(self, x): for b in self: x = b(x) return x
class DinoVisionTransformer(nn.Module): def __init__( self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4.0, qkv_bias=True, ffn_bias=True, proj_bias=True, drop_path_rate=0.0, drop_path_uniform=False, init_values=None, # for layerscale: None or 0 => no layerscale embed_layer=PatchEmbed, act_layer=nn.GELU, block_fn=Block, ffn_layer="mlp", block_chunks=1, num_register_tokens=0, interpolate_antialias=False, interpolate_offset=0.1, ): """ Args: img_size (int, tuple): input image size patch_size (int, tuple): patch size in_chans (int): number of input channels embed_dim (int): embedding dimension depth (int): depth of transformer num_heads (int): number of attention heads mlp_ratio (int): ratio of mlp hidden dim to embedding dim qkv_bias (bool): enable bias for qkv if True proj_bias (bool): enable bias for proj in attn if True ffn_bias (bool): enable bias for ffn if True drop_path_rate (float): stochastic depth rate drop_path_uniform (bool): apply uniform drop rate across blocks weight_init (str): weight init scheme init_values (float): layer-scale init values embed_layer (nn.Module): patch embedding layer act_layer (nn.Module): MLP activation layer block_fn (nn.Module): transformer block class ffn_layer (str): "mlp", "swiglu", "swiglufused" or "identity" block_chunks: (int) split block sequence into block_chunks units for FSDP wrap num_register_tokens: (int) number of extra cls tokens (so-called "registers") interpolate_antialias: (str) flag to apply anti-aliasing when interpolating positional embeddings interpolate_offset: (float) work-around offset to apply when interpolating positional embeddings """ super().__init__() norm_layer = partial(nn.LayerNorm, eps=1e-6)
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models self.num_tokens = 1 self.n_blocks = depth self.num_heads = num_heads self.patch_size = patch_size self.num_register_tokens = num_register_tokens self.interpolate_antialias = interpolate_antialias self.interpolate_offset = interpolate_offset
self.patch_embed = embed_layer(img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim) num_patches = self.patch_embed.num_patches
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + self.num_tokens, embed_dim)) assert num_register_tokens >= 0 self.register_tokens = ( nn.Parameter(torch.zeros(1, num_register_tokens, embed_dim)) if num_register_tokens else None )
if drop_path_uniform is True: dpr = [drop_path_rate] * depth else: dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
if ffn_layer == "mlp": # logger.info("using MLP layer as FFN") ffn_layer = Mlp elif ffn_layer == "swiglufused" or ffn_layer == "swiglu": # logger.info("using SwiGLU layer as FFN") ffn_layer = SwiGLUFFNFused elif ffn_layer == "identity": # logger.info("using Identity layer as FFN")
def f(*args, **kwargs): return nn.Identity()
ffn_layer = f else: raise NotImplementedError
blocks_list = [ block_fn( dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, proj_bias=proj_bias, ffn_bias=ffn_bias, drop_path=dpr[i], norm_layer=norm_layer, act_layer=act_layer, ffn_layer=ffn_layer, init_values=init_values, attn_class=FlashAttention ) for i in range(depth) ] if block_chunks > 0: self.chunked_blocks = True chunked_blocks = [] chunksize = depth // block_chunks for i in range(0, depth, chunksize): # this is to keep the block index consistent if we chunk the block list chunked_blocks.append([nn.Identity()] * i + blocks_list[i : i + chunksize]) self.blocks = nn.ModuleList([BlockChunk(p) for p in chunked_blocks]) else: self.chunked_blocks = False self.blocks = nn.ModuleList(blocks_list)
self.norm = norm_layer(embed_dim) self.head = nn.Identity()
self.mask_token = nn.Parameter(torch.zeros(1, embed_dim))
self.init_weights()
def init_weights(self): trunc_normal_(self.pos_embed, std=0.02) nn.init.normal_(self.cls_token, std=1e-6) if self.register_tokens is not None: nn.init.normal_(self.register_tokens, std=1e-6) named_apply(init_weights_vit_timm, self)
def interpolate_pos_encoding(self, x, w, h): previous_dtype = x.dtype npatch = x.shape[1] - 1 N = self.pos_embed.shape[1] - 1 if npatch == N and w == h: return self.pos_embed pos_embed = self.pos_embed.float() class_pos_embed = pos_embed[:, 0] patch_pos_embed = pos_embed[:, 1:] dim = x.shape[-1] w0 = w // self.patch_size h0 = h // self.patch_size M = int(math.sqrt(N)) # Recover the number of patches in each dimension assert N == M * M kwargs = {} if self.interpolate_offset: # Historical kludge: add a small number to avoid floating point error in the interpolation, see https://github.com/facebookresearch/dino/issues/8 # Note: still needed for backward-compatibility, the underlying operators are using both output size and scale factors sx = float(w0 + self.interpolate_offset) / M sy = float(h0 + self.interpolate_offset) / M kwargs["scale_factor"] = (sx, sy) else: # Simply specify an output size instead of a scale factor kwargs["size"] = (w0, h0) patch_pos_embed = nn.functional.interpolate( patch_pos_embed.reshape(1, M, M, dim).permute(0, 3, 1, 2), mode="bicubic", antialias=self.interpolate_antialias, **kwargs, ) assert (w0, h0) == patch_pos_embed.shape[-2:] patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1).to(previous_dtype)
def prepare_tokens_with_masks(self, x, masks=None): B, nc, w, h = x.shape x = self.patch_embed(x) if masks is not None: x = torch.where(masks.unsqueeze(-1), self.mask_token.to(x.dtype).unsqueeze(0), x)
x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) x = x + self.interpolate_pos_encoding(x, w, h)
if self.register_tokens is not None: x = torch.cat( ( x[:, :1], self.register_tokens.expand(x.shape[0], -1, -1), x[:, 1:], ), dim=1, )
return x
def forward_features_list(self, x_list, masks_list): x = [self.prepare_tokens_with_masks(x, masks) for x, masks in zip(x_list, masks_list)] for blk in self.blocks: if self.training: x = checkpoint(blk, x, use_reentrant=False) else: x = blk(x)
all_x = x output = [] for x, masks in zip(all_x, masks_list): x_norm = self.norm(x) output.append( { "x_norm_clstoken": x_norm[:, 0], "x_norm_regtokens": x_norm[:, 1 : self.num_register_tokens + 1], "x_norm_patchtokens": x_norm[:, self.num_register_tokens + 1 :], "x_prenorm": x, "masks": masks, } ) return output
def forward_features(self, x, masks=None): if isinstance(x, list): return self.forward_features_list(x, masks)
x = self.prepare_tokens_with_masks(x, masks)
for blk in self.blocks: if self.training: x = checkpoint(blk, x, use_reentrant=False) else: x = blk(x)
x_norm = self.norm(x) return { "x_norm_clstoken": x_norm[:, 0], "x_norm_regtokens": x_norm[:, 1 : self.num_register_tokens + 1], "x_norm_patchtokens": x_norm[:, self.num_register_tokens + 1 :], "x_prenorm": x, "masks": masks, }
def _get_intermediate_layers_not_chunked(self, x, n=1): x = self.prepare_tokens_with_masks(x) # If n is an int, take the n last blocks. If it's a list, take them output, total_block_len = [], len(self.blocks) blocks_to_take = range(total_block_len - n, total_block_len) if isinstance(n, int) else n for i, blk in enumerate(self.blocks): x = blk(x) if i in blocks_to_take: output.append(x) assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" return output
def _get_intermediate_layers_chunked(self, x, n=1): x = self.prepare_tokens_with_masks(x) output, i, total_block_len = [], 0, len(self.blocks[-1]) # If n is an int, take the n last blocks. If it's a list, take them blocks_to_take = range(total_block_len - n, total_block_len) if isinstance(n, int) else n for block_chunk in self.blocks: for blk in block_chunk[i:]: # Passing the nn.Identity() x = blk(x) if i in blocks_to_take: output.append(x) i += 1 assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found" return output
def get_intermediate_layers( self, x: torch.Tensor, n: Union[int, Sequence] = 1, # Layers or n last layers to take reshape: bool = False, return_class_token: bool = False, norm=True, ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]]]: if self.chunked_blocks: outputs = self._get_intermediate_layers_chunked(x, n) else: outputs = self._get_intermediate_layers_not_chunked(x, n) if norm: outputs = [self.norm(out) for out in outputs] class_tokens = [out[:, 0] for out in outputs] outputs = [out[:, 1 + self.num_register_tokens :] for out in outputs] if reshape: B, _, w, h = x.shape outputs = [ out.reshape(B, w // self.patch_size, h // self.patch_size, -1).permute(0, 3, 1, 2).contiguous() for out in outputs ] if return_class_token: return tuple(zip(outputs, class_tokens)) return tuple(outputs)
def forward(self, *args, is_training=False, **kwargs): ret = self.forward_features(*args, **kwargs) if is_training: return ret else: return self.head(ret["x_norm_clstoken"])
def init_weights_vit_timm(module: nn.Module, name: str = ""): """ViT weight initialization, original timm impl (for reproducibility)""" if isinstance(module, nn.Linear): trunc_normal_(module.weight, std=0.02) if module.bias is not None: nn.init.zeros_(module.bias)
def vit_small(patch_size=16, num_register_tokens=0, **kwargs): model = DinoVisionTransformer( patch_size=patch_size, embed_dim=384, depth=12, num_heads=6, mlp_ratio=4, block_fn=partial(Block, attn_class=MemEffAttention), num_register_tokens=num_register_tokens, **kwargs, ) return model
def vit_base(patch_size=16, num_register_tokens=0, **kwargs): model = DinoVisionTransformer( patch_size=patch_size, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4, block_fn=partial(Block, attn_class=MemEffAttention), num_register_tokens=num_register_tokens, **kwargs, ) return model
def vit_large(patch_size=16, num_register_tokens=0, **kwargs): model = DinoVisionTransformer( patch_size=patch_size, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4, block_fn=partial(Block, attn_class=MemEffAttention), num_register_tokens=num_register_tokens, **kwargs, ) return model
def vit_giant2(patch_size=16, num_register_tokens=0, **kwargs): """ Close to ViT-giant, with embed-dim 1536 and 24 heads => embed-dim per head 64 """ model = DinoVisionTransformer( patch_size=patch_size, embed_dim=1536, depth=40, num_heads=24, mlp_ratio=4, block_fn=partial(Block, attn_class=MemEffAttention), num_register_tokens=num_register_tokens, **kwargs, ) return model1234# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
from enum import Enumimport osfrom pathlib import Pathfrom typing import Any, Dict, Optional
class ClusterType(Enum): AWS = "aws" FAIR = "fair" RSC = "rsc"
def _guess_cluster_type() -> ClusterType: uname = os.uname() if uname.sysname == "Linux": if uname.release.endswith("-aws"): # Linux kernel versions on AWS instances are of the form "5.4.0-1051-aws" return ClusterType.AWS elif uname.nodename.startswith("rsc"): # Linux kernel versions on RSC instances are standard ones but hostnames start with "rsc" return ClusterType.RSC
return ClusterType.FAIR
def get_cluster_type(cluster_type: Optional[ClusterType] = None) -> Optional[ClusterType]: if cluster_type is None: return _guess_cluster_type()
return cluster_type
def get_checkpoint_path(cluster_type: Optional[ClusterType] = None) -> Optional[Path]: cluster_type = get_cluster_type(cluster_type) if cluster_type is None: return None
CHECKPOINT_DIRNAMES = { ClusterType.AWS: "checkpoints", ClusterType.FAIR: "checkpoint", ClusterType.RSC: "checkpoint/dino", } return Path("/") / CHECKPOINT_DIRNAMES[cluster_type]
def get_user_checkpoint_path(cluster_type: Optional[ClusterType] = None) -> Optional[Path]: checkpoint_path = get_checkpoint_path(cluster_type) if checkpoint_path is None: return None
username = os.environ.get("USER") assert username is not None return checkpoint_path / username
def get_slurm_partition(cluster_type: Optional[ClusterType] = None) -> Optional[str]: cluster_type = get_cluster_type(cluster_type) if cluster_type is None: return None
SLURM_PARTITIONS = { ClusterType.AWS: "learnlab", ClusterType.FAIR: "learnlab", ClusterType.RSC: "learn", } return SLURM_PARTITIONS[cluster_type]
def get_slurm_executor_parameters( nodes: int, num_gpus_per_node: int, cluster_type: Optional[ClusterType] = None, **kwargs) -> Dict[str, Any]: # create default parameters params = { "mem_gb": 0, # Requests all memory on a node, see https://slurm.schedmd.com/sbatch.html "gpus_per_node": num_gpus_per_node, "tasks_per_node": num_gpus_per_node, # one task per GPU "cpus_per_task": 10, "nodes": nodes, "slurm_partition": get_slurm_partition(cluster_type), } # apply cluster-specific adjustments cluster_type = get_cluster_type(cluster_type) if cluster_type == ClusterType.AWS: params["cpus_per_task"] = 12 del params["mem_gb"] elif cluster_type == ClusterType.RSC: params["cpus_per_task"] = 12 # set additional parameters / apply overrides params.update(kwargs) return params123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import mathimport loggingimport os
from omegaconf import OmegaConf
import dinov2.distributed as distributedfrom dinov2.logging import setup_loggingfrom dinov2.utils import utilsfrom dinov2.configs import dinov2_default_config
logger = logging.getLogger("dinov2")
def apply_scaling_rules_to_cfg(cfg): # to fix if cfg.optim.scaling_rule == "sqrt_wrt_1024": base_lr = cfg.optim.base_lr cfg.optim.lr = base_lr cfg.optim.lr *= math.sqrt(cfg.train.batch_size_per_gpu * distributed.get_global_size() / 1024.0) logger.info(f"sqrt scaling learning rate; base: {base_lr}, new: {cfg.optim.lr}") else: raise NotImplementedError return cfg
def write_config(cfg, output_dir, name="config.yaml"): logger.info(OmegaConf.to_yaml(cfg)) saved_cfg_path = os.path.join(output_dir, name) with open(saved_cfg_path, "w") as f: OmegaConf.save(config=cfg, f=f) return saved_cfg_path
def get_cfg_from_args(args): args.output_dir = os.path.abspath(args.output_dir) args.opts += [f"train.output_dir={args.output_dir}"] default_cfg = OmegaConf.create(dinov2_default_config) cfg = OmegaConf.load(args.config_file) cfg = OmegaConf.merge(default_cfg, cfg, OmegaConf.from_cli(args.opts)) return cfg
def default_setup(args): distributed.enable(overwrite=True) seed = getattr(args, "seed", 0) rank = distributed.get_global_rank()
global logger setup_logging(output=args.output_dir, level=logging.INFO) logger = logging.getLogger("dinov2")
utils.fix_random_seeds(seed + rank) logger.info("git:\n {}\n".format(utils.get_sha())) logger.info("\n".join("%s: %s" % (k, str(v)) for k, v in sorted(dict(vars(args)).items())))
def setup(args): """ Create configs and perform basic setups. """ cfg = get_cfg_from_args(args) os.makedirs(args.output_dir, exist_ok=True) default_setup(args) apply_scaling_rules_to_cfg(cfg) write_config(cfg, args.output_dir) return cfg12345678910111213141516171819202122232425262728293031323334353637# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
from typing import Dict, Union
import numpy as npimport torch
TypeSpec = Union[str, np.dtype, torch.dtype]
_NUMPY_TO_TORCH_DTYPE: Dict[np.dtype, torch.dtype] = { np.dtype("bool"): torch.bool, np.dtype("uint8"): torch.uint8, np.dtype("int8"): torch.int8, np.dtype("int16"): torch.int16, np.dtype("int32"): torch.int32, np.dtype("int64"): torch.int64, np.dtype("float16"): torch.float16, np.dtype("float32"): torch.float32, np.dtype("float64"): torch.float64, np.dtype("complex64"): torch.complex64, np.dtype("complex128"): torch.complex128,}
def as_torch_dtype(dtype: TypeSpec) -> torch.dtype: if isinstance(dtype, torch.dtype): return dtype if isinstance(dtype, str): dtype = np.dtype(dtype) assert isinstance(dtype, np.dtype), f"Expected an instance of nunpy dtype, got {type(dtype)}" return _NUMPY_TO_TORCH_DTYPE[dtype]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
from collections import defaultdictimport logging
logger = logging.getLogger("dinov2")
def get_vit_lr_decay_rate(name, lr_decay_rate=1.0, num_layers=12, force_is_backbone=False, chunked_blocks=False): """ Calculate lr decay rate for different ViT blocks. Args: name (string): parameter name. lr_decay_rate (float): base lr decay rate. num_layers (int): number of ViT blocks. Returns: lr decay rate for the given parameter. """ layer_id = num_layers + 1 if name.startswith("backbone") or force_is_backbone: if ( ".pos_embed" in name or ".patch_embed" in name or ".mask_token" in name or ".cls_token" in name or ".register_tokens" in name ): layer_id = 0 elif force_is_backbone and ( "pos_embed" in name or "patch_embed" in name or "mask_token" in name or "cls_token" in name or "register_tokens" in name ): layer_id = 0 elif ".blocks." in name and ".residual." not in name: layer_id = int(name[name.find(".blocks.") :].split(".")[2]) + 1 elif chunked_blocks and "blocks." in name and "residual." not in name: layer_id = int(name[name.find("blocks.") :].split(".")[2]) + 1 elif "blocks." in name and "residual." not in name: layer_id = int(name[name.find("blocks.") :].split(".")[1]) + 1
return lr_decay_rate ** (num_layers + 1 - layer_id)
def get_params_groups_with_decay(model, lr_decay_rate=1.0, patch_embed_lr_mult=1.0): chunked_blocks = False if hasattr(model, "n_blocks"): logger.info("chunked fsdp") n_blocks = model.n_blocks chunked_blocks = model.chunked_blocks elif hasattr(model, "blocks"): logger.info("first code branch") n_blocks = len(model.blocks) elif hasattr(model, "backbone"): logger.info("second code branch") n_blocks = len(model.backbone.blocks) else: logger.info("else code branch") n_blocks = 0 all_param_groups = []
for name, param in model.named_parameters(): name = name.replace("_fsdp_wrapped_module.", "") if not param.requires_grad: continue decay_rate = get_vit_lr_decay_rate( name, lr_decay_rate, num_layers=n_blocks, force_is_backbone=n_blocks > 0, chunked_blocks=chunked_blocks ) d = {"params": param, "is_last_layer": False, "lr_multiplier": decay_rate, "wd_multiplier": 1.0, "name": name}
if "last_layer" in name: d.update({"is_last_layer": True})
if name.endswith(".bias") or "norm" in name or "gamma" in name: d.update({"wd_multiplier": 0.0})
if "patch_embed" in name: d.update({"lr_multiplier": d["lr_multiplier"] * patch_embed_lr_mult})
all_param_groups.append(d) logger.info(f"""{name}: lr_multiplier: {d["lr_multiplier"]}, wd_multiplier: {d["wd_multiplier"]}""")
return all_param_groups
def fuse_params_groups(all_params_groups, keys=("lr_multiplier", "wd_multiplier", "is_last_layer")): fused_params_groups = defaultdict(lambda: {"params": []}) for d in all_params_groups: identifier = "" for k in keys: identifier += k + str(d[k]) + "_"
for k in keys: fused_params_groups[identifier][k] = d[k] fused_params_groups[identifier]["params"].append(d["params"])
return fused_params_groups.values()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
import loggingimport osimport randomimport subprocessfrom urllib.parse import urlparse
import numpy as npimport torchfrom torch import nn
# logger = logging.getLogger("dinov2")
def load_pretrained_weights(model, pretrained_weights, checkpoint_key): if urlparse(pretrained_weights).scheme: # If it looks like an URL state_dict = torch.hub.load_state_dict_from_url(pretrained_weights, map_location="cpu") else: state_dict = torch.load(pretrained_weights, map_location="cpu") if checkpoint_key is not None and checkpoint_key in state_dict: # logger.info(f"Take key {checkpoint_key} in provided checkpoint dict") state_dict = state_dict[checkpoint_key] # remove `module.` prefix state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()} # remove `backbone.` prefix induced by multicrop wrapper state_dict = {k.replace("backbone.", ""): v for k, v in state_dict.items()} msg = model.load_state_dict(state_dict, strict=False) # logger.info("Pretrained weights found at {} and loaded with msg: {}".format(pretrained_weights, msg))
def fix_random_seeds(seed=31): """ Fix random seeds. """ torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) np.random.seed(seed) random.seed(seed)
def get_sha(): cwd = os.path.dirname(os.path.abspath(__file__))
def _run(command): return subprocess.check_output(command, cwd=cwd).decode("ascii").strip()
sha = "N/A" diff = "clean" branch = "N/A" try: sha = _run(["git", "rev-parse", "HEAD"]) subprocess.check_output(["git", "diff"], cwd=cwd) diff = _run(["git", "diff-index", "HEAD"]) diff = "has uncommitted changes" if diff else "clean" branch = _run(["git", "rev-parse", "--abbrev-ref", "HEAD"]) except Exception: pass message = f"sha: {sha}, status: {diff}, branch: {branch}" return message
class CosineScheduler(object): def __init__(self, base_value, final_value, total_iters, warmup_iters=0, start_warmup_value=0, freeze_iters=0): super().__init__() self.final_value = final_value self.total_iters = total_iters
freeze_schedule = np.zeros((freeze_iters))
warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters)
iters = np.arange(total_iters - warmup_iters - freeze_iters) schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters))) self.schedule = np.concatenate((freeze_schedule, warmup_schedule, schedule))
assert len(self.schedule) == self.total_iters
def __getitem__(self, it): if it >= self.total_iters: return self.final_value else: return self.schedule[it]
def has_batchnorms(model): bn_types = (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.SyncBatchNorm) for name, module in model.named_modules(): if isinstance(module, bn_types): return True return False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py
import loggingimport osimport warnings
from torch import Tensorfrom torch import nnimport torch
from torch.nn.functional import scaled_dot_product_attentionfrom torch.nn.attention import SDPBackend
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is Nonetry: if XFORMERS_ENABLED: from xformers.ops import memory_efficient_attention, unbind
XFORMERS_AVAILABLE = True # warnings.warn("xFormers is available (Attention)") else: # warnings.warn("xFormers is disabled (Attention)") raise ImportErrorexcept ImportError: XFORMERS_AVAILABLE = False # warnings.warn("xFormers is not available (Attention)")
class Attention(nn.Module): def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, proj_bias: bool = True, attn_drop: float = 0.0, proj_drop: float = 0.0, ) -> None: super().__init__() self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim**-0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim, bias=proj_bias) self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x: Tensor, attn_bias=None) -> Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0] * self.scale, qkv[1], qkv[2] attn = q @ k.transpose(-2, -1)
attn = attn.softmax(dim=-1) attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x
class MemEffAttention(Attention): def forward(self, x: Tensor, attn_bias=None) -> Tensor: if not XFORMERS_AVAILABLE: if attn_bias is not None: raise AssertionError("xFormers is required for using nested tensors") return super().forward(x)
B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads)
# q, k, v = unbind(qkv, 2) q, k, v = [qkv[:,:,i] for i in range(3)]
x = memory_efficient_attention(q, k, v, attn_bias=attn_bias) x = x.reshape([B, N, C])
x = self.proj(x) x = self.proj_drop(x) return x
class FlashAttention(Attention): def forward(self, x: Tensor, attn_bias=None) -> Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).transpose(1, 3)
# q, k, v = unbind(qkv, 2) q, k, v = [qkv[:,:,i] for i in range(3)]
if q.dtype == torch.bfloat16: with nn.attention.sdpa_kernel(SDPBackend.FLASH_ATTENTION): x = scaled_dot_product_attention(q, k, v) else: with nn.attention.sdpa_kernel([SDPBackend.MATH, SDPBackend.EFFICIENT_ATTENTION]): x = scaled_dot_product_attention(q, k, v)
x = x.transpose(1, 2).reshape([B, N, C])
x = self.proj(x) x = self.proj_drop(x) return x
"""Following is written by GPT-4o"""class CrossAttentionRope(nn.Module): def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, proj_bias: bool = True, attn_drop: float = 0.0, proj_drop: float = 0.0, qk_norm: bool = False, norm_layer: nn.Module = nn.LayerNorm, rope=None, ) -> None: super().__init__() self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim**-0.5
# Separate projection layers for query, key, and value self.q_proj = nn.Linear(dim, dim, bias=qkv_bias) self.k_proj = nn.Linear(dim, dim, bias=qkv_bias) self.v_proj = nn.Linear(dim, dim, bias=qkv_bias)
self.q_norm = norm_layer(head_dim) if qk_norm else nn.Identity() self.k_norm = norm_layer(head_dim) if qk_norm else nn.Identity()
self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim, bias=proj_bias) self.proj_drop = nn.Dropout(proj_drop)
self.rope = rope
def forward(self, query: Tensor, key: Tensor, value: Tensor, attn_bias=None, qpos=None, kpos=None) -> Tensor: """ Args: query: Tensor of shape (B, N, C), input query key: Tensor of shape (B, M, C), input key value: Tensor of shape (B, M, C), input value attn_bias: Optional tensor for attention bias Returns: Tensor of shape (B, N, C), output of cross-attention """ B, N, C = query.shape _, M, _ = key.shape
# Project query, key, and value q = self.q_proj(query).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) k = self.k_proj(key).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) v = self.v_proj(value).reshape(B, M, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3) q, k = self.q_norm(q).to(v.dtype), self.k_norm(k).to(v.dtype)
if self.rope is not None: q = self.rope(q, qpos) k = self.rope(k, kpos)
# Scale query q = q * self.scale
# Compute attention scores attn = q @ k.transpose(-2, -1) # (B, num_heads, N, M) if attn_bias is not None: attn = attn + attn_bias
attn = attn.softmax(dim=-1) attn = self.attn_drop(attn)
# Compute attention output x = (attn @ v).transpose(1, 2).reshape(B, N, C) # (B, N, C)
# Final projection x = self.proj(x) x = self.proj_drop(x) return x
class MemEffCrossAttentionRope(CrossAttentionRope): def forward(self, query: Tensor, key: Tensor, value: Tensor, attn_bias=None, qpos=None, kpos=None) -> Tensor: """ Args: query: Tensor of shape (B, N, C), input query key: Tensor of shape (B, M, C), input key value: Tensor of shape (B, M, C), input value attn_bias: Optional tensor for attention bias Returns: Tensor of shape (B, N, C), output of cross-attention """ if not XFORMERS_AVAILABLE: if attn_bias is not None: raise AssertionError("xFormers is required for using nested tensors") return super().forward(query, key, value, attn_bias)
B, N, C = query.shape _, M, _ = key.shape
# Project query, key, and value q = self.q_proj(query).reshape(B, N, self.num_heads, C // self.num_heads) k = self.k_proj(key).reshape(B, M, self.num_heads, C // self.num_heads) v = self.v_proj(value).reshape(B, M, self.num_heads, C // self.num_heads)
q = q.transpose(1, 2) k = k.transpose(1, 2) q, k = self.q_norm(q).to(v.dtype), self.k_norm(k).to(v.dtype)
if self.rope is not None: q = self.rope(q, qpos) k = self.rope(k, kpos)
q = q.transpose(1, 2) k = k.transpose(1, 2)
# Compute memory-efficient attention x = memory_efficient_attention(q, k, v, attn_bias=attn_bias) x = x.reshape(B, N, C)
# Final projection x = self.proj(x) x = self.proj_drop(x) return x
class AttentionRope(nn.Module): def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, proj_bias: bool = True, attn_drop: float = 0.0, proj_drop: float = 0.0, qk_norm: bool = False, norm_layer: nn.Module = nn.LayerNorm, rope=None ) -> None: super().__init__() self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim**-0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim, bias=proj_bias) self.proj_drop = nn.Dropout(proj_drop)
self.q_norm = norm_layer(head_dim) if qk_norm else nn.Identity() self.k_norm = norm_layer(head_dim) if qk_norm else nn.Identity()
self.rope = rope
def forward(self, x: Tensor, attn_bias=None, xpos=None) -> Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] q, k = self.q_norm(q).to(v.dtype), self.k_norm(k).to(v.dtype)
if self.rope is not None: q = self.rope(q, xpos) k = self.rope(k, xpos) q = q * self.scale attn = q @ k.transpose(-2, -1)
attn = attn.softmax(dim=-1) attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x
class MemEffAttentionRope(AttentionRope): def forward(self, x: Tensor, attn_bias=None, xpos=None) -> Tensor: if not XFORMERS_AVAILABLE: if attn_bias is not None: raise AssertionError("xFormers is required for using nested tensors") return super().forward(x)
B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads) qkv = qkv.transpose(1, 3) # q, k, v = unbind(qkv, 2) q, k, v = [qkv[:,:,i] for i in range(3)] q, k = self.q_norm(q).to(v.dtype), self.k_norm(k).to(v.dtype)
if self.rope is not None: q = self.rope(q, xpos) k = self.rope(k, xpos)
q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2)
x = memory_efficient_attention(q, k, v, attn_bias=attn_bias) x = x.reshape([B, N, C])
# score_matrix = (q.permute(0, 2, 1, 3) * self.scale @ k.permute(0, 2, 1, 3).transpose(-2, -1)).sum(dim=1).reshape(frame_num, 261, frame_num, 261).mean(dim=[1, 3]).sum(1) # for frame attention matrix # global_valid_id = torch.where(score_matrix > 0) # score_matrix = (q.permute(0, 2, 1, 3) * self.scale @ k.permute(0, 2, 1, 3).transpose(-2, -1)).sum(dim=1)
x = self.proj(x) x = self.proj_drop(x) return x
class FlashAttentionRope(AttentionRope): def forward(self, x: Tensor, attn_bias=None, xpos=None) -> Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).transpose(1, 3)
# q, k, v = unbind(qkv, 2) q, k, v = [qkv[:,:,i] for i in range(3)] q, k = self.q_norm(q).to(v.dtype), self.k_norm(k).to(v.dtype)
if self.rope is not None: q = self.rope(q, xpos) k = self.rope(k, xpos)
if q.dtype == torch.bfloat16: with nn.attention.sdpa_kernel(SDPBackend.FLASH_ATTENTION): x = scaled_dot_product_attention(q, k, v) else: with nn.attention.sdpa_kernel([SDPBackend.MATH, SDPBackend.EFFICIENT_ATTENTION]): x = scaled_dot_product_attention(q, k, v)
x = x.transpose(1, 2).reshape([B, N, C])
x = self.proj(x) x = self.proj_drop(x) return x
def get_attn_score(blk_class, x, frame_num, token_length, xpos=None): x = blk_class.norm1(x) B, N, C = x.shape qkv = blk_class.attn.qkv(x).reshape(B, N, 3, blk_class.attn.num_heads, C // blk_class.attn.num_heads) qkv = qkv.transpose(1, 3) # q, k, v = unbind(qkv, 2) q, k, v = [qkv[:,:,i] for i in range(3)] q, k = blk_class.attn.q_norm(q).to(v.dtype), blk_class.attn.k_norm(k).to(v.dtype)
if blk_class.attn.rope is not None: q = blk_class.attn.rope(q, xpos) k = blk_class.attn.rope(k, xpos)
q = q.transpose(1, 2) k = k.transpose(1, 2)
score = (q.permute(0, 2, 1, 3) * blk_class.attn.scale @ k.permute(0, 2, 1, 3).transpose(-2, -1)).sum(dim=1).reshape(B, frame_num, token_length, frame_num, token_length).mean(dim=[2, 4]).sum(-1)
return score123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406# Copyright (c) Meta Platforms, Inc. and affiliates.## This source code is licensed under the Apache License, Version 2.0# found in the LICENSE file in the root directory of this source tree.
# References:# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/patch_embed.py
import loggingimport osfrom typing import Callable, List, Any, Tuple, Dictimport warnings
import torchfrom torch import nn, Tensor
from .attention import Attention, MemEffAttention, CrossAttentionRope, MemEffCrossAttentionRope, FlashAttentionRopefrom ..dinov2.layers.drop_path import DropPathfrom ..dinov2.layers.layer_scale import LayerScalefrom ..dinov2.layers.mlp import Mlp
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is Nonetry: if XFORMERS_ENABLED: from xformers.ops import fmha, scaled_index_add, index_select_cat
XFORMERS_AVAILABLE = True # warnings.warn("xFormers is available (Block)") else: # warnings.warn("xFormers is disabled (Block)") raise ImportErrorexcept ImportError: XFORMERS_AVAILABLE = False # warnings.warn("xFormers is not available (Block)")
class Block(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_ratio: float = 4.0, qkv_bias: bool = False, proj_bias: bool = True, ffn_bias: bool = True, drop: float = 0.0, attn_drop: float = 0.0, init_values=None, drop_path: float = 0.0, act_layer: Callable[..., nn.Module] = nn.GELU, norm_layer: Callable[..., nn.Module] = nn.LayerNorm, attn_class: Callable[..., nn.Module] = Attention, ffn_layer: Callable[..., nn.Module] = Mlp, ) -> None: super().__init__() # print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}") self.norm1 = norm_layer(dim) self.attn = attn_class( dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias, attn_drop=attn_drop, proj_drop=drop, )
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = ffn_layer( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, bias=ffn_bias, ) self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.sample_drop_ratio = drop_path
def forward(self, x: Tensor) -> Tensor: def attn_residual_func(x: Tensor) -> Tensor: return self.ls1(self.attn(self.norm1(x)))
def ffn_residual_func(x: Tensor) -> Tensor: return self.ls2(self.mlp(self.norm2(x)))
if self.training and self.sample_drop_ratio > 0.1: # the overhead is compensated only for a drop path rate larger than 0.1 x = drop_add_residual_stochastic_depth( x, residual_func=attn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) x = drop_add_residual_stochastic_depth( x, residual_func=ffn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) elif self.training and self.sample_drop_ratio > 0.0: x = x + self.drop_path1(attn_residual_func(x)) x = x + self.drop_path1(ffn_residual_func(x)) # FIXME: drop_path2 else: x = x + attn_residual_func(x) x = x + ffn_residual_func(x) return x
def drop_add_residual_stochastic_depth( x: Tensor, residual_func: Callable[[Tensor], Tensor], sample_drop_ratio: float = 0.0,) -> Tensor: # 1) extract subset using permutation b, n, d = x.shape sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1) brange = (torch.randperm(b, device=x.device))[:sample_subset_size] x_subset = x[brange]
# 2) apply residual_func to get residual residual = residual_func(x_subset)
x_flat = x.flatten(1) residual = residual.flatten(1)
residual_scale_factor = b / sample_subset_size
# 3) add the residual x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor) return x_plus_residual.view_as(x)
def get_branges_scales(x, sample_drop_ratio=0.0): b, n, d = x.shape sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1) brange = (torch.randperm(b, device=x.device))[:sample_subset_size] residual_scale_factor = b / sample_subset_size return brange, residual_scale_factor
def add_residual(x, brange, residual, residual_scale_factor, scaling_vector=None): if scaling_vector is None: x_flat = x.flatten(1) residual = residual.flatten(1) x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor) else: x_plus_residual = scaled_index_add( x, brange, residual.to(dtype=x.dtype), scaling=scaling_vector, alpha=residual_scale_factor ) return x_plus_residual
attn_bias_cache: Dict[Tuple, Any] = {}
def get_attn_bias_and_cat(x_list, branges=None): """ this will perform the index select, cat the tensors, and provide the attn_bias from cache """ batch_sizes = [b.shape[0] for b in branges] if branges is not None else [x.shape[0] for x in x_list] all_shapes = tuple((b, x.shape[1]) for b, x in zip(batch_sizes, x_list)) if all_shapes not in attn_bias_cache.keys(): seqlens = [] for b, x in zip(batch_sizes, x_list): for _ in range(b): seqlens.append(x.shape[1]) attn_bias = fmha.BlockDiagonalMask.from_seqlens(seqlens) attn_bias._batch_sizes = batch_sizes attn_bias_cache[all_shapes] = attn_bias
if branges is not None: cat_tensors = index_select_cat([x.flatten(1) for x in x_list], branges).view(1, -1, x_list[0].shape[-1]) else: tensors_bs1 = tuple(x.reshape([1, -1, *x.shape[2:]]) for x in x_list) cat_tensors = torch.cat(tensors_bs1, dim=1)
return attn_bias_cache[all_shapes], cat_tensors
def drop_add_residual_stochastic_depth_list( x_list: List[Tensor], residual_func: Callable[[Tensor, Any], Tensor], sample_drop_ratio: float = 0.0, scaling_vector=None,) -> Tensor: # 1) generate random set of indices for dropping samples in the batch branges_scales = [get_branges_scales(x, sample_drop_ratio=sample_drop_ratio) for x in x_list] branges = [s[0] for s in branges_scales] residual_scale_factors = [s[1] for s in branges_scales]
# 2) get attention bias and index+concat the tensors attn_bias, x_cat = get_attn_bias_and_cat(x_list, branges)
# 3) apply residual_func to get residual, and split the result residual_list = attn_bias.split(residual_func(x_cat, attn_bias=attn_bias)) # type: ignore
outputs = [] for x, brange, residual, residual_scale_factor in zip(x_list, branges, residual_list, residual_scale_factors): outputs.append(add_residual(x, brange, residual, residual_scale_factor, scaling_vector).view_as(x)) return outputs
class NestedTensorBlock(Block): def forward_nested(self, x_list: List[Tensor]) -> List[Tensor]: """ x_list contains a list of tensors to nest together and run """ assert isinstance(self.attn, MemEffAttention)
if self.training and self.sample_drop_ratio > 0.0:
def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.attn(self.norm1(x), attn_bias=attn_bias)
def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.mlp(self.norm2(x))
x_list = drop_add_residual_stochastic_depth_list( x_list, residual_func=attn_residual_func, sample_drop_ratio=self.sample_drop_ratio, scaling_vector=self.ls1.gamma if isinstance(self.ls1, LayerScale) else None, ) x_list = drop_add_residual_stochastic_depth_list( x_list, residual_func=ffn_residual_func, sample_drop_ratio=self.sample_drop_ratio, scaling_vector=self.ls2.gamma if isinstance(self.ls1, LayerScale) else None, ) return x_list else:
def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.ls1(self.attn(self.norm1(x), attn_bias=attn_bias))
def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor: return self.ls2(self.mlp(self.norm2(x)))
attn_bias, x = get_attn_bias_and_cat(x_list) x = x + attn_residual_func(x, attn_bias=attn_bias) x = x + ffn_residual_func(x) return attn_bias.split(x)
def forward(self, x_or_x_list): if isinstance(x_or_x_list, Tensor): return super().forward(x_or_x_list) elif isinstance(x_or_x_list, list): if not XFORMERS_AVAILABLE: raise AssertionError("xFormers is required for using nested tensors") return self.forward_nested(x_or_x_list) else: raise AssertionError
class BlockRope(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_ratio: float = 4.0, qkv_bias: bool = False, proj_bias: bool = True, ffn_bias: bool = True, drop: float = 0.0, attn_drop: float = 0.0, init_values=None, drop_path: float = 0.0, act_layer: Callable[..., nn.Module] = nn.GELU, norm_layer: Callable[..., nn.Module] = nn.LayerNorm, attn_class: Callable[..., nn.Module] = Attention, ffn_layer: Callable[..., nn.Module] = Mlp, qk_norm: bool=False, rope=None ) -> None: super().__init__() # print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}") self.norm1 = norm_layer(dim) self.attn = attn_class( dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias, attn_drop=attn_drop, proj_drop=drop, qk_norm=qk_norm, rope=rope )
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = ffn_layer( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, bias=ffn_bias, ) self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.sample_drop_ratio = drop_path
def forward(self, x: Tensor, xpos=None) -> Tensor: def attn_residual_func(x: Tensor) -> Tensor: return self.ls1(self.attn(self.norm1(x), xpos=xpos))
def ffn_residual_func(x: Tensor) -> Tensor: return self.ls2(self.mlp(self.norm2(x)))
if self.training and self.sample_drop_ratio > 0.1: # the overhead is compensated only for a drop path rate larger than 0.1 x = drop_add_residual_stochastic_depth( x, residual_func=attn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) x = drop_add_residual_stochastic_depth( x, residual_func=ffn_residual_func, sample_drop_ratio=self.sample_drop_ratio, ) elif self.training and self.sample_drop_ratio > 0.0: x = x + self.drop_path1(attn_residual_func(x)) x = x + self.drop_path1(ffn_residual_func(x)) # FIXME: drop_path2 else: x = x + attn_residual_func(x) x = x + ffn_residual_func(x) return x
class CrossBlockRope(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_ratio: float = 4.0, qkv_bias: bool = False, proj_bias: bool = True, ffn_bias: bool = True, act_layer: Callable[..., nn.Module] = nn.GELU, norm_layer: Callable[..., nn.Module] = nn.LayerNorm, attn_class: Callable[..., nn.Module] = Attention, cross_attn_class: Callable[..., nn.Module] = CrossAttentionRope, ffn_layer: Callable[..., nn.Module] = Mlp, init_values=None, qk_norm: bool=False, rope=None ) -> None: super().__init__() # print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}") self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.norm1 = norm_layer(dim) self.attn = attn_class( dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias, rope=rope, qk_norm=qk_norm )
self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.ls_y = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() self.norm2 = norm_layer(dim) self.norm_y = norm_layer(dim) self.cross_attn = cross_attn_class( dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias, rope=rope, qk_norm=qk_norm )
self.norm3 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = ffn_layer( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, bias=ffn_bias, )
def forward(self, x: Tensor, y: Tensor, xpos=None, ypos=None) -> Tensor: def attn_residual_func(x: Tensor) -> Tensor: return self.ls1(self.attn(self.norm1(x), xpos=xpos))
def cross_attn_residual_func(x: Tensor, y: Tensor) -> Tensor: return self.ls_y(self.cross_attn(self.norm2(x), y, y, qpos=xpos, kpos=ypos))
def ffn_residual_func(x: Tensor) -> Tensor: return self.ls2(self.mlp(self.norm3(x)))
x = x + attn_residual_func(x) y_ = self.norm_y(y) x = x + cross_attn_residual_func(x, y_) x = x + ffn_residual_func(x)
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import torchimport torch.nn as nnfrom copy import deepcopyimport torch.nn.functional as F
# code adapted from 'https://github.com/nianticlabs/marepo/blob/9a45e2bb07e5bb8cb997620088d352b439b13e0e/transformer/transformer.py#L172'class ResConvBlock(nn.Module): """ 1x1 convolution residual block """ def __init__(self, in_channels, out_channels): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.head_skip = nn.Identity() if self.in_channels == self.out_channels else nn.Conv2d(self.in_channels, self.out_channels, 1, 1, 0) # self.res_conv1 = nn.Conv2d(self.in_channels, self.out_channels, 1, 1, 0) # self.res_conv2 = nn.Conv2d(self.out_channels, self.out_channels, 1, 1, 0) # self.res_conv3 = nn.Conv2d(self.out_channels, self.out_channels, 1, 1, 0)
# change 1x1 convolution to linear self.res_conv1 = nn.Linear(self.in_channels, self.out_channels) self.res_conv2 = nn.Linear(self.out_channels, self.out_channels) self.res_conv3 = nn.Linear(self.out_channels, self.out_channels)
def forward(self, res): x = F.relu(self.res_conv1(res)) x = F.relu(self.res_conv2(x)) x = F.relu(self.res_conv3(x)) res = self.head_skip(res) + x return res
class CameraHead(nn.Module): def __init__(self, dim=512): super().__init__() output_dim = dim self.res_conv = nn.ModuleList([deepcopy(ResConvBlock(output_dim, output_dim)) for _ in range(2)]) self.avgpool = nn.AdaptiveAvgPool2d(1) self.more_mlps = nn.Sequential( nn.Linear(output_dim,output_dim), nn.ReLU(), nn.Linear(output_dim,output_dim), nn.ReLU() ) self.fc_t = nn.Linear(output_dim, 3) self.fc_rot = nn.Linear(output_dim, 9)
def forward(self, feat, patch_h, patch_w): BN, hw, c = feat.shape
for i in range(2): feat = self.res_conv[i](feat)
# feat = self.avgpool(feat) feat = self.avgpool(feat.permute(0, 2, 1).reshape(BN, -1, patch_h, patch_w).contiguous()) ########## feat = feat.view(feat.size(0), -1)
feat = self.more_mlps(feat) # [B, D_] with torch.amp.autocast(device_type='cuda', enabled=False): out_t = self.fc_t(feat.float()) # [B,3] out_r = self.fc_rot(feat.float()) # [B,9] pose = self.convert_pose_to_4x4(BN, out_r, out_t, feat.device)
return pose
def convert_pose_to_4x4(self, B, out_r, out_t, device): out_r = self.svd_orthogonalize(out_r) # [N,3,3] pose = torch.zeros((B, 4, 4), device=device) pose[:, :3, :3] = out_r pose[:, :3, 3] = out_t pose[:, 3, 3] = 1. return pose
def svd_orthogonalize(self, m): """Convert 9D representation to SO(3) using SVD orthogonalization.
Args: m: [BATCH, 3, 3] 3x3 matrices.
Returns: [BATCH, 3, 3] SO(3) rotation matrices. """ if m.dim() < 3: m = m.reshape((-1, 3, 3)) m_transpose = torch.transpose(torch.nn.functional.normalize(m, p=2, dim=-1), dim0=-1, dim1=-2) u, s, v = torch.svd(m_transpose) det = torch.det(torch.matmul(v, u.transpose(-2, -1))) # Check orientation reflection. r = torch.matmul( torch.cat([v[:, :, :-1], v[:, :, -1:] * det.view(-1, 1, 1)], dim=2), u.transpose(-2, -1) ) return r123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174# Copyright (C) 2022-present Naver Corporation. All rights reserved.# Licensed under CC BY-NC-SA 4.0 (non-commercial use only).
# --------------------------------------------------------# Position embedding utils# --------------------------------------------------------
import numpy as np
import torch
# --------------------------------------------------------# 2D sine-cosine position embedding# References:# MAE: https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py# Transformer: https://github.com/tensorflow/models/blob/master/official/nlp/transformer/model_utils.py# MoCo v3: https://github.com/facebookresearch/moco-v3# --------------------------------------------------------def get_2d_sincos_pos_embed(embed_dim, grid_size, n_cls_token=0): """ grid_size: int of the grid height and width return: pos_embed: [grid_size*grid_size, embed_dim] or [n_cls_token+grid_size*grid_size, embed_dim] (w/ or w/o cls_token) """ grid_h = np.arange(grid_size, dtype=np.float32) grid_w = np.arange(grid_size, dtype=np.float32) grid = np.meshgrid(grid_w, grid_h) # here w goes first grid = np.stack(grid, axis=0)
grid = grid.reshape([2, 1, grid_size, grid_size]) pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid) if n_cls_token>0: pos_embed = np.concatenate([np.zeros([n_cls_token, embed_dim]), pos_embed], axis=0) return pos_embed
def get_2d_sincos_pos_embed_from_grid(embed_dim, grid): assert embed_dim % 2 == 0
# use half of dimensions to encode grid_h emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) # (H*W, D/2) emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1]) # (H*W, D/2)
emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D) return emb
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): """ embed_dim: output dimension for each position pos: a list of positions to be encoded: size (M,) out: (M, D) """ assert embed_dim % 2 == 0 omega = np.arange(embed_dim // 2, dtype=float) omega /= embed_dim / 2. omega = 1. / 10000**omega # (D/2,)
pos = pos.reshape(-1) # (M,) out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
emb_sin = np.sin(out) # (M, D/2) emb_cos = np.cos(out) # (M, D/2)
emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D) return emb
# --------------------------------------------------------# Interpolate position embeddings for high-resolution# References:# MAE: https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py# DeiT: https://github.com/facebookresearch/deit# --------------------------------------------------------def interpolate_pos_embed(model, checkpoint_model): if 'pos_embed' in checkpoint_model: pos_embed_checkpoint = checkpoint_model['pos_embed'] embedding_size = pos_embed_checkpoint.shape[-1] num_patches = model.patch_embed.num_patches num_extra_tokens = model.pos_embed.shape[-2] - num_patches # height (== width) for the checkpoint position embedding orig_size = int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5) # height (== width) for the new position embedding new_size = int(num_patches ** 0.5) # class_token and dist_token are kept unchanged if orig_size != new_size: print("Position interpolate from %dx%d to %dx%d" % (orig_size, orig_size, new_size, new_size)) extra_tokens = pos_embed_checkpoint[:, :num_extra_tokens] # only the position tokens are interpolated pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:] pos_tokens = pos_tokens.reshape(-1, orig_size, orig_size, embedding_size).permute(0, 3, 1, 2) pos_tokens = torch.nn.functional.interpolate( pos_tokens, size=(new_size, new_size), mode='bicubic', align_corners=False) pos_tokens = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2) new_pos_embed = torch.cat((extra_tokens, pos_tokens), dim=1) checkpoint_model['pos_embed'] = new_pos_embed
#----------------------------------------------------------# RoPE2D: RoPE implementation in 2D#----------------------------------------------------------
try: from models.curope import cuRoPE2D RoPE2D = cuRoPE2Dexcept ImportError: print('Warning, cannot find cuda-compiled version of RoPE2D, using a slow pytorch version instead')
class RoPE2D(torch.nn.Module): def __init__(self, freq=100.0, F0=1.0): super().__init__() self.base = freq self.F0 = F0 self.cache = {}
def get_cos_sin(self, D, seq_len, device, dtype): if (D,seq_len,device,dtype) not in self.cache: inv_freq = 1.0 / (self.base ** (torch.arange(0, D, 2).float().to(device) / D)) t = torch.arange(seq_len, device=device, dtype=inv_freq.dtype) freqs = torch.einsum("i,j->ij", t, inv_freq).to(dtype) freqs = torch.cat((freqs, freqs), dim=-1) cos = freqs.cos() # (Seq, Dim) sin = freqs.sin() self.cache[D,seq_len,device,dtype] = (cos,sin) return self.cache[D,seq_len,device,dtype] @staticmethod def rotate_half(x): x1, x2 = x[..., : x.shape[-1] // 2], x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1) def apply_rope1d(self, tokens, pos1d, cos, sin): assert pos1d.ndim==2 cos = torch.nn.functional.embedding(pos1d, cos)[:, None, :, :] sin = torch.nn.functional.embedding(pos1d, sin)[:, None, :, :] return (tokens * cos) + (self.rotate_half(tokens) * sin) def forward(self, tokens, positions): """ input: * tokens: batch_size x nheads x ntokens x dim * positions: batch_size x ntokens x 2 (y and x position of each token) output: * tokens after appplying RoPE2D (batch_size x nheads x ntokens x dim) """ assert tokens.size(3)%2==0, "number of dimensions should be a multiple of two" D = tokens.size(3) // 2 assert positions.ndim==3 and positions.shape[-1] == 2 # Batch, Seq, 2 cos, sin = self.get_cos_sin(D, int(positions.max())+1, tokens.device, tokens.dtype) # split features into two along the feature dimension, and apply rope1d on each half y, x = tokens.chunk(2, dim=-1) y = self.apply_rope1d(y, positions[:,:,0], cos, sin) x = self.apply_rope1d(x, positions[:,:,1], cos, sin) tokens = torch.cat((y, x), dim=-1) return tokens # patch embeddingclass PositionGetter(object): """ return positions of patches """
def __init__(self): self.cache_positions = {} def __call__(self, b, h, w, device): if not (h,w) in self.cache_positions: x = torch.arange(w, device=device) y = torch.arange(h, device=device) self.cache_positions[h,w] = torch.cartesian_prod(y, x) # (h, w, 2) pos = self.cache_positions[h,w].view(1, h*w, 2).expand(b, -1, 2).clone() return pos123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081from .attention import FlashAttentionRopefrom .block import BlockRopefrom ..dinov2.layers import Mlpimport torch.nn as nnfrom functools import partialfrom torch.utils.checkpoint import checkpointimport torch.nn.functional as F class TransformerDecoder(nn.Module): def __init__( self, in_dim, out_dim, dec_embed_dim=512, depth=5, dec_num_heads=8, mlp_ratio=4, rope=None, need_project=True, use_checkpoint=False, ): super().__init__()
self.projects = nn.Linear(in_dim, dec_embed_dim) if need_project else nn.Identity() self.use_checkpoint = use_checkpoint
self.blocks = nn.ModuleList([ BlockRope( dim=dec_embed_dim, num_heads=dec_num_heads, mlp_ratio=mlp_ratio, qkv_bias=True, proj_bias=True, ffn_bias=True, drop_path=0.0, norm_layer=partial(nn.LayerNorm, eps=1e-6), act_layer=nn.GELU, ffn_layer=Mlp, init_values=None, qk_norm=False, # attn_class=MemEffAttentionRope, attn_class=FlashAttentionRope, rope=rope ) for _ in range(depth)])
self.linear_out = nn.Linear(dec_embed_dim, out_dim)
def forward(self, hidden, xpos=None): hidden = self.projects(hidden) for i, blk in enumerate(self.blocks): if self.use_checkpoint and self.training: hidden = checkpoint(blk, hidden, xpos=xpos, use_reentrant=False) else: hidden = blk(hidden, xpos=xpos) out = self.linear_out(hidden) return out
class LinearPts3d (nn.Module): """ Linear head for dust3r Each token outputs: - 16x16 3D points (+ confidence) """
def __init__(self, patch_size, dec_embed_dim, output_dim=3,): super().__init__() self.patch_size = patch_size
self.proj = nn.Linear(dec_embed_dim, (output_dim)*self.patch_size**2)
def forward(self, decout, img_shape): H, W = img_shape tokens = decout[-1] B, S, D = tokens.shape
# extract 3D points feat = self.proj(tokens) # B,S,D feat = feat.transpose(-1, -2).view(B, -1, H//self.patch_size, W//self.patch_size) feat = F.pixel_shuffle(feat, self.patch_size) # B,3,H,W
# permute + norm depth return feat.permute(0, 2, 3, 1)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216import torchimport torch.nn as nnfrom functools import partialfrom copy import deepcopy
from .dinov2.layers import Mlpfrom ..utils.geometry import homogenize_pointsfrom .layers.pos_embed import RoPE2D, PositionGetterfrom .layers.block import BlockRopefrom .layers.attention import FlashAttentionRopefrom .layers.transformer_head import TransformerDecoder, LinearPts3dfrom .layers.camera_head import CameraHeadfrom .dinov2.hub.backbones import dinov2_vitl14, dinov2_vitl14_regfrom huggingface_hub import PyTorchModelHubMixin
class Pi3(nn.Module, PyTorchModelHubMixin): def __init__( self, pos_type='rope100', decoder_size='large', ): super().__init__()
# ---------------------- # Encoder # ---------------------- self.encoder = dinov2_vitl14_reg(pretrained=False) self.patch_size = 14 del self.encoder.mask_token
# ---------------------- # Positonal Encoding # ---------------------- self.pos_type = pos_type if pos_type is not None else 'none' self.rope=None if self.pos_type.startswith('rope'): # eg rope100 if RoPE2D is None: raise ImportError("Cannot find cuRoPE2D, please install it following the README instructions") freq = float(self.pos_type[len('rope'):]) self.rope = RoPE2D(freq=freq) self.position_getter = PositionGetter() else: raise NotImplementedError
# ---------------------- # Decoder # ---------------------- enc_embed_dim = self.encoder.blocks[0].attn.qkv.in_features # 1024 if decoder_size == 'small': dec_embed_dim = 384 dec_num_heads = 6 mlp_ratio = 4 dec_depth = 24 elif decoder_size == 'base': dec_embed_dim = 768 dec_num_heads = 12 mlp_ratio = 4 dec_depth = 24 elif decoder_size == 'large': dec_embed_dim = 1024 dec_num_heads = 16 mlp_ratio = 4 dec_depth = 36 else: raise NotImplementedError self.decoder = nn.ModuleList([ BlockRope( dim=dec_embed_dim, num_heads=dec_num_heads, mlp_ratio=mlp_ratio, qkv_bias=True, proj_bias=True, ffn_bias=True, drop_path=0.0, norm_layer=partial(nn.LayerNorm, eps=1e-6), act_layer=nn.GELU, ffn_layer=Mlp, init_values=0.01, qk_norm=True, attn_class=FlashAttentionRope, rope=self.rope ) for _ in range(dec_depth)]) self.dec_embed_dim = dec_embed_dim
# ---------------------- # Register_token # ---------------------- num_register_tokens = 5 self.patch_start_idx = num_register_tokens self.register_token = nn.Parameter(torch.randn(1, 1, num_register_tokens, self.dec_embed_dim)) nn.init.normal_(self.register_token, std=1e-6)
# ---------------------- # Local Points Decoder # ---------------------- self.point_decoder = TransformerDecoder( in_dim=2*self.dec_embed_dim, dec_embed_dim=1024, dec_num_heads=16, out_dim=1024, rope=self.rope, ) self.point_head = LinearPts3d(patch_size=14, dec_embed_dim=1024, output_dim=3)
# ---------------------- # Conf Decoder # ---------------------- self.conf_decoder = deepcopy(self.point_decoder) self.conf_head = LinearPts3d(patch_size=14, dec_embed_dim=1024, output_dim=1)
# ---------------------- # Camera Pose Decoder # ---------------------- self.camera_decoder = TransformerDecoder( in_dim=2*self.dec_embed_dim, dec_embed_dim=1024, dec_num_heads=16, # 8 out_dim=512, rope=self.rope, use_checkpoint=False ) self.camera_head = CameraHead(dim=512)
# For ImageNet Normalize image_mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1) image_std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
self.register_buffer("image_mean", image_mean) self.register_buffer("image_std", image_std)
def decode(self, hidden, N, H, W): BN, hw, _ = hidden.shape B = BN // N
final_output = [] hidden = hidden.reshape(B*N, hw, -1)
register_token = self.register_token.repeat(B, N, 1, 1).reshape(B*N, *self.register_token.shape[-2:])
# Concatenate special tokens with patch tokens hidden = torch.cat([register_token, hidden], dim=1) hw = hidden.shape[1]
if self.pos_type.startswith('rope'): pos = self.position_getter(B * N, H//self.patch_size, W//self.patch_size, hidden.device)
if self.patch_start_idx > 0: # do not use position embedding for special tokens (camera and register tokens) # so set pos to 0 for the special tokens pos = pos + 1 pos_special = torch.zeros(B * N, self.patch_start_idx, 2).to(hidden.device).to(pos.dtype) pos = torch.cat([pos_special, pos], dim=1) for i in range(len(self.decoder)): blk = self.decoder[i]
if i % 2 == 0: pos = pos.reshape(B*N, hw, -1) hidden = hidden.reshape(B*N, hw, -1) else: pos = pos.reshape(B, N*hw, -1) hidden = hidden.reshape(B, N*hw, -1)
hidden = blk(hidden, xpos=pos)
if i+1 in [len(self.decoder)-1, len(self.decoder)]: final_output.append(hidden.reshape(B*N, hw, -1))
return torch.cat([final_output[0], final_output[1]], dim=-1), pos.reshape(B*N, hw, -1) def forward(self, imgs): imgs = (imgs - self.image_mean) / self.image_std
B, N, _, H, W = imgs.shape patch_h, patch_w = H // 14, W // 14 # encode by dinov2 imgs = imgs.reshape(B*N, _, H, W) hidden = self.encoder(imgs, is_training=True)
if isinstance(hidden, dict): hidden = hidden["x_norm_patchtokens"]
hidden, pos = self.decode(hidden, N, H, W)
point_hidden = self.point_decoder(hidden, xpos=pos) conf_hidden = self.conf_decoder(hidden, xpos=pos) camera_hidden = self.camera_decoder(hidden, xpos=pos)
with torch.amp.autocast(device_type='cuda', enabled=False): # local points point_hidden = point_hidden.float() ret = self.point_head([point_hidden[:, self.patch_start_idx:]], (H, W)).reshape(B, N, H, W, -1) xy, z = ret.split([2, 1], dim=-1) z = torch.exp(z) local_points = torch.cat([xy * z, z], dim=-1)
# confidence conf_hidden = conf_hidden.float() conf = self.conf_head([conf_hidden[:, self.patch_start_idx:]], (H, W)).reshape(B, N, H, W, -1)
# camera camera_hidden = camera_hidden.float() camera_poses = self.camera_head(camera_hidden[:, self.patch_start_idx:], patch_h, patch_w).reshape(B, N, 4, 4)
# unproject local points using camera poses points = torch.einsum('bnij, bnhwj -> bnhwi', camera_poses, homogenize_points(local_points))[..., :3]
return dict( points=points, local_points=local_points, conf=conf, camera_poses=camera_poses, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223import osimport os.path as ospimport mathimport cv2from PIL import Imageimport torchfrom torchvision import transformsfrom plyfile import PlyData, PlyElementimport numpy as np
def load_images_as_tensor(path='data/truck', interval=1, PIXEL_LIMIT=255000): """ Loads images from a directory or video, resizes them to a uniform size, then converts and stacks them into a single [N, 3, H, W] PyTorch tensor. """ sources = [] # --- 1. Load image paths or video frames --- if osp.isdir(path): print(f"Loading images from directory: {path}") filenames = sorted([x for x in os.listdir(path) if x.lower().endswith(('.png', '.jpg', '.jpeg'))]) for i in range(0, len(filenames), interval): img_path = osp.join(path, filenames[i]) try: sources.append(Image.open(img_path).convert('RGB')) except Exception as e: print(f"Could not load image {filenames[i]}: {e}") elif path.lower().endswith('.mp4'): print(f"Loading frames from video: {path}") cap = cv2.VideoCapture(path) if not cap.isOpened(): raise IOError(f"Cannot open video file: {path}") frame_idx = 0 while True: ret, frame = cap.read() if not ret: break if frame_idx % interval == 0: rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) sources.append(Image.fromarray(rgb_frame)) frame_idx += 1 cap.release() else: raise ValueError(f"Unsupported path. Must be a directory or a .mp4 file: {path}")
if not sources: print("No images found or loaded.") return torch.empty(0)
print(f"Found {len(sources)} images/frames. Processing...")
# --- 2. Determine a uniform target size for all images based on the first image --- # This is necessary to ensure all tensors have the same dimensions for stacking. first_img = sources[0] W_orig, H_orig = first_img.size scale = math.sqrt(PIXEL_LIMIT / (W_orig * H_orig)) if W_orig * H_orig > 0 else 1 W_target, H_target = W_orig * scale, H_orig * scale k, m = round(W_target / 14), round(H_target / 14) while (k * 14) * (m * 14) > PIXEL_LIMIT: if k / m > W_target / H_target: k -= 1 else: m -= 1 TARGET_W, TARGET_H = max(1, k) * 14, max(1, m) * 14 print(f"All images will be resized to a uniform size: ({TARGET_W}, {TARGET_H})")
# --- 3. Resize images and convert them to tensors in the [0, 1] range --- tensor_list = [] # Define a transform to convert a PIL Image to a CxHxW tensor and normalize to [0,1] to_tensor_transform = transforms.ToTensor() for img_pil in sources: try: # Resize to the uniform target size resized_img = img_pil.resize((TARGET_W, TARGET_H), Image.Resampling.LANCZOS) # Convert to tensor img_tensor = to_tensor_transform(resized_img) tensor_list.append(img_tensor) except Exception as e: print(f"Error processing an image: {e}")
if not tensor_list: print("No images were successfully processed.") return torch.empty(0)
# --- 4. Stack the list of tensors into a single [N, C, H, W] batch tensor --- return torch.stack(tensor_list, dim=0)
def tensor_to_pil(tensor): """ Converts a PyTorch tensor to a PIL image. Automatically moves the channel dimension (if it has size 3) to the last axis before converting.
Args: tensor (torch.Tensor): Input tensor. Expected shape can be [C, H, W], [H, W, C], or [H, W]. Returns: PIL.Image: The converted PIL image. """ if torch.is_tensor(tensor): array = tensor.detach().cpu().numpy() else: array = tensor
return array_to_pil(array)
def array_to_pil(array): """ Converts a NumPy array to a PIL image. Automatically: - Squeezes dimensions of size 1. - Moves the channel dimension (if it has size 3) to the last axis. Args: array (np.ndarray): Input array. Expected shape can be [C, H, W], [H, W, C], or [H, W]. Returns: PIL.Image: The converted PIL image. """ # Remove singleton dimensions array = np.squeeze(array) # Ensure the array has the channel dimension as the last axis if array.ndim == 3 and array.shape[0] == 3: # If the channel is the first axis array = np.transpose(array, (1, 2, 0)) # Move channel to the last axis # Handle single-channel grayscale images if array.ndim == 2: # [H, W] return Image.fromarray((array * 255).astype(np.uint8), mode="L") elif array.ndim == 3 and array.shape[2] == 3: # [H, W, C] with 3 channels return Image.fromarray((array * 255).astype(np.uint8), mode="RGB") else: raise ValueError(f"Unsupported array shape for PIL conversion: {array.shape}")
def rotate_target_dim_to_last_axis(x, target_dim=3): shape = x.shape axis_to_move = -1 # Iterate backwards to find the first occurrence from the end # (which corresponds to the last dimension of size 3 in the original order). for i in range(len(shape) - 1, -1, -1): if shape[i] == target_dim: axis_to_move = i break
# 2. If the axis is found and it's not already in the last position, move it. if axis_to_move != -1 and axis_to_move != len(shape) - 1: # Create the new dimension order. dims_order = list(range(len(shape))) dims_order.pop(axis_to_move) dims_order.append(axis_to_move) # Use permute to reorder the dimensions. ret = x.transpose(*dims_order) else: ret = x
return ret
def write_ply( xyz, rgb=None, path='output.ply',) -> None: if torch.is_tensor(xyz): xyz = xyz.detach().cpu().numpy()
if torch.is_tensor(rgb): rgb = rgb.detach().cpu().numpy()
if rgb is not None and rgb.max() > 1: rgb = rgb / 255.
xyz = rotate_target_dim_to_last_axis(xyz, 3) xyz = xyz.reshape(-1, 3)
if rgb is not None: rgb = rotate_target_dim_to_last_axis(rgb, 3) rgb = rgb.reshape(-1, 3) if rgb is None: min_coord = np.min(xyz, axis=0) max_coord = np.max(xyz, axis=0) normalized_coord = (xyz - min_coord) / (max_coord - min_coord + 1e-8) hue = 0.7 * normalized_coord[:,0] + 0.2 * normalized_coord[:,1] + 0.1 * normalized_coord[:,2] hsv = np.stack([hue, 0.9*np.ones_like(hue), 0.8*np.ones_like(hue)], axis=1)
c = hsv[:,2:] * hsv[:,1:2] x = c * (1 - np.abs( (hsv[:,0:1]*6) % 2 - 1 )) m = hsv[:,2:] - c rgb = np.zeros_like(hsv) cond = (0 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 1) rgb[cond] = np.hstack([c[cond], x[cond], np.zeros_like(x[cond])]) cond = (1 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 2) rgb[cond] = np.hstack([x[cond], c[cond], np.zeros_like(x[cond])]) cond = (2 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 3) rgb[cond] = np.hstack([np.zeros_like(x[cond]), c[cond], x[cond]]) cond = (3 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 4) rgb[cond] = np.hstack([np.zeros_like(x[cond]), x[cond], c[cond]]) cond = (4 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 5) rgb[cond] = np.hstack([x[cond], np.zeros_like(x[cond]), c[cond]]) cond = (5 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 6) rgb[cond] = np.hstack([c[cond], np.zeros_like(x[cond]), x[cond]]) rgb = (rgb + m)
dtype = [ ("x", "f4"), ("y", "f4"), ("z", "f4"), ("nx", "f4"), ("ny", "f4"), ("nz", "f4"), ("red", "u1"), ("green", "u1"), ("blue", "u1"), ] normals = np.zeros_like(xyz) elements = np.empty(xyz.shape[0], dtype=dtype) attributes = np.concatenate((xyz, normals, rgb * 255), axis=1) elements[:] = list(map(tuple, attributes)) vertex_element = PlyElement.describe(elements, "vertex") ply_data = PlyData([vertex_element]) ply_data.write(path)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import osimport jsonimport debugpyimport socketimport random
def update_vscode_launch_file(host: str, port: int): """Update the .vscode/launch.json file with the new host and port.""" launch_file_path = ".vscode/launch.json" # Desired configuration new_config = { "version": "0.2.0", "configurations": [ { "name": "bash_debug", "type": "debugpy", "request": "attach", "connect": { "host": host, "port": port }, "justMyCode": False }, ] }
# Ensure the .vscode directory exists if not os.path.exists(".vscode"): os.makedirs(".vscode")
# Write the updated configuration to launch.json with open(launch_file_path, "w") as f: json.dump(new_config, f, indent=4) print(f"Updated {launch_file_path} with host: {host} and port: {port}")
def is_port_in_use(host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex((host, port)) == 0
def setup_debug(is_main_process=True, max_retries=10, port_range=(10000, 20000)): if is_main_process: host = os.environ['SLURM_NODELIST'].split(',')[0]
for _ in range(max_retries): port = random.randint(*port_range) try: if is_port_in_use(host, port): print(f"Port {port} is already in use, trying another...") continue
# 更新 launch.json update_vscode_launch_file(host, port)
print("master_addr = ", host) debugpy.listen((host, port)) print(f"Waiting for debugger attach at port {port}...", flush=True) debugpy.wait_for_client() print("Debugger attached", flush=True) return except Exception as e: print(f"Failed to bind to port {port}: {e}")
raise RuntimeError("Could not find a free port for debugpy after several attempts.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375import numpy as npimport torchimport torch.nn.functional as F
def se3_inverse(T): """ Computes the inverse of a batch of SE(3) matrices. T: Tensor of shape (B, 4, 4) """ if len(T.shape) == 2: T = T[None] unseq_flag = True else: unseq_flag = False
if torch.is_tensor(T): R = T[:, :3, :3] t = T[:, :3, 3].unsqueeze(-1) R_inv = R.transpose(-2, -1) t_inv = -torch.matmul(R_inv, t) T_inv = torch.cat([ torch.cat([R_inv, t_inv], dim=-1), torch.tensor([0, 0, 0, 1], device=T.device, dtype=T.dtype).repeat(T.shape[0], 1, 1) ], dim=1) else: R = T[:, :3, :3] t = T[:, :3, 3, np.newaxis]
R_inv = np.swapaxes(R, -2, -1) t_inv = -R_inv @ t
bottom_row = np.zeros((T.shape[0], 1, 4), dtype=T.dtype) bottom_row[:, :, 3] = 1
top_part = np.concatenate([R_inv, t_inv], axis=-1) T_inv = np.concatenate([top_part, bottom_row], axis=1)
if unseq_flag: T_inv = T_inv[0] return T_inv
def get_pixel(H, W): # get 2D pixels (u, v) for image_a in cam_a pixel space u_a, v_a = np.meshgrid(np.arange(W), np.arange(H)) # u_a = np.flip(u_a, axis=1) # v_a = np.flip(v_a, axis=0) pixels_a = np.stack([ u_a.flatten() + 0.5, v_a.flatten() + 0.5, np.ones_like(u_a.flatten()) ], axis=0) return pixels_a
def depthmap_to_absolute_camera_coordinates(depthmap, camera_intrinsics, camera_pose, z_far=0, **kw): """ Args: - depthmap (HxW array): - camera_intrinsics: a 3x3 matrix - camera_pose: a 4x3 or 4x4 cam2world matrix Returns: pointmap of absolute coordinates (HxWx3 array), and a mask specifying valid pixels.""" X_cam, valid_mask = depthmap_to_camera_coordinates(depthmap, camera_intrinsics) if z_far > 0: valid_mask = valid_mask & (depthmap < z_far)
X_world = X_cam # default if camera_pose is not None: # R_cam2world = np.float32(camera_params["R_cam2world"]) # t_cam2world = np.float32(camera_params["t_cam2world"]).squeeze() R_cam2world = camera_pose[:3, :3] t_cam2world = camera_pose[:3, 3]
# Express in absolute coordinates (invalid depth values) X_world = np.einsum("ik, vuk -> vui", R_cam2world, X_cam) + t_cam2world[None, None, :]
return X_world, valid_mask
def depthmap_to_camera_coordinates(depthmap, camera_intrinsics, pseudo_focal=None): """ Args: - depthmap (HxW array): - camera_intrinsics: a 3x3 matrix Returns: pointmap of absolute coordinates (HxWx3 array), and a mask specifying valid pixels. """ camera_intrinsics = np.float32(camera_intrinsics) H, W = depthmap.shape
# Compute 3D ray associated with each pixel # Strong assumption: there are no skew terms # assert camera_intrinsics[0, 1] == 0.0 # assert camera_intrinsics[1, 0] == 0.0 if pseudo_focal is None: fu = camera_intrinsics[0, 0] fv = camera_intrinsics[1, 1] else: assert pseudo_focal.shape == (H, W) fu = fv = pseudo_focal cu = camera_intrinsics[0, 2] cv = camera_intrinsics[1, 2]
u, v = np.meshgrid(np.arange(W), np.arange(H)) z_cam = depthmap x_cam = (u - cu) * z_cam / fu y_cam = (v - cv) * z_cam / fv X_cam = np.stack((x_cam, y_cam, z_cam), axis=-1).astype(np.float32)
# Mask for valid coordinates valid_mask = (depthmap > 0.0) # Invalid any depth > 80m valid_mask = valid_mask return X_cam, valid_mask
def homogenize_points( points,): """Convert batched points (xyz) to (xyz1).""" return torch.cat([points, torch.ones_like(points[..., :1])], dim=-1)
def get_gt_warp(depth1, depth2, T_1to2, K1, K2, depth_interpolation_mode = 'bilinear', relative_depth_error_threshold = 0.05, H = None, W = None): if H is None: B,H,W = depth1.shape else: B = depth1.shape[0] with torch.no_grad(): x1_n = torch.meshgrid( *[ torch.linspace( -1 + 1 / n, 1 - 1 / n, n, device=depth1.device ) for n in (B, H, W) ], indexing = 'ij' ) x1_n = torch.stack((x1_n[2], x1_n[1]), dim=-1).reshape(B, H * W, 2) mask, x2 = warp_kpts( x1_n.double(), depth1.double(), depth2.double(), T_1to2.double(), K1.double(), K2.double(), depth_interpolation_mode = depth_interpolation_mode, relative_depth_error_threshold = relative_depth_error_threshold, ) prob = mask.float().reshape(B, H, W) x2 = x2.reshape(B, H, W, 2) return x2, prob
@torch.no_grad()def warp_kpts(kpts0, depth0, depth1, T_0to1, K0, K1, smooth_mask = False, return_relative_depth_error = False, depth_interpolation_mode = "bilinear", relative_depth_error_threshold = 0.05): """Warp kpts0 from I0 to I1 with depth, K and Rt Also check covisibility and depth consistency. Depth is consistent if relative error < 0.2 (hard-coded). # https://github.com/zju3dv/LoFTR/blob/94e98b695be18acb43d5d3250f52226a8e36f839/src/loftr/utils/geometry.py adapted from here Args: kpts0 (torch.Tensor): [N, L, 2] - <x, y>, should be normalized in (-1,1) depth0 (torch.Tensor): [N, H, W], depth1 (torch.Tensor): [N, H, W], T_0to1 (torch.Tensor): [N, 3, 4], K0 (torch.Tensor): [N, 3, 3], K1 (torch.Tensor): [N, 3, 3], Returns: calculable_mask (torch.Tensor): [N, L] warped_keypoints0 (torch.Tensor): [N, L, 2] <x0_hat, y1_hat> """ ( n, h, w, ) = depth0.shape if depth_interpolation_mode == "combined": # Inspired by approach in inloc, try to fill holes from bilinear interpolation by nearest neighbour interpolation if smooth_mask: raise NotImplementedError("Combined bilinear and NN warp not implemented") valid_bilinear, warp_bilinear = warp_kpts(kpts0, depth0, depth1, T_0to1, K0, K1, smooth_mask = smooth_mask, return_relative_depth_error = return_relative_depth_error, depth_interpolation_mode = "bilinear", relative_depth_error_threshold = relative_depth_error_threshold) valid_nearest, warp_nearest = warp_kpts(kpts0, depth0, depth1, T_0to1, K0, K1, smooth_mask = smooth_mask, return_relative_depth_error = return_relative_depth_error, depth_interpolation_mode = "nearest-exact", relative_depth_error_threshold = relative_depth_error_threshold) nearest_valid_bilinear_invalid = (~valid_bilinear).logical_and(valid_nearest) warp = warp_bilinear.clone() warp[nearest_valid_bilinear_invalid] = warp_nearest[nearest_valid_bilinear_invalid] valid = valid_bilinear | valid_nearest return valid, warp kpts0_depth = F.grid_sample(depth0[:, None], kpts0[:, :, None], mode = depth_interpolation_mode, align_corners=False)[ :, 0, :, 0 ] kpts0 = torch.stack( (w * (kpts0[..., 0] + 1) / 2, h * (kpts0[..., 1] + 1) / 2), dim=-1 ) # [-1+1/h, 1-1/h] -> [0.5, h-0.5] # Sample depth, get calculable_mask on depth != 0 # nonzero_mask = kpts0_depth != 0 # Sample depth, get calculable_mask on depth > 0 nonzero_mask = kpts0_depth > 0
# Unproject kpts0_h = ( torch.cat([kpts0, torch.ones_like(kpts0[:, :, [0]])], dim=-1) * kpts0_depth[..., None] ) # (N, L, 3) kpts0_n = K0.inverse() @ kpts0_h.transpose(2, 1) # (N, 3, L) kpts0_cam = kpts0_n
# Rigid Transform w_kpts0_cam = T_0to1[:, :3, :3] @ kpts0_cam + T_0to1[:, :3, [3]] # (N, 3, L) w_kpts0_depth_computed = w_kpts0_cam[:, 2, :]
# Project w_kpts0_h = (K1 @ w_kpts0_cam).transpose(2, 1) # (N, L, 3) w_kpts0 = w_kpts0_h[:, :, :2] / ( w_kpts0_h[:, :, [2]] + 1e-4 ) # (N, L, 2), +1e-4 to avoid zero depth
# Covisible Check h, w = depth1.shape[1:3] covisible_mask = ( (w_kpts0[:, :, 0] > 0) * (w_kpts0[:, :, 0] < w - 1) * (w_kpts0[:, :, 1] > 0) * (w_kpts0[:, :, 1] < h - 1) ) w_kpts0 = torch.stack( (2 * w_kpts0[..., 0] / w - 1, 2 * w_kpts0[..., 1] / h - 1), dim=-1 ) # from [0.5,h-0.5] -> [-1+1/h, 1-1/h] # w_kpts0[~covisible_mask, :] = -5 # xd
w_kpts0_depth = F.grid_sample( depth1[:, None], w_kpts0[:, :, None], mode=depth_interpolation_mode, align_corners=False )[:, 0, :, 0] relative_depth_error = ( (w_kpts0_depth - w_kpts0_depth_computed) / w_kpts0_depth ).abs() if not smooth_mask: consistent_mask = relative_depth_error < relative_depth_error_threshold else: consistent_mask = (-relative_depth_error/smooth_mask).exp() valid_mask = nonzero_mask * covisible_mask * consistent_mask if return_relative_depth_error: return relative_depth_error, w_kpts0 else: return valid_mask, w_kpts0
def geotrf(Trf, pts, ncol=None, norm=False): """ Apply a geometric transformation to a list of 3-D points.
H: 3x3 or 4x4 projection matrix (typically a Homography) p: numpy/torch/tuple of coordinates. Shape must be (...,2) or (...,3)
ncol: int. number of columns of the result (2 or 3) norm: float. if != 0, the resut is projected on the z=norm plane.
Returns an array of projected 2d points. """ assert Trf.ndim >= 2 if isinstance(Trf, np.ndarray): pts = np.asarray(pts) elif isinstance(Trf, torch.Tensor): pts = torch.as_tensor(pts, dtype=Trf.dtype)
# adapt shape if necessary output_reshape = pts.shape[:-1] ncol = ncol or pts.shape[-1]
# optimized code if (isinstance(Trf, torch.Tensor) and isinstance(pts, torch.Tensor) and Trf.ndim == 3 and pts.ndim == 4): d = pts.shape[3] if Trf.shape[-1] == d: pts = torch.einsum("bij, bhwj -> bhwi", Trf, pts) elif Trf.shape[-1] == d + 1: pts = torch.einsum("bij, bhwj -> bhwi", Trf[:, :d, :d], pts) + Trf[:, None, None, :d, d] else: raise ValueError(f'bad shape, not ending with 3 or 4, for {pts.shape=}') else: if Trf.ndim >= 3: n = Trf.ndim - 2 assert Trf.shape[:n] == pts.shape[:n], 'batch size does not match' Trf = Trf.reshape(-1, Trf.shape[-2], Trf.shape[-1])
if pts.ndim > Trf.ndim: # Trf == (B,d,d) & pts == (B,H,W,d) --> (B, H*W, d) pts = pts.reshape(Trf.shape[0], -1, pts.shape[-1]) elif pts.ndim == 2: # Trf == (B,d,d) & pts == (B,d) --> (B, 1, d) pts = pts[:, None, :]
if pts.shape[-1] + 1 == Trf.shape[-1]: Trf = Trf.swapaxes(-1, -2) # transpose Trf pts = pts @ Trf[..., :-1, :] + Trf[..., -1:, :] elif pts.shape[-1] == Trf.shape[-1]: Trf = Trf.swapaxes(-1, -2) # transpose Trf pts = pts @ Trf else: pts = Trf @ pts.T if pts.ndim >= 2: pts = pts.swapaxes(-1, -2)
if norm: pts = pts / pts[..., -1:] # DONT DO /= BECAUSE OF WEIRD PYTORCH BUG if norm != 1: pts *= norm
res = pts[..., :ncol].reshape(*output_reshape, ncol) return res
def inv(mat): """ Invert a torch or numpy matrix """ if isinstance(mat, torch.Tensor): return torch.linalg.inv(mat) if isinstance(mat, np.ndarray): return np.linalg.inv(mat) raise ValueError(f'bad matrix type = {type(mat)}')
def opencv_camera_to_plucker(poses, K, H, W): device = poses.device B = poses.shape[0]
pixel = torch.from_numpy(get_pixel(H, W).astype(np.float32)).to(device).T.reshape(H, W, 3)[None].repeat(B, 1, 1, 1) # (3, H, W) pixel = torch.einsum('bij, bhwj -> bhwi', torch.inverse(K), pixel) ray_directions = torch.einsum('bij, bhwj -> bhwi', poses[..., :3, :3], pixel)
ray_origins = poses[..., :3, 3][:, None, None].repeat(1, H, W, 1)
ray_directions = ray_directions / ray_directions.norm(dim=-1, keepdim=True) plucker_normal = torch.cross(ray_origins, ray_directions, dim=-1) plucker_ray = torch.cat([ray_directions, plucker_normal], dim=-1)
return plucker_ray
def depth_edge(depth: torch.Tensor, atol: float = None, rtol: float = None, kernel_size: int = 3, mask: torch.Tensor = None) -> torch.BoolTensor: """ Compute the edge mask of a depth map. The edge is defined as the pixels whose neighbors have a large difference in depth. Args: depth (torch.Tensor): shape (..., height, width), linear depth map atol (float): absolute tolerance rtol (float): relative tolerance
Returns: edge (torch.Tensor): shape (..., height, width) of dtype torch.bool """ shape = depth.shape depth = depth.reshape(-1, 1, *shape[-2:]) if mask is not None: mask = mask.reshape(-1, 1, *shape[-2:])
if mask is None: diff = (F.max_pool2d(depth, kernel_size, stride=1, padding=kernel_size // 2) + F.max_pool2d(-depth, kernel_size, stride=1, padding=kernel_size // 2)) else: diff = (F.max_pool2d(torch.where(mask, depth, -torch.inf), kernel_size, stride=1, padding=kernel_size // 2) + F.max_pool2d(torch.where(mask, -depth, -torch.inf), kernel_size, stride=1, padding=kernel_size // 2))
edge = torch.zeros_like(depth, dtype=torch.bool) if atol is not None: edge |= diff > atol if rtol is not None: edge |= (diff / depth).nan_to_num_() > rtol edge = edge.reshape(*shape) return edge12345678910111213torch==2.5.1torchvision==0.20.1numpy==1.26.4pillowopencv-pythonplyfilehuggingface_hub
# below for gradiogradiotrimeshmatplotlibscipy.gitattributes+1
.gitignoreAdded+28
LICENSEAdded+21
app.pyAdded+938
example.pyAdded+67
examples/cartoon_horse.mp4Added+3
examples/gradio_examples/house.mp4Added+3
examples/gradio_examples/man_walking_long.mp4Added+3
examples/gradio_examples/parkour.mp4Added+3
examples/gradio_examples/valley.mp4Added+3
examples/parkour_long.mp4Added+3
examples/skating.mp4Added+3
examples/skiing.mp4Added+3
pi3/models/dinov2/__init__.pyAdded+6
pi3/models/dinov2/hub/__init__.pyAdded+4
pi3/models/dinov2/hub/backbones.pyAdded+156
pi3/models/dinov2/hub/utils.pyAdded+39
pi3/models/dinov2/layers/__init__.pyAdded+11
pi3/models/dinov2/layers/attention.pyAdded+89
pi3/models/dinov2/layers/block.pyAdded+259
pi3/models/dinov2/layers/dino_head.pyAdded+58
pi3/models/dinov2/layers/drop_path.pyAdded+34
pi3/models/dinov2/layers/layer_scale.pyAdded+27
pi3/models/dinov2/layers/mlp.pyAdded+40
pi3/models/dinov2/layers/patch_embed.pyAdded+88
pi3/models/dinov2/layers/swiglu_ffn.pyAdded+72
pi3/models/dinov2/models/__init__.pyAdded+43
pi3/models/dinov2/models/vision_transformer.pyAdded+404
pi3/models/dinov2/utils/__init__.pyAdded+4
pi3/models/dinov2/utils/cluster.pyAdded+95
pi3/models/dinov2/utils/config.pyAdded+72
pi3/models/dinov2/utils/dtype.pyAdded+37
pi3/models/dinov2/utils/param_groups.pyAdded+103
pi3/models/dinov2/utils/utils.pyAdded+95
pi3/models/layers/attention.pyAdded+369
pi3/models/layers/block.pyAdded+406
pi3/models/layers/camera_head.pyAdded+93
pi3/models/layers/pos_embed.pyAdded+174
pi3/models/layers/transformer_head.pyAdded+81
pi3/models/pi3.pyAdded+216
pi3/utils/basic.pyAdded+223
pi3/utils/debug.pyAdded+63
pi3/utils/geometry.pyAdded+375
requirements.txtAdded+13