本文主要是介绍基于飞浆resnet50的102分类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.数据预处理
2.数据导入
3.模型导入
4.批训练
5. 输出结果
6.结果参考
1.数据预处理
T=transforms.Compose([transforms.Resize((250,250)),transforms.RandomCrop(size=224),transforms.RandomHorizontalFlip(0.5),transforms.RandomRotation(degrees=15),transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),transforms.ToTensor(),transforms.Normalize(mean=[0.46010968,0.4837371,0.49916607],std=[0.25398722,0.25408414,0.25931123]) ])
2.数据导入
datas=[] labels=[] train_path='data/data146107/dataset/train.txt' eval_path='data/data146107/dataset/test.txt' base='data/data146107/dataset/images/' contents=[] with open(train_path,mode='r',encoding='utf-8') as f:contents=f.read().split('\n') for content in contents:if content=='':continueimg=content.split('\t')[0]label=content.split('\t')[1]data=np.array(T(cv2.imread(base+img)))datas.append(data)labels.append(int(label)) datas=np.array(datas) labels=np.array(labels)
3.模型导入
model=resnet50(pretrained=True,num_classes=102) criterion=paddle.nn.CrossEntropyLoss() optimizer=paddle.optimizer.Adam(learning_rate=0.0001,parameters=model.parameters(),weight_decay=0.001)
4.批训练
epochs=30 batch_size=125 dataset=TensorDataset([datas,labels]) dataloader=DataLoader(dataset,shuffle=True,batch_size=batch_size) total_loss=[] for epoch in range(epochs):for batch_data,batch_label in dataloader:batch_data=paddle.to_tensor(batch_data,dtype='float32')batch_label=paddle.to_tensor(batch_label,dtype='int64')output=model(batch_data)loss=criterion(output,batch_label)print(epoch,loss.numpy()[0])total_loss.append(loss.numpy()[0])optimizer.clear_grad()loss.backward()optimizer.step() paddle.save({'model':model.state_dict(),'optimizer':optimizer.state_dict()},'checkpoint.param') plt.plot(range(len(total_loss)),total_loss) plt.show()
5. 输出结果
contents=[] batch_size=64 with open('data/data146107/dataset/test.txt',mode='r',encoding='utf-8') as f:contents=f.read().split('\n') evals=[] imgs=[] base='data/data146107/dataset/images/' for content in contents:if content=='':continueimg=contentdata=np.array(T(cv2.imread(base+img)))evals.append(data)imgs.append(img) evals=np.array(evals) imgs=np.array(imgs) dataset=TensorDataset([evals,imgs]) dataloader=DataLoader(dataset,shuffle=True,batch_size=batch_size) with open('result.txt',mode='w',encoding='utf-8'):pass with paddle.no_grad():for batch_data,batch_img in dataloader:batch_data=paddle.to_tensor(batch_data,dtype='float32')output=model(batch_data)output=np.array(paddle.argmax(output,axis=1))with open('result.txt',mode='a',encoding='utf-8') as f:for img,ans in zip(batch_img,output):f.write(img+'\t'+str(ans)+'\n')
6.结果参考
loss收敛到0.001 ,准确率到达93%左右
这篇关于基于飞浆resnet50的102分类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!