Commit ·
2a3a1cc
1
Parent(s): 14544c7
Handle both 'label' and 'labels' column names in datasets
Browse files209 unmodified lines21021121221321321421521621721821721821922022122022222322422522622722522822923023122923223323423512 unmodified lines24824925024825125225325459 unmodified lines31431531631431731831932032131932232332432595 unmodified lines42142242342142442542642711 unmodified lines439440441439442443444441445446447448209 unmodified lines from datasets import load_dataset
print(f"Loading dataset: {dataset_name}...") dataset = load_dataset(dataset_name, trust_remote_code=True) dataset = load_dataset(dataset_name) print(f"Train: {len(dataset['train']):,} samples") print(f"Test: {len(dataset['test']):,} samples")
# Get label info label_feature = dataset["train"].features["label"] # Get label column name (datasets use 'label' or 'labels') _features = dataset["train"].features label_column = "label" if "label" in _features else "labels" label_feature = _features[label_column] labels = label_feature.names if hasattr(label_feature, "names") else None num_labels = label_feature.num_classes if hasattr(label_feature, "num_classes") else len(set(dataset["train"]["label"])) num_labels = label_feature.num_classes if hasattr(label_feature, "num_classes") else len(set(dataset["train"][label_column]))
print(f"Label column: '{label_column}'") print(f"Labels ({num_labels}): {labels}")
mo.md(f"**Loaded {len(dataset['train']):,} training samples with {num_labels} classes**") return dataset, labels, num_labels return dataset, label_column, labels, num_labels
@app.celldef _(dataset, labels, mo):def _(dataset, label_column, labels, mo): # Show sample images (notebook mode only) import base64 as _base64 from io import BytesIO as _BytesIO12 unmodified lines _images_html = [] for _sample in _samples: _img_b64 = _image_to_base64(_sample["image"]) _label_name = labels[_sample["label"]] if labels else _sample["label"] _label_name = labels[_sample[label_column]] if labels else _sample[label_column] _images_html.append( f""" <div style="text-align: center; margin: 5px;">59 unmodified lines
@app.celldef _(dataset, image_processor):def _(dataset, image_processor, label_column): def preprocess(examples): """Apply image processor to batch of images.""" images = [img.convert("RGB") for img in examples["image"]] inputs = image_processor(images, return_tensors="pt") inputs["label"] = examples["label"] inputs["labels"] = examples[label_column] # Trainer expects 'labels' return inputs
print("Preprocessing dataset...")95 unmodified lines
@app.celldef _(dataset, id2label, image_processor, mo, model):def _(dataset, id2label, image_processor, label_column, mo, model): import torch import base64 as _b64 from io import BytesIO as _BIO11 unmodified lines _outputs = model(**_inputs) _pred_idx = _outputs.logits.argmax(-1).item()
_true_label = id2label[_sample["label"]] if id2label else _sample["label"] _true_idx = _sample[label_column] _true_label = id2label[_true_idx] if id2label else _true_idx _pred_label = id2label[_pred_idx] if id2label else _pred_idx _correct = "correct" if _pred_idx == _sample["label"] else "wrong" _correct = "correct" if _pred_idx == _true_idx else "wrong"
# Convert image for display _img_copy = _img.copy()train-image-classifier.py+16-12