Image features 训练代码
import torch
import torch.utils.data as Data
import torch.nn as nn
import numpy as np
import torchvision
from torchvision import datasets,transforms
#hyper parameters
EPOCH = 80
BATCH_SIZE=50
LR = 0.0001
transform =transforms.Compose([
transforms.RandomResizedCrop(80),
torchvision.transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# # ['im_Dyskeratotic', 'im_Koilocytotic', 'im_Metaplastic', 'im_Parabasal', 'im_Superficial-Intermediate']
# print(train_data.classes)
#
# # {'im_Dyskeratotic': 0, 'im_Koilocytotic': 1, 'im_Metaplastic': 2, 'im_Parabasal': 3, 'im_Superficial-Intermediate': 4}
# print(train_data.class_to_idx)
#
# # (path of imgs[0], label)
# print(train_data.imgs[0])
#
# # 16196
# print(train_data.imgs.__len__())
#
# # print(train_data[0])
#
# # print(train_data[0][0])
accuracy = [None*5]
confu_matrices = [None*5]
#16196/50 = 324
#200000/324 = 618
for i in range(0,5):
vgg = None
vgg = torchvision.models.vgg19(pretrained=True)
vgg.classifier = nn.Sequential(
nn.Linear(in_features=25088, out_features=4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=4096, out_features=4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=4096, out_features=5, bias=True)
)
vgg = vgg.cuda()
optim = torch.optim.SGD(vgg.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()
train_data = datasets.ImageFolder(root='your path \\datasets\\sipakmedPics\\train_test\\train'+str(i), transform=transform)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
test_data = datasets.ImageFolder(root='your path \\datasets\\sipakmedPics\\train_test\\test'+str(i), transform=transform)
test_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
for epoch in range(EPOCH):
confu_matrix = np.zeros((5, 5))
total_matrix = np.array([0, 0, 0, 0, 0])
for step,(x,y) in enumerate(train_loader,0):
x, y = x.cuda(), y.cuda()
out = vgg(x)
loss = loss_func(out, y)
optim.zero_grad()
loss.backward()
optim.step()
out = torch.max(out, 1)[1]
if step%100 ==0:
if step % 100 == 0:
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
img, lab = data
img, lab = img.cuda(), lab.cuda()
test_out = vgg(img)
test_out = torch.max(test_out, 1)[1]
correct += (test_out == lab).sum().item()
total += img.size(0)
print('epoch', epoch, '|', 'loss: ', loss.item(), ' acc: ', 100 * correct / total, '%')
accuracy[i] = 100 * correct / total
out,y = out.cpu().detach().numpy(),y.cpu().detach().numpy()
for i in range(out.size):
confu_matrix[out[i]][y[i]] = confu_matrix[out[i]][y[i]]+1
total_matrix[y[i]] = total_matrix[y[i]]+1
confu_matrix = confu_matrix/total_matrix *100
confu_matrices[i] = confu_matrix
print("epoch",epoch," confusion matrix : ")
print(confu_matrix)
torch.save(vgg, "vgg19-sipakmed-classification-"+str(i)
测试结果