Spaces:
Sleeping
Sleeping
File size: 3,674 Bytes
5a5e72e | 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 | 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
|