Keras ValueError:形状(256、652)和(256、64)不兼容

时间:2020-07-01 17:20:59

标签: python tensorflow keras neural-network recurrent-neural-network

我正在尝试使用fma-small数据集基于this link实现神经网络。 我完成了.csv和数据集.npy工作,但是当我尝试开始学习时,它给了我

    _________________________________________________________________
Traceback (most recent call last):
  File "C:/music_genre_classification-master/code/rnn_genre_classification.py", line 38, in <module>
    model.load_weights(h5_name)
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 181, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1177, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 699, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3343, in batch_set_value
    x.assign(np.asarray(value, dtype=dtype(x)))
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py", line 814, in assign
    self._shape.assert_is_compatible_with(value_tensor.shape)
  File "C:\miniconda\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1115, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (256, 652) and (256, 64) are incompatible

以下代码是模型定义:

def rnn_classifier(
    d_model=128, n_layers=2, n_classes=16,
):
    inp = Input((None, d_model))

    x = Bidirectional(GRU(d_model, return_sequences=True))(inp)

    if n_classes > 1:
        for i in range(n_layers - 1):
            x = Bidirectional(GRU(d_model, return_sequences=True))(x)

    x = Dropout(0.2)(x)

    x = GlobalAvgPool1D()(x)

    x = Dense(4 * n_classes, activation="selu")(x)

    out = Dense(n_classes, activation="sigmoid")(x)

    model = Model(inputs=inp, outputs=out)

    opt = Adam(0.00001)

    model.compile(
        optimizer=opt, loss=custom_binary_crossentropy, metrics=[custom_binary_accuracy]
    )

    model.summary()

    return model

以下是打印属性:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, None, 128)]       0         
_________________________________________________________________
bidirectional (Bidirectional (None, None, 256)         198144    
_________________________________________________________________
bidirectional_1 (Bidirection (None, None, 256)         296448    
_________________________________________________________________
dropout (Dropout)            (None, None, 256)         0         
_________________________________________________________________
global_average_pooling1d (Gl (None, 256)               0         
_________________________________________________________________
dense (Dense)                (None, 652)               167564    
_________________________________________________________________
dense_1 (Dense)              (None, 163)               106439    
=================================================================
Total params: 768,595
Trainable params: 768,595
Non-trainable params: 0
_________________________________________________________________

除了使用fma_large数据集和一些相对路径编辑外,我完全按照指南进行操作。

我正在使用带有miniconda的pycharm和该项目所需的所有库(tensorflow 2.0.1等)。

上载经过最小组织的代码是正确的,但是出现问题的代码的原始文本如下:

import json
from glob import glob

from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau

from models import rnn_classifier
from prepare_data import get_id_from_path, DataGenerator

if __name__ == "__main__":
    from collections import Counter

    h5_name = "rnn.h5"
    batch_size = 32
    epochs = 50
    CLASS_MAPPING = json.load(open("./media/ml/data_ml/fma_metadata/mapping.json"))
    id_to_genres = json.load(open("./media/ml/data_ml/fma_metadata/tracks_genre.json"))
    id_to_genres = {int(k): v for k, v in id_to_genres.items()}

    base_path = "./media/ml/data_ml/fma_small"
    files = sorted(list(glob(base_path + "/*/*.npy")))
    files = [x for x in files if id_to_genres[int(get_id_from_path(x))]]
    labels = [id_to_genres[int(get_id_from_path(x))] for x in files]
    print(len(labels))

    samples = list(zip(files, labels))

    strat = [a[-1] for a in labels]
    cnt = Counter(strat)
    strat = [a if cnt[a] > 2 else "" for a in strat]

    train, val = train_test_split(
        samples, test_size=0.2, random_state=1337, stratify=strat
    )

    model = rnn_classifier(n_classes=len(CLASS_MAPPING))

    model.load_weights(h5_name)

    checkpoint = ModelCheckpoint(
        h5_name,
        monitor="val_loss",
        verbose=1,
        save_best_only=True,
        mode="min",
        save_weights_only=True,
    )
    reduce_o_p = ReduceLROnPlateau(
        monitor="val_loss", patience=20, min_lr=1e-7, mode="min"
    )

    model.fit_generator(
        DataGenerator(train, batch_size=batch_size, class_mapping=CLASS_MAPPING),
        validation_data=DataGenerator(
            val, batch_size=batch_size, class_mapping=CLASS_MAPPING
        ),
        epochs=epochs,
        callbacks=[checkpoint, reduce_o_p],
        use_multiprocessing=True,
        workers=12,
        verbose=2,
        max_queue_size=64,
    )

0 个答案:

没有答案