Spaces:
Sleeping

Speech2Face / s2f /voice_model.py
ProgramComputer's picture
Super-squash branch 'main' using huggingface_hub
5a5e72e
Raw History Blame Contribute Delete
3.67 kB
from __future__ import annotations
from typing import Any
from .audio_features import PAPER_SPECTROGRAM_SHAPE
def speech2face_voice_spatial_shapes(
input_time: int = PAPER_SPECTROGRAM_SHAPE[0],
input_frequency: int = PAPER_SPECTROGRAM_SHAPE[1],
) -> list[tuple[str, int, int]]:
time_steps, frequency_bins = int(input_time), int(input_frequency)
shapes: list[tuple[str, int, int]] = [("input", time_steps, frequency_bins)]
def valid_conv(name: str, kernel: int = 4, stride: int = 1) -> None:
nonlocal time_steps, frequency_bins
time_steps = (time_steps - kernel) // stride + 1
frequency_bins = (frequency_bins - kernel) // stride + 1
if min(time_steps, frequency_bins) <= 0:
raise ValueError(f"{name} collapses spatial dimensions")
shapes.append((name, time_steps, frequency_bins))
def temporal_pool(name: str) -> None:
nonlocal time_steps
time_steps = (time_steps - 2) // 2 + 1
if time_steps <= 0:
raise ValueError(f"{name} collapses the time dimension")
shapes.append((name, time_steps, frequency_bins))
valid_conv("conv1")
valid_conv("conv2")
valid_conv("conv3")
temporal_pool("pool1")
valid_conv("conv4")
temporal_pool("pool2")
valid_conv("conv5")
temporal_pool("pool3")
valid_conv("conv6")
temporal_pool("pool4")
valid_conv("conv7")
valid_conv("conv8", stride=2)
valid_conv("conv9", stride=2)
shapes.append(("temporal_average", 1, frequency_bins))
return shapes
def build_voice_encoder_4096(input_shape: tuple[int, ...] = PAPER_SPECTROGRAM_SHAPE) -> Any:
input_shape = tuple(int(value) for value in input_shape)
if input_shape != PAPER_SPECTROGRAM_SHAPE:
raise ValueError(
f"Paper voice input must be {PAPER_SPECTROGRAM_SHAPE}; got {input_shape}"
)
shape_trace = speech2face_voice_spatial_shapes(input_shape[0], input_shape[1])
temporal_pool_size = shape_trace[-2][1]
try:
from tensorflow.keras import Sequential
from tensorflow.keras.layers import (
AveragePooling2D,
BatchNormalization,
Conv2D,
Dense,
Flatten,
MaxPooling2D,
ReLU,
)
except ImportError as exc:
raise ImportError(
"TensorFlow is required to build the trainable voice encoder; "
"use speech2face_voice_spatial_shapes for shape-only inspection"
) from exc
model = Sequential(name="voice_encoder_speech2face_table1")
def add_conv(filters: int, *, strides: int = 1, first: bool = False) -> None:
kwargs: dict[str, tuple[int, ...]] = {"input_shape": input_shape} if first else {}
model.add(Conv2D(filters, (4, 4), strides=strides, padding="valid", **kwargs))
model.add(ReLU())
model.add(BatchNormalization())
add_conv(64, first=True)
add_conv(64)
add_conv(128)
model.add(MaxPooling2D((2, 1), strides=(2, 1), padding="valid"))
add_conv(128)
model.add(MaxPooling2D((2, 1), strides=(2, 1), padding="valid"))
add_conv(128)
model.add(MaxPooling2D((2, 1), strides=(2, 1), padding="valid"))
add_conv(256)
model.add(MaxPooling2D((2, 1), strides=(2, 1), padding="valid"))
add_conv(512)
add_conv(512, strides=2)
model.add(Conv2D(512, (4, 4), strides=2, padding="valid"))
model.add(AveragePooling2D((temporal_pool_size, 1), strides=(1, 1), padding="valid"))
model.add(ReLU())
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(4096, activation="relu"))
model.add(Dense(4096))
return model