训练3dconv神经网络失败;损失收敛于.6931

时间:2020-08-29 01:46:36

标签: python tensorflow machine-learning neural-network nibabel

我使用TensorFlow的教程https://www.tensorflow.org/tutorials/load_data/images,编写了一个脚本来训练神经网络使用.nii文件作为输入。我将其稍作更改以使用NiBabel和.nii文件,但它仍遵循相同的基本结构。但是,我遇到了一个问题,我的损失收敛到0.6931,我认为这是因为无论输入,图像形状或批处理大小如何,模型都开始猜测同一件事。因此,我认为该模型不是在学习。谁能识别我的代码中的任何致命缺陷;我已经累了:

  • 更改LR的回调
  • 更改数据,清理数据并重新组织
  • 更改每个类的金额比例
  • 使用不同的优化器和损失函数
  • 使用简单的密集,密集,密集模型,但这似乎不起作用,因为它甚至不想开始训练
  • 使用重复的数据集以及固定的大小(尽管我不清楚这有什么区别)
public class CoordinateSystem {

    private int length;
    private int width;
    private Field[][] map;

    public CoordinateSystem(int width, int length) throws MyException {
        this.width = width;
        this.length = length;
        map = createMap(width, length);
    }

    public int getLength() {
        return this.length;
    }

    public int getWidth() {
        return this.width;
    }

    public class Field {
        private int x;
        private int y;

        public void setX(int x) {
            this.x = x;
        }

        public int getX() {
            return x;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getY() {
            return y;
        }
    }

    public Field[][] getMap() {
        return map;
    }

    // Initializing a coordinate to each "field"
    public Field[][] createMap(int width, int length) throws MyException {
        if(width > 0 && length > 0){
            Field[][] map = new Field[width][length];
            for( int i = 0 ; i < width ; i++ ){
                for( int j = 0 ; j < length  ; j++ ){
                    map[i][j] = new Field();
                    map[i][j].setX(j + 1);
                    map[i][j].setY(i + 1);
                }
            }
            return map;
        }
        else{
            throw new Exception("Sorry, can't create a field of width or height = 0 ");
        }
    }

    public static void main(String[] args) throws MyException {
        CoordinateSystem board = new CoordinateSystem(8, 9);
        for( int i = 0 ; i < 8 ; i++ ) {
            for( int j = 0 ; j < 9  ; j++ ) {
                System.out.print(board.getMap()[i][j].getX());
                System.out.println(board.getMap()[i][j].getY());
            }
        }
    }
}

我正在使用这些功能来处理我的数据,并将其映射到列表文件数据集上以处理我的数据。

# 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

我直接从TensorFlow教程中提取了此信息。

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

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

任何建议将不胜感激!

2 个答案:

答案 0 :(得分:0)

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

我首先要使您的模型尽可能简单,并且仍然可以使用,请测试模型是否仍收敛到0.6931或其他数值。然后尝试使用其他激活功能,即relu。另一种方法可能是使用一些批标准化。我的理论是,在tanh函数中输入的值非常大或很小,这会使输出每次接近0或1。这也阻止了进一步的训练,因为训练的梯度很小。更改为relu可以解决较大值但可能不是小的值的问题。使用批量归一化将使您的值远离tanh输出仅为0或1的末端。

Tanh graph

答案 1 :(得分:0)

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