1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
| import os import numpy as np import pandas as pd import cv2 as cv from PIL import Image import xmltodict import shutil import torch import torch.nn as nn import torch.optim as optim import torchvision.transforms as transforms from torch.utils.data import Dataset from matplotlib import pyplot as plt
originial_dataset_dir = 'D:/download/original_data' base_dir = 'Vegetables/'
input_dim = 224
LR = 0.003 BATCH_SIZE = 16 EPOCH = 30
save_dir = './model/1.pth'
def image_preparation(original_dir, base_dir, labels): ''' 文件准备, 将文件复制到训练\测试集目录 INPUT -> 原始数据集地址, 数据集存放地址, 分类列表 ''' base_dir = base_dir if not os.path.exists(base_dir): os.mkdir(base_dir) train_dir = os.path.join(base_dir, 'traindata') if not os.path.exists(train_dir): os.mkdir(train_dir) test_dir = os.path.join(base_dir, 'testdata') if not os.path.exists(test_dir): os.mkdir(test_dir) names = locals() for label in labels: names["train_"+str(label)+"dir"] = os.path.join(train_dir, str(label)) if not os.path.exists(names["train_"+str(label)+"dir"]): os.mkdir(names["train_"+str(label)+"dir"]) names["test_"+str(label)+"dir"] = os.path.join(test_dir, str(label)) if not os.path.exists(names["test_"+str(label)+"dir"]): os.mkdir(names["test_"+str(label)+"dir"])
fnames = [os.path.splitext(i)[0] for i in os.listdir(original_dir) if i.split('_')[0] == str(label)] for fname in fnames[:int(len(fnames)*0.5)]: img_src = os.path.join(original_dir, '%s.jpg' % (fname)) img_dst = os.path.join(names["train_"+str(label)+"dir"], '%s.jpg' % (fname)) shutil.copyfile(img_src, img_dst) xml_src = os.path.join(original_dir, '%s.xml' % (fname)) xml_dst = os.path.join(names["train_"+str(label)+"dir"], '%s.xml' % (fname)) shutil.copyfile(xml_src, xml_dst) for fname in fnames[int(len(fnames)*0.5):]: img_src = os.path.join(original_dir, '%s.jpg' % (fname)) img_dst = os.path.join(names["test_"+str(label)+"dir"], '%s.jpg' % (fname)) shutil.copyfile(img_src, img_dst) xml_src = os.path.join(original_dir, '%s.xml' % (fname)) xml_dst = os.path.join(names["test_"+str(label)+"dir"], '%s.xml' % (fname)) shutil.copyfile(xml_src, xml_dst) print('total train '+str(label)+' images:', len(os.listdir(names["train_"+str(label)+"dir"]))) print('total test '+str(label)+' images:', len(os.listdir(names["test_"+str(label)+"dir"])))
image_preparation(originial_dataset_dir, base_dir, ['cucumber','eggplant','mushroom'])
n_classes = 0 for fn in os.listdir(os.path.join(base_dir, 'traindata')): n_classes += 1
class LocationDataSet(Dataset): def __init__(self, root_dir, transform=None, input_dim=224): cates = ['cucumber', 'eggplant', 'mushroom'] class_binary_label = pd.get_dummies(cates).values self.transform = transform
self.imgs = [] self.bboxes = [] self.classes = []
for cate in cates: cate_dir = os.path.join(root_dir, str(cate)) fnames = [os.path.splitext(i)[0] for i in os.listdir(cate_dir) if i.split('_')[0] == str(cate)] for fname in fnames: img_path = os.path.join(cate_dir, '%s.jpg' % (fname)) img = Image.open(img_path)
xml_path = os.path.join(cate_dir, '%s.xml' % (fname)) x = xmltodict.parse(open(xml_path, 'rb')) bndbox = x['annotation']['object']['bndbox'] bndbox = np.array([float(bndbox['xmin']), float(bndbox['ymin']), float(bndbox['xmax']), float(bndbox['ymax'])]) bndbox = bndbox / input_dim
self.imgs.append(img) self.bboxes.append(np.hstack((bndbox, class_binary_label[cates.index(cate)]))) self.classes.append(cate)
def __getitem__(self, idx): img = self.imgs[idx] if self.transform: sample = self.transform(img) else: sample = img return sample, torch.Tensor(self.bboxes[idx]).float()
def __len__(self): return len(self.imgs)
transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ])
trainset = LocationDataSet(os.path.join(base_dir, 'traindata'), transform=transform, input_dim=input_dim)
trainloader = torch.utils.data.DataLoader( trainset, batch_size=BATCH_SIZE, shuffle=True )
testset = LocationDataSet(os.path.join(base_dir, 'testdata'), transform=transform, input_dim=input_dim)
testloader = torch.utils.data.DataLoader( testset, batch_size=BATCH_SIZE, shuffle=False )
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Sequential( nn.Conv2d(3, 6, kernel_size=5, padding=2, bias=False), nn.LeakyReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) self.conv2 = nn.Sequential( nn.Conv2d(6, 16, kernel_size=5, padding=2, bias=False), nn.LeakyReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) self.conv3 = nn.Sequential( nn.Conv2d(16, 120, kernel_size=5, padding=2, bias=False), nn.LeakyReLU(), nn.MaxPool2d(kernel_size=2, stride=2) )
self.classifier = nn.Sequential( nn.Linear(120*28*28, 120), nn.LeakyReLU(), nn.Dropout(), nn.Linear(120, 84), nn.LeakyReLU(), nn.Dropout(), nn.Linear(84, n_classes+4) )
def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.conv3(x)
x = x.view(x.size(0), 120*28*28)
out = self.classifier(x) return out
class LocationLoss(nn.Module): ''' 损失函数(同时使用了平方差和交并比) ''' def calculate_iou(self, target_boxes, pred_boxes): x_min = torch.max(target_boxes[:, 0], pred_boxes[:, 0]) y_min = torch.max(target_boxes[:, 1], pred_boxes[:, 1]) x_max = torch.min(target_boxes[:, 2], pred_boxes[:, 2]) y_max = torch.min(target_boxes[:, 3], pred_boxes[:, 3]) intersection = torch.max(torch.zeros(x_max.shape).cuda(), x_max - x_min) * torch.max(torch.zeros(y_max.shape).cuda(), y_max - y_min)
boxAArea = (target_boxes[:, 2] - target_boxes[:, 0]) * (target_boxes[:, 3] - target_boxes[:, 1]) boxBArea = (pred_boxes[:, 2] - pred_boxes[:, 0]) * (pred_boxes[:, 3] - pred_boxes[:, 1])
iou = intersection / (boxAArea + boxBArea - intersection) return iou
def forward(self, target_boxes, pred_boxes): mseloss = nn.MSELoss().forward(target_boxes, pred_boxes) iouloss = torch.mean(1 - self.calculate_iou(target_boxes, pred_boxes))
return mseloss + iouloss
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") net = Net().to(device)
criterion = LocationLoss() optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9)
def train_work(mode=None): ''' 训练阶段 ''' net.train() if mode == 'Update' and os.path.exists(save_dir): net.load_state_dict(torch.load(save_dir)) loss_over_time = [] for epoch in range(EPOCH): running_loss = 0.0 i = 0 for data in trainloader: i += 1 images, labels = data images, labels = images.to(device), labels.to(device)
optimizer.zero_grad() outputs = net(images) loss = criterion.forward(labels, outputs) loss.backward() optimizer.step()
running_loss += loss.item() if i % 10 == 9: avg_loss = running_loss/10 loss_over_time.append(avg_loss) print('[%d] loss: %.03f' % (epoch + 1, avg_loss)) running_loss = 0.0
with torch.no_grad(): correct = 0 total = 0 for data in trainloader: images, labels = data images, labels = images.to(device), labels.to(device) outputs = net(images) predicted = torch.nn.functional.one_hot(torch.argmax(outputs[:, 4:7], dim=1), num_classes=n_classes) correct += torch.mean((predicted.float() == labels[:, 4:7].float()).float()).item() total += 1 print('第%d个epoch的识别准确率为:%d%%' % (epoch + 1, (100 * correct / total))) print('Finished Training') torch.save(net.state_dict(), save_dir) return loss_over_time
def show_loss(training_loss): ''' 可视化损失变化 ''' plt.plot(training_loss) plt.xlabel('10\'s of batches') plt.ylabel('loss') plt.ylim(0, 2.5) plt.show()
def show_predicted(): ''' 展示预测结果 ''' img, label = testloader.dataset.__getitem__(0) image = img.unsqueeze(0).to(device) label = label.unsqueeze(0)
net.load_state_dict(torch.load(save_dir)) net.eval()
outputs = net(image) predict_cate = torch.argmax(outputs[:, 4:7], dim=1) truth_cate = torch.argmax(label[:, 4:7], dim=1)
img = img * 0.5 + 0.5 pil_transform = transforms.ToPILImage()
truth_rect = label[:, :4] * input_dim predict_rect = outputs[:, :4] * input_dim print(truth_rect) print(predict_rect)
origin = np.array(pil_transform(img), dtype=np.uint8)
x_min, y_min, x_max, y_max = truth_rect.squeeze()[:4] cv.rectangle(origin, (x_min, y_min), (x_max, y_max), (0, 255, 0), thickness=2) x_min, y_min, x_max, y_max = predict_rect.squeeze()[:4] cv.rectangle(origin, (x_min, y_min), (x_max, y_max), (0, 0, 255), thickness=2) cv.imwrite('box_detector.png', origin)
if __name__ == "__main__": training_loss = train_work('Update') show_loss(training_loss) show_predicted()
|