首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >训练3 3dconv神经网络失败,损失在.6931收敛

训练3 3dconv神经网络失败,损失在.6931收敛
EN

Stack Overflow用户
提问于 2020-08-29 01:46:36
回答 2查看 157关注 0票数 1

我编写了一个脚本来训练神经网络使用.nii文件作为输入,使用来自TensorFlow这里的https://www.tensorflow.org/tutorials/load_data/images教程。我稍微修改了它,以处理NiBabel和.nii文件,但它仍然遵循相同的基本结构。然而,我遇到了一个问题,我的损失收敛到0.6931,我假设这是因为模型开始猜测相同的事情,不管输入,图像形状或批处理大小。因此,我相信这个模式并不是学习。有人能识别出我的代码中有什么致命的缺陷吗?我已经累了:

使用不同的优化器和丢失functions

  • Using (简单、密集、密集的模型)更改LR

  • 、清理数据和重组数据的
  • 回调(
  • ),改变每个类
  • 数量的比例,但这似乎不起作用,因为它甚至不想开始training
  • Using重复数据集和固定大小(虽然我不清楚这有什么不同)

F 214

代码语言:javascript
复制
# Gets the label of the image, the label determines how tensorflow will classify the image
def get_label(file_path):
    # Convert the path to a list of path components
    parts = tf.strings.split(file_path, os.path.sep)
    # The fourth last is the class-directory
    return float(parts[-4] == "class1")


# Reads the data from a .nii file and returns a NumPy ndarray that is compatible with tensorflow
def decode_img(img):
    img = nib.load(img.numpy().decode('utf-8'))
    # convert the compressed string to a NumPy ndarray
    data = img.get_fdata()
    # Resize img
    data = np.resize(data, imgshape)
    # Normalize
    max = np.amax(data)
    min = np.amin(data)
    data = ((data-min)/(max-min))
    return data


# Processes a path to return a image data and label pair
def process_path(file_path):
    # Gets the files label
    label = get_label(file_path)
    img = decode_img(file_path)
    return img, label

我使用这些函数来处理我的数据,并将它映射到我的列表文件数据集上来处理我的数据。

代码语言:javascript
复制
def configure_for_performance(ds):
    #ds = ds.cache(filename='cachefile')
    ds = ds.cache()
    ds = ds.shuffle(buffer_size=1000)
    ds = ds.repeat()
    ds = ds.batch(BATCH_SIZE)
    ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
    return ds

我直接从TensorFlow教程中获得了这个结果。

代码语言:javascript
复制
# Create a sequential network
model = tf.keras.Sequential([
    tf.keras.layers.Convolution3D(
        4, 4, padding='same', data_format="channels_last", input_shape=imgshape, activation='tanh'),
    tf.keras.layers.MaxPooling3D(padding='same'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Convolution3D(4, 4, padding='same', activation='tanh'),
    tf.keras.layers.MaxPooling3D(padding='same'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Convolution3D(4, 4, padding='same', activation='tanh'),
    tf.keras.layers.MaxPooling3D(padding='same'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Convolution3D(4, 4, padding='same', activation='tanh'),
    tf.keras.layers.MaxPooling3D(padding='same'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(2048, activation='tanh'),
    tf.keras.layers.Dense(1024, activation='tanh'),
    tf.keras.layers.Dense(512, activation='tanh'),
    tf.keras.layers.Dense(256, activation='tanh'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.summary()
model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=500,
    steps_per_epoch=BATCH_SIZE,
    validation_steps=BATCH_SIZE
)

这是我的模型,我使用3 2dconv,类似于2 2dconv在常规图像分类中的使用。

如有任何建议,将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2020-08-29 02:21:08

您的获取图像的代码看起来不错,但是我无法亲自测试它,因为我不确定您的数据是如何存储的。此外,您的模型将开始培训的事实表明,错误可能不在这里。如果要确保可以使用matplotlib来显示图像,请确保它们正确加载。

首先,我会让你的模型尽可能简单,并且仍然有效,测试它是否仍然收敛到0.6931或其他一些数字。然后尝试使用不同的激活函数(即relu )。另一种方法可能是使用一些批量正常化。我的理论是,在tanh函数中有很大或很小的值,这导致每次输出接近0或1。这也阻止了进一步的训练,因为有一个很小的梯度训练。更改为relu可以绕过这个问题,因为值很大,但可能不是很小的值。使用批量规范化将使您的值远离极限,在那里,tanh输出仅为0或1。

票数 0
EN

Stack Overflow用户

发布于 2020-08-29 09:30:10

如果您一直收敛到完全相同的损失,那么在我的经验中只有一个解释--您对数据加载器进行了错误的编码。正在发生的是图像和标签不匹配。它试图学习纯粹的随机性。在这种情况下,它只需尽最大努力输出“平均”正确的答案。我怀疑0.69的值来自你的数据标签,比如你有69%的1级和31%的0级。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63642687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档