def main(): """ """ pyplot.style.use("bmh") base_path = Path.home() / "Data" / "Datasets" / "Clouds" resized_loc = base_path / "resized" save_model_path = PROJECT_APP_PATH.user_data / "cloud_seg.model" SEED = 87539842 batch_size = 8 num_workers = 0 seed_stack(SEED) min_size = (10000, 10000, 10000, 10000) train_loader = DataLoader( CloudSegmentationDataset( df_path=base_path / "train.csv", resized_loc=resized_loc, subset=SplitEnum.training, ), batch_size=batch_size, shuffle=True, num_workers=num_workers, ) valid_loader = DataLoader( CloudSegmentationDataset( df_path=base_path / "train.csv", resized_loc=resized_loc, subset=SplitEnum.validation, ), batch_size=batch_size, shuffle=False, num_workers=num_workers, ) model = SkipHourglassFission( CloudSegmentationDataset.predictors_shape[-1], CloudSegmentationDataset.response_shape, encoding_depth=2, ) model.to(global_torch_device()) if save_model_path.exists(): model.load_state_dict(torch.load( str(save_model_path))) # load last model criterion = BCEDiceLoss(eps=1.0) optimiser = torch.optim.SGD(model.parameters(), lr=0.01) current_lr = next(iter(optimiser.param_groups))["lr"] scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimiser, 7, eta_min=current_lr / 100, last_epoch=-1) model = train_d( model, train_loader, valid_loader, criterion, optimiser, scheduler, str(save_model_path), ) if save_model_path.exists(): model.load_state_dict(torch.load( str(save_model_path))) # load best model model.eval() # %% valid_masks = [] count = 0 tr = min(len(valid_loader.dataset) * 4, 2000) probabilities = numpy.zeros((tr, 350, 525), dtype=numpy.float32) for data, target in tqdm(valid_loader): data = data.to(global_torch_device()) target = target.cpu().detach().numpy() outpu, *_ = model(data) outpu = torch.sigmoid(outpu).cpu().detach().numpy() for p in range(data.shape[0]): output, mask = outpu[p], target[p] for m in mask: valid_masks.append(resize_image_cv(m)) for probability in output: probabilities[count, :, :] = resize_image_cv(probability) count += 1 if count >= tr - 1: break if count >= tr - 1: break class_parameters = grid_search(model, probabilities, valid_masks, valid_loader) submission( model, class_parameters, base_path=base_path, batch_size=batch_size, resized_loc=resized_loc, )
def main( base_path: Path = Path.home() / "Data" / "Datasets" / "PennFudanPed", train_model: bool = True, load_prev_model: bool = True, writer: Writer = TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "instanced_person_segmentation" / f"{time.time()}"), ): """ """ # base_path = Path("/") / "encrypted_disk" / "heider" / "Data" / "PennFudanPed" base_path: Path = Path.home() / "Data3" / "PennFudanPed" # base_path = Path('/media/heider/OS/Users/Christian/Data/Datasets/') / "PennFudanPed" pyplot.style.use("bmh") save_model_path = ( ensure_existence(PROJECT_APP_PATH.user_data / "models") / "instanced_penn_fudan_ped_seg.model") eval_model = not train_model SEED = 9221 batch_size = 32 num_workers = 0 encoding_depth = 2 learning_rate = 6e-6 # sequence 6e-2 6e-3 6e-4 6e-5 seed_stack(SEED) train_set = PennFudanDataset( base_path, SplitEnum.training, return_variant=PennFudanDataset.PennFudanReturnVariantEnum.instanced, ) train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=num_workers) valid_loader = DataLoader( PennFudanDataset( base_path, SplitEnum.validation, return_variant=PennFudanDataset.PennFudanReturnVariantEnum. instanced, ), batch_size=batch_size, shuffle=False, num_workers=num_workers, ) model = SkipHourglassFission( input_channels=train_set.predictor_shape[-1], output_heads=(train_set.response_shape[-1], ), encoding_depth=encoding_depth, ) model.to(global_torch_device()) if load_prev_model and save_model_path.exists(): model.load_state_dict(torch.load(str(save_model_path))) print("loading saved model") if train_model: with TorchTrainSession(model): criterion = BCEDiceLoss() # optimiser = torch.optim.SGD(model.parameters(), lr=learning_rate) optimiser = torch.optim.Adam(model.parameters(), lr=learning_rate) # scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimiser, T_max=7, eta_min=learning_rate / 100, last_epoch=-1 ) model = train_person_segmentor( model, train_loader, valid_loader, criterion, optimiser, save_model_path=save_model_path, learning_rate=learning_rate, writer=writer, ) if eval_model: validate_model(model, valid_loader)
def main(model_name: str = "maskrcnn_pennfudanped", score_threshold=0.55): """ Args: model_name: score_threshold: """ base_path = PROJECT_APP_PATH.user_data / "maskrcnn" dataset_root = Path.home() / "Data" seed_stack(3825) dataset = ( PennFudanDataset # (dataset_root / "PennFudanPed", SplitEnum.training) ) categories = dataset.categories if True: model = load_model(model_name=model_name, model_directory=base_path / "models") else: model = get_pretrained_instance_segmentation_maskrcnn( dataset.response_channels) model.to(global_torch_device()) cpu_device = torch.device("cpu") with torch.no_grad(): with TorchEvalSession(model): for image in tqdm( to_tensor_generator( frame_generator(cv2.VideoCapture(0)), device=global_torch_device(), )): prediction = model( # torch_vision_normalize_batch_nchw( uint_hwc_to_chw_float_tensor(image).unsqueeze(0) # ) )[0] (boxes, labels, scores) = ( prediction["boxes"].to(cpu_device).numpy(), prediction["labels"].to(cpu_device).numpy(), torch.sigmoid( prediction["scores"]).to(cpu_device).numpy(), ) indices = scores > score_threshold if show_image( draw_bounding_boxes( quick_to_pil_image(image), boxes[indices], labels=labels[indices], scores=scores[indices], categories=categories, ), model_name, wait=True, ): break # esc to quit
def export_detection_model( model_export_path: Path = ensure_existence( PROJECT_APP_PATH.user_data / "penn_fudan_segmentation" ) / "seg_skip_fis", SEED: int = 87539842, ) -> None: """ :param model_export_path: :type model_export_path: :return: :rtype:""" model = OutputActivationModule( SkipHourglassFission(input_channels=3, output_heads=(1,), encoding_depth=1) ) with TorchDeviceSession(device=global_torch_device("cpu"), model=model): with TorchEvalSession(model): seed_stack(SEED) # standard PyTorch mean-std input image normalization transform = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ] ) frame_g = frame_generator(cv2.VideoCapture(0)) for image in tqdm(frame_g): example_input = ( transform(image).unsqueeze(0).to(global_torch_device()), ) try: traced_script_module = torch.jit.trace( model, example_input, # strict=strict_jit, check_inputs=( transform(next(frame_g)) .unsqueeze(0) .to(global_torch_device()), transform(next(frame_g)) .unsqueeze(0) .to(global_torch_device()), ), ) exp_path = model_export_path.with_suffix(".traced") traced_script_module.save(str(exp_path)) print( f"Traced Ops used {torch.jit.export_opnames(traced_script_module)}" ) sprint( f"Successfully exported JIT Traced model at {exp_path}", color="green", ) except Exception as e_i: sprint(f"Torch JIT Trace export does not work!, {e_i}", color="red") break
def main( dataset_root=Path.home() / "DataWin" / "Datasets", model_name=f"maskrcnn_pennfudanped", ): """ """ base_path = ensure_existence(PROJECT_APP_PATH.user_data / "maskrcnn") log_path = ensure_existence(PROJECT_APP_PATH.user_log / "maskrcnn") export_root = ensure_existence(base_path / "models") batch_size = 4 num_epochs = 10 optimiser_spec = GDKC(torch.optim.Adam, lr=3e-4) scheduler_spec = GDKC( torch.optim.lr_scheduler. StepLR, # a learning rate scheduler which decreases the learning rate by step_size=3, # 10x every 3 epochs gamma=0.1, ) num_workers = 0 seed_stack(3825) dataset = PennFudanDataset( dataset_root / "PennFudanPed", SplitEnum.training, return_variant=PennFudanDataset.PennFudanReturnVariantEnum.all, ) dataset_validation = PennFudanDataset( dataset_root / "PennFudanPed", SplitEnum.validation, return_variant=PennFudanDataset.PennFudanReturnVariantEnum.all, ) split = SplitIndexer(len(dataset), validation=0.3, testing=0) split_indices = torch.randperm(split.total_num).tolist() data_loader = DataLoader( Subset(dataset, split_indices[:-split.validation_num]), batch_size=batch_size, shuffle=True, num_workers=num_workers, collate_fn=collate_first_dim, ) data_loader_val = DataLoader( Subset(dataset_validation, split_indices[-split.validation_num:]), batch_size=1, shuffle=False, num_workers=num_workers, collate_fn=collate_first_dim, ) model = get_pretrained_instance_segmentation_maskrcnn( dataset.response_channels) optimiser = optimiser_spec(trainable_parameters(model)) lr_scheduler = scheduler_spec(optimiser) if True: candidate = load_model( model_name=model_name, model_directory=export_root, raise_on_failure=False, ) if candidate: model = candidate if True: with TorchTrainSession(model): with TensorBoardPytorchWriter(log_path / model_name) as writer: for epoch_i in tqdm(range(num_epochs), desc="Epoch #"): maskrcnn_train_single_epoch( model=model, optimiser=optimiser, data_loader=data_loader, writer=writer, ) lr_scheduler.step() # update the learning rate maskrcnn_evaluate( model, data_loader_val, writer=writer ) # evaluate on the validation dataset save_model(model, model_name=model_name, save_directory=export_root) if True: with TorchEvalSession(model): # put the model in evaluation mode img, _ = dataset_validation[ 0] # pick one image from the test set with torch.no_grad(): prediction = model([img.to(global_torch_device())]) from matplotlib import pyplot pyplot.imshow( Image.fromarray( img.mul(255).permute(1, 2, 0).byte().numpy())) pyplot.show() import cv2 pyplot.imshow( Image.fromarray(prediction[0]["masks"][0, 0].mul( 255).byte().cpu().numpy())) pyplot.show() (boxes, labels, scores) = ( prediction[0]["boxes"].to("cpu").numpy(), prediction[0]["labels"].to("cpu").numpy(), torch.sigmoid(prediction[0]["scores"]).to("cpu").numpy(), ) from draugr.opencv_utilities import draw_bounding_boxes from draugr.torch_utilities.images.conversion import quick_to_pil_image indices = scores > 0.1 cv2.namedWindow(model_name, cv2.WINDOW_NORMAL) cv2.imshow( model_name, draw_bounding_boxes( quick_to_pil_image(img), boxes[indices], labels=labels[indices], scores=scores[indices], # categories=categories, ), ) cv2.waitKey()
def training_procedure(n_epoch=60, train: bool = True, load_previous: bool = True): train_set = SpeechCommands(split=Split.Training) waveform, sample_rate, category, speaker_id, utterance_number = train_set[ 0] transform = torch.nn.Sequential( torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=new_sample_rate)) model = M5(n_input=transform(waveform).shape[0], n_output=len(train_set.categories)) persistence_model_name = f'{model_name}_{get_model_hash(transform)}_{get_model_hash(model)}' if load_previous: candidate = load_model(model_name=persistence_model_name, model_directory=model_storage_path, raise_on_failure=False) if candidate: model = candidate transform.to(device) model.to(device) collate_fn = collate_transform_wrapped(train_set.label_to_index, transform) # print(f"Trainable parameters: {named_trainable_parameters(model)}") print( f"Number of trainable parameters: {get_num_parameters(model, only_trainable=True)}" ) if train: train_loader = DataLoader( train_set, batch_size=batch_size, shuffle=True, drop_last=drop_last_train, collate_fn=collate_fn, num_workers=num_workers, pin_memory=pin_memory, ) valid_loader = DataLoader( SpeechCommands(split=Split.Validation), batch_size=batch_size, shuffle=False, drop_last=False, collate_fn=collate_fn, num_workers=num_workers, pin_memory=pin_memory, ) optimiser = optim.Adam(trainable_parameters(model), lr=0.01, weight_decay=0.0001) scheduler = optim.lr_scheduler.StepLR( optimiser, step_size=20, gamma=0.1) # reduce the learning after 20 epochs by a factor of 10 with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / model_name / str(time.time())) as writer: best_valid_acc = math.inf for epoch in tqdm(range(1, n_epoch + 1), total=n_epoch, desc='Epoch #'): single_epoch_fitting(model, optimiser, train_loader, epoch=epoch, writer=writer, device_=device) scheduler.step() acc = single_epoch_evaluation(model, valid_loader, subset=Split.Validation, epoch=epoch, writer=writer, device=device) if acc < best_valid_acc: best_valid_acc = acc save_model(model, model_name=persistence_model_name, save_directory=model_storage_path) writer.blip('new_best_model', epoch) else: seed_stack(0) with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / model_name / str(time.time())) as writer: test_loader = DataLoader( SpeechCommands(split=Split.Testing), batch_size=1, shuffle=True, drop_last=False, collate_fn=collate_fn, num_workers=num_workers, pin_memory=pin_memory, ) acc = single_epoch_evaluation(model, test_loader, subset=Split.Testing, epoch=0, writer=writer, device=device) print(acc) find_n_misclassified(model, test_loader, mapper=train_set.index_to_label)
from neodroidaudition.classification import M5 from neodroidaudition.data import SpeechCommands from neodroidaudition.torch_utilities.collation import collate_transform_wrapped model_name, model_storage_path = 'm5_speech_command', PROJECT_APP_PATH.user_data / 'speech_command' / 'models' device = global_torch_device(True) if device == "cuda": num_workers = 1 pin_memory = True else: num_workers = 0 pin_memory = False drop_last_train = False seed_stack(0) batch_size = 256 new_sample_rate = 8000 def training_procedure(n_epoch=60, train: bool = True, load_previous: bool = True): train_set = SpeechCommands(split=Split.Training) waveform, sample_rate, category, speaker_id, utterance_number = train_set[ 0] transform = torch.nn.Sequential( torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=new_sample_rate))
def main(): """ """ pyplot.style.use("bmh") base_dataset_path = Path.home() / "Data" / "Datasets" / "Clouds" image_path = base_dataset_path / "resized" save_model_path = PROJECT_APP_PATH.user_data / "cloud_seg.model" SEED = 87539842 batch_size = 8 num_workers = 0 seed_stack(SEED) train_loader = DataLoader( CloudSegmentationDataset(base_dataset_path, image_path, subset=SplitEnum.training), batch_size=batch_size, shuffle=True, num_workers=num_workers, ) valid_loader = DataLoader( CloudSegmentationDataset(base_dataset_path, image_path, subset=SplitEnum.validation), batch_size=batch_size, shuffle=False, num_workers=num_workers, ) test_loader = DataLoader( CloudSegmentationDataset(base_dataset_path, image_path, subset=SplitEnum.testing), batch_size=batch_size, shuffle=False, num_workers=num_workers, ) model = SkipHourglassFission( CloudSegmentationDataset.predictor_channels, (CloudSegmentationDataset.response_channels, ), encoding_depth=1, ) model.to(global_torch_device()) if save_model_path.exists(): model.load_state_dict(torch.load( str(save_model_path))) # load last model print("loading previous model") criterion = BCEDiceLoss(eps=1.0) lr = 3e-3 optimiser = torch.optim.SGD(model.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( optimiser, 7, eta_min=lr / 100, last_epoch=-1) model = train_model( model, train_loader, valid_loader, criterion, optimiser, scheduler, save_model_path, ) if save_model_path.exists(): model.load_state_dict(torch.load( str(save_model_path))) # load best model model.eval() class_parameters = threshold_grid_search(model, valid_loader) for _, (data, target) in zip(range(2), valid_loader): data = data.to(global_torch_device(), dtype=torch.float) output, *_ = model(data) output = torch.sigmoid(output) output = output[0].cpu().detach().numpy() image_vis = data[0].cpu().detach().numpy() mask = target[0].cpu().detach().numpy() mask = chw_to_hwc(mask) output = chw_to_hwc(output) image_vis = float_chw_to_hwc_uint(image_vis) pr_mask = numpy.zeros(CloudSegmentationDataset.response_shape) for j in range(len(CloudSegmentationDataset.categories)): probability_ = output[..., j] thr, min_size = class_parameters[j][0], class_parameters[j][1] pr_mask[..., j], _ = threshold_mask(probability_, thr, min_size) CloudSegmentationDataset.visualise_prediction( image_vis, pr_mask, original_image=image_vis, original_mask=mask, raw_image=image_vis, raw_mask=output, ) prepare_submission(model, class_parameters, test_loader)