OOD分类项目训练

文章介绍了如何在GitHub的UIOS项目中创建和处理训练、验证和测试数据的标签,包括将分类信息转换为CSV文件,以及在遇到ValueError时处理ROCAUC计算的问题。此外,还提到调整config.py参数如shuffle和batch_size以优化模型训练过程。
摘要由CSDN通过智能技术生成

一、项目地址

GitHub - LooKing9218/UIOS

二、label制作

      将训练、验证、测试数据的分类信息转换入.csv文件中,运行如下脚本即可:

import os
import csv
 
#要读取的训练、验证、测试文件的目录,该文件下保存着以各个类别命名的文件夹和对应的分类图片
root_path=r'/media/*********************/train' 
#类别种类
classes=['cls1','cls2']

def get_Write_file_infos(path):
    # 文件信息列表
    file_infos_list=[]
    typeclothes=os.listdir(path)
    for ii in typeclothes:
        everyfile=os.path.join(path , ii)
        for root, dirnames, filenames in os.walk(everyfile):
            for filename in filenames:
                file_infos = {}
                dirname=root
                 
                #根据自己的需求更改路径地址
                filename1 ='train/'+ii+'/'+ filename#.split('.jpg')[0]
                flag = filename1[-1]
                file_infos["ImageId"] = filename1
     
                file_infos["Flag"] = classes.index(ii)
                #将数据追加字典到列表中
                file_infos_list.append(file_infos)
                
    return file_infos_list
 
 
#写入csv文件
def write_csv(file_infos_list):
    with open('train_label.csv','a+',newline='') as csv_file_train:
        csv_writer = csv.DictWriter(csv_file_train,fieldnames=['ImageId','Flag'])
        csv_writer.writeheader()
        for each in file_infos_list:
            print(each)
            csv_writer.writerow(each)
            
def main():
    file_infos_list =get_Write_file_infos(root_path)
    write_csv(file_infos_list)
 
 
if __name__ == '__main__':
    main()
    print('The End!')

生成情况如下:

三、运行程序

     (1)修改参数文件 utils/config.py

# -*- coding: utf-8 -*-
class DefaultConfig(object):
    net_work = 'ResUnNet50'
    num_classes = 2
    num_epochs = 100
    batch_size = 256
    validation_step = 1
    root = "/media/code/"
    train_file = "train_label.csv"
    val_file = "val_label.csv"
    test_file = "test_label.csv"
    lr = 1e-4
    lr_mode = 'poly'
    momentum = 0.9
    weight_decay = 1e-4
    save_model_path = './Model_Saved'.format(net_work,lr)
    log_dirs = './Logs_Adam_0304'
    pretrained =True# False
    pretrained_model_path ='/media/code/UIOS-master/Trained/archive/data/99843712' #None
    cuda = 0
    num_workers = 4
    use_gpu = True
    trained_model_path = ''
    predict_fold = 'predict_mask'

(2)运行

   命令:

python train.py

(3)运行界面

四、踩坑记录

问题原因:ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.

解决方法:

     (1)网上看了很多:

              方法1:添加 try-except

        try:
            epoch_train_auc = metrics.roc_auc_score(labels, outputs)

            writer.add_scalar('Train/train_auc', float(epoch_train_auc),
                          epoch)
            print('loss for train : {},{}'.format(loss_train_mean,round(epoch_train_auc,6)))

        except ValueError:
            pass

        方法2:DataLoader的参数设置shuffle=True

   train_loader = DataLoader(DatasetCFP(
        root=args.root,
        mode='train',
        data_file=args.train_file,
    ),
        batch_size=args.batch_size, shuffle=True, pin_memory=True)
    val_loader = DataLoader(DatasetCFP(
        root=args.root,
        mode='val',
        data_file=args.val_file,
    ),
        batch_size=args.batch_size, shuffle=True, pin_memory=True)
    test_loader = DataLoader(DatasetCFP(
        root=args.root,
        mode='test',
        data_file=args.test_file,
    ),
        batch_size=args.batch_size, shuffle=True, pin_memory=True)

    方法3:增大batch_size

    (2)我的方法:

        其实是我马虎大意

       修改好config.py中的num_classes参数就行了,

       见谅(不好意思~( ̄▽ ̄)~*)

This error occurs when there is only one class present in the true labels (y_true) of a classification problem. This means that there is no variation in the classes, which can be a problem for the model to learn from. To resolve this error, you can try the following steps: 1. Check if the data is imbalanced: If there is only one class present in the true labels, it is possible that the data is imbalanced. In this case, you may need to balance the data by collecting more samples of the underrepresented class or using techniques such as oversampling or undersampling. 2. Check the data preprocessing: Make sure that the data preprocessing is done correctly. It is possible that the data has not been transformed or encoded properly, leading to only one class present in y_true. 3. Check the model architecture: Ensure that the model architecture is suitable for the given problem. If the model is too simple, it may not be able to learn the patterns in the data. In this case, you may need to use a more complex model or add more layers to the existing model. 4. Check the loss function: If the loss function is not appropriate for the given problem, it may not be able to learn the patterns in the data. In this case, you may need to use a different loss function that is better suited for the problem. 5. Check the evaluation metric: If the evaluation metric is not appropriate for the given problem, it may not be able to measure the performance of the model accurately. In this case, you may need to use a different evaluation metric that is better suited for the problem.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值