子豪兄2天完成论文系列之预测图像

1 预测单张图像

1
2
3
4
5
6
7
8
9
10
11
import os

import cv2

import pandas as pd
import numpy as np

import torch

import matplotlib.pyplot as plt
%matplotlib inline

设备

1
2
# 有 GPU 就用 GPU,没有就用 CPU
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
1
print('device', device)
device cuda:0

载入预训练图像分类模型

1
2
3
4
5
import torch
from torch import nn
import torchvision
from torch.nn import functional as F
from efficientnet_pytorch import EfficientNet
1
2
3
4
5
6
7
8
9
10
class Detector(nn.Module):

def __init__(self):
super(Detector, self).__init__()
self.net=EfficientNet.from_pretrained("efficientnet-b4",advprop=True,num_classes=2)


def forward(self,x):
x=self.net(x)
return x
1
2
3
4
5
model=Detector()
model=model.to(device)
cnn_sd=torch.load('weights/FFraw')
model.load_state_dict(cnn_sd)
model.eval()

图像预处理

1
2
3
4
5
import numpy as np
import cv2
from PIL import Image
import sys
from tqdm import tqdm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def extract_face(frame,model,image_size=(380,380)):


faces = model.predict_jsons(frame)

if len(faces)==0:
print('No face is detected' )
return []

croppedfaces=[]
for face_idx in range(len(faces)):
x0,y0,x1,y1=faces[face_idx]['bbox']
bbox=np.array([[x0,y0],[x1,y1]])
croppedfaces.append(cv2.resize(crop_face(frame,None,bbox,False,crop_by_bbox=True,only_img=True,phase='test'),dsize=image_size).transpose((2,0,1)))

return croppedfaces

载入一张测试图片

1
frame = cv2.imread("")
1
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
1
2
3
# 用 pillow 载入
from PIL import Image
img_pil = Image.open(frame)
1
np.array(img_pil).shape

执行分类预测

1

1
2
3
4
from retinaface.pre_trained_models import get_model
face_detector = get_model("resnet50_2020-07-20", max_size=max(frame.shape),device=device)
face_detector.eval()
face_list=extract_face(frame,face_detector)
1
face_list.shape
1
2
3
# img = face_list.unsqueeze(0).to(device)
# 执行前向预测,得到所有类别的 logit 预测分数
#pred_logits = model(input_img)
1
2
3
# pred_logits
#import torch.nn.functional as F
#pred_softmax = F.softmax(pred_logits, dim=1) # 对 logit 分数做 softmax 运算
1
#pred_softmax.shape
1
2
3
4
with torch.no_grad():
img=torch.tensor(face_list).to(device).float()/255
# torchvision.utils.save_image(img, f'test.png', nrow=8, normalize=False, range=(0, 1))
pred=model(img).softmax(1)[:,1].cpu().data.numpy().tolist()
1
pred.shape
1

-------------已经到底啦!-------------