Commit ·
2a3a1cc
1
Parent(s): 14544c7
Handle both 'label' and 'labels' column names in datasets
Browse files209 unmodified lines21021121221321421521621721821922022122222322422522622722822923023123212 unmodified lines24524624724824925025159 unmodified lines31131231331431531631731831932032132295 unmodified lines41841942042142242342411 unmodified lines436437438439440441442443444209 unmodified linesfrom datasets import load_datasetprint(f"Loading dataset: {dataset_name}...")dataset = load_dataset(dataset_name, trust_remote_code=True)print(f"Train: {len(dataset['train']):,} samples")print(f"Test: {len(dataset['test']):,} samples")# Get label infolabel_feature = dataset["train"].features["label"]labels = label_feature.names if hasattr(label_feature, "names") else Nonenum_labels = label_feature.num_classes if hasattr(label_feature, "num_classes") else len(set(dataset["train"]["label"]))print(f"Labels ({num_labels}): {labels}")mo.md(f"**Loaded {len(dataset['train']):,} training samples with {num_labels} classes**")return dataset, labels, num_labels@app.celldef _(dataset, labels, mo):# Show sample images (notebook mode only)import base64 as _base64from 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"]_images_html.append(f"""<div style="text-align: center; margin: 5px;">59 unmodified lines@app.celldef _(dataset, image_processor):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"]return inputsprint("Preprocessing dataset...")95 unmodified lines@app.celldef _(dataset, id2label, image_processor, mo, model):import torchimport base64 as _b64from 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"]_pred_label = id2label[_pred_idx] if id2label else _pred_idx_correct = "correct" if _pred_idx == _sample["label"] else "wrong"# Convert image for display_img_copy = _img.copy()209 unmodified lines21021121221321421521621721821922022122222322422522622722822923023123223323423512 unmodified lines24824925025125225325459 unmodified lines31431531631731831932032132232332432595 unmodified lines42142242342442542642711 unmodified lines439440441442443444445446447448209 unmodified linesfrom datasets import load_datasetprint(f"Loading dataset: {dataset_name}...")dataset = load_dataset(dataset_name)print(f"Train: {len(dataset['train']):,} samples")print(f"Test: {len(dataset['test']):,} samples")# Get label column name (datasets use 'label' or 'labels')_features = dataset["train"].featureslabel_column = "label" if "label" in _features else "labels"label_feature = _features[label_column]labels = label_feature.names if hasattr(label_feature, "names") else Nonenum_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, label_column, labels, num_labels@app.celldef _(dataset, label_column, labels, mo):# Show sample images (notebook mode only)import base64 as _base64from 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_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, 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["labels"] = examples[label_column] # Trainer expects 'labels'return inputsprint("Preprocessing dataset...")95 unmodified lines@app.celldef _(dataset, id2label, image_processor, label_column, mo, model):import torchimport base64 as _b64from io import BytesIO as _BIO11 unmodified lines_outputs = model(**_inputs)_pred_idx = _outputs.logits.argmax(-1).item()_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 == _true_idx else "wrong"# Convert image for display_img_copy = _img.copy()
train-image-classifier.py+16-12