Basic experiment for deeplearning
a tutorial about basic operations in deeplearning
- common used type convert
- draw a plot
- data augmentation in fastai
- Patch操作
- aug_transforms
- Kornia for image operation
- numpy tricks
- mmsegmentation
from fastcore.basics import *
from fastai.vision.all import *
from fastai.torch_basics import *
import warnings
warnings.filterwarnings("ignore")
set_seed(66)
aa = L(1,2,3)
aa
aa[2]
# map function (lazy generator)
aa = range(10)
bb = map(lambda o:o**2,aa)
L(bb)
# from list to tensor
aa2 = tensor(aa)
aa2
# convert to different device
to_device(aa2,'cpu')
to_device(aa2,'cuda:1')
aa2.cuda()
to_cpu(aa2)
# from tensor to list
aa3 = to_np(aa2)
aa3
TEST_IMAGE
im = PILImage.create('/home/ubuntu/sharedData/swp/dlLabSwp/favourite/fastai/nbs/images/puppy.jpg')
im
type(im)
tensorIm = image2tensor(im)
tensorIm
type(tensorIm)
im2 = to_image(tensorIm)
type(im2)
im.shape
tensorIm.shape
axs = get_grid(4,2,2,figsize=(10,10),title='just for a test')
# [i.set_axis_off() for i in axs]
im.show(ctx=axs[1])
im.show(ctx=axs[0])
im.show(ctx=axs[3])
DihedralItem
set_seed(105)
train_a_path = Path("/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/top")
label_a_path = Path("/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/gts_for_participants/")
dsm_path = Path("/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/dsm/")
ndsm_path = Path("/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Potsdam/1_dsm_normalisation/")
imgNames = get_image_files(train_a_path)
lblNames = get_image_files(label_a_path)
dsmNames = get_image_files(dsm_path)
imgN = imgNames[random.randint(1,10)]
lblN = [random.randint(1,10)]
imgN = PILImage.create(imgN)
type(imgN)
# imgN
imgNames
_,axs = subplots(2, 4)
for ax in axs.flatten():
# p control the probability of the transform
print(1)
# show_image(DihedralItem(p=1.)(imgN, split_idx=0), ctx=ax)
分别从original与DSM,GT中分别取出来一个512*512的patch
dsmNames
imgNames
# data
# imgNames[0]= Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/top/top_mosaic_09cm_area17.tif')
# lblNames[0]= Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/gts_for_participants/top_mosaic_09cm_area17.tif')
# dsmNames[0]=Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Vaihingen/ISPRS_semantic_labeling_Vaihingen/dsm/dsm_09cm_matching_area17.tif')
# data
imgNames[0]= Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Potsdam/2_Ortho_RGB/top_potsdam_2_11_RGB.tif')
lblNames[0]= Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Potsdam/5_labels_for_participants/top_potsdam_2_11_label.tif')
dsmNames[0]=Path('/home/ubuntu/sharedData/swp/dlLab/fastaiRepository/fastai/data/rsData/kaggleOriginal/Potsdam/1_dsm/1_DSM/dsm_potsdam_02_11.tif')
from matplotlib import colors
def colormap():
# #FFFFFF #0000FF #00FFFF #00FF00 #FFFF00 #FF0000
cdict = ['#FFFFFF', '#0000FF', '#00FFFF', '#00FF00', '#FFFF00']
# 按照上面定义的colordict,将数据分成对应的部分,indexed:代表顺序
return colors.ListedColormap(cdict, 'indexed')
my_cmap = colormap()
# 和上面求得的结果值一样,将其写成固定值
r2gValues = {0: 255, 1: 29, 2: 179, 3: 150, 4: 226, 5: 76}
# print(f'original palette grey value:{pixel2Class}')
# print(f'mapped value:{r2gValues}')
codes = ['ImpSurf', 'Building', 'LowVeg', 'Tree', 'Car', 'Clutter']
codeNums = [0, 1, 2, 3, 4, 5]
# %%
def getMappedMask(dataPath, pixel2Class=r2gValues):
" 将RGB的值转为对应的连续灰度值,同时替换雾气image中的特殊标志名,r2gValues中,(0,1,2,3,4,5)就是将PILMask.create找到的RGB值对应成相应的连续灰度值,不再是原先那么大的数字"
#
# 替换雾气image中的特殊标志名
# map the original label value to the r2gValues, turn each pixel value to the corresponding grey value and limit to {0 - 5}
# # r2gValues中,(0,1,2,3,4,5)就是将PILMask.create找到的RGB值对应成相应的连续灰度值,不再是原先那么大的数字
# _temp = re.sub(r"(thin|moderate|thick)", "mosaic", fname.name)
# # 插入label的字样 \1 代表的是在符合正则表达式匹配的位置处插入,是定位符
# _temp = re.sub(r"(area[0-9]{1,2})", "\\1_label", _temp)
# lbl_name = dataPath/str(_temp)
# lbl_name = path_lbl / str(re.sub('RGB', 'label', fname.name))
# take the original
_originalLb = PILMask.create(dataPath)
arrays = np.array(_originalLb)
for k, v in pixel2Class.items():
arrays[arrays == v] = k
return PILMask.create(arrays)
# show the dsm images
rgbImage = PILImage.create(imgNames[0])
# lbImage = PILMask.create(lblNames[0])
lbImage = getMappedMask(lblNames[0])
dsmImage = PILImage.create(dsmNames[0],mode="F")
rgbImage.show(figsize=(10,10))
lbImage.show(figsize=(10,10),cmap=my_cmap)
dsmImage.show(figsize=(10,10), cmap='Greys')
# 同时取一个512*512的小块
_,axs = plt.subplots(1,3,figsize=(12,4))
f = Resize(512)
show_image(f(rgbImage), ctx=axs[0])
show_image(f(lbImage), ctx=axs[1],cmap=my_cmap)
show_image(f(dsmImage), ctx=axs[2],cmap='Greys')
tensor(rgbImage).shape
tfms = [Rotate(), Zoom(), Warp(), Brightness(), Flip(), Contrast(),RandomErasing(p=1., max_count=6)]
comp = setup_aug_tfms(tfms)
len(comp)
rgbTensor = image2tensor(rgbImage)
下面这一步类型转换非常重要,不然下面的转化无法进行
rgbTensor2 =torch.tensor(rgbTensor,dtype=torch.float32)
type(rgbTensor2)
comp[0](rgbTensor2)
comp
tfms = aug_transforms(pad_mode='zeros', mult=2, min_scale=0.5)
for t in tfms: y = t(rgbTensor2, split_idx=0)
_,axs = plt.subplots(1,3, figsize=(12,3))
for i,ax in enumerate(axs.flatten()): show_image(y[i], ctx=ax)
tfms
len(tfms)
import kornia
from kornia.constants import Resample
from kornia.color import *
from kornia import augmentation as K
import kornia.augmentation as F
import kornia.augmentation.random_generator as rg
from torchvision.transforms import functional as tvF
from torchvision.transforms import transforms
import matplotlib.pyplot as plt
import numpy as np
to_tensor = transforms.ToTensor()
to_pil = transforms.ToPILImage()
rgbImage = Image.open(imgNames[0])
lblImage = Image.open(lblNames[0])
dsmImage = Image.open(dsmNames[0])
# dsmImage = PILImage.create(dsmNames[0],mode='F')
# rgbImage
aug = K.AugmentationSequential(
K.RandomRotation(degrees=[80,80], return_transform=True, p=1.),
K.CenterCrop(256, p=1., cropping_mode="resample"),
K.ColorJitter(0.06, 0.06, 0.06, 0, p=1.),
data_keys=["input"], # Just to define the future input here.
return_transform=False,
same_on_batch=False,
)
aug2 = K.AugmentationSequential(
K.RandomRotation(degrees=[80,80], return_transform=True, p=1.),
K.CenterCrop(256, p=1., cropping_mode="resample"),
data_keys=["input"], # Just to define the future input here.
return_transform=False,
same_on_batch=False,
)
rgbTensor = to_tensor(rgbImage)
lblTensor = to_tensor(lblImage)
dsmTensor = to_tensor(dsmImage)
# forward the operation
out_rgb_tensors = aug(rgbTensor)
out_lbl_tensors = aug(lblTensor)
out_dsm_tensors = aug2(grayscale_to_rgb(dsmTensor))
rgbTrans = to_pil(out_rgb_tensors.squeeze())
lblTrans = to_pil(out_lbl_tensors.squeeze())
dsmTrans = to_pil(out_dsm_tensors.squeeze())
fig,axs = plt.subplots(2,3,figsize=(12,8))
fig.set_tight_layout(True)
show_image(rgbImage,ctx=axs[0][0])
show_image(lblImage,ctx=axs[0][2])
show_image(dsmImage,ctx=axs[0][1], cmap='Greys')
show_image(rgbTrans,ctx=axs[1][0])
show_image(lblTrans,ctx=axs[1][2])
show_image(dsmTrans,ctx=axs[1][1], cmap='Greys')
# fig.savefig(f'/home/ubuntu/sharedData/swp/dlLabSwp/testImage/potsdamDataset.png',dpi=500)
show_image(rgbTrans,figsize=(5,5))
out_rgb_tensors.shape
from torch.functional import F
testPad = (123,123,123,123)
padOutRGBTensor = F.pad(out_rgb_tensors,testPad,mode='reflect')
padOutRGBTensor.shape
show_image(to_pil(padOutRGBTensor.squeeze()),figsize=(5,5))
# 探究 np.where, np.all, np.any
# set_seed(11)
aa = np.random.randint(0,255,(2,2,3))
aa.shape
aa
aa[0]
i = np.array((253,160,45)).reshape(1,1,3)
i
bb = np.all(aa==i,axis=2)
bb
bb.shape
aa[bb]
aa[bb].shape