This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dls = db.dataloaders(source=imgs_path, bs=4, num_workers=0) | |
dls.show_batch(figsize=(10,10)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ds = db.datasets(source=imgs_path) | |
idx = 1 | |
img = ds.train[idx][0] | |
msk = ds.train[idx][1] | |
_, ax = plt.subplots(1, 2, figsize=(10, 5)) | |
img.show(ctx=ax[0], bright=2.) | |
msk.show(ctx=ax[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_lbl_fn(img_fn: Path): | |
lbl_path = img_fn.parent.parent/'labels' | |
lbl_name = img_fn.name | |
return (lbl_path/lbl_name) | |
db = DataBlock(blocks=(TransformBlock(type_tfms=partial(MSTensorImage.create, chnls_first=True)), | |
TransformBlock(type_tfms=[get_lbl_fn, partial(open_npy, cls=TensorMask)], | |
item_tfms=AddMaskCodes(codes=['clear', 'water', 'shadow'])), | |
), | |
get_items=partial(get_files, extensions='.npy'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mask = TensorMask(open_npy(msk_path)) | |
print(mask.shape) | |
_, ax = plt.subplots(1, 2, figsize=(10, 5)) | |
img.show(bright=3., ctx=ax[0]) | |
mask.show(ctx=ax[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img = MSTensorImage.create(img_path) | |
print(img) | |
_, ax = plt.subplots(1, 3, figsize=(12, 4)) | |
img.show(bright=3., ctx=ax[0]) | |
img.show(chnls=[2, 7, 10], ctx=ax[1]) | |
img.show(chnls=[11], ctx=ax[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_npy(fn, chnls=None, cls=torch.Tensor): | |
im = torch.from_numpy(np.load(str(fn))).type(torch.float32) | |
if chnls is not None: im = im[chnls] | |
return cls(im) | |
class MSTensorImage(TensorImage): | |
def __init__(self, x, chnls_first=False): | |
self.chnls_first = chnls_first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plotting a sample | |
_, ax = plt.subplots(1, 2, figsize=(10, 5)) | |
ax[0].imshow(img.transpose((1, 2, 0))[..., [3, 2, 1]]*3.0) | |
ax[1].imshow(msk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Checking files | |
path = Path('./Data/') | |
imgs_path = path/'images' | |
lbls_path = path/'labels' | |
print(f'Checking number of files - images:{len([f for f in imgs_path.iterdir()])}\ | |
masks:{len([f for f in lbls_path.iterdir()])}') | |
# Checking file shapes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def expand_block(in_channels, out_channels, kernel_size, padding): | |
expand = nn.Sequential( | |
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=padding), | |
torch.nn.BatchNorm2d(out_channels), | |
torch.nn.ReLU(), | |
torch.nn.Conv2d(out_channels, out_channels, kernel_size, stride=1, padding=padding), | |
torch.nn.BatchNorm2d(out_channels), | |
torch.nn.ReLU(), | |
torch.nn.ConvTranspose2d(out_channels, out_channels, kernel_size=3, stride=2, padding=1, output_padding=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __getitem__(self, idx): | |
x = torch.tensor(self.open_as_array(idx, invert=self.pytorch, include_nir=True), dtype=torch.float32) | |
y = torch.tensor(self.open_mask(idx, add_dims=False), dtype=torch.torch.int64) | |
return x, y | |
def open_as_pil(self, idx): | |
arr = 256*self.open_as_array(idx) |
NewerOlder