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

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

我使用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的方式。

任何建议将不胜感激!

解决方法

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

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

Tanh graph

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-