Skip to content

Commit cd2c0f6

Browse files
authored
1 parent 8d6fe81 commit cd2c0f6

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

packages/tasks/src/model-libraries-snippets.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,53 @@ export const bm25s = (model: ModelData): string[] => [
8282
retriever = BM25HF.load_from_hub("${model.id}")`,
8383
];
8484

85+
export const depth_anything_v2 = (model: ModelData): string[] => {
86+
let encoder: string;
87+
let features: string;
88+
let out_channels: string;
89+
90+
encoder = "<ENCODER>";
91+
features = "<NUMBER_OF_FEATURES>";
92+
out_channels = "<OUT_CHANNELS>";
93+
94+
if (model.id === "depth-anything/Depth-Anything-V2-Small") {
95+
encoder = "vits";
96+
features = "64";
97+
out_channels = "[48, 96, 192, 384]";
98+
} else if (model.id === "depth-anything/Depth-Anything-V2-Base") {
99+
encoder = "vitb";
100+
features = "128";
101+
out_channels = "[96, 192, 384, 768]";
102+
} else if (model.id === "depth-anything/Depth-Anything-V2-Large") {
103+
encoder = "vitl";
104+
features = "256";
105+
out_channels = "[256, 512, 1024, 1024";
106+
}
107+
108+
return [
109+
`
110+
# Install from https://github.com/DepthAnything/Depth-Anything-V2
111+
112+
# Load the model and infer depth from an image
113+
import cv2
114+
import torch
115+
116+
from depth_anything_v2.dpt import DepthAnythingV2
117+
118+
# instantiate the model
119+
model = DepthAnythingV2(encoder="${encoder}", features=${features}, out_channels=${out_channels})
120+
121+
# load the weights
122+
filepath = hf_hub_download(repo_id="${model.id}", filename="depth_anything_v2_${encoder}.pth", repo_type="model")
123+
state_dict = torch.load(filepath, map_location="cpu")
124+
model.load_state_dict(state_dict).eval()
125+
126+
raw_img = cv2.imread("your/image/path")
127+
depth = model.infer_image(raw_img) # HxW raw depth map in numpy
128+
`,
129+
];
130+
};
131+
85132
const diffusers_default = (model: ModelData) => [
86133
`from diffusers import DiffusionPipeline
87134

packages/tasks/src/model-libraries.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
143143
filter: false,
144144
countDownloads: `path:"adapter_config.json"`,
145145
},
146+
"depth-anything-v2": {
147+
prettyLabel: "DepthAnythingV2",
148+
repoName: "Depth Anything V2",
149+
repoUrl: "https://github.com/DepthAnything/Depth-Anything-V2",
150+
snippets: snippets.depth_anything_v2,
151+
filter: false,
152+
countDownloads: `path_extension:"pth"`,
153+
},
146154
diffusers: {
147155
prettyLabel: "Diffusers",
148156
repoName: "🤗/diffusers",

0 commit comments

Comments
 (0)