基于飞浆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=='':continue
    img=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=='':continue
    img=content
    data=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%左右

你可能感兴趣的:(Python,分类,数据挖掘,人工智能)