<jupyter_start><jupyter_text>测试<jupyter_code>from PIL import Image
from models.mtcnn import MTCNN  # noqa
mtcnn = MTCNN()
model_path = '/content/drive/My Drive/model_save/cv/vggface2_mingxing_finetune.pt'
resnet = torch.load(model_path).to('cpu').eval()
# print('resnet:{}'.format(resnet))
img_path = '/content/drive/My Drive/data/cv/face/mingxing/test/tongliya.jpg'
save_path = '/content/drive/My Drive/data/cv/face/mingxing/test/tongliya_cropped.jpg'
img = Image.open(img_path)
# Get cropped and prewhitened image tensor
img_cropped = mtcnn(img, save_path=save_path)
# Calculate embedding (unsqueeze to add batch dimension)
img_embedding = resnet(img_cropped.unsqueeze(0))
# Or, if using for VGGFace2 classification
resnet.classify = True
img_probs = resnet(img_cropped.unsqueeze(0))
probs = torch.nn.functional.softmax(img_probs, dim=-1)
print('probs:{}'.format(probs))
print('img_probs:{}'.format(probs.max(dim=1)))<jupyter_output>probs:tensor([[0.0476, 0.0567, 0.0487, 0.0548, 0.0481, 0.0514, 0.0461, 0.0473, 0.0460,
         0.0454, 0.0441, 0.0489, 0.0540, 0.0645, 0.0622, 0.0503, 0.0421, 0.0475,
         0.0476, 0.0468]], grad_fn=<SoftmaxBackward>)
img_probs:torch.return_types.max(
values=tensor([0.0645], grad_fn=<MaxBackward0>),
indices=tensor([13]))<jupyter_text>成龙：img_probs:torch.return_types.max(values=tensor([0.1486], grad_fn=),indices=tensor([0]))胡歌：img_probs:torch.return_types.max(values=tensor([0.1181], grad_fn=),indices=tensor([5]))<jupyter_code>img_names = dataset.classes
print(img_names)
right_count = 0
sum_count = 0
for img_id, img_name in enumerate(img_names):
  try:
    img_path = '/content/drive/My Drive/data/cv/face/mingxing/test/{}.jpg'.format(img_name)
    save_path = '/content/drive/My Drive/data/cv/face/mingxing/test/{}_cropped.jpg'.format(img_name)
    img = Image.open(img_path)
    # Get cropped and prewhitened image tensor
    img_cropped = mtcnn(img, save_path=save_path)
    # Calculate embedding (unsqueeze to add batch dimension)
    img_embedding = resnet(img_cropped.unsqueeze(0))
    # Or, if using for VGGFace2 classification
    resnet.classify = True
    img_probs = resnet(img_cropped.unsqueeze(0))
    probs = torch.nn.functional.softmax(img_probs, dim=-1)
    label_prob, label = probs.max(dim=1)
    label_prob = label_prob.item()
    label = label.item()
    # print('probs:{}'.format(probs))
    print('label_prob:{},label:{}'.format(label_prob, label))
    print('img_id:{},label:{}'.format(img_id, label))
    if img_id == label:
      right_count += 1
    sum_count += 1
  except:
    pass
print('{}/{}'.format(right_count, sum_count))
# 预训练微调，测试集与验证集相同：16/19 14/19
# 预训练微调，测试集与验证集8:2切分：16/19<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15785554728220402, 'fraction_numerical': 0.05547282204020849, 'mean_word_length': 5.3522458628841605, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '539574', 'license': 'MIT', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1116, 'n_words': 161, 'path': 'tests/facenet_finetune_predict.ipynb', 'repo_name': 'q759729997/facenet-pytorch', 'size': '2686'}