| 178 | ################ Condition Encoders ################ |
| 179 | class AudioEncoder(BaseModel): |
| 180 | def __init__(self, opt): |
| 181 | super().__init__() |
| 182 | self.opt = opt |
| 183 | self.only_last_features = opt.only_last_features |
| 184 | |
| 185 | self.num_frames_for_clip = int(opt.wav2vec_sec * self.opt.fps) |
| 186 | self.num_prev_frames = int(opt.num_prev_frames) |
| 187 | |
| 188 | self.wav2vec2 = Wav2VecModel.from_pretrained(opt.wav2vec_model_path, local_files_only = True) |
| 189 | self.wav2vec2.feature_extractor._freeze_parameters() |
| 190 | |
| 191 | for name, param in self.wav2vec2.named_parameters(): |
| 192 | param.requires_grad = False |
| 193 | |
| 194 | audio_input_dim = 768 if opt.only_last_features else 12 * 768 |
| 195 | |
| 196 | self.audio_projection = nn.Sequential( |
| 197 | nn.Linear(audio_input_dim, opt.dim_w), |
| 198 | nn.LayerNorm(opt.dim_w), |
| 199 | nn.SiLU() |
| 200 | ) |
| 201 | |
| 202 | def get_wav2vec2_feature(self, a: torch.Tensor, seq_len:int) -> torch.Tensor: |
| 203 | a = self.wav2vec2(a, seq_len=seq_len, output_hidden_states = not self.only_last_features) |