usakira
(hana)
January 22, 2018, 12:39pm
1
Hi! I’m trying to fine-tune some network, like vgg, alexnet, resnet, to meet with my dataset.
input: image, ByteTensor(3×200×200)
output: hot vector of labels, ByteTensor(1×98)
I checked the transfer learning tutorial (http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html )
and changed the last layer and the calculation part with resnet18 as follows, but the error is happened.
#model change
train_resnet = models.resnet18(pretrained=True)
num_ftrs = pretrain_resnet.fc.in_features
pretrain_resne.fc = nn.Liear(num_ftrs,98)
#forward + backward + optimize
m = nn.LogSoftmax()
outputs = pretrain_resnet(inputs)
_, preds = torch.max(outputs.data,1)
loss = criterion(m(outputs), labels)
#error
outputs = pretrain_resnet(inputs)
→RuntimeError: expexted Byte tensor (got Float tensor)
Best regards,
hana
SimonW
(Simon Wang)
January 22, 2018, 2:50pm
2
Byte is discrete. Your image tensor should be floating point type.
usakira
(hana)
January 23, 2018, 2:11am
3
Thank you.
I changed image, byte tensor to float tensor
and lables, byte tensor to long tensor like
data['image'].float()
data['labels'].long()
but another Run time error happened at loss calculation.
Runtime Error: multi-target not supported at /opt/conda/conda-bld/pytorch_1503965122592/work/torch/lib/THNN/generic/ClassNLLCriterion.c:22
outputs : FloatTensor 1×98
labels: FloatTensor 1×98
hmmm, What it is caused by?
usakira
(hana)
January 23, 2018, 4:01am
4
I just changed at loss function like that and it worked!
criterion = MultiLabelMarginLoss()
for i in epoch:
loss = criterion(outputs, labels)
Thank you so much!
SimonW
(Simon Wang)
January 23, 2018, 4:50am
5
If your label vector is one-hot, using MultiLabelMarginLoss is doing the wrong thing, although it won’t give error. Pytorch convention directly uses indices, so your target should be [batch_size x 1]
. With MultiLabelMarginLoss, you are essentially saying that your target labels contain a lot of label 0s and one label 1.
usakira
(hana)
January 23, 2018, 5:26am
6
Thank you for telling me that.
My each target vector has a lot of label 0s and some 1s. ex)[0,0,0,1,0,0,1,…,0]
So what should I do is to convert 1×98
tensor to 98×1 tensor and to change the loss function…
I’m glad if you let me know the suitable loss function.
SimonW
(Simon Wang)
January 23, 2018, 5:33am
7
It seems that each image belongs to more than one target class. Am I understanding correctly?
usakira
(hana)
January 23, 2018, 5:48am
8
yes.
Each data has labeled at least with 5 classes.