使用 tf.data 和 mode.fit 时 1DConv 输入的维度错误

Sat*_*ngo 5 python neural-network deep-learning keras tensorflow

我正在使用 TensorFlow 2.0.0 并尝试使用 tf.data.Dataset.from_generator() 创建我自己的数据集

这是我的代码:

def trainDatagen():
    for npy in train_list:
        x = tf.convert_to_tensor(np.load(npy), dtype=tf.float32)
        if npy in gbmlist:
             y = to_categorical(0, num_classes=2)
        else:
             y = to_categorical(1, num_classes=2)
        yield x, y

def tfDatasetGen(datagen, output_types, is_training, batch_size):
    dataset = tf.data.Dataset.from_generator(generator=datagen, output_types=output_types)
    if is_training:
        dataset.shuffle(buffer_size=100)
    dataset.repeat()
    dataset.batch(batch_size=batch_size)
    dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
    return dataset

train_set = tfDatasetGen(
    datagen = trainDatagen, 
    output_types = (tf.float32, tf.float32), 
    is_training = True, 
    batch_size = 16)
Run Code Online (Sandbox Code Playgroud)

所有这些 npy 文件都是形状为 [4000,2048] 的 np.array 从具有 4000 个切片的大型病理切片中获得。每个瓦片的特征由 ResNet50 计算。

这是我的模型:

def top_k(inputs, k):
    return tf.nn.top_k(inputs, k=k, sorted=True).values

def least_k(inputs, k):
    return -tf.nn.top_k(-inputs, k=k, sorted=True).values

def minmax_k(inputs, k):
    return tf.concat([top_k(inputs, k), least_k(inputs, k)], axis = -1)

inputs = keras.Input(shape=(4000,2048))
y = layers.Conv1D(1, 2048, use_bias=False, padding='same', data_format='channels_last')(inputs)
y = layers.Flatten()(y)
y = layers.Lambda(minmax_k, arguments={'k': 5})(y)
y = layers.Dense(units=200, activation=tf.nn.relu)(y)
y = layers.Dropout(rate=0.5)(y)
y = layers.Dense(units=100, activation=tf.nn.relu)(y)
y = layers.Dense(units=2, activation=tf.nn.softmax)(y)
model = keras.Model(inputs=inputs, outputs=y)
Run Code Online (Sandbox Code Playgroud)

使用 model.fit() 训练模型时,我收到了这个:

ValueError: Error when checking input: expected input_4 to have 3 dimensions, but got array with shape (4000, 2048)
Run Code Online (Sandbox Code Playgroud)

所有这些想法都来自论文arXiv:1802.02212。这是我试图重现的神经网络图。

CHOWDER架构描述


我按照 Mahsa Hassankashi 的建议将输入重塑为(4000,2048,1)

ValueError: Error when checking input: expected input_4 to have 3 dimensions, but got array with shape (4000, 2048)
Run Code Online (Sandbox Code Playgroud)

并根据GitHub问题修改了这部分以修复错误:

x = tf.convert_to_tensor(np.load(npy).reshape(4000,2048,1), dtype=tf.float32)
Run Code Online (Sandbox Code Playgroud)

但我得到了这个:

InvalidArgumentError:  input and filter must have the same depth: 1 vs 2048
Run Code Online (Sandbox Code Playgroud)

最后我尝试将输入重塑为(1,4000,2048),这次又出现了另一种错误:

InvalidArgumentError:  Expected size[0] in [0, 1], but got 2
Run Code Online (Sandbox Code Playgroud)

Mah*_*shi 1

请查看训练列表,如果卷积神经网络需要二维,请使用:

二维卷积

卷积神经网络中 1D、2D 和 3D 卷积之间的区别(深度学习)

CNN 维度

否则对于最后一个错误:

InvalidArgumentError:  Expected size[0] in [0, 1], but got 2
Run Code Online (Sandbox Code Playgroud)

当生成的元素是张量时,from_generator函数会将其展平为output_types。并且这种转换不会起作用。

解决方案是,当生成器生成张量时,使用from_tensorsfrom_tensor_slices 代替from_generator 。

请测试以下解决方案:

您能测试一下吗:

1.张量流GPU

conda create --name tensorflow
activate tensorflow
pip install tensorflow
pip install tensorflow-gpu
Run Code Online (Sandbox Code Playgroud)

2.Timesteps 根据这个,你的 1d 卷积需要 3 个维度,2d 需要 4 个维度。 在此输入图像描述

input_shape = (timesteps, input_dim)
Run Code Online (Sandbox Code Playgroud)

时间步=1

然后将 X_train 和 X_test 重塑为:

X1_Train = X1_Train.reshape((4000,2048,1))
#call model.fit()
Run Code Online (Sandbox Code Playgroud)

3.使用

model.fit_generator()
Run Code Online (Sandbox Code Playgroud)

4.在最后一次密实之前添加压平。

model.add(Flatten())
Run Code Online (Sandbox Code Playgroud)

keras 卷积层