def valid(val_loader, val_cls_list, val_attrs, model_dir): # model = resnet50(pretrained=False, cut_at_pooling=False, num_features=1024, norm=False, dropout=0, num_classes=30) # model = get_network(num_classes=30, depth=50) model = ResNet50M(num_classes=30) model = nn.DataParallel(model).cuda() checkpoint = load_checkpoint( osp.join(model_dir, 'checkpoint_64_33.pth.tar')) model.module.load_state_dict(checkpoint['state_dict']) model.eval() feat, label = [], [] for i, d in enumerate(val_loader): imgs, _, l = d inputs = Variable(imgs) _, outputs = model(inputs) feat.append(outputs.data.cpu().numpy()) label.extend(l) feat = np.vstack(feat) # name = name.hstack(name) dist = compute_dist(feat, val_attrs, 'cosine') result = [] for i, v in enumerate(dist): max = v.max() v = list(v) index = v.index(max) result.append((int(label[i]), val_cls_list[index])) n = 0 for tar, pre in result: if pre == tar: n = n + 1 print('the acc is {}/{}'.format(n, len(result))) return n
def test(test_loader, test_cls_list, test_attrs, model_dir): model = resnet50(pretrained=False, cut_at_pooling=False, num_features=1024, norm=False, dropout=0, num_classes=30) model = nn.DataParallel(model).cuda() checkpoint = load_checkpoint(osp.join(model_dir, 'checkpoint.pth.tar')) model.module.load_state_dict(checkpoint['state_dict']) model.eval() feat, name = [], [] for i, d in enumerate(test_loader): imgs, fnames, _ = d inputs = Variable(imgs) _, outputs = model(inputs) # outputs = F.sigmoid(outputs) feat.append(outputs.data.cpu().numpy()) name.extend(fnames) feat = np.vstack(feat) # name = name.hstack(name) dist = compute_dist(feat, test_attrs, 'cosine') result = [] for i, v in enumerate(dist): max = v.max() v = list(v) index = v.index(max) result.append((int(name[i][:3]), test_cls_list[index])) n = 0 for pre, tar in result: if pre == tar: n = n+1 print('the acc is {}/{}'.format(n, len(result)))