text_html_output_1.png
label_enc = {'Ant-Man': 0, 'Aquaman': 1, 'Avengers': 2, 'Batman': 3, 'Black Panther': 4, 'Captain America': 5, 'Catwoman': 6, 'Ghost Rider': 7, 'Hulk': 8, 'Iron Man': 9, 'Spiderman': 10, 'Superman': 11}
label_deco = {0: 'Ant-Man', 1: 'Aquaman', 2: 'Avengers', 3: 'Batman', 4: 'Black Panther', 5: 'Captain America', 6: 'Catwoman', 7: 'Ghost Rider', 8: 'Hulk', 9: 'Iron Man', 10: 'Spiderman', 11: 'Superman'}

def encode_label(label):
    target = torch.zeros(12, dtype=torch.float)
    target[int(label)] = 1.0
    return target
def decode_target(target, text_labels=False, threshold=0.5):
    label = None
    for i, x in enumerate(target):
        if x >= threshold:
            label = i
            break
    if text_labels:
        return f'{label_deco[label]}({label})'
    return label

encoded_lab = encode_label(4)
decoded_lab = decode_target(encoded_lab)
text = decode_target(encoded_lab, True)
print(encoded_lab, decoded_lab, text, sep='\n')
del (encoded_lab, decoded_lab, text)