(class_name_or_list, batch_size=1)
return one_hot_from_int(classes, batch_size=batch_size)
Create a one-hot vector from the name of an imagenet class ('tennis ball', 'daisy', ...).
        We use NLTK's wordnet search to try to find the relevant synset of ImageNet and take the first one.
        If we can't find it direcly, we look at the hyponyms and hypernyms of the class name.

        Params:
            class_name_or_list: string containing the name of an imagenet object or a list of such strings (for a batch).
        Output:
            array of shape (batch_size, 1000)
Create a one-hot vector from the name of an imagenet class ('tennis ball', 'daisy', ...).
        We use NLTK's wordnet search to try to find the relevant synset of ImageNet and take the first one.
        If we can't find it direcly, we look at the hyponyms and hypernyms of the class name.
Create a one - hot vector from the name of an imagenet class ( tennis ball daisy ... ) . We use NLTK s wordnet search to try to find the relevant synset of ImageNet and take the first one . If we can t find it direcly we look at the hyponyms and hypernyms of the class name .
def one_hot_from_names(class_name_or_list, batch_size=1):
    """ Create a one-hot vector from the name of an imagenet class ('tennis ball', 'daisy', ...).
        We use NLTK's wordnet search to try to find the relevant synset of ImageNet and take the first one.
        If we can't find it direcly, we look at the hyponyms and hypernyms of the class name.

        Params:
            class_name_or_list: string containing the name of an imagenet object or a list of such strings (for a batch).
        Output:
            array of shape (batch_size, 1000)
    """
    try:
        from nltk.corpus import wordnet as wn
    except ImportError:
        raise ImportError("You need to install nltk to use this function")

    if not isinstance(class_name_or_list, (list, tuple)):
        class_name_or_list = [class_name_or_list]
    else:
        batch_size = max(batch_size, len(class_name_or_list))

    classes = []
    for class_name in class_name_or_list:
        class_name = class_name.replace(" ", "_")

        original_synsets = wn.synsets(class_name)
        original_synsets = list(filter(lambda s: s.pos() == 'n', original_synsets))  # keep only names
        if not original_synsets:
            return None

        possible_synsets = list(filter(lambda s: s.offset() in IMAGENET, original_synsets))
        if possible_synsets:
            classes.append(IMAGENET[possible_synsets[0].offset()])
        else:
            # try hypernyms and hyponyms
            possible_synsets = sum([s.hypernyms() + s.hyponyms() for s in original_synsets], [])
            possible_synsets = list(filter(lambda s: s.offset() in IMAGENET, possible_synsets))
            if possible_synsets:
                classes.append(IMAGENET[possible_synsets[0].offset()])

    return one_hot_from_int(classes, batch_size=batch_size)
def one_hot_from_names ( class_name_or_list , batch_size = 1 ) : try : from nltk . corpus import wordnet as wn except ImportError : raise ImportError ( "You need to install nltk to use this function" ) if not isinstance ( class_name_or_list , ( list , tuple ) ) : class_name_or_list = [ class_name_or_list ] else : batch_size = max ( batch_size , len ( class_name_or_list ) ) classes = [ ] for class_name in class_name_or_list : class_name = class_name . replace ( " " , "_" ) original_synsets = wn . synsets ( class_name ) original_synsets = list ( filter ( lambda s : s . pos ( ) == 'n' , original_synsets ) ) # keep only names if not original_synsets : return None possible_synsets = list ( filter ( lambda s : s . offset ( ) in IMAGENET , original_synsets ) ) if possible_synsets : classes . append ( IMAGENET [ possible_synsets [ 0 ] . offset ( ) ] ) else : # try hypernyms and hyponyms possible_synsets = sum ( [ s . hypernyms ( ) + s . hyponyms ( ) for s in original_synsets ] , [ ] ) possible_synsets = list ( filter ( lambda s : s . offset ( ) in IMAGENET , possible_synsets ) ) if possible_synsets : classes . append ( IMAGENET [ possible_synsets [ 0 ] . offset ( ) ] ) return one_hot_from_int ( classes , batch_size = batch_size )