def __init__(self, hparams): super().__init__() self.lr = hparams.lr self.net = UNet(num_classes=19, num_layers=hparams.num_layers, features_start=hparams.features_start, bilinear=hparams.bilinear)
def __init__(self, hparams): super(SegModel, self).__init__() self.root_path = hparams.root self.batch_size = hparams.batch_size self.learning_rate = hparams.lr self.net = UNet(num_classes=19) self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.35675976, 0.37380189, 0.3764753], std=[0.32064945, 0.32098866, 0.32325324]) ]) self.trainset = KITTI(self.root_path, split='train', transform=self.transform) self.testset = KITTI(self.root_path, split='test', transform=self.transform)
class SegModel(pl.LightningModule): ''' Semantic Segmentation Module This is a basic semantic segmentation module implemented with Lightning. It uses CrossEntropyLoss as the default loss function. May be replaced with other loss functions as required. It is specific to KITTI dataset i.e. dataloaders are for KITTI and Normalize transform uses the mean and standard deviation of this dataset. It uses the FCN ResNet50 model as an example. Adam optimizer is used along with Cosine Annealing learning rate scheduler. ''' def __init__(self, hparams): super(SegModel, self).__init__() self.root_path = hparams.root self.batch_size = hparams.batch_size self.learning_rate = hparams.lr self.net = UNet(num_classes=19) self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.35675976, 0.37380189, 0.3764753], std=[0.32064945, 0.32098866, 0.32325324]) ]) self.trainset = KITTI(self.root_path, split='train', transform=self.transform) self.testset = KITTI(self.root_path, split='test', transform=self.transform) def forward(self, x): return self.net(x) def training_step(self, batch, batch_nb): img, mask = batch img = img.float() mask = mask.long() out = self.forward(img) loss_val = F.cross_entropy(out, mask, ignore_index=250) return {'loss': loss_val} def configure_optimizers(self): opt = torch.optim.Adam(self.net.parameters(), lr=self.learning_rate) sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10) return [opt], [sch] def train_dataloader(self): return DataLoader(self.trainset, batch_size=self.batch_size, shuffle=True) def test_dataloader(self): return DataLoader(self.testset, batch_size=self.batch_size, shuffle=False)
def __init__(self, hparams): super().__init__() self.hparams = hparams self.root_path = hparams.root self.batch_size = hparams.batch_size self.learning_rate = hparams.lr self.net = UNet(num_classes=19, num_layers=hparams.num_layers, features_start=hparams.features_start, bilinear=hparams.bilinear) self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.35675976, 0.37380189, 0.3764753], std=[0.32064945, 0.32098866, 0.32325324]) ]) self.trainset = KITTI(self.root_path, split='train', transform=self.transform) self.validset = KITTI(self.root_path, split='valid', transform=self.transform)
class SegModel(pl.LightningModule): ''' Semantic Segmentation Module This is a basic semantic segmentation module implemented with Lightning. It uses CrossEntropyLoss as the default loss function. May be replaced with other loss functions as required. It uses the FCN ResNet50 model as an example. Adam optimizer is used along with Cosine Annealing learning rate scheduler. ''' def __init__(self, hparams): super().__init__() self.lr = hparams.lr self.net = UNet(num_classes=19, num_layers=hparams.num_layers, features_start=hparams.features_start, bilinear=hparams.bilinear) def forward(self, x): return self.net(x) def training_step(self, batch, batch_nb): img, mask = batch img = img.float() mask = mask.long() out = self(img) loss_val = F.cross_entropy(out, mask, ignore_index=250) self.log('train_loss', loss_val) # log training loss return loss_val def validation_step(self, batch, batch_idx): img, mask = batch img = img.float() mask = mask.long() out = self(img) loss_val = F.cross_entropy(out, mask, ignore_index=250) self.log('val_loss', loss_val) # will be automatically averaged over an epoch def configure_optimizers(self): opt = torch.optim.Adam(self.net.parameters(), lr=self.lr) sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10) return [opt], [sch]
def train(model_type): print('Model "{}" is selected.'.format(model_type)) df_train = pd.read_csv(HERE + '../../../data/train_masks.csv') ids_train = df_train['img'].map(lambda s: s.split('.')[0]) ids_train_split, ids_valid_split = train_test_split(ids_train, test_size=0.2, random_state=42) print('Training on {} samples'.format(len(ids_train_split))) print('Validating on {} samples'.format(len(ids_valid_split))) input_shape = (128, 128) batch_size = 32 if model_type == 'unet': model = UNet(input_shape=(input_shape[0], input_shape[1], 3)) elif model_type == 'linknet': model = LinkNet(input_shape=(input_shape[0], input_shape[1], 3)) else: raise RuntimeError('Model "{}" is not found.'.format(model_type)) model.build() print(model.model.summary()) callbacks = [EarlyStopping(monitor='val_loss', patience=8, verbose=1, min_delta=1e-4), ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=4, verbose=1, epsilon=1e-4), ModelCheckpoint(monitor='val_loss', filepath='weights/best_weights_{}.hdf5'.format(input_shape[0]), save_best_only=True, save_weights_only=True), TensorBoard(log_dir='logs/input_size_{}'.format(input_shape[0]))] train_gen = train_generator( ids_train_split=ids_train_split, train_dir=os.path.abspath(HERE + '../../../data/train/'), train_masks_dir=os.path.abspath(HERE + '../../../data/train_masks/'), batch_size=batch_size, input_shape=input_shape ) valid_gen = valid_generator( ids_valid_split=ids_valid_split, valid_dir=os.path.abspath(HERE + '../../../data/train/'), valid_masks_dir=os.path.abspath(HERE + '../../../data/train_masks/'), batch_size=batch_size, input_shape=input_shape ) model.fit( train_generator=train_gen, steps_per_epoch=np.ceil(float(len(ids_train_split)) / float(batch_size)), epochs=100, verbose=2, callbacks=callbacks, validation_data=valid_gen, validation_steps=np.ceil(float(len(ids_valid_split)) / float(batch_size)) )