pytorch-image-classification项目-修改最后的model.fc

直接可以修改 

def get_net():
    #return MyModel(torchvision.models.resnet101(pretrained = True))
    model = torchvision.models.resnet50(pretrained = True)
    #for param in model.parameters():
    #    param.requires_grad = False
    model.avgpool = nn.AdaptiveAvgPool2d(1)
    model.fc = nn.Linear(2048,config.num_classes)
    return model

如果是并行处理,则:

    model = torch.nn.DataParallel(model, device_ids=[0,1,2,3])
    model.to(device)
    model.module.avgpool = nn.AdaptiveAvgPool2d(7)

 

地址:

https://github.com/spytensor/pytorch-image-classification 

  File "main.py", line 8, in 
    import pandas as pd 
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/__init__.py", line 42, in 
    from pandas.core.api import *
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/api.py", line 26, in 
    from pandas.core.groupby import Grouper
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/groupby/__init__.py", line 1, in 
    from pandas.core.groupby.groupby import GroupBy  # noqa: F401
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 37, in 
    from pandas.core.frame import DataFrame
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 100, in 
    from pandas.core.series import Series
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/series.py", line 4390, in 
    Series._add_series_or_dataframe_operations()
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 10138, in _add_series_or_dataframe_operations
    from pandas.core import window as rwindow
  File "/root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/core/window.py", line 14, in 
    import pandas._libs.window as libwindow
ImportError: /root/train/results/ynh_copy/anaconda3_py3.7/lib/python3.7/site-packages/pandas/_libs/window.cpython-37m-x86_64-linux-gnu.so: symbol _ZTINSt8ios_base7failureB5cxx11E, version GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference

pytorch-image-classification项目-修改最后的model.fc_第1张图片

遇到这种错误:

symbol _ZTINSt8ios_base7failureB5cxx11E, version GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference

直接选择升级即可解决

 ~/train/results/ynh_copy/anaconda3_py3.7/bin/python -mpip install pandas==0.25.2 

修改top1-n的检测,可以实现二分类的训练:

def evaluate(val_loader,model,criterion,epoch):
    #2.1 define meters
    losses = AverageMeter()
    top1 = AverageMeter()
    #progress bar
    val_progressor = ProgressBar(mode="Val  ",epoch=epoch,total_epoch=config.epochs,model_name=config.model_name,total=len(val_loader))
    #2.2 switch to evaluate mode and confirm model has been transfered to cuda
    model.cuda()
    #model.eval()
    with torch.no_grad():
        for i,(input,target) in enumerate(val_loader):
            val_progressor.current = i 
            input = Variable(input).cuda()
            target = Variable(torch.from_numpy(np.array(target)).long()).cuda()
            #target = Variable(target).cuda()
            #2.2.1 compute output
            output = model(input)
            loss = criterion(output,target)

            #2.2.2 measure accuracy and record loss
            precision1 = accuracy(output,target,topk=(1))
            if(len(precision1)>1):
                precision1, precision2 = precision1
                
                
            losses.update(loss.item(),input.size(0))
            top1.update(precision1[0],input.size(0))
            val_progressor.current_loss = losses.avg
            val_progressor.current_top1 = top1.avg
            val_progressor()
        val_progressor.done()
    return [losses.avg,top1.avg]

 

def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""

    with torch.no_grad():
        if isinstance(topk, int):
            maxk = topk
        else:
            maxk = max(topk)
        batch_size = target.size(0)

        _, pred = output.topk(maxk, 1, True, True)
        pred = pred.t()
        correct = pred.eq(target.view(1, -1).expand_as(pred))

        if isinstance(topk, int):
            correct_k = correct[0].view(-1).float().sum(0, keepdim=True)
            res = correct_k.mul_(100.0 / batch_size)
        else:
            res = []
            for k in topk:
                correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
                res.append(correct_k.mul_(100.0 / batch_size))
        return res

 

你可能感兴趣的:(深度学习)