Pytorch rename labels of a trained model
I have a trained model. During training there was some problem with string characters. So i converted my labels into numbers like:
red : 0
blue: 1
green: 2
Now is it possible to rename my label back to actual label names.
Hours of training. Would be  helpful if anyone has an idea.
Train and validate the model
for epoch in range(1, epoch_num + 1):
    loss_train, acc_train = train(train_loader, model, criterion, optimizer, epoch)
    loss_val, acc_val = validate(val_loader, model, criterion, epoch)
    total_loss_val.append(loss_val)
    total_acc_val.append(acc_val)

Test Single Image:
def eval_image(file_path):
    model = torch.load(file_path)
    model.eval()
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    X = Image.open('red.jpeg')
    test_transforms = transforms.Compose([transforms.ToTensor()])
    image_tensor = test_transforms(X).float()
    image_tensor = image_tensor.unsqueeze_(0)
    input = Variable(image_tensor)
    input = input.to(device)
    output = model(input)
    index = output.data.cpu().numpy().argmax()
    print(index)

Once the training is done, The evaluation script cannot be made generic. I will have to pass following code
idx_to_class = {
        0: &quot;red&quot;,
        1: &quot;blue&quot;,
        2: &quot;green&quot;,
    }
    class_name = idx_to_class[index]

But I do not want to pass the above code. As my evaluation script needs to generic.

Use a dictionary for it!
labels = [0, 1, 2, 1, 0, 2, 1]

dct = {0: &quot;red&quot;, 1: &quot;blue&quot;, 2: &quot;green&quot;}

renamed_labels = [dct[x] for x in labels]

renamed_labels ## [&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;red&quot;, &quot;green&quot;, &quot;blue&quot;]

