如何修复张量流图像分类中的平面精度和NaN损失 [英] How to fix flatlined accuracy and NaN loss in tensorflow image classification

查看:95
本文介绍了如何修复张量流图像分类中的平面精度和NaN损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试TensorFlow和机器学习,作为挑战,我决定尝试在Kaggle网站上编写机器学习软件的代码,该软件可以分析脑部MRI扫描并预测是否存在肿瘤.我使用下面的代码执行了此操作,并开始训练该模型.但是,在训练过程中显示的文字表明,损失值(训练或验证)均不具有适当的值,并且精度在两个数字(每次相同)之间呈线性变化或波动.

I am currently experimenting with TensorFlow and machine learning, and as a challenge, I decided to try and code a machine learning software, on the Kaggle website, that can analyze brain MRI scans and predict if a tumour exists or not. I did so with the code below and began training the model. However, the text that showed up during training showed that none of the loss values (training or validation) had proper values and that the accuracies flatlined, or fluctuated between two numbers (the same numbers each time).

我看过其他帖子,但找不到任何可以给我提示的内容.我更改了损失函数(从sparse_categorical_crossentropy更改为binary_crossentropy).但是这些都没有改变值.

I have looked at other posts but was unable to find anything that gave me tips. I changed my loss function (from sparse_categorical_crossentropy to binary_crossentropy). But none of these changed the values.

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os


import tensorflow as tf
from tensorflow import keras

import numpy as np
import cv2
import pandas as pd
from random import shuffle

IMG_SIZE = 50

data_path = "../input/brain_tumor_dataset"
data = []
folders = os.listdir(data_path)
for folder in folders:
    for file in os.listdir(os.path.join(data_path, folder)):
        if file.endswith("jpg") or file.endswith("jpeg") or file.endswith("png") or file.endswith("JPG"):
            data.append(os.path.join(data_path, folder, file))

shuffle(data)
images = []
labels = []
for file in data:
    img = cv2.imread(file)
    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
    images.append(img)
    if "Y" in file:
        labels.append(1)
    else:
        labels.append(0)

union_list = list(zip(images, labels))
shuffle(union_list)
images, labels = zip(*union_list)
images = np.array(images)
labels = np.array(labels)

train_img = images[:200]
train_lbl = labels[:200]

val_img = images[200:]
val_lbl = labels[200:]


train_img = np.array(train_img)
val_img = np.array(val_img)
train_img = train_img.astype("float32") / 255.0
val_img = val_img.astype("float32") / 255.0

model = keras.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation=tf.nn.relu, input_shape=(IMG_SIZE, IMG_SIZE, 3)),
    tf.keras.layers.MaxPooling2D((2,2), strides=2),

    tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D((2,2), strides=2),

    tf.keras.layers.Dropout(0.8),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])



history = model.fit(train_img, train_lbl, epochs = 100, validation_data=(val_img, val_lbl))

这应该给出精度更高,损耗减少的结果,但是损耗是微不足道的,并且精度是平坦的.

This should give a result with increasing accuracy, and decreasing loss, but the loss is nan, and the accuracy is flatlined.

推荐答案

我设法解决了这个问题.我再次查看代码,意识到我的输出层只有一个节点.但是,它需要输出两种不同类别的概率(无论是否为肿瘤,为是"或否").一旦将其更改为2个节点,网络便开始正常工作,并且在训练和验证集上的准确性均达到95%.我的验证准确性仍在几个值之间波动,但这很可能是因为我在验证集中只有23张图像.但是,为了减少波动,我也将纪元数减少到了10.现在一切似乎都很好.

I managed to solve the problem. I looked at my code again and realized that my output layer only had one node. However, it needed to output the probabilities for two different categories ('yes' or 'no' for whether it is a tumour or not). Once I changed it to 2 nodes, the network began working properly and reached 95% accuracy on both the training and validation sets. My validation accuracy still fluctuates a little between a few values, but this is most likely because I only have 23 images in the validation set. In order to decrease the fluctuations, however, I also decreased the epoch number to just 10. Everything seems to be great now.

这篇关于如何修复张量流图像分类中的平面精度和NaN损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆