Spaces:
Paused
Paused
Adding new code to randomly sample from the observed distribution over labels.
Browse files123456787 unmodified lines16171819202122232416 unmodified lines414243444546474849502 unmodified lines535455565758593 unmodified lines6364656667683 unmodified lines727374757677787980import osimport gradio as grimport randomimport timefrom datasets import load_dataset, Datasetfrom PIL import PngImagePlugin, ImageFileImageFile.LOAD_TRUNCATED_IMAGES = TruePngImagePlugin.MAX_TEXT_CHUNK = 1048576 * 10 # this is 10x the amount.7 unmodified lines# The number of examples/instances in this dataset is copied from the model card:# https://huggingface.co/datasets/ILSVRC/imagenet-1kNUM_EXAMPLES = 1281167# Arbitrary small number, only using in devv'ing (uncomment #.take(DEV_AMOUNT) below to use it).DEV = FalseDEV_AMOUNT = 10GATED_IMAGENET = os.environ.get("GATED_IMAGENET")16 unmodified linesdataset = load_dataset("ILSVRC/imagenet-1k", split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET)# Create a set of indices that are randomly chosen, to change their labels.# Specifically, randomly choose NUM_EXAMPLES/percentage indices.randomize_subset = set(random.sample(range(0, NUM_EXAMPLES), round(NUM_EXAMPLES / float(percentage))))# Update the dataset so that the labels are randomizedupdated_dataset = dataset.map(randomize_labels, with_indices=True,2 unmodified lines# 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(percentage), token=GATED_IMAGENET)else:Dataset.from_generator(updated_dataset.__iter__).push_to_hub("datacomp/imagenet-1k-random" + str(percentage), token=GATED_IMAGENET)3 unmodified linesprint("That took %d seconds" % (end - start))def randomize_labels(examples, indices):# 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.3 unmodified lines# Change the label to a random integer between 0 and 9for n in range(len(indices)):index = indices[n]examples["label"][n] = random.randint(0,9) if index in batch_subset else \examples["label"][n]return examplesdemo = gr.Interface(fn=main, inputs="text", outputs="text")12345678910111213147 unmodified lines222324252627282930313216 unmodified lines49505152535455565758596061626364652 unmodified lines686970717273743 unmodified lines787980818283848586878889909192939495969798991003 unmodified lines104105106107108109110import osimport jsonimport gradio as grimport randomimport timefrom collections import Counterfrom numpy.random import choicefrom datasets import load_dataset, Datasetfrom PIL import PngImagePlugin, ImageFileImageFile.LOAD_TRUNCATED_IMAGES = TruePngImagePlugin.MAX_TEXT_CHUNK = 1048576 * 10 # this is 10x the amount.7 unmodified lines# The number of examples/instances in this dataset is copied from the model card:# https://huggingface.co/datasets/ILSVRC/imagenet-1kNUM_EXAMPLES = 1281167# Arbitrary small number of dataset examples to look at, only using in devv'ing.DEV = TrueDEV_AMOUNT = 10# Whether to read in the distribution over labels from an external text file.READ_DISTRO = FalseGATED_IMAGENET = os.environ.get("GATED_IMAGENET")16 unmodified linesdataset = load_dataset("ILSVRC/imagenet-1k", split="train", streaming=True,trust_remote_code=True, token=GATED_IMAGENET)label_list, label_fractions = get_label_fractions()# How many new random labels are we creating?num_new_labels = int(round(NUM_EXAMPLES/float(percentage)))# 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_keys, size=num_new_labels, p=label_fractions))# Update the dataset so that the labels are randomizedupdated_dataset = dataset.map(randomize_labels, with_indices=True,2 unmodified lines# 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)3 unmodified linesprint("That took %d seconds" % (end - start))def get_label_fractions(dataset):print("Getting label proportions.")if READ_DISTRO:with open("label_distro.json", "r+") as f:label_counts = json.loads(f.read())else:label_counts = Counter([example['label'] for example in dataset])# Don't overrwrite the distribution when devving.if not DEV:with open("label_distro.json", "w+") as f:f.write(json.dumps(label_counts))label_list = list(label_counts.keys())denom = sum(label_counts.values())label_fractions = [label_counts[key]/denom for key in label_keys]return label_list, label_fractionsdef randomize_labels(examples, indices):# 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.3 unmodified lines# 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 examplesdemo = gr.Interface(fn=main, inputs="text", outputs="text")
app.py+39-9