pytorch分类和回归:阿里天池宠物年龄预测

2023-11-06 06:40

本文主要是介绍pytorch分类和回归:阿里天池宠物年龄预测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • dog年龄预测
    • 论文Deep expectation of real and apparent age from a single image without facial landmarks
    • 分类的损失函数
      • 1.多分类交叉熵损失函数:
      • 2.KLDiv Loss: 分布差异
      • 3.facenet 三元组损失函数
    • timm and torchvision
      • torchvision
    • 尝试一:分类模型,按年龄分为1-191个类别
    • 尝试二:回归模型
    • 尝试三:分类模型
    • 尝试四:KLDivLoss 拟合年龄分布
    • 尝试五:首先提取狗的face,然后再训练模型
    • 最后主要根据二和五进行优化。prediect

dog年龄预测

阿里天池宠物年龄预测
https://tianchi.aliyun.com/competition/
实验了多种方法,最终成绩并不是特别好,比赛结束后如果有更好的思路,欢迎指教。

论文Deep expectation of real and apparent age from a single image without facial landmarks

  1. 直接回归
  2. 分段分类
  3. 分段求概率,求加权期望,其实相当于回归。
    三种方法的表现
    在这里插入图片描述

其他方法:

  1. 拟合分布,而不是one-hot
  2. 排序的方式
  3. 数据均衡性

参考:

https://github.com/NICE-FUTURE/predict-gender-and-age-from-camera/tree/master

分类的损失函数

1.多分类交叉熵损失函数:

torch.nn.CrossEntropyLoss() = log_softmax + nll_loss

详细介绍:https://zhuanlan.zhihu.com/p/159477597

2.KLDiv Loss: 分布差异

描述分布的差异,如果分类的目标不是one-hot而是soft-label的时候可以用

https://zhuanlan.zhihu.com/p/340088331

3.facenet 三元组损失函数

https://github.com/kvsnoufal/Pytorch-FaceNet-DogDataset

timm and torchvision

https://datawhalechina.github.io/thorough-pytorch/index.html
https://datawhalechina.github.io/thorough-pytorch/%E7%AC%AC%E5%85%AD%E7%AB%A0/6.3%20%E6%A8%A1%E5%9E%8B%E5%BE%AE%E8%B0%83-timm.html

torchvision

import torchvision.models as models
resnet18 = models.resnet18()
# resnet18 = models.resnet18(pretrained=False)  等价于与上面的表达式
alexnet = models.alexnet()
vgg16 = models.vgg16()
squeezenet = models.squeezenet1_0()
densenet = models.densenet161()
inception = models.inception_v3()
googlenet = models.googlenet()
shufflenet = models.shufflenet_v2_x1_0()
mobilenet_v2 = models.mobilenet_v2()
mobilenet_v3_large = models.mobilenet_v3_large()
mobilenet_v3_small = models.mobilenet_v3_small()
resnext50_32x4d = models.resnext50_32x4d()
wide_resnet50_2 = models.wide_resnet50_2()
mnasnet = models.mnasnet1_0()

在这里插入图片描述

尝试一:分类模型,按年龄分为1-191个类别

主要参考:dog品种分类

利用交叉熵损失,然后当成一个分类模型来训练

import glob
import osimport cv2
import numpy as np
import torch
from torch import nn, optim
from torchvision import datasets,transforms
from torch.utils.data import DataLoaderimport torch
import torchvision.models as models
from PIL import Image
import torchvision.transforms as transformsfrom tqdm import tqdmfrom dog_age2 import Netdef VGG16_predict(img_path):'''Use pre-trained VGG-16 model to obtain index corresponding topredicted ImageNet class for image at specified pathArgs:img_path: path to an imageReturns:Index corresponding to VGG-16 model's prediction'''# define VGG16 modelVGG16 = models.vgg16(pretrained=True)# check if CUDA is availableuse_cuda = torch.cuda.is_available()# move model to GPU if CUDA is availableif use_cuda:VGG16 = VGG16.cuda()## Load and pre-process an image from the given img_path## Return the *index* of the predicted class for that image# Image Resize to 256image = Image.open(img_path)mean = [0.485, 0.456, 0.406]std = [0.229, 0.224, 0.225]image_transforms = transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean, std)])image_tensor = image_transforms(image)image_tensor.unsqueeze_(0)if use_cuda:image_tensor = image_tensor.cuda()output = VGG16(image_tensor)_, classes = torch.max(output, dim=1)return classes.item()  # predicted class index### returns "True" if a dog is detected in the image stored at img_path
def dog_detector(img_path):## TODO: Complete the function.class_dog=VGG16_predict(img_path)return class_dog >= 151 and class_dog <=268 # true/falsedef resnet50_predict(img_path):resnet50 = models.resnet50(pretrained=True)use_cuda = torch.cuda.is_available()if use_cuda:resnet50.cuda()image = Image.open(img_path)mean=[0.485, 0.456, 0.406]std=[0.229, 0.224, 0.225]image_transforms = transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean,std)])image_tensor = image_transforms(image)image_tensor.unsqueeze_(0)if use_cuda:image_tensor=image_tensor.cuda()resnet50.eval()output = resnet50(image_tensor)_,classes = torch.max(output,dim=1)return classes.item()
def resnet50_dog_detector(image_path):class_idx = resnet50_predict(image_path)return class_idx >= 151 and class_idx <=268def get_train_set_info(dir):dog_files_train = glob.glob(dir + '\\*.jpg')mean = np.array([0.,0.,0.])std = np.array([0.,0.,0.])for i in tqdm(range(len(dog_files_train))):image=cv2.imread(dog_files_train[i])image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)image = image/255.0mean[0] += np.mean(image[:,:,0])mean[1] += np.mean(image[:,:,1])mean[2] += np.mean(image[:,:,2])std[0] += np.std(image[:,:,0])std[1] += np.std(image[:,:,1])std[2] += np.std(image[:,:,2])mean = mean/len(dog_files_train)std = std/len(dog_files_train)return mean,stdfrom PIL import ImageFiledef train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path):"""returns trained model"""# initialize tracker for minimum validation lossvalid_loss_min = np.Inffor epoch in range(1, n_epochs + 1):# initialize variables to monitor training and validation losstrain_loss = 0.0valid_loss = 0.0#################### train the model ####################model.train()for batch_idx, (data, target) in enumerate(tqdm(loaders['train'])):# move to GPUif use_cuda:data, target = data.cuda(), target.cuda()## find the loss and update the model parameters accordingly## record the average training loss, using something like## train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))optimizer.zero_grad()output = model(data)loss = criterion(output, target)loss.backward()optimizer.step()train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))####################### validate the model #######################correct = 0.correct2 = 0correct3 = 0correct4 = 0total = 0.model.eval()for batch_idx, (data, target) in enumerate(tqdm(loaders['valid'])):# move to GPUif use_cuda:data, target = data.cuda(), target.cuda()## update the average validation lossoutput = model(data)loss = criterion(output, target)valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))pred = output.data.max(1, keepdim=True)[1]# compare predictions to true labelcorrect += np.sum(np.squeeze(pred.eq(target.data.view_as(pred))).cpu().numpy())correct2 += np.sum(np.squeeze(np.abs(pred.cpu().numpy() - (target.data.view_as(pred).cpu().numpy())) < 5))correct3 += np.sum(np.squeeze(np.abs(pred.cpu().numpy() - (target.data.view_as(pred).cpu().numpy())) < 10))correct4 += np.sum(np.squeeze(np.abs(pred.cpu().numpy() - (target.data.view_as(pred).cpu().numpy()))))total += data.size(0)# print training/validation statisticsprint('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(epoch,train_loss,valid_loss))print('Test Accuracy: %2d%% (%2d/%2d)' % (100. * correct / total, correct, total))print('Test Accuracy: %2d%% (%2d/%2d)' % (100. * correct2 / total, correct2, total))print('Test Accuracy: %2d%% (%2d/%2d)' % (100. * correct3 / total, correct3, total))print('Test Accuracy: %2d' % (correct4 / total))## TODO: save the model if validation loss has decreasedif valid_loss_min > valid_loss:print('Saving Model...')valid_loss_min = valid_losstorch.save(model.state_dict(), save_path)# return trained modelreturn modelif __name__ == "__main__":# 1. vgg16 和 resnet50 的识别能力dir = r'D:\commit\trainset\trainset'# dog_files = glob.glob(dir + '\\*.jpg')## dog_files_short = dog_files[:100]## dog_percentage_dog = 0# dog_percentage_dog2 = 0# for i in tqdm(range(100)):#     dog_percentage_dog += int(dog_detector(dog_files_short[i]))#     dog_percentage_dog2 += int(resnet50_dog_detector(dog_files_short[i]))## print(' Dog Percentage in Dog Dataset:{}% {} %'.format( dog_percentage_dog, dog_percentage_dog2)) # 98%, 97%# 2. 训练数据的均值和方差# mean, std = get_train_set_info(dir)# print(mean, std) # [0.595504   0.54956806 0.51172713] [0.2101685  0.21753638 0.22078435]# 3. 训练mean_train_set = [0.595504,  0.54956806, 0.51172713]std_train_set = [0.2101685, 0.21753638, 0.22078435]train_dir = r'D:\commit\trainset\trainset2'valid_dir = r'D:\commit\valset\valset2'test_dir = r'D:\commit\valset\valset2'train_transforms = transforms.Compose([transforms.Resize([256, 256]),transforms.ColorJitter(brightness=0.5, contrast=0.2, saturation=0.2, hue=0.1),transforms.RandomHorizontalFlip(p=0.5),transforms.ToTensor(),transforms.Normalize(mean_train_set, std_train_set)])valid_test_transforms = transforms.Compose([transforms.Resize([256, 256]),#transforms.CenterCrop(256),transforms.ToTensor(),transforms.Normalize(mean_train_set, std_train_set)])train_dataset = datasets.ImageFolder(train_dir, transform=train_transforms)valid_dataset = datasets.ImageFolder(valid_dir, transform=valid_test_transforms)#test_dataset = datasets.ImageFolder(test_dir, transform=valid_test_transforms)# num_workers=8, pin_memory=True 很重要,训练速度明显trainloader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=16, pin_memory=True)validloader = DataLoader(valid_dataset, batch_size=32, shuffle=False,num_workers=8, pin_memory=True) #testloader = DataLoader(test_dataset, batch_size=32, shuffle=False)loaders_scratch = {}loaders_scratch['train'] = trainloaderloaders_scratch['valid'] = validloader#loaders_scratch['test'] = testloaderuse_cuda = torch.cuda.is_available()# instantiate the CNNnum_class = 191# model_scratch = Net(num_class)model_scratch = models.resnet50(pretrained=True)for param in model_scratch.parameters():param.requires_grad = True# model_scratch.classifier = nn.Sequential(nn.Linear(1024, 512),#                                           nn.ReLU(),#                                           nn.Dropout(0.2),#                                           nn.Linear(512, 133))## model_scratch.load_state_dict(torch.load('model_transfer.pt', map_location='cuda:0'))model_scratch.classifier = nn.Sequential(nn.Linear(1024, 512),nn.ReLU(),nn.Dropout(0.2),nn.Linear(512, num_class))# move tensors to GPU if CUDA is availableif use_cuda:model_scratch.cuda()criterion_scratch = nn.CrossEntropyLoss()optimizer_scratch = optim.Adam(model_scratch.parameters(), lr=0.0005)print('training !')# epochImageFile.LOAD_TRUNCATED_IMAGES = Truemodel_scratch = train(100, loaders_scratch, model_scratch, optimizer_scratch,criterion_scratch, use_cuda, 'model_scratch2.pt')# load the model that got the best validation accuracy# model_scratch.load_state_dict(torch.load('model_scratch.pt'))

效果不好。

尝试二:回归模型

model.py

import torch
import torch.nn as nn
from torchinfo import summaryimport timmclass base_net(nn.Module):def __init__(self, input_features, num_features=64):super().__init__()self.num_features = num_featuresself.conv = nn.Sequential(nn.Conv2d(input_features, num_features, kernel_size=3, padding=3//2),#nn.BatchNorm2d(num_features),nn.ReLU(inplace=True),nn.Conv2d(num_features, num_features*2, kernel_size=3, padding=3//2),#nn.BatchNorm2d(num_features*2),nn.ReLU(inplace=True),nn.Conv2d(num_features*2, num_features, kernel_size=3, padding=3 // 2),#nn.BatchNorm2d(num_features),nn.ReLU(inplace=True),nn.Conv2d(num_features, num_features, kernel_size=3, padding=3 // 2),#nn.BatchNorm2d(num_features),nn.ReLU(inplace=True),nn.Conv2d(num_features, num_features, kernel_size=3, padding=3//2),)def forward(self, x):x = self.conv(x)return x
class Predictor(nn.Module):""" The header to predict age (regression branch) """def __init__(self, num_features, num_classes=1):super().__init__()self.conv = nn.Sequential(nn.Conv2d(num_features, num_features // 4, kernel_size=3, padding=3 // 2),nn.BatchNorm2d(num_features // 4),nn.ReLU(inplace=True),nn.Dropout(0.5),nn.Conv2d(num_features // 4, num_features // 8, kernel_size=3, padding=3 // 2),nn.BatchNorm2d(num_features // 8),nn.ReLU(inplace=True),nn.Dropout(0.5),nn.Conv2d(num_features // 8, num_features // 16, kernel_size=3, padding=3 // 2),)self.gap = nn.AdaptiveAvgPool2d(1)self.fc = nn.Conv2d(num_features//16, num_classes, kernel_size=1, bias=True)#self.dp = nn.Dropout(0.5)def forward(self, x):x = self.conv(x)x = self.gap(x)#x = self.dp(x)x = self.fc(x)x = x.squeeze(-1).squeeze(-1).squeeze(-1)return xclass Classifier(nn.Module):""" The header to predict gender (classification branch) """def __init__(self, num_features, num_classes=100):super().__init__()self.conv = nn.Sequential(nn.Conv2d(num_features, num_features // 4, kernel_size=3, padding=3 // 2),nn.BatchNorm2d(num_features // 4),nn.ReLU(inplace=True),nn.Dropout(0.5),nn.Conv2d(num_features // 4, num_features // 8, kernel_size=3, padding=3 // 2),nn.BatchNorm2d(num_features // 8),nn.ReLU(inplace=True),nn.Dropout(0.5),nn.Conv2d(num_features // 8, num_features // 16, kernel_size=3, padding=3 // 2),)self.gap = nn.AdaptiveAvgPool2d(1)self.fc = nn.Conv2d(num_features//16, num_classes, kernel_size=1, bias=True)self.dp = nn.Dropout(0.4)def forward(self, x):x = self.conv(x)x = self.gap(x)x = self.dp(x)x = self.fc(x)x = x.squeeze(-1).squeeze(-1)# x = nn.functional.softmax(x, dim=1)return x#https://github.com/NICE-FUTURE/predict-gender-and-age-from-camera/tree/master
class Model(nn.Module):""" A model to predict age and gender """def __init__(self, timm_pretrained=True):super().__init__()self.backbone = timm.create_model("resnet18", pretrained=timm_pretrained)self.predictor = Predictor(self.backbone.num_features)# self.classifier = Classifier(self.backbone.num_features)def forward(self, x):x = self.backbone.forward_features(x)  # shape: B, D, H, Wage = self.predictor(x)#gender = self.classifier(x)return ageclass Model2(nn.Module):""" A model to predict age and gender """def __init__(self, timm_pretrained=True):super().__init__()self.backbone = timm.create_model("resnet18", pretrained=timm_pretrained)  #base_net(3, 64) ## self.predictor = Predictor(self.backbone.num_features)self.classifier = Classifier(self.backbone.num_features) # 100类概率def forward(self, x):x = self.backbone.forward_features(x)  # shape: B, D, H, W#x = self.backbone.forward(x)  # shape: B, D, H, Wprob = self.classifier(x)#gender = self.classifier(x)return probclass Model3(nn.Module):""" A model to predict age and gender """def __init__(self, timm_pretrained=False):super().__init__()self.backbone = base_net(3, 64) # timm.create_model("resnet18", pretrained=timm_pretrained)  ## self.predictor = Predictor(self.backbone.num_features)self.classifier = Classifier(self.backbone.num_features) # 100类概率def forward(self, x):#x = self.backbone.forward_features(x)  # shape: B, D, H, Wx = self.backbone.forward(x)  # shape: B, D, H, Wprob = self.classifier(x)#gender = self.classifier(x)return prob
if __name__ == "__main__":device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')# device = 'cpu'print(device)modelviz = Model2().to(device)# 打印模型结构print(modelviz)summary(modelviz, input_size=(2, 3, 256, 256), col_names=["kernel_size", "output_size", "num_params", "mult_adds"])# for p in modelviz.parameters():#     if p.requires_grad:#         print(p.shape)input = torch.rand(2, 3, 256, 256).to(device)out = modelviz(input)from ptflops import get_model_complexity_infomacs, params = get_model_complexity_info(modelviz, (3, 256, 256), verbose=True, print_per_layer_stat=True)print(macs, params)params = float(params[:-3])macs = float(macs[:-4])print(macs * 2, params)  # 8个图像的 FLOPs, 这里的结果 和 其他方法应该一致print('out:', out.shape, out)

训练模型:


import glob
import os.pathimport cv2
import numpy as np
import rawpy
import torch
import torch.optim as optim
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from tqdm import tqdmfrom datasets import BatchDataset
from model import Model, Model2import torchvisionif __name__ == "__main__":# 1.当前版本信息print(torch.__version__)print(torch.version.cuda)print(torch.backends.cudnn.version())print(torch.cuda.get_device_name(0))np.random.seed(0)torch.manual_seed(0)torch.cuda.manual_seed_all(0)torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False# 2. 设置device信息 和 创建model# os.environ['CUDA_VISIBLE_DEVICES'] = '2,3'# device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu")model = Model()gpus = [2,3]model = nn.DataParallel(model, device_ids=gpus)device = torch.device('cuda:2')model = model.cuda(device=gpus[0])# 3. dataset 和 data loader, num_workers设置线程数目,pin_memory设置固定内存img_size = 256transform1 = transforms.Compose([transforms.ToTensor(),transforms.Resize([img_size, img_size]),transforms.RandomHorizontalFlip(p=0.5),transforms.ColorJitter(brightness=0.4, contrast=0.2, saturation=0.2, hue=0.1),#transforms.RandomPerspective(distortion_scale=0.6, p=1.0),transforms.RandomRotation(degrees=(-90, 90)),])transform2 = transforms.Compose([transforms.ToTensor(),transforms.Resize([img_size, img_size]),])train_dataset = BatchDataset('train', transform1)train_dataset_loader = DataLoader(train_dataset, batch_size=32*4, shuffle=True, num_workers=8, pin_memory=True)eval_dataset = BatchDataset('eval', transform2)eval_dataset_loader = DataLoader(eval_dataset, batch_size=8, shuffle=True, num_workers=8, pin_memory=True)print('load dataset !', len(train_dataset), len(eval_dataset))# 4. 损失函数 和  优化器age_criterion = nn.MSELoss()gender_criterion = nn.CrossEntropyLoss().to(device)loss_fn = nn.L1Loss().to(device)loss_fn2 = nn.SmoothL1Loss().to(device)learning_rate = 1 * 1e-4#optimizer = optim.Adam(model.parameters(), lr=learning_rate)optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)lr_step = 50scheduler = torch.optim.lr_scheduler.StepLR(optimizer, lr_step, gamma=0.5)# 5. hyper para 设置epochs = 800save_epoch = 100save_model_dir = 'saved_model_age'eval_epoch = 100save_sample_dir = 'saved_sample_age'if not os.path.exists(save_model_dir):os.makedirs(save_model_dir)# 6. 是否恢复模型resume = 1last_epoch = 12if resume and last_epoch > 1:model.load_state_dict(torch.load(save_model_dir + '/checkpoint_%04d.pth' % (last_epoch),map_location=device))print('resume ' , save_model_dir + '/checkpoint_%04d.pth' % (last_epoch))# 7. 训练epochf1 = open('traininfo1.txt', 'a')f2 = open('evalinfo1.txt', 'a')for epoch in range(last_epoch + 1, epochs + 1):print('current epoch:', epoch, 'current lr:', optimizer.state_dict()['param_groups'][0]['lr'])if epoch < last_epoch + 101:save_epoch = 2eval_epoch = 2else:save_epoch = 10eval_epoch = 10# 8. train loopmodel.train()g_loss = []g_mae = []for data in tqdm(train_dataset_loader):image, age, filename = data# print(image.shape, age, filename)image = image.to(device)age = age.to(device)pred_age = model(image)#print(image.shape, pred_age.shape)loss = loss_fn(age, pred_age)#loss = age_criterion(age, pred_age)#print('dd:', age.detach().cpu().numpy().reshape(-1), pred_age.detach().cpu().numpy().reshape(-1))optimizer.zero_grad()loss.backward()optimizer.step()# training resultg_loss.append(loss.item())mae = np.sum(np.abs(age.detach().cpu().numpy().reshape(-1) - pred_age.detach().cpu().numpy().reshape(-1))) / len(age)g_mae.append(mae)#print( loss.item(), mae)#print(len(g_loss), len(g_mae))mean_loss = np.mean(np.array(g_loss))mean_mae = np.mean(np.array(g_mae))print(f'epoch{epoch:04d} ,train loss: {mean_loss},train mae: {mean_mae}')f1.write("%d, %.6f, %.4f\n" % (epoch, mean_loss, mean_mae))# 9. save modelif epoch % save_epoch == 0:save_model_path = os.path.join(save_model_dir, f'checkpoint_{epoch:04d}.pth')torch.save(model.state_dict(), save_model_path)# 10. eval test and save some samples if neededif epoch % eval_epoch == 0:model.eval()maes = []with torch.no_grad():for data in  tqdm(eval_dataset_loader):image, age, filename = dataimage = image.to(device)age = age.to(device)out = model(image)mae = loss_fn(out, age)#print( age.detach().cpu().numpy().reshape(-1), out.detach().cpu().numpy().reshape(-1), mae.item())maes.append(mae.item())print('eval dataset  mae: ', np.array(maes).mean())f2.write("%d, %.6f\n" % (epoch,  np.array(maes).mean()))scheduler.step()  # 更新学习率

效果还可以,eval mae 可以达到22左右

尝试三:分类模型

1)为了应对数据不均衡,五个年龄何为一组数据,这样每组取50个得到一个epoch的dataset进行训练
2)分类得到的是概率,除了交叉熵损失,再加上一个期望损失

效果不佳

尝试四:KLDivLoss 拟合年龄分布

除了 age的 mae损失
还有 年龄分布损失, 利用KLDivLoss来实现。

比如 label =21 的年龄设置分布为 21周边的年龄不为0,其他为0

 prob = model(image)
pred_age = torch.sum(prob * torch.arange(0, 100).reshape(1, -1).to(device), axis=1) * 2 + 1#print(prob.shape, label.shape)
loss1 = loss_kl(prob.log(), label) # label是一个分布
loss2 = loss_fn(age, pred_age)
loss =   loss1 + loss2 / 10

尝试五:首先提取狗的face,然后再训练模型

由于直接训练很容易过拟合,因此怀疑图片中的其他特征干扰了模型训练,因此提取狗face后进行训练效果会不会好一些,如何提取狗face呢?

主要利用下面的仓库
https://github.com/metinozkan/DogAndCat-Face-Opencv

import glob
import osimport cv2files = glob.glob(r'D:\commit\testset\testset' + '\\*.jpg')for file in files:#file = r'D:\commit\trainset\trainset\02e5218a80b44139ab07c547e1d6c4b9.jpg'img=cv2.imread(file)#picture pathheight, width, channel = img.shapeyuz_cascade=cv2.CascadeClassifier('dog_face.xml')#used haarcascade Classifier#kedi_cascade=cv2.CascadeClassifier('haarcascade_frontalcatface.xml path')#used haarcascade Classifiergriton = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#Picture conversion gray tone with haarcascadeit = yuz_cascade.detectMultiScale(griton,1.1,4)#search for the object you want in photos#kedi=kedi_cascade.detectMultiScale(griton,1.1,4)kopeksay=0#increases the number of found objectskedisay=0#objects in the rectanglewh = 0i = 0if len(it) == 0:x, y, w, h = 0,0,width,heightprint(file, 'not changed ')else:for (x, y, w, h) in it:if w* h > wh:wh = w*hj = ii += 1(x, y, w, h) = it[j]T = 20# saveimg2 = img[ max(y-T, 0): min(y + h+T, height), max(x-T, 0) : min(x + w + T,width)]cv2.imwrite(os.path.join(r'D:\commit\testset\testset3', os.path.basename(file)), img2)# showshow_fig = 0if show_fig:cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)cv2.rectangle(img, (max(x-T, 0), max(y-T, 0)), (min(x + w + T,width) ,  min(y + h+T, height)), (0, 255, 255), 3)kopeksay=kopeksay+1# for (x, y, w, h) in kedi:#     cv2.rectangle(img, (x, y), (x + w,y + h), (0, 10, 0), 3)#     kedisay=kedisay+1print("kopek->",kopeksay)#number of found objectsprint("kedi-->",kedisay)cv2.imshow('yuzler', img)cv2.waitKey(0)cv2.destroyAllWindows()

最后主要根据二和五进行优化。prediect

import glob
import os.pathimport cv2
import numpy as np
import rawpy
import torch
import torch.optim as optim
from PIL import Image
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from tqdm import tqdmfrom skimage.metrics import peak_signal_noise_ratio as compare_psnr
from skimage.metrics import structural_similarity as compare_ssimfrom datasets import BatchDataset, get_images
from model import UNetSeeInDark, Model, Model2if __name__ == "__main__":img_size = 256transform2 = transforms.Compose([transforms.ToTensor(),transforms.Resize([img_size, img_size]),])model = Model()gpus = [0]# model = nn.DataParallel(model, device_ids=gpus)device = torch.device('cuda:0')print(device)m_path = 'saved_model_age/checkpoint_0014.pth'#m_path = 'saved_model_res18_reg/checkpoint_0010.pth'checkpoint = torch.load(m_path, map_location=device)model.load_state_dict({k.replace('module.', ''): v for k, v in checkpoint.items()})#model.load_state_dict(torch.load(m_path, map_location=device))model = model.cuda(device=gpus[0])model.eval()files = glob.glob("testset\\testset\\*.jpg")# image_dir = 'valset\\valset'# file_txt = 'annotations\\annotations\\val.txt'# files = get_images(image_dir, file_txt)print(len(files))f = open('predict_res50_14.txt', 'w')st = ''ret = []for file in files:# file, label = fileimage = Image.open(file).convert('RGB')# image = cv2.imread(file, 1).astype(np.float32) / 255image = np.array(image)input = transform2(image).unsqueeze(0).to(device)#print(input.shape)out = model(input)out = out.detach().cpu().numpy().reshape(-1)pred_age = out[0]#pred_age = np.sum(out * np.arange(0, 100).reshape(1, -1)) * 2 + 1#print(int(label), pred_age, np.abs(pred_age -int(label)))#ret.append([int(label), pred_age, pred_age -int(label), np.abs(pred_age -int(label))])#print(out)st = os.path.basename(file)+'\t%.2f\n' % (pred_age.item())f.write(st)# ret = np.array(ret)# print(ret)# print(np.mean(ret, axis=0))#np.savetxt('ret54.txt', ret+2, fmt='%.1f', delimiter=' ')

这篇关于pytorch分类和回归:阿里天池宠物年龄预测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/355036

相关文章

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

Pytorch介绍与安装过程

《Pytorch介绍与安装过程》PyTorch因其直观的设计、卓越的灵活性以及强大的动态计算图功能,迅速在学术界和工业界获得了广泛认可,成为当前深度学习研究和开发的主流工具之一,本文给大家介绍Pyto... 目录1、Pytorch介绍1.1、核心理念1.2、核心组件与功能1.3、适用场景与优势总结1.4、优

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测