I hope you can help me. I’m having some trouble with the training of a net. In particular I’m fine-tuning HydraPlusNet, a model that performs Pedestrian Attribute. I only need to classify 2 classes and I’m having strange results on the test set.
How is it possible that the tensor value is like this?
tensor([[-19.8942, 20.0554]]
I share with you the test code:
import os
import pickle
from matplotlib import pyplot as plt
import torch
import torch.utils.data as data
import torchvision.transforms as transforms
import argparse
import torchvision
from lib import dataload
from lib.AF import AF
from lib.MNet import MNet
from lib.Hydraplus import HP
from att_vis import att_plot
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-m', help="choose model", choices=['AF1', 'AF2', 'AF3', 'HP', 'MNet'], required=True)
parser.add_argument('-p', help='weight file path', required=True)
parser.add_argument('-att', help='attention results', choices=['no_att', 'img_save', 'img_show', 'pkl_save'], default='no_att')
args = parser.parse_args()
return args
def model_pred(data_input, model_name, img_name, network, att_mode):
out_dict = {}
if att_mode == 'no_att':
outputs = network(data_input)
else:
if model_name == 'HP':
att1, att2, att3, outputs = network(data_input)
out_dict = {"filename": img_name[0], 'AF1': att1[0].detach().numpy(), 'AF2': att2[0].detach().numpy(), 'AF3': att3[0].detach().numpy()}
elif 'AF' in model_name:
outputs, att = network(data_input)
out_dict = {"filename": img_name[0], model_name: att[0].detach().numpy()}
if att_mode == 'pkl_save':
pickle.dump(out_dict, open('result/att_output_' + model_name + '.pkl', 'ab'))
else:
att_plot(model_name, out_dict, att_mode)
return outputs
def imshow(img):
img = img.numpy().transpose((1, 2, 0))
plt.imshow(img)
plt.show()
def main():
args = parse_args()
mytransform = transforms.Compose([transforms.Resize((299, 299)), transforms.ToTensor()])
test_set = dataload.myImageFloder(root="/home/alfonso/Desktop/Dumping_Garbage_Dataset.v8i.retinanet/test/test", annotation_file="/home/alfonso/Desktop/Dumping_Garbage_Dataset.v8i.retinanet/test/_annotations.csv", transform=mytransform, mode='test')
imgLoader = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=True, num_workers=2)
print('image number in test set: {}'.format(len(test_set)))
classes = [1, 0]
path = args.p
net = None
num_err = 0
if args.att == 'no_att':
if 'AF' in args.m:
net = AF(af_name=args.m)
elif args.m == 'HP':
net = HP()
elif args.m == 'MNet':
net = MNet()
else:
if 'AF' in args.m:
net = AF(att_out=True, af_name=args.m)
elif args.m == 'HP':
net = HP(att_out=True)
elif args.m == 'MNet':
net = MNet()
if net is None:
print("Error: Network not defined.")
return
net.load_state_dict(torch.load(path))
print("para_load_done")
net.eval()
net.cuda()
TP = [0.0, 0.0] * 2
P = [0.0, 0.0] * 2
TN = [0.0] * 2
N = [0.0] * 2
Acc = 0.0
Prec = 0.0
Rec = 0.0
if args.att == 'pkl_save':
if os.path.exists(pkl_file := 'result/att_output_' + args.m + '.pkl'):
os.remove(pkl_file)
for count, (images, labels, filename) in enumerate(imgLoader):
# Visualizza un batch di immagini
with torch.no_grad():
inputs, labels = images.cuda(), labels.cuda()
print(inputs.size())
outputs,confidence = model_pred(data_input=inputs, model_name=args.m, img_name=filename, network=net, att_mode=args.att)
print(outputs)
pred_labels = torch.argmax(outputs, dim=1)
correct = pred_labels.eq(labels.view_as(pred_labels)).cpu().numpy()
#print(labels.view_as(pred_labels))
if not correct:
num_err += 1
print(filename)
print(outputs)
for i in range(len(correct)):
class_idx = int(labels[i])
TP[class_idx] += correct[i]
P[class_idx] += 1
#if count % 1 == 0:
#print('test on {}th img'.format(count))
for i in range(2):
Acc += TP[i] / P[i] if P[i] != 0 else 0
meanAccuracy = Acc / 2
Acc1= TP[0] / P[0] if P[0] != 0 else 0
Acc2 = TP[1] / P[1] if P[1] != 0 else 0
print("mAP per la classe NotCarrying: ")
print(Acc1)
print("nmAP per la classe Carrying: ")
print(Acc2)
print ("n Sono stati commessi : " + str(num_err) + " errori su "+str(len(test_set)) +" immagini n")
print("path: %s mA: %f" % (path, meanAccuracy))
print(num_err)
#Acc /= 10000
#Prec /= 10000
#Rec /= 10000
#F1 = 2 * Prec * Rec / (Prec + Rec)
#print("ACC: %f" % Acc)
#print("Prec: %f" % Prec)
#print("Rec: %f" % Rec)
#print("F1: %f" % F1)
if __name__ == '__main__':
main()
Just for information, I print the tensor value also during the training and it is in [0:1]
interval.
Moreover, I need to integrate the net in a tracker and when the net receive the bounding box, the tensor value will explode and the net gives each time the same output.
If the other parts of the code are useful I will share it with you.
Alfonso Giso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.