pytorch resnet50 How to change the model output to a probability value instead of a determined category

name1ess: <p>import os<br>
import glob<br>
import random<br>
import shutil</p>
<p>dataset_dir = ‘数据集2’<br>
train_dir = ‘数据集2/train’<br>
valid_dir = ‘数据集2/val’<br>
test_dir = ‘数据集2/’</p>
<p>train_per = 0.8<br>
valid_per = 0.2<br>
test_per = 0</p>
<p>def makedir(new_dir):<br>
if not os.path.exists(new_dir):<br>
os.makedirs(new_dir)</p>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:</p>
<pre><code>for root, dirs, files in os.walk(dataset_dir):
    for sDir in dirs:
        imgs_list = glob.glob(os.path.join(root, sDir) + '/*.jpg')
        random.seed(666)
        random.shuffle(imgs_list)
        imgs_num = len(imgs_list)

        train_point = int(imgs_num * train_per)
        valid_point = int(imgs_num * (train_per + valid_per))

        for i in range(imgs_num):
            if i &lt; train_point:
                out_dir = train_dir + sDir + '/'
            elif i &lt; valid_point:
                out_dir = valid_dir + sDir + '/'
            else:
                out_dir = test_dir + sDir + '/'

            makedir(out_dir)
            out_path = out_dir + os.path.split(imgs_list[i])[-1]
            shutil.copy(imgs_list[i], out_path)

        print('Class:{}, train:{}, valid:{}, test:{}'.format(sDir, train_point, valid_point - train_point,
                                                             imgs_num - valid_point))
</code></pre>
<p>import glob<br>
import torch<br>
import torchvision.models<br>
from torch.utils import data<br>
from PIL import Image<br>
import numpy as np<br>
from torchvision import transforms, datasets<br>
import matplotlib.pyplot as plt<br>
import torch.nn.functional as F<br>
import torch.nn as nn<br>
from torch import nn, optim<br>
import torchvision.models as models<br>
import os<br>
from torch.utils.data import DataLoader<br>
import time</p>
<p>image_transforms = {<br>
‘train’: transforms.Compose([<br>
transforms.CenterCrop(size=1024),<br>
transforms.Resize(size=256),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406],<br>
[0.229, 0.224, 0.225])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.CenterCrop(size=1024),<br>
transforms.Resize(size=256),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406],<br>
[0.229, 0.224, 0.225])<br>
])<br>
}</p>
<p>dataset = ‘数据集2’<br>
train_directory = os.path.join(dataset, ‘train’)<br>
valid_directory = os.path.join(dataset, ‘val’)</p>
<h1>
<a name="batch_size-1" class="anchor" href="#batch_size-1"></a>batch_size以及类别数</h1>
<p>batch_size = 32<br>
num_classes = 4</p>
<p>data = {<br>
‘train’: datasets.ImageFolder(root=train_directory, transform=image_transforms[‘train’]),<br>
‘val’: datasets.ImageFolder(root=valid_directory, transform=image_transforms[‘val’])</p>
<p>}</p>
<p>train_data_size = len(data[‘train’])<br>
valid_data_size = len(data[‘val’])</p>
<p>train_data = DataLoader(data[‘train’], batch_size=batch_size, shuffle=True)<br>
valid_data = DataLoader(data[‘val’], batch_size=batch_size, shuffle=True)</p>
<p>print(train_data_size, valid_data_size)</p>
<p>device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’)<br>
resnet50 = models.resnet50(pretrained=False)<br>
fc_inputs = resnet50.fc.in_features<br>
resnet50.fc = nn.Sequential(<br>
nn.Linear(fc_inputs, 256),<br>
nn.ReLU(),<br>
nn.Dropout(0.5),<br>
# nn.Linear output layer<br>
nn.Linear(256, 4),<br>
nn.LogSoftmax(dim=1)<br>
)</p>
<p>resnet50 = resnet50.to(device)</p>
<p>loss_func = nn.NLLLoss()<br>
optimizer = optim.Adam(resnet50.parameters())</p>
<h1>
<a name="printmodel-2" class="anchor" href="#printmodel-2"></a>print(model)</h1>
<p>def train_and_valid(model, loss_function, optimizer, epochs=25):<br>
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
history = []<br>
best_acc = 0.0<br>
best_epoch = 0</p>
<pre><code>for epoch in range(epochs):
    epoch_start = time.time()
    print("Epoch: {}/{}".format(epoch + 1, epochs))

    model.train()

    train_loss = 0.0
    train_acc = 0.0
    valid_loss = 0.0
    valid_acc = 0.0

    for i, (inputs, labels) in enumerate(train_data):
        inputs = inputs.to(device)
        labels = labels.to(device)

        # 因为这里梯度是累加的，所以每次记得清零
        optimizer.zero_grad()

        outputs = model(inputs)



        loss = loss_function(outputs, labels)

        loss.backward()

        optimizer.step()

        train_loss += loss.item() * inputs.size(0)

        ret, predictions = torch.max(outputs.data, 1)
        correct_counts = predictions.eq(labels.data.view_as(predictions))

        acc = torch.mean(correct_counts.type(torch.FloatTensor))

        train_acc += acc.item() * inputs.size(0)

    with torch.no_grad():
        model.eval()

        for j, (inputs, labels) in enumerate(valid_data):
            inputs = inputs.to(device)
            labels = labels.to(device)

            outputs = model(inputs)

            outputs2 = F.softmax(outputs, dim=1)
            # b_probs = torch.argmax(outputs, 1)

            loss = loss_function(outputs, labels)

            valid_loss += loss.item() * inputs.size(0)

            ret, predictions = torch.max(outputs.data, 1)
            correct_counts = predictions.eq(labels.data.view_as(predictions))

            acc = torch.mean(correct_counts.type(torch.FloatTensor))

            valid_acc += acc.item() * inputs.size(0)

    avg_train_loss = train_loss / train_data_size
    avg_train_acc = train_acc / train_data_size

    avg_valid_loss = valid_loss / valid_data_size
    avg_valid_acc = valid_acc / valid_data_size

    history.append([avg_train_loss, avg_valid_loss, avg_train_acc, avg_valid_acc])

    if best_acc &lt; avg_valid_acc:
        best_acc = avg_valid_acc
        best_epoch = epoch + 1

    epoch_end = time.time()


    print(
        "Epoch: {:03d}, Training: Loss: {:.4f}, Accuracy: {:.4f}%, \n\t\tValidation: Loss: {:.4f}, Accuracy: {:.4f}%, Time: {:.4f}s".format(
            epoch + 1, avg_valid_loss, avg_train_acc * 100, avg_valid_loss, avg_valid_acc * 100,
            epoch_end - epoch_start
        ))
    print("Best Accuracy for validation : {:.4f} at epoch {:03d}".format(best_acc, best_epoch))

    torch.save(model, dataset + '\\models1\\' + '_model_' + str(epoch + 1) + '.pt')
return model, history
</code></pre>
<h1>
<a name="h-3" class="anchor" href="#h-3"></a>迭代次数设置</h1>
<p>num_epochs = 3<br>
trained_model, history = train_and_valid(resnet50, loss_func, optimizer, num_epochs)</p>
<h1>
<a name="h-4" class="anchor" href="#h-4"></a>模型保存的路径</h1>
<p>torch.save(history, dataset + ‘\models1\’ + ‘_history.pt’)</p>
<p>history = np.array(history)<br>
plt.plot(history[:, 0:2])<br>
plt.legend([‘Tr Loss’, ‘Val Loss’])<br>
plt.xlabel(‘Epoch Number’)<br>
plt.ylabel(‘Loss’)<br>
plt.ylim(0, 1)<br>
plt.savefig(dataset + ‘_loss_curve.png’)<br>
plt.show()</p>
<p>plt.plot(history[:, 2:4])<br>
plt.legend([‘Tr Accuracy’, ‘Val Accuracy’])<br>
plt.xlabel(‘Epoch Number’)<br>
plt.ylabel(‘Accuracy’)<br>
plt.ylim(0, 1)<br>
plt.savefig(dataset + ‘_accuracy_curve.png’)<br>
plt.show()</p>

ptrblck: <p>Based on your posted code it seems you are using <code>nn.LogSoftmax</code> in your model which will output log probabilities. You could apply <code>torch.exp</code> on them to get the probabilities in <code>[0, 1]</code> again.<br>
PS: you can post code snippets by wrapping them into three backticks ``` which makes debugging easier.</p>

name1ess: <p>Thank you for your answer.I’m a student at school, and I’m still at the basic learning stage.<br>
I found this method in your other post responses: probs = F.softmax(outputs, dim=1)<br>
and outputs = model(inputs)<br>
probs seems to be the probability of each class I want,Is that right？<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd19136a887cab802954f23bc2595433f27d690d.png" data-download-href="https://discuss.pytorch.org/uploads/default/cd19136a887cab802954f23bc2595433f27d690d" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd19136a887cab802954f23bc2595433f27d690d.png" alt="image" data-base62-sha1="tgnBAcdTcZIicgmSrvf3xU2h86x" width="690" height="216" data-dominant-color="343434"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">806×253 26.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

srishti-git1110: <p><a class="mention" href="/u/name1ess">@name1ess</a> softmax shall not work for your model specification as you are already using a log softmax layer that outputs log(p) - the log probabilities. You can use torch.exp as <a class="mention" href="/u/ptrblck">@ptrblck</a> has suggested.</p>
<p>exp(log(p)) = p</p>

name1ess: <p>Oh, I see!<br>
outputs = model(inputs)<br>
output = torch.exp(outputs)</p>
<p>then output is the probability I want?</p>

srishti-git1110: <p>Yes.</p>
<p>See for example:</p>
<pre><code class="lang-auto">inp_batch = torch.randn(8, 4) # batch_size=8
layer = nn.LogSoftmax(dim=1)
output = layer(inp_batch)
output = torch.exp(output)

print(output.sum(dim=1))  # tensor([1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000])
</code></pre>

name1ess: <p>love u <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20">   <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>