Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

import os import cv2 import argparse import glob import torch import pdb import numpy as…

  1. User

    import os import cv2 import argparse import glob import torch import pdb import numpy as np from tqdm import tqdm from torchvision.transforms.functional import normalize from basicsr.utils import imwrite, img2tensor, tensor2img from basicsr.utils.download_util import load_file_from_url from basicsr.utils.misc import gpu_is_available, get_device from scipy.ndimage import gaussian_filter1d from facelib.utils.face_restoration_helper import FaceRestoreHelper from facelib.utils.misc import is_gray from basicsr.utils.video_util import VideoReader, VideoWriter

    from basicsr.utils.registry import ARCH_REGISTRY

    def interpolate_sequence(sequence): interpolated_sequence = np.copy(sequence) missing_indices = np.isnan(sequence)

    if np.any(missing_indices):
        valid_indices = ~missing_indices
        x = np.arange(len(sequence))
    
        # Interpolate missing values using valid data points
        interpolated_sequence[missing_indices] = np.interp(
            x[missing_indices], x[valid_indices], sequence[valid_indices])
    
    return interpolated_sequence
    

    def set_realesrgan(): from basicsr.archs.rrdbnet_arch import RRDBNet from basicsr.utils.realesrgan_utils import RealESRGANer

    use_half = False
    if torch.cuda.is_available():  # set False in CPU/MPS mode
        # set False for GPUs that don't support f16
        no_half_gpu_list = ['1650', '1660']
        if not True in [gpu in torch.cuda.get_device_name(0) for gpu in no_half_gpu_list]:
            use_half = True
    
    model = RRDBNet(
        num_in_ch=3,
        num_out_ch=3,
        num_feat=64,
        num_block=23,
        num_grow_ch=32,
        scale=2,
    )
    upsampler = RealESRGANer(
        scale=2,
        model_path="https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth",
        # model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
        model=model,
        tile=args.bg_tile,
        tile_pad=40,
        pre_pad=0,
        half=use_half
    )
    
    if not gpu_is_available():  # CPU
        import warnings
        warnings.warn('Running on CPU now! Make sure your PyTorch version matches your CUDA.'
                      'The unoptimized RealESRGAN is slow on CPU. '
                      'If you want to disable it, please remove `--bg_upsampler` and `--face_upsample` in command.',
                      category=RuntimeWarning)
    return upsampler
    

    python inference_keep.py -i=../dataset/real_LQ/1To-NNjRvRU_0 -o=results/ --draw_box --save_video

    if name == 'main': device = get_device() parser = argparse.ArgumentParser()

    parser.add_argument('-i', '--input_path', type=str, default='../dataset/real_LQ/',
                        help='Input image, video or folder. Default: inputs/whole_imgs')
    parser.add_argument('-o', '--output_path', type=str, default='results/',
                        help='Output folder. Default: results/')
    parser.add_argument('--save_video', action='store_true',
                        help='Save output as video. Default: False')
    parser.add_argument('-s', '--upscale', type=int, default=1,
                        help='The final upsampling scale of the image. Default: 1')
    parser.add_argument('--max_length', type=int, default=20,
                        help='Max length of per sub-clip depending of GPU memory. Default: 20')
    parser.add_argument('--has_aligned', action='store_true',
                        help='Input are cropped and aligned faces. Default: False')
    parser.add_argument('--only_center_face', type=bool, default=True,
                        help='Only restore the center face. Default: True')
    parser.add_argument('--draw_box', action='store_true',default=False,
                        help='Draw the bounding box for the detected faces. Default: False')
    # large det_model: 'YOLOv5l', 'retinaface_resnet50'
    # small det_model: 'YOLOv5n', 'retinaface_mobile0.25'
    parser.add_argument('--detection_model', type=str, default='YOLOv5l',
                        help='Face detector. Optional: retinaface_resnet50, retinaface_mobile0.25, YOLOv5l, YOLOv5n, dlib. \
                Default: retinaface_resnet50')
    parser.add_argument('--bg_upsampler', type=str, default='None',
                        help='Background upsampler. Optional: realesrgan')
    parser.add_argument('--face_upsample', action='store_true',
                        help='Face upsampler after enhancement. Default: False')
    parser.add_argument('--bg_tile', type=int, default=400,
                        help='Tile size for background sampler. Default: 400')
    parser.add_argument('--suffix', type=str, default=None,
                        help='Suffix of the restored faces. Default: None')
    parser.add_argument('--save_video_fps', type=float, default=25,
                        help='Frame rate for saving video. Default: 25')
    
    args = parser.parse_args()
    input_video = False
    
    # ------------------ set up background upsampler ------------------
    if args.bg_upsampler == 'realesrgan':
        bg_upsampler = set_realesrgan()
    else:
        bg_upsampler = None
    
    # ------------------ set up face upsampler ------------------
    if args.face_upsample:
        if bg_upsampler is not None:
            face_upsampler = bg_upsampler
        else:
            face_upsampler = set_realesrgan()
    else:
        face_upsampler = None
    
    # ------------------ set up restorer -------------------
    device="cuda:0"
    net = ARCH_REGISTRY.get('KEEP')(img_size=512, emb_dim=256, dim_embd=512,
                    n_head=8, n_layers=9, codebook_size=1024, connect_list=['16', '32', '64'],
                    flow_type='gmflow', flownet_path=None,
                    kalman_attn_head_dim=48, num_uncertainty_layers=3, cross_fuse_list=['16', '32'],
                    cross_fuse_nhead=4, cross_fuse_dim=256).to(device)
    
    # ckpt_path = 'weights/KEEP/KEEP-a1a14d46.pth'
    # checkpoint = torch.load(ckpt_path)['params_ema']
    
    ckpt_path = load_file_from_url(
        url='https://github.com/jnjaby/KEEP/releases/download/v0.1.0/KEEP-a1a14d46.pth',
        model_dir='weights/KEEP', progress=True, file_name=None)
    #checkpoint = torch.load(ckpt_path)
    state_dict = torch.load(ckpt_path)
    model_state_dict = net.state_dict()
    filtered_state_dict = {k: v for k, v in state_dict.items() if k in model_state_dict and v.size() == model_state_dict[k].size()}
    net.load_state_dict(filtered_state_dict, strict=False)
    #net.load_state_dict(checkpoint)
    net.eval()
    
    # ------------------ set up FaceRestoreHelper -------------------
    # large det_model: 'YOLOv5l', 'retinaface_resnet50'
    # small det_model: 'YOLOv5n', 'retinaface_mobile0.25'
    if not args.has_aligned:
        print(f'Face detection model: {args.detection_model}')
    if bg_upsampler is not None:
        print(
            f'Background upsampling: True, Face upsampling: {args.face_upsample}')
    else:
        print(
            f'Background upsampling: False, Face upsampling: {args.face_upsample}')
    
    face_helper = FaceRestoreHelper(
        args.upscale,
        face_size=512,
        crop_ratio=(1, 1),
        det_model=args.detection_model,
        save_ext='png',
        use_parse=True,
        device=device)
    
    # -------------------- start processing ---------------------
    input_img_list = []
    
    if args.input_path.endswith(('mp4', 'mov', 'avi', 'MP4', 'MOV', 'AVI')): # input video path
        vidreader = VideoReader(args.input_path)
        image = vidreader.get_frame()
        while image is not None:
            input_img_list.append(image)
            image = vidreader.get_frame()
        fps = vidreader.get_fps() if args.save_video_fps is None else args.save_video_fps   
        vidreader.close()
    
        clip_name = os.path.basename(args.input_path)[:-4]
        result_root = os.path.join(args.output_path, clip_name)
    
    elif os.path.isdir(args.input_path): # input img folder
        # scan all the jpg and png images
        for img_path in sorted(glob.glob(os.path.join(args.input_path, '*.[jpJP][pnPN]*[gG]'))):
            input_img_list.append(cv2.imread(img_path))
        clip_name = os.path.basename(args.input_path)
        result_root = os.path.join(args.output_path, clip_name)
    
    else:
        raise TypeError(f'Unrecognized type of input video {args.input_path}.')
    
    
    if len(input_img_list) == 0:
        raise FileNotFoundError('No input image/video is found...\n'
                                '\tNote that --input_path for video should end with .mp4|.mov|.avi')
    
    # Smoothing aligned landmarks
    print('Detecting keypoints and smooth alignment ...')
    if not args.has_aligned:
        raw_landmarks = []
        for i, img in enumerate(input_img_list):
            # clean all the intermediate results to process the next image
            face_helper.clean_all()
            face_helper.read_image(img)
    
            # get face landmarks for each face
            num_det_faces = face_helper.get_face_landmarks_5(
                only_center_face=args.only_center_face, resize=640, eye_dist_threshold=5,
                only_keep_largest=True)
    
            if num_det_faces == 1:
                raw_landmarks.append(
                    face_helper.all_landmarks_5[0].reshape((10,)))
            elif num_det_faces == 0:
                raw_landmarks.append(np.array([np.nan]*10))
    
        raw_landmarks = np.array(raw_landmarks)
        for i in range(10):
            raw_landmarks[:, i] = interpolate_sequence(raw_landmarks[:, i])
        video_length = len(input_img_list)
        avg_landmarks = gaussian_filter1d(
            raw_landmarks, 5, axis=0).reshape(video_length, 5, 2)
    
    # Pack cropped faces.
    cropped_faces = []
    for i, img in enumerate(input_img_list):
        # clean all the intermediate results to process the next image
        face_helper.clean_all()
        face_helper.read_image(img)
        face_helper.all_landmarks_5 = [avg_landmarks[i]]
        face_helper.align_warp_face()
    
        cropped_face_t = img2tensor(
            face_helper.cropped_faces[0] / 255., bgr2rgb=True, float32=True)
        normalize(cropped_face_t, (0.5, 0.5, 0.5),
                    (0.5, 0.5, 0.5), inplace=True)
        cropped_faces.append(cropped_face_t)
    cropped_faces = torch.stack(
        cropped_faces, dim=0).unsqueeze(0).to(device)
    
    print('Restoring faces ...')
    with torch.no_grad():
        video_length = cropped_faces.shape[1]
        output = []
        for start_idx in range(0, video_length, args.max_length):
            end_idx = min(start_idx + args.max_length, video_length)
            if end_idx - start_idx == 1:
                output.append(net(
                    cropped_faces[:, [start_idx, start_idx], ...], need_upscale=False)[:, 0:1, ...])
            else:
                output.append(net(
                    cropped_faces[:, start_idx:end_idx, ...], need_upscale=False))
        output = torch.cat(output, dim=1).squeeze(0)
        assert output.shape[0] == video_length, "Differer number of frames"
    
        restored_faces = [tensor2img(
            x, rgb2bgr=True, min_max=(-1, 1)) for x in output]
        del output
        torch.cuda.empty_cache()
    
    print('Pasting faces back ...')
    
    for i, img in enumerate(input_img_list):
        # clean all the intermediate results to process the next image
        face_helper.clean_all()
    
        if args.has_aligned:
            # the input faces are already cropped and aligned
            img = cv2.resize(
                img, (512, 512), interpolation=cv2.INTER_LINEAR)
            face_helper.is_gray = is_gray(img, threshold=10)
            if face_helper.is_gray:
                print('Grayscale input: True')
            face_helper.cropped_faces = [img]
        else:
            # align and warp each face
            face_helper.read_image(img)
            face_helper.all_landmarks_5 = [avg_landmarks[i]]
            face_helper.align_warp_face()
    
        face_helper.add_restored_face(restored_faces[i].astype('uint8'))
    
        # paste_back
        if not args.has_aligned:
            # upsample the background
            if bg_upsampler is not None:
                # Now only support RealESRGAN for upsampling background
                bg_img = bg_upsampler.enhance(
                    img, outscale=args.upscale)[0]
            else:
                bg_img = None
            face_helper.get_inverse_affine(None)
            # paste each restored face to the input image
            if args.face_upsample and face_upsampler is not None:
                restored_img = face_helper.paste_faces_to_input_image(
                    upsample_img=bg_img, draw_box=args.draw_box, face_upsampler=face_upsampler)
            else:
                restored_img = face_helper.paste_faces_to_input_image(
                    upsample_img=bg_img, draw_box=args.draw_box)
    
        # save faces
        save_face_name = f'{i:08d}.png'
    
        for face_idx, (cropped_face, restored_face) in enumerate(zip(face_helper.cropped_faces, face_helper.restored_faces)):
            # Only support restoring the center faces for now.
            # save cropped face
            if not args.has_aligned:
                save_crop_path = os.path.join(
                    result_root, 'cropped_faces', save_face_name)
                imwrite(cropped_face, save_crop_path)
            # save restored face
            save_restore_path = os.path.join(
                result_root, 'restored_faces', save_face_name)
            imwrite(restored_face, save_restore_path)
    
        # save restored img
        if not args.has_aligned and restored_img is not None:
            save_restore_path = os.path.join(
                result_root, 'final_results', save_face_name)
            imwrite(restored_img, save_restore_path)
    
    # save enhanced video
    if args.save_video:
        print('Saving video ...')
        # load images
        video_frames = []
        img_list = sorted(glob.glob(os.path.join(
            result_root, 'final_results', '*.[jp][pn]g')))
        for img_path in img_list:
            img = cv2.imread(img_path)
            video_frames.append(img)
        # write images to video
        height, width = video_frames[0].shape[:2]
        save_restore_path = os.path.join(result_root, f'{clip_name}.mp4')
        vidwriter = VideoWriter(
            save_restore_path, height, width, args.save_video_fps)
    
        for f in video_frames:
            vidwriter.write_frame(f)
        vidwriter.close()
    
    print(f'\nAll results are saved in {result_root}')   让这个性能根好,结果也更好
    
  2. Assistant

    要提高您提供的脚本的性能和增强结果质量,可以从以下几个方面入手:

    1. 模型优化与加载
    2. 批处理和并行化
    3. GPU 资源的高效利用
    4. 代码优化
    5. 结果质量提升

    以下将详细介绍每个方面的改进方法,并提供优化后的代码示例。


    1. 模型优化与加载

    a. 使用半精度(FP16)加速

    在确保GPU支持半精度的情况下,使用FP16可以显著提高推理速度并减少显存占用。

    b. 模型缓存和重复使用

    避免在循环内重复加载模型,确保模型只加载一次并在整个处理过程中重用。

    c. 使用优化过的模型

    确保使用的模型版本是经过优化的,如torchscript 或 ONNX 格式的优化模型,能够在推理时提供更快的速度。

    2. 批处理和并行化

    a. 批处理图像

    而不是逐帧处理图像,可以将多个图像打包成批次进行处理,以充分利用GPU的并行计算能力。

    b. 多线程数据加载

    使用多线程或异步数据加载来减少I/O瓶颈,确保GPU在处理时不被数据加载阻塞。

    3. GPU 资源的高效利用

    a. 避免不必要的GPU-CPU 之间的数据传输

    尽量在GPU上完成所有计算,减少数据在设备间传输的次数。

    b. 内存管理

    确保在处理完一批数据后及时释放不需要的GPU内存,避免显存泄漏。

    4. 代码优化

    a. 避免重复计算

    检查循环内是否有可以提前计算或移出的操作,减少不必要的计算。

    b. 使用高效的数据结构

    替换低效的数据结构(如列表操作)为更高效的数据结构(如NumPy数组或Tensor操作)。

    c. 利用矢量化操作

    使用矢量化操作替代Python的for循环,以加快数据处理速度。

    5. 结果质量提升

    a. 更好的对齐与平滑

    优化关键点检测与平滑算法,以获得更精确的面部对齐。

    b. 高质量的上采样

    使用更高质量或更适合的上采样模型,确保增强后的图像细节更丰富。

    c. 参数调优

    根据具体数据集调整模型参数,如检测模型的阈值、平滑的高斯滤波器的核大小等。


    优化后的代码示例

    以下是根据上述优化方法调整后的代码示例。请注意,这只是一个示例,具体的优化需要根据实际情况和需求进行调整。

    import os
    import cv2
    import argparse
    import glob
    import torch
    import numpy as np
    from tqdm import tqdm
    from torchvision.transforms.functional import normalize
    from basicsr.utils import imwrite, img2tensor, tensor2img
    from basicsr.utils.download_util import load_file_from_url
    from basicsr.utils.misc import gpu_is_available, get_device
    from scipy.ndimage import gaussian_filter1d
    from facelib.utils.face_restoration_helper import FaceRestoreHelper
    from facelib.utils.misc import is_gray
    from basicsr.utils.video_util import VideoReader, VideoWriter
    from basicsr.utils.registry import ARCH_REGISTRY
    from torch.cuda.amp import autocast
    
    def interpolate_sequence(sequence):
        interpolated_sequence = np.copy(sequence)
        missing_indices = np.isnan(sequence)
    
        if np.any(missing_indices):
            valid_indices = ~missing_indices
            x = np.arange(len(sequence))
    
            # Interpolate missing values using valid data points
            interpolated_sequence[missing_indices] = np.interp(
                x[missing_indices], x[valid_indices], sequence[valid_indices]
            )
    
        return interpolated_sequence
    
    def set_realesrgan(args):
        from basicsr.archs.rrdbnet_arch import RRDBNet
        from basicsr.utils.realesrgan_utils import RealESRGANer
    
        use_half = False
        if torch.cuda.is_available():  # set False in CPU/MPS mode
            # set False for GPUs that don't support f16
            no_half_gpu_list = ['1650', '1660']
            if not any(
                gpu in torch.cuda.get_device_name(0) for gpu in no_half_gpu_list
            ):
                use_half = True
    
        model = RRDBNet(
            num_in_ch=3,
            num_out_ch=3,
            num_feat=64,
            num_block=23,
            num_grow_ch=32,
            scale=2,
        )
        upsampler = RealESRGANer(
            scale=2,
            model_path="https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth",
            # model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
            model=model,
            tile=args.bg_tile,
            tile_pad=40,
            pre_pad=0,
            half=use_half
        )
    
        if not gpu_is_available():  # CPU
            import warnings
            warnings.warn(
                'Running on CPU now! Ensure your PyTorch version matches your CUDA.'
                'The unoptimized RealESRGAN is slow on CPU. '
                'To disable, remove `--bg_upsampler` and `--face_upsample` in command.',
                category=RuntimeWarning
            )
        return upsampler
    
    def main():
        device = get_device()
        parser = argparse.ArgumentParser()
    
        parser.add_argument('-i', '--input_path', type=str, default='../dataset/real_LQ/',
                            help='Input image, video or folder. Default: inputs/whole_imgs')
        parser.add_argument('-o', '--output_path', type=str, default='results/',
                            help='Output folder. Default: results/')
        parser.add_argument('--save_video', action='store_true',
                            help='Save output as video. Default: False')
        parser.add_argument('-s', '--upscale', type=int, default=1,
                            help='The final upsampling scale of the image. Default: 1')
        parser.add_argument('--max_length', type=int, default=20,
                            help='Max length of per sub-clip depending on GPU memory. Default: 20')
        parser.add_argument('--has_aligned', action='store_true',
                            help='Input are cropped and aligned faces. Default: False')
        parser.add_argument('--only_center_face', type=bool, default=True,
                            help='Only restore the center face. Default: True')
        parser.add_argument('--draw_box', action='store_true', default=False,
                            help='Draw the bounding box for the detected faces. Default: False')
        # large det_model: 'YOLOv5l', 'retinaface_resnet50'
        # small det_model: 'YOLOv5n', 'retinaface_mobile0.25'
        parser.add_argument('--detection_model', type=str, default='YOLOv5l',
                            help='Face detector. Options: retinaface_resnet50, retinaface_mobile0.25, YOLOv5l, YOLOv5n, dlib. '
                                 'Default: YOLOv5l')
        parser.add_argument('--bg_upsampler', type=str, default='None',
                            help='Background upsampler. Options: realesrgan')
        parser.add_argument('--face_upsample', action='store_true',
                            help='Face upsampler after enhancement. Default: False')
        parser.add_argument('--bg_tile', type=int, default=400,
                            help='Tile size for background sampler. Default: 400')
        parser.add_argument('--suffix', type=str, default=None,
                            help='Suffix of the restored faces. Default: None')
        parser.add_argument('--save_video_fps', type=float, default=25,
                            help='Frame rate for saving video. Default: 25')
    
        args = parser.parse_args()
        input_video = False
    
        # ------------------ set up background upsampler ------------------
        if args.bg_upsampler == 'realesrgan':
            bg_upsampler = set_realesrgan(args)
        else:
            bg_upsampler = None
    
        # ------------------ set up face upsampler ------------------
        if args.face_upsample:
            if bg_upsampler is not None:
                face_upsampler = bg_upsampler
            else:
                face_upsampler = set_realesrgan(args)
        else:
            face_upsampler = None
    
        # ------------------ set up restorer -------------------
        net = ARCH_REGISTRY.get('KEEP')(img_size=512, emb_dim=256, dim_embd=512,
                                        n_head=8, n_layers=9, codebook_size=1024, connect_list=['16', '32', '64'],
                                        flow_type='gmflow', flownet_path=None,
                                        kalman_attn_head_dim=48, num_uncertainty_layers=3, cross_fuse_list=['16', '32'],
                                        cross_fuse_nhead=4, cross_fuse_dim=256).to(device)
    
        # 使用AMP进行混合精度加载模型
        ckpt_path = load_file_from_url(
            url='https://github.com/jnjaby/KEEP/releases/download/v0.1.0/KEEP-a1a14d46.pth',
            model_dir='weights/KEEP', progress=True, file_name=None
        )
        state_dict = torch.load(ckpt_path, map_location=device)
        model_state_dict = net.state_dict()
        filtered_state_dict = {k: v for k, v in state_dict.items() if k in model_state_dict and v.size() == model_state_dict[k].size()}
        net.load_state_dict(filtered_state_dict, strict=False)
        net.eval()
        net.half() if device.type == 'cuda' else net.float()  # 使用FP16,如果支持的话
    
        # ------------------ set up FaceRestoreHelper -------------------
        if not args.has_aligned:
            print(f'Face detection model: {args.detection_model}')
        if bg_upsampler is not None:
            print(f'Background upsampling: True, Face upsampling: {args.face_upsample}')
        else:
            print(f'Background upsampling: False, Face upsampling: {args.face_upsample}')
    
        face_helper = FaceRestoreHelper(
            args.upscale,
            face_size=512,
            crop_ratio=(1, 1),
            det_model=args.detection_model,
            save_ext='png',
            use_parse=True,
            device=device
        )
    
        # -------------------- start processing ---------------------
        input_img_list = []
    
        if args.input_path.endswith(('mp4', 'mov', 'avi', 'MP4', 'MOV', 'AVI')):  # input video path
            vidreader = VideoReader(args.input_path)
            image = vidreader.get_frame()
            pbar = tqdm(total=vidreader.total_frames, desc='Reading video frames')
            while image is not None:
                input_img_list.append(image)
                image = vidreader.get_frame()
                pbar.update(1)
            pbar.close()
            fps = vidreader.get_fps() if args.save_video_fps is None else args.save_video_fps
            vidreader.close()
    
            clip_name = os.path.basename(args.input_path)[:-4]
            result_root = os.path.join(args.output_path, clip_name)
    
        elif os.path.isdir(args.input_path):  # input img folder
            img_paths = sorted(glob.glob(os.path.join(args.input_path, '*.[jpJP][pnPN]*[gG]')))
            pbar = tqdm(total=len(img_paths), desc='Reading images')
            for img_path in img_paths:
                img = cv2.imread(img_path)
                input_img_list.append(img)
                pbar.update(1)
            pbar.close()
            clip_name = os.path.basename(args.input_path)
            result_root = os.path.join(args.output_path, clip_name)
    
        else:
            raise TypeError(f'Unrecognized type of input video {args.input_path}.')
    
        if not input_img_list:
            raise FileNotFoundError('No input image/video is found...\n'
                                    '\tNote that --input_path for video should end with .mp4|.mov|.avi')
    
        os.makedirs(result_root, exist_ok=True)
        os.makedirs(os.path.join(result_root, 'cropped_faces'), exist_ok=True)
        os.makedirs(os.path.join(result_root, 'restored_faces'), exist_ok=True)
        if not args.has_aligned:
            os.makedirs(os.path.join(result_root, 'final_results'), exist_ok=True)
    
        # Smoothing aligned landmarks
        print('Detecting keypoints and smoothing alignment ...')
        if not args.has_aligned:
            raw_landmarks = []
            pbar = tqdm(total=len(input_img_list), desc='Detecting faces')
            for img in input_img_list:
                face_helper.clean_all()
                face_helper.read_image(img)
    
                # get face landmarks for each face
                num_det_faces = face_helper.get_face_landmarks_5(
                    only_center_face=args.only_center_face, resize=640, eye_dist_threshold=5,
                    only_keep_largest=True
                )
    
                if num_det_faces == 1:
                    raw_landmarks.append(face_helper.all_landmarks_5[0].reshape((10,)))
                else:
                    raw_landmarks.append(np.array([np.nan]*10))
                pbar.update(1)
            pbar.close()
    
            raw_landmarks = np.array(raw_landmarks)
            for i in range(10):
                raw_landmarks[:, i] = interpolate_sequence(raw_landmarks[:, i])
    
            video_length = len(input_img_list)
            avg_landmarks = gaussian_filter1d(
                raw_landmarks, 5, axis=0
            ).reshape(video_length, 5, 2)
    
        # Pack cropped faces.
        print('Cropping and preparing faces...')
        cropped_faces = []
        pbar = tqdm(total=len(input_img_list), desc='Cropping faces')
        for i, img in enumerate(input_img_list):
            face_helper.clean_all()
            face_helper.read_image(img)
            face_helper.all_landmarks_5 = [avg_landmarks[i]] if not args.has_aligned else face_helper.all_landmarks_5
    
            face_helper.align_warp_face()
    
            cropped_face_t = img2tensor(
                face_helper.cropped_faces[0] / 255., bgr2rgb=True, float32=True
            )
            cropped_face_t = normalize(cropped_face_t, (0.5, 0.5, 0.5),
                                       (0.5, 0.5, 0.5), inplace=True)
            cropped_faces.append(cropped_face_t)
            pbar.update(1)
        pbar.close()
    
        cropped_faces = torch.stack(cropped_faces, dim=0).to(device)
        cropped_faces = cropped_faces.half() if device.type == 'cuda' else cropped_faces.float()
    
        print('Restoring faces ...')
        restored_faces = []
        batch_size = args.max_length  # 根据GPU内存调整批量大小
        pbar = tqdm(total=math.ceil(len(input_img_list)/batch_size), desc='Restoring faces')
        with torch.no_grad():
            for start_idx in range(0, len(input_img_list), batch_size):
                end_idx = min(start_idx + batch_size, len(input_img_list))
                batch = cropped_faces[start_idx:end_idx].unsqueeze(0)  # Shape: [1, batch_size, C, H, W]
    
                with autocast(device.type == 'cuda'):
                    output = net(batch, need_upscale=False)
    
                output = output.squeeze(0).cpu()
                if device.type == 'cuda':
                    torch.cuda.empty_cache()
    
                restored_batch = [tensor2img(x, rgb2bgr=True, min_max=(-1, 1)) for x in output]
                restored_faces.extend(restored_batch)
                pbar.update(1)
        pbar.close()
    
        print('Pasting faces back ...')
        pbar = tqdm(total=len(input_img_list), desc='Pasting faces')
        for i, img in enumerate(input_img_list):
            face_helper.clean_all()
    
            if args.has_aligned:
                img = cv2.resize(
                    img, (512, 512), interpolation=cv2.INTER_LINEAR
                )
                face_helper.is_gray = is_gray(img, threshold=10)
                if face_helper.is_gray:
                    print('Grayscale input: True')
                face_helper.cropped_faces = [img]
            else:
                face_helper.read_image(img)
                face_helper.all_landmarks_5 = [avg_landmarks[i]]
                face_helper.align_warp_face()
    
            face_helper.add_restored_face(restored_faces[i].astype('uint8'))
    
            restored_img = None
            if not args.has_aligned:
                # upsample the background
                if bg_upsampler is not None:
                    bg_img = bg_upsampler.enhance(
                        img, outscale=args.upscale
                    )[0]
                else:
                    bg_img = None
                face_helper.get_inverse_affine(None)
                # paste each restored face to the input image
                if args.face_upsample and face_upsampler is not None:
                    restored_img = face_helper.paste_faces_to_input_image(
                        upsample_img=bg_img, draw_box=args.draw_box, face_upsampler=face_upsampler
                    )
                else:
                    restored_img = face_helper.paste_faces_to_input_image(
                        upsample_img=bg_img, draw_box=args.draw_box
                    )
    
            # save cropped face
            if not args.has_aligned:
                save_crop_path = os.path.join(
                    result_root, 'cropped_faces', f'{i:08d}.png'
                )
                imwrite(face_helper.cropped_faces[0], save_crop_path)
    
            # save restored face
            save_restore_path = os.path.join(
                result_root, 'restored_faces', f'{i:08d}.png'
            )
            imwrite(restored_faces[i], save_restore_path)
    
            # save restored img
            if not args.has_aligned and restored_img is not None:
                save_final_path = os.path.join(
                    result_root, 'final_results', f'{i:08d}.png'
                )
                imwrite(restored_img, save_final_path)
    
            pbar.update(1)
        pbar.close()
    
        # save enhanced video
        if args.save_video:
            print('Saving video ...')
            video_frames = []
            img_list = sorted(glob.glob(os.path.join(result_root, 'final_results', '*.[jp][pn]g')))
            pbar = tqdm(total=len(img_list), desc='Saving video frames')
            for img_path in img_list:
                img = cv2.imread(img_path)
                video_frames.append(img)
                pbar.update(1)
            pbar.close()
    
            if video_frames:
                height, width = video_frames[0].shape[:2]
                save_video_path = os.path.join(result_root, f'{clip_name}.mp4')
                vidwriter = VideoWriter(
                    save_video_path, height, width, args.save_video_fps
                )
    
                for frame in tqdm(video_frames, desc='Writing video'):
                    vidwriter.write_frame(frame)
                vidwriter.close()
    
        print(f'\nAll results are saved in {result_root}')
    
    if __name__ == '__main__':
        import math
        main()
    

    详细优化说明

    1. 使用混合精度(AMP)

    通过引入 torch.cuda.amp.autocast(),可以在保证结果质量的前提下,利用半精度加速模型推理。

    from torch.cuda.amp import autocast
    
    # 在推理过程中使用autocast
    with autocast(device.type == 'cuda'):
        output = net(batch, need_upscale=False)
    

    2. 进度条显示

    使用 tqdm 为不同的处理阶段提供进度条,增强用户体验。

    3. 批量处理

    将图像分批处理,确保在GPU资源允许的情况下尽可能多地并行处理,提高吞吐量。

    batch_size = args.max_length  # 根据GPU内存调整批量大小
    for start_idx in range(0, len(input_img_list), batch_size):
        end_idx = min(start_idx + batch_size, len(input_img_list))
        batch = cropped_faces[start_idx:end_idx].unsqueeze(0)  # Shape: [1, batch_size, C, H, W]
        # 推理
    

    4. 高效的内存管理

    在处理完每一批数据后,及时清理不再需要的变量并清空缓存,防止显存溢出。

    del output
    torch.cuda.empty_cache()
    

    5. 并行化数据加载

    使用 tqdm 的并行读取和处理图像,减少I/O等待时间。

    6. 文件夹创建

    确保所有需要的文件夹在开始处理前已经创建,避免在循环内连续创建文件夹带来的性能开销。

    os.makedirs(result_root, exist_ok=True)
    os.makedirs(os.path.join(result_root, 'cropped_faces'), exist_ok=True)
    os.makedirs(os.path.join(result_root, 'restored_faces'), exist_ok=True)
    if not args.has_aligned:
        os.makedirs(os.path.join(result_root, 'final_results'), exist_ok=True)
    

    7. 减少不必要的复制

    避免在不必要的地方复制图像数据,以节省内存和提高速度。例如,在处理对齐的面部图像时,直接使用已调整大小的图像。

    8. 优化关键点检测和对齐

    确保关键点检测和对齐步骤高效,必要时可以使用更高效的检测模型或减少检测的分辨率来加快速度。


    结论

    通过以上的优化,您的脚本在性能和结果质量方面都将有所提升。具体的优化效果取决于硬件环境和输入数据的特性,建议在实际应用中根据需要进行微调和测试。此外,保持所使用的库和模型的更新,以利用最新的性能改进和功能增强,也是提升整体性能和结果质量的重要手段。