def reduce_data(oidata, catmid2name, keep_classes=[]):
    """
    Reduce the amount of data by only keeping images that are in the classes we want.
    :param oidata: oidata, as outputted by parse_open_images
    :param catmid2name: catid2name dict, as produced by read_catMIDtoname
    :param keep_classes: List of classes to be kept.
    :return:
    """
    print(" Reducing the dataset. Initial dataset has length", len(oidata))
    # First build a dictionary of imageID:[classnames]
    imageid2classmid = {}
    for dd in oidata:
        imageid = dd['ImageID']
        if imageid not in imageid2classmid:
            imageid2classmid[imageid] = [dd['LabelName']]
        else:
            imageid2classmid[imageid].append(dd['LabelName'])

    # Work out which images we are including.
    imageid2include = {} # dict to store True if this imageid is included.

    for imgid, classmids in imageid2classmid.items():
        imageid2include[imgid] = False # Assume we don't include this.
        for mid in classmids:
            this_name = catmid2name[mid]
            if this_name in keep_classes:
                imageid2include[imgid] = True

    # Now work through list, appending if ImageID has imageid2include[imageid] = True
    returned_data = []
    for dd in oidata:
        imageid = dd['ImageID']
        if imageid2include[imageid]:
            returned_data.append(dd)

    print(" Reducing the dataset. Final dataset has length", len(returned_data))
    return returned_data