Skip to content

Commit 2e2d552

Browse files
committed
使用 Colaboratory 建立
1 parent 4284b53 commit 2e2d552

1 file changed

Lines changed: 213 additions & 0 deletions

File tree

W&B_Artifacts_API_Demo.ipynb

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "W&B Artifacts API Demo",
7+
"private_outputs": true,
8+
"provenance": [],
9+
"collapsed_sections": [],
10+
"toc_visible": true,
11+
"include_colab_link": true
12+
},
13+
"kernelspec": {
14+
"name": "python3",
15+
"display_name": "Python 3"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/JosephBless/DL/blob/main/W%26B_Artifacts_API_Demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {
32+
"id": "hpPsCoeioB9F"
33+
},
34+
"source": [
35+
"# W&B Artifacts API Demo\n",
36+
"\n",
37+
"In this notebook, we'll show you how to use W&B Artifacts to keep track of dataset versions in Weights & Biases.\n",
38+
"\n",
39+
"### How it works\n",
40+
" Using our Artifacts API, you can log artifacts as outputs of W&B runs, or use artifacts as input to runs.\n",
41+
" \n",
42+
"\n",
43+
"<img src=\"https://i.imgur.com/Pw8luzD.png\" width=\"500\">\n",
44+
"\n",
45+
"Since a run can use another run’s output artifact as input, artifacts and runs together form a directed graph. You don’t need to define pipelines ahead of time. Just use and log artifacts, and we’ll stitch everything together."
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {
51+
"id": "G03d9HSHuKF9"
52+
},
53+
"source": [
54+
"## 1. Install Weights & Biases"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"metadata": {
60+
"id": "Ny-d-pHrnrui"
61+
},
62+
"source": [
63+
"# Install Weights & Biases\n",
64+
"!pip install wandb -qqq\n",
65+
"\n",
66+
"import os\n",
67+
"import random\n",
68+
"import wandb"
69+
],
70+
"execution_count": null,
71+
"outputs": []
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {
76+
"id": "4PE2_FgKodSi"
77+
},
78+
"source": [
79+
"## 2. Create an Artifact\n",
80+
"\n",
81+
"Call `wandb.init` to initialize a new run. Use a run to track any script in your pipeline— anything from training and evaluation to scraping and preprocessing data. Specify what type of run it is in the **job_type**, and you'll be able to filter and group based on **job_type** in the web interface.\n",
82+
"\n",
83+
"`wandb.Artifact()`\n",
84+
"- **name**: The name of this dataset, model, or other set of files. The files inside can change, but the name stays the same across versions.\n",
85+
"- **type**: This helps you group together top-level artifacts, like \"dataset\" and \"model\".\n",
86+
"- **metadata**: This dictionary lets you track class distributions, UUIDs, and any important details you want to save and search over later."
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"metadata": {
92+
"id": "tHvfBXS0oig1"
93+
},
94+
"source": [
95+
"# This will create a new run in the W&B database.\n",
96+
"# Init starts tracking stdout/stderr and system metrics automatically.\n",
97+
"run = wandb.init(project='artifacts-demo', job_type='producer')\n",
98+
"\n",
99+
"# Create an Artifact. Give it a name and a type. Type is used for\n",
100+
"# organizational purposes and should typically be \"dataset\" or \"model\".\n",
101+
"artifact = wandb.Artifact('mountain-view-scenes', type='dataset', metadata={})\n",
102+
"\n",
103+
"# Store a new file in the artifact\n",
104+
"with artifact.new_file('overview.txt') as f:\n",
105+
" f.write('This is my first artifact!\\n')\n",
106+
"\n",
107+
"# Save the artifact to W&B. It will be tracked as output of the current run\n",
108+
"# and appended to the Artifact Sequence called 'hello artifacts dataset'.\n",
109+
"run.log_artifact(artifact)"
110+
],
111+
"execution_count": null,
112+
"outputs": []
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {
117+
"id": "MLhTfDMNokk7"
118+
},
119+
"source": [
120+
"Open [wandb.ai](https://app.wandb.ai/home) and click on your latest run to see the artifact tab appear on the left sidebar."
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {
126+
"id": "ihqE_Qn7pK1g"
127+
},
128+
"source": [
129+
"## 3. Use that Artifact\n",
130+
"Next, let's use that artifact as input to another run."
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"metadata": {
136+
"id": "ZKpridn_pczr"
137+
},
138+
"source": [
139+
"# Start a new run\n",
140+
"run = wandb.init(project='artifacts-demo', job_type='consumer')\n",
141+
"\n",
142+
"# We'll use the artifact we created in the previous run as input to this run.\n",
143+
"# This will fetch the latest entry in the 'hello artifacts dataset' Artifact Sequence\n",
144+
"artifact = run.use_artifact('mountain-view-scenes:latest')\n",
145+
"\n",
146+
"# Download all of the files contained in the artifact.\n",
147+
"artifact_dir = artifact.download()\n",
148+
"\n",
149+
"# Let's take a look at the downloaded files contents\n",
150+
"print(open(os.path.join(artifact_dir, 'overview.txt')).read())"
151+
],
152+
"execution_count": null,
153+
"outputs": []
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {
158+
"id": "o1GXQwx3rU0G"
159+
},
160+
"source": [
161+
"Let's log an output artifact too— in this example it's just a fake model file."
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"metadata": {
167+
"id": "oFchQuvVrY9T"
168+
},
169+
"source": [
170+
"artifact = wandb.Artifact('run-%s-model' % run.id, type='model')\n",
171+
"\n",
172+
"# This time we'll use artifact.add_file, to add a file that already exists.\n",
173+
"f = open('mymodel.txt', 'w')\n",
174+
"f.write('This is a really awesome trained model: %s' % random.random())\n",
175+
"f.close()\n",
176+
"\n",
177+
"artifact.add_file('mymodel.txt')\n",
178+
"run.log_artifact(artifact)\n",
179+
"\n",
180+
"# end the current run\n",
181+
"run.finish()"
182+
],
183+
"execution_count": null,
184+
"outputs": []
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {
189+
"id": "Z9lxlSqnsY8I"
190+
},
191+
"source": [
192+
"Now you can navigate to your project page (linked above), and then click on the artifacts tab, to dig into all the artifacts you've created so far.\n",
193+
"\n",
194+
"If you click through to an artifact, and then click on the \"Graph\" tab, you'll see a visualization that shows how your artifacts and runs are related to each other."
195+
]
196+
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {
200+
"id": "J3JeRwj_2oG-"
201+
},
202+
"source": [
203+
"## Documentation\n",
204+
"\n",
205+
"For more details, [see the docs →](https://docs.wandb.com/artifacts)\n",
206+
"- Storing directories in artifacts\n",
207+
"- Referring to external data using references\n",
208+
"- Automatic file and artifact deduplication\n",
209+
"- Best practices for dataset versioning and model management"
210+
]
211+
}
212+
]
213+
}

0 commit comments

Comments
 (0)