Spaces:
Paused
Paused
Update app.py
Browse files23 unmodified lines2425262728291 unmodified line31323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611723 unmodified lines# https://huggingface.co/datasets/ILSVRC/imagenet-1kNUM_EXAMPLES = 1281167DEV = False# Arbitrary small number of dataset examples to look at, only using in devv'ing.DEV_AMOUNT = 10if DEV:1 unmodified line# Whether to read in the distribution over labels from an external text file.READ_DISTRO = FalseGATED_IMAGENET = os.environ.get("GATED_IMAGENET")LABELS_FILE = "label_frequencies_full.csv"def read_label_frequencies():label_counts_dict = {}header_row = ['Label', 'Frequency']with open(LABELS_FILE) as csvfile:label_reader = csv.DictReader(csvfile)assert label_reader.fieldnames == header_rowfor row in label_reader:assert row['Label'] not in label_counts_dictlabel_counts_dict[row['Label']] = int(row['Frequency'])# TODO: Can we just do this instead of the fractions? Do they really need to be normalized?# label_list, label_counts = zip(*label_counts_dict.items())return label_counts_dictdef get_label_fractions(label_counts_dict):print("Getting label proportions.")label_list = list(label_counts_dict.keys())denom = sum(label_counts_dict.values())label_fractions = [label_counts_dict[key]/denom for key in label_counts_dict]return label_list, label_fractionsdef randomize_labels(examples, indices, new_random_labels):# What set of examples should be randomized in this batch?# This is the intersection of the batch indices and the indices we randomly selected to change the labels of.batch_subset = list(set(indices) & randomize_subset)# If this batch has indices that we're changing the label of....if batch_subset != []:# Change the label to a random integer between 0 and 9for n in range(len(indices)):index = indices[n]examples["label"][n] = new_random_labels.pop() if index in batch_subset else examples["label"][n]return examplesdef main(percentage=10):global randomize_subset# Just for timing how long this takes.start = time.time()percentage = float(percentage)print("Randomizing %d percent of the data." % percentage)# Set the random seed, based on the percentage, so that our random changes are reproducible.random.seed(percentage)# Load the dataset from the HF hub. Use streaming so as not to load the entire dataset at once.# Use the .take(DEV_AMOUNT) to only grab a small chunk of instances to develop with.if DEV:dataset = load_dataset("ILSVRC/imagenet-1k", split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET).take(DEV_AMOUNT)else:dataset = load_dataset("ILSVRC/imagenet-1k", split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET)label_list, label_fractions = get_label_fractions(read_label_frequencies())# How many new random labels are we creating?num_new_labels = int(round(NUM_EXAMPLES * float(percentage) * .01))# Create a set of indices that are randomly chosen, to change their labels.# Specifically, randomly choose num_new_labels indices.randomize_subset = set(random.sample(range(0, NUM_EXAMPLES), num_new_labels))# Randomly choose what the new label values are, following the observed label frequencies.new_random_labels = list(choice(a=label_list, size=num_new_labels, p=label_fractions))# Update the dataset so that the labels are randomizedupdated_dataset = dataset.map(randomize_labels, with_indices=True,fn_kwargs={'new_random_labels':new_random_labels},features=dataset.features, batched=True)# Upload the new version of the dataset (this will take awhile)if DEV:Dataset.from_generator(updated_dataset.__iter__).push_to_hub("datacomp/imagenet-1k-random-debug" + str(DEV_AMOUNT) + "-" + str(percentage), token=GATED_IMAGENET)else:Dataset.from_generator(updated_dataset.__iter__).push_to_hub("datacomp/imagenet-1k-random" + str(percentage), token=GATED_IMAGENET)end = time.time()23 unmodified lines242526272829301 unmodified line323334353637383940414243444546474849505152535455565758596061626364656667686923 unmodified lines# https://huggingface.co/datasets/ILSVRC/imagenet-1kNUM_EXAMPLES = 1281167DEV = FalseFRACTIONS = [2, 4, 8, 16, 32, 64]# Arbitrary small number of dataset examples to look at, only using in devv'ing.DEV_AMOUNT = 10if DEV:1 unmodified line# Whether to read in the distribution over labels from an external text file.READ_DISTRO = FalseGATED_IMAGENET = os.environ.get("GATED_IMAGENET")def create_subset_dataset(dataset, fraction_size):dataset = dataset.shuffle(buffer_size=NUM_EXAMPLES)num_samples = int(NUM_EXAMPLES / fraction_size)sampled_dataset = dataset.take(num_samples)return sampled_datasetdef main(percentage=10):global randomize_subset# Just for timing how long this takes.start = time.time()percentage = float(percentage)if DEV:dataset = load_dataset("datacomp/imagenet-1k-random" + str(percentage), split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET).take(DEV_AMOUNT)else:dataset = load_dataset("datacomp/imagenet-1k-random" + str(percentage), split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET)for frac in FRACTIONS:sampled_dataset = create_subset_dataset(dataset, frac)# Upload the new version of the dataset (this will take awhile)if DEV:Dataset.from_generator(updated_dataset.__iter__).push_to_hub("datacomp/debug-imagenet-1k-random-" + "-" + str(percentage) + '-frac-1/' + str(frac), token=GATED_IMAGENET)else:Dataset.from_generator(updated_dataset.__iter__).push_to_hub("datacomp/imagenet-1k-random" + str(percentage) + '-frac-1/' + str(frac), token=GATED_IMAGENET)end = time.time()
app.py+19-67