实验结论请看github
voc.py
'''
@Author : {AishuaiYao}
@License : (C) Copyright 2013-2020, {None}
@Contact : {aishuaiyao@163.com}
@Software: ${segmentation}
@File : ${voc}.py
@Time : ${2020-04-04}
@Desc : deconvlution experiment
'''
import os
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.optim.lr_scheduler as lr_scheduler
from torchsummary import summary
from torchvision import transforms,datasets
from torch.utils.data import Dataset,DataLoader
from FCN.fcn import *
import numpy as np
import cv2
from PIL import Image
classes = ['background', 'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse', 'motorbike',
'person', 'potted plant', 'sheep', 'sofa', 'train',
'tv/monitor']
# RGB color for each class
colormap = [[0,0,0], [128,0,0], [0,128,0], [128,128,0], [0,0,128],
[128,0,128], [0,128,128], [128,128,128], [64,0,0], [192,0,0],
[64,128,0], [192,128,0], [64,0,128], [192,0,128], [64,128,128],
[192,128,128], [0,64,0], [128,64,0], [0,192,0], [128,192,0],
[0,64,128]]
voc_path = '../data/VOC2012'
BATCH_SIZE = 1
num_classes = 21
epochs = 200
input_size = 512
def read_images(path = voc_path, train = True):
file = path + '/ImageSets/Segmentation/' + ('train.txt' if train else 'val.txt')
with open(file) as f:
imgs = f.read().split()
datas = [path + '/JPEGImages/%s.jpg'%img for img in imgs]
labels = [path + '/SegmentationClass/%s.png'%img for img in imgs]
return datas, labels
def preproccessing(datas,labels):
for i,img, label in enumerate(zip(datas, labels)):
img_canvas,label_canvas = img2label(img,label)
def img2label(img,label,canvas_size = input_size):
img = cv2.imread(img)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
label = cv2.imread(label)
label = cv2.cvtColor(label,cv2.COLOR_BGR2RGB)
max_width, max_height = canvas_size,canvas_size
height, width, channel = img.shape
pad_width = (max_width - width) // 2
pad_height = (max_height - height) // 2
img_canvas = np.full((max_width, max_height, 3), 0)
label_canvas = np.full((max_width, max_height, 3), 0)
img_canvas[pad_height: pad_height + height, pad_width: pad_width + width, :] = img
label_canvas[pad_height: pad_height + height, pad_width: pad_width + width, :] = label
transform = transforms.Co