Skip to content

Instantly share code, notes, and snippets.

View Diyago's full-sized avatar
🎯
Focusing

Insaf Ashrapov Diyago

🎯
Focusing
View GitHub Profile
class SimpleGNN(torch.nn.Module):
"""Original from http://pages.di.unipi.it/citraro/files/slides/Landolfi_tutorial.pdf"""
def __init__(self, dataset, hidden=64, layers=6):
super(SimpleGNN, self).__init__()
self.dataset = dataset
self.convs = torch.nn.ModuleList()
self.convs.append(GCNConv(in_channels=dataset.num_node_features,
out_channels=hidden))
for _ in range(1, layers):
augmented = aug_with_crop(image_size = 1024)(image=img, mask=mask)
image_aug = augmented['image']
mask_aug = augmented['mask']
def aug_with_crop(image_size = 256, crop_prob = 1):
return Compose([
RandomCrop(width = image_size, height = image_size, p=crop_prob),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
RandomRotate90(p=0.5),
Transpose(p=0.5),
ShiftScaleRotate(shift_limit=0.01, scale_limit=0.04, rotate_limit=0, p=0.25),
RandomBrightnessContrast(p=0.5),
RandomGamma(p=0.25),
model = Unet(backbone_name = 'efficientnetb0', encoder_weights='imagenet', encoder_freeze = False)
model.compile(optimizer = Adam(), loss=bce_jaccard_loss, metrics=[iou_score])
history = model.fit_generator(train_generator, shuffle =True,
epochs=50, workers=4, use_multiprocessing=True,
validation_data = test_generator,
verbose = 1, callbacks=callbacks)
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping, TensorBoard
# reduces learning rate on plateau
lr_reducer = ReduceLROnPlateau(factor=0.1,
cooldown= 10,
patience=10,verbose =1,
min_lr=0.1e-5)
# model autosave callbacks
mode_autosave = ModelCheckpoint("./weights/road_crop.efficientnetb0imgsize.h5",
monitor='val_iou_score',
test_generator = DataGeneratorFolder(root_dir = './data/road_segmentation_ideal/training',
image_folder = 'input/',
mask_folder = 'output/',
nb_y_features = 1)
train_generator = DataGeneratorFolder(root_dir = './data/road_segmentation_ideal/training',
image_folder = 'input/',
mask_folder = 'output/',
batch_size=4,
image_size=512,
def __getitem__(self, index):
data_index_min = int(index*self.batch_size)
data_index_max = int(min((index+1)*self.batch_size, len(self.image_filenames)))
indexes = self.image_filenames[data_index_min:data_index_max]
this_batch_size = len(indexes) # The last batch can be smaller than the others
X = np.empty((this_batch_size, self.image_size, self.image_size, 3), dtype=np.float32)
y = np.empty((this_batch_size, self.image_size, self.image_size, self.nb_y_features), dtype=np.uint8)
def __init__(self, root_dir=r'../data/val_test', image_folder='img/', mask_folder='masks/',
batch_size=1, image_size=768, nb_y_features=1,
augmentation=None,
suffle=True):
self.image_filenames = listdir_fullpath(os.path.join(root_dir, image_folder))
self.mask_names = listdir_fullpath(os.path.join(root_dir, mask_folder))
self.batch_size = batch_size
self.augmentation = augmentation
self.image_size = image_size
self.nb_y_features = nb_y_features
conda install -c conda-forge keras
pip install git+https://github.com/qubvel/efficientnet
pip install git+https://github.com/qubvel/classification_models.git
pip install git+https://github.com/qubvel/segmentation_models
pip install git+https://github.com/albu/albumentations
pip install tta-wrapper
@Diyago
Diyago / Get toronto road dataset.py
Last active November 25, 2021 01:27
Download Massachusetts Roads Dataset from https://www.cs.toronto.edu/~vmnih/data/
# based on https://github.com/BBarbosa/tflearn-image-recognition-toolkit/blob/
# 4a0528dcfb206b1e45997f2fbc097aafacfa0fa0/scripts/html_link_parser.py
import re
import argparse
from PIL import Image
from io import BytesIO
from bs4 import BeautifulSoup
from skimage import io as skio