Skip to content

Instantly share code, notes, and snippets.

Avatar

Toubi AntoineToubhans

View GitHub Profile
View dvc-streamlit-article-python-11.py
from PIL import Image
model = load_model(rev=selected_commit.hexsha)
uploaded_file = st.file_uploader("Upload an image")
if uploaded_file:
image = Image.open(uploaded_file)
image_name = uploaded_file.name
resized_image = image.resize(IMG_SIZE)
View dvc-streamlit-article-python-10.py
from pathlib import Path
import tensorflow as tf
# Warning: this is private internal dvc api, it may change with future version
import dvc.repo.get
ROOT_MODEL_CACHE_DIR = Path(".model_cache")
ROOT_MODEL_CACHE_DIR.mkdir(exist_ok=True)
@st.cache
def load_model(rev: str):
View dvc-streamlit-article-python-09.py
experiments = pd.DataFrame([
{
"hash": model_commit.hexsha,
"message": model_commit.message,
"committed_datetime": str(model_commit.committed_datetime),
"committer": str(model_commit.committer),
**MODELS_PARAMETERS[model_commit.hexsha],
**MODELS_EVALUATION_METRICS[model_commit.hexsha],
}
for model_commit in MODELS_COMMITS
View dvc-streamlit-article-python-08.py
import json
def _read_model_evaluation_metrics(rev: str) -> dict:
with dvc.api.open("data/evaluation/metrics.json", rev=rev) as file:
return json.load(file)
MODELS_EVALUATION_METRICS = {
commit.hexsha: _read_model_evaluation_metrics(model_rev=commit.hexsha)
for commit in MODELS_COMMITS
}
View dvc-streamlit-article-python-07.py
import yaml
def _read_train_params(rev: str) -> dict:
with git_open("dvc.lock", rev=rev) as file:
dvc_lock = yaml.safe_load(file)
return dvc_lock["stages"]["train"]["params"]["params.yaml"]
MODELS_PARAMETERS = {
commit.hexsha: _read_train_params(rev=commit.hexsha)
for commit in MODELS_COMMITS
View dvc-streamlit-article-python-06.py
FIRST_COMMIT = list(REPO.iter_commits())[-1]
@contextmanager
def git_open(path: str, rev: str):
commit = REPO.commit(rev)
# Hack to get the full blob data stream: compute diff with initial commit
diff = commit.diff(FIRST_COMMIT, str(path))[0]
yield diff.a_blob.data_stream
View dvc-streamlit-article-python-05.py
st.dataframe(disagree_predictions)
for _, row in disagree_predictions.iterrows():
st.image(
row["image_path"],
caption=f"{row['image_name']}: A={row['predicted_label_x']}, B={row['predicted_label_y']} (true={row['true_label']})",
width=150,
)
View dvc-streamlit-article-python-04.py
predictions_a = load_predictions(rev=selected_commit_a.hexsha)
predictions_b = load_predictions(rev=selected_commit_b.hexsha)
disagree_predictions = pd.merge(
left=predictions_a.drop(columns=["prediction"]),
right=predictions_b.drop(columns=["true_label", "image_path", "prediction"]),
on="image_name",
).loc[lambda df: df.predicted_label_x != df.predicted_label_y]
View dvc-streamlit-article-python-03.py
selected_commit_a = st.selectbox(
"Choose commit A",
[commit for commit in MODELS_COMMITS],
format_func=lambda commit: f"{commit.hexsha[:6]} - {commit.message} - {commit.committed_datetime}",
)
selected_commit_b = st.selectbox(
"Choose commit B",
[commit for commit in MODELS_COMMITS],
format_func=lambda commit: f"{commit.hexsha[:6]} - {commit.message} - {commit.committed_datetime}",
View dvc-streamlit-article-python-02.py
import dvc.api
import pandas as pd
@st.cache
def load_predictions(rev: str) -> pd.DataFrame:
with dvc.api.open("data/evaluation/predictions.csv", rev=rev) as f:
return pd.read_csv(f)
selected_commit = ... # Use the commit selector introduced previous section