File size: 4,979 Bytes
99848c5 f0622dd 99848c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import os, sys
import torch
import numpy as np
import torchvision
import os
from PIL import Image
from torchvision import transforms
import torch.nn.functional as F
from moviepy.editor import *
import audio
def load_image(filename, size):
img = Image.open(filename).convert('RGB')
img = img.resize((size, size))
img = np.asarray(img)
img = np.transpose(img, (2, 0, 1)) # 3 x 256 x 256
return img / 255.0
def img_preprocessing(img_path, size):
img = load_image(img_path, size) # [0, 1]
img = torch.from_numpy(img).unsqueeze(0).float() # [0, 1]
imgs_norm = (img - 0.5) * 2.0 # [-1, 1]
return imgs_norm
def vid_preprocessing(vid_path):
import av
container = av.open(vid_path)
stream = container.streams.video[0]
fps = float(stream.average_rate)
frames = []
for frame in container.decode(video=0):
frames.append(torch.from_numpy(frame.to_ndarray(format='rgb24')))
container.close()
vid = torch.stack(frames).permute(0, 3, 1, 2).unsqueeze(0) # (1, T, 3, H, W)
vid_norm = (vid / 255.0 - 0.5) * 2.0 # [-1, 1]
transform = transforms.Compose([
transforms.Resize((256, 256)),
])
resized_frames = torch.stack([transform(frame) for frame in vid_norm[0]], dim=0).unsqueeze(0)
return resized_frames, fps
def save_video(vid_target_recon, save_path, fps):
vid = vid_target_recon.permute(0, 2, 3, 4, 1)
vid = vid.clamp(-1, 1).cpu()
vid = ((vid - vid.min()) / (vid.max() - vid.min()) * 255).type('torch.ByteTensor')
import imageio
writer = imageio.get_writer(save_path, fps=fps, codec='libx264', quality=8)
for frame in vid[0]:
writer.append_data(frame.numpy())
writer.close()
def parse_audio_length(audio_length, sr, fps):
bit_per_frames = sr / fps
num_frames = int(audio_length / bit_per_frames)
audio_length = int(num_frames * bit_per_frames)
return audio_length, num_frames
def crop_pad_audio(wav, audio_length):
if len(wav) > audio_length:
wav = wav[:audio_length]
elif len(wav) < audio_length:
wav = np.pad(wav, [0, audio_length - len(wav)], mode='constant', constant_values=0)
return wav
def get_mel(audio_path):
wav = audio.load_wav(audio_path, 16000)
wav_length, num_frames = parse_audio_length(len(wav), 16000, 25)
wav = crop_pad_audio(wav, wav_length)
orig_mel = audio.melspectrogram(wav).T
spec = orig_mel.copy() # nframes 80
indiv_mels = []
fps = 25
syncnet_mel_step_size = 16
for i in range(num_frames):
start_frame_num = i-2
start_idx = int(80. * (start_frame_num / float(fps)))
end_idx = start_idx + syncnet_mel_step_size
seq = list(range(start_idx, end_idx))
seq = [ min(max(item, 0), orig_mel.shape[0]-1) for item in seq ]
m = spec[seq, :]
indiv_mels.append(m.T)
indiv_mels = np.asarray(indiv_mels) # T 80 16
_device = os.environ.get("CMET_DEVICE", "cpu")
indiv_mels = torch.FloatTensor(indiv_mels).unsqueeze(1).unsqueeze(0).to(_device)
source_audio_feature = indiv_mels.type(torch.FloatTensor).to(_device)
mel_input = source_audio_feature # bs T 1 80 16
bs = mel_input.shape[0]
T = mel_input.shape[1]
audiox = mel_input.view(-1, 1, 80, 16) # bs*T 1 80 16
return audiox, bs, T
def audio_preprocessing(wav_path):
source_audio_feature, bs, T = get_mel(wav_path)
return source_audio_feature, bs, T
def conv_feat(features, k_size, weight=None, sigma=1.0):
c = features.shape[1] # torch.Size([101, 500])
if weight is None:
pad = k_size // 2
k = np.zeros(k_size).astype(np.float64)
for x in range(-pad, k_size-pad):
k[x+pad] = np.exp(-x**2 / (2 * (sigma ** 2)))
k = k / k.sum()
print(k) # [0.27406862 0.45186276 0.27406862]
else:
k_size = len(weight)
k = np.array(weight)
pad = k_size // 2
print(k)
k = torch.from_numpy(k).to(features.device).float().unsqueeze(0).unsqueeze(0)
k = k.repeat(c, 1, 1)
features = features.unsqueeze(0).permute(0, 2, 1) # [1, 512, n]
features = F.conv1d(features, k, padding=pad, groups=c)
features = features.permute(0, 2, 1).squeeze(0)
return features
def _load(checkpoint_path, device):
if device == 'cuda':
checkpoint = torch.load(checkpoint_path)
else:
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
return checkpoint
def load_model(model, path, device='cuda'):
print("Load checkpoint from: {}".format(path))
checkpoint = _load(path, device)
s = checkpoint["state_dict"]
new_s = {}
for k, v in s.items():
if k[:6] == 'module':
new_k=k.replace('module.', '', 1)
else:
new_k =k
new_s[new_k] = v
model.load_state_dict(new_s)
model = model.to(device)
return model.eval() |