我正在try 使用频谱图作为输入,为语音情感识别任务训练一个CNN模型.我已经对 spectral 图进行了reshape ,使其具有我认为足够的形状(num_frequency_bins, num_time_frames, 1),但在try 将模型与存储在Tensorflow数据集中的数据集相匹配时,我得到了以下错误:

Input 0 of layer "sequential_12" is incompatible with the layer: expected shape=(None, 257, 1001, 1), found shape=(257, 1001, 1)

我try 重新塑造 spectral 图,使其形状为(1, num_frequency_bins, num_time_frames, 1),但在创建顺序模型时产生了一个错误:

ValueError: Exception encountered when calling layer "resizing_14" (type Resizing).

'images' must have either 3 or 4 dimensions.

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1, 257, 1001, 1), dtype=float32)

因此,在创建模型时,我将形状传递为(num_frequency_bins, num_time_frames, 1),然后用四维数据将模型与训练数据进行拟合,但这会产生此错误:

InvalidArgumentError: slice index 0 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/

所以我现在有点不知所措.我真的不知道该怎么做,也不知道该如何解决这个问题.我读了很多书,但没有发现任何有用的东西.非常感谢您的帮助.

下面是一些上下文代码.

dataset = [[specgram_files[i], labels[i]] for i in range(len(specgram_files))]
specgram_files_and_labels_dataset = tf.data.Dataset.from_tensor_slices((specgram_files, labels))

def read_npy_file(data):
    # 'data' stores the file name of the numpy binary file storing the features of a particular sound file
    # item() returns numpy array of size 1 as a suitable python scalar.
    # data.item() then returns the bytes string stored in the numpy array.
    # decode() is then called on the bytes string to decode it from a bytes string to a regular string
    # so that it can be passed as a parameter in np.load()
    data = np.load(data.item().decode())
    # Shape of data is now (1, rows, columns)
    # Needs to be reshaped to (rows, columns, 1):
    data = np.reshape(data, (data.shape[0], data.shape[1], 1))
    return data.astype(np.float32)

specgram_dataset = specgram_files_and_labels_dataset.map(
                    lambda file, label: tuple([tf.numpy_function(read_npy_file, [file], [tf.float32]), label]),
                    num_parallel_calls=tf.data.AUTOTUNE)

num_files = len(train_df)
num_train = int(0.8 * num_files)
num_val = int(0.1 * num_files)
num_test = int(0.1 * num_files)

specgram_dataset.shuffle(buffer_size=1000)
specgram_train_ds = specgram_dataset.take(num_train)
specgram_test_ds = specgram_dataset.skip(num_train)
specgram_val_ds = specgram_test_ds.take(num_val)
specgram_test_ds = specgram_test_ds.skip(num_val)

batch_size = 32
specgram_train_ds.batch(batch_size)
specgram_val_ds.batch(batch_size)

specgram_train_ds = specgram_train_ds.cache().prefetch(tf.data.AUTOTUNE)
specgram_val_ds = specgram_val_ds.cache().prefetch(tf.data.AUTOTUNE)

for specgram, label in specgram_train_ds.take(1):
    input_shape = specgram.shape

num_emotions = len(train_df["emotion"].unique())

model = models.Sequential([
    layers.Input(shape=input_shape),
    # downsampling the input. 
    layers.Resizing(32, 128),
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, activation="relu"),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation="softmax"),
    layers.Dense(num_emotions)
])

model.compile(
    optimizer=tf.keras.optimizers.Adam(0.01),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(),
    metrics=["accuracy"]
)

EPOCHS = 10

model.fit(
    specgram_train_ds,
    validation_data=specgram_val_ds,
    epochs=EPOCHS,
    callbacks=tf.keras.callbacks.EarlyStopping(verbose=1, patience=2)
)

推荐答案

假设您知道自己的input_shape,我建议您首先将其硬编码到您的模型中:

model = models.Sequential([
    layers.Input(shape=(257, 1001, 1),
    # downsampling the input. 
    layers.Resizing(32, 128),
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, activation="relu"),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation="softmax"),
    layers.Dense(num_emotions)
])

此外,使用tf.data.Dataset.batch时,应将Dataset输出分配给变量:

batch_size = 32
specgram_train_ds = specgram_train_ds.batch(batch_size)
specgram_val_ds = specgram_val_ds.batch(batch_size)

之后,确保specgram_train_ds的形状正确:

specgrams, _ = next(iter(specgram_train_ds.take(1)))
assert specgrams.shape == (32, 257, 1001, 1)

Python相关问答推荐

如何修复使用turtle和tkinter制作的绘画应用程序的撤销功能

Pandas 在时间序列中设定频率

在matplotlib动画gif中更改配色方案

Odoo -无法比较使用@api.depends设置计算字段的日期

如果索引不存在,pandas系列将通过索引获取值,并填充值

无法使用equals_html从网址获取全文

Polars LazyFrame在收集后未返回指定的模式顺序

运行总计基于多列pandas的分组和总和

Odoo 16使用NTFS使字段只读

在单个对象中解析多个Python数据帧

如何并行化/加速并行numba代码?

用砂箱开发Web统计分析

什么是合并两个embrame的最佳方法,其中一个有日期范围,另一个有日期没有任何共享列?

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

Pandas Data Wrangling/Dataframe Assignment

在Admin中显示从ManyToMany通过模型的筛选结果

Pandas—堆栈多索引头,但不包括第一列

语法错误:文档. evaluate:表达式不是合法表达式

解决Geopandas和Altair中的正图和投影问题

将像素信息写入文件并读取该文件