Visitar URL original
Robustness fix: Annotation file not dropping likelihood column if present from machine labels by C-Achard · Pull Request #3323 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Drop likelihood cols and validate coords
Fix format_training_data to handle MultiIndex columns with likelihoods: detect coord level, keep only x/y columns (logging when likelihoods are dropped), and raise if x/y are missing. Use the row values to ensure an even number of coordinates after dropping non-coord columns (error if odd), reshape into (N,2), filter NaNs, and clip out-of-image joints. Also skip images without labels.
  • Loading branch information
C-Achard committed May 11, 2026
commit 791efacb85afcb545a51e6bc88ffa9eee57eac32
35 changes: 34 additions & 1 deletion deeplabcut/generate_training_dataset/trainingsetmanipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,22 +728,54 @@ def to_matlab_cell(array):
outer[0, 0] = array.astype("int64")
return outer

# Keep only x/y columns if likelihood is present
if isinstance(df.columns, pd.MultiIndex):
coord_level = "coords" if "coords" in df.columns.names else df.columns.names[-1]
coord_values = df.columns.get_level_values(coord_level)

has_x = "x" in coord_values
has_y = "y" in coord_values
has_likelihood = "likelihood" in coord_values

if has_x and has_y:
if has_likelihood:
logging.info(
"Detected likelihood columns in training data; dropping them before formatting training dataset."
)
df = df.loc[:, coord_values.isin(["x", "y"])]
else:
raise ValueError(
f"Training data must contain x/y coordinates. Found coordinate labels: {list(pd.unique(coord_values))}"
)

for i in train_inds:
data = dict()
filename = df.index[i]
data["image"] = filename
img_shape = read_image_shape_fast(os.path.join(project_path, *filename))
data["size"] = img_shape
temp = df.iloc[i].values.reshape(-1, 2)

row = df.iloc[i].values

if row.size % 2 != 0:
raise ValueError(
"Training data row does not contain an even number of coordinate values "
f"after dropping non-coordinate columns. Row size={row.size}, "
f"image={filename}"
)

temp = row.reshape(-1, 2)
joints = np.c_[range(nbodyparts), temp]
joints = joints[~np.isnan(joints).any(axis=1)].astype(int)

# Check that points lie within the image
inside = np.logical_and(
np.logical_and(joints[:, 1] < img_shape[2], joints[:, 1] > 0),
np.logical_and(joints[:, 2] < img_shape[1], joints[:, 2] > 0),
)
if not all(inside):
joints = joints[inside]

if joints.size: # Exclude images without labels
data["joints"] = joints
train_data.append(data)
Expand All @@ -754,6 +786,7 @@ def to_matlab_cell(array):
to_matlab_cell(data["joints"]),
)
)

matlab_data = np.asarray(matlab_data, dtype=[("image", "O"), ("size", "O"), ("joints", "O")])
return train_data, matlab_data

Expand Down