Skip to content

Instantly share code, notes, and snippets.

View training_entry_point.py
def main():
params = create_default_parser(__file__).parse_args()
super_seed(params.seed)
loaders, meta = create_data_loaders(dataset_name=params.dataset,
dataset_root=params.dataset_path,
num_workers=params.num_workers,
batch_size=params.batch_size,
sample_transformer=transforms.Compose([
View lightning_experiment.py
class BasicCNNExperiment(BaseExperiment):
def create_model(self) -> nn.Module:
return nn.Sequential(
nn.Conv2d(3, 16, 3),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 3),
nn.ReLU(inplace=True),
nn.AdaptiveMaxPool2d(1),
nn.Flatten(1, -1),
View cli_small.py
def basic_parser(script_filename: str = None) -> ArgumentParser:
experiment_name = (
Path(script_filename).stem
if script_filename is not None
else None
)
datasets = available_datasets()
p = ArgumentParser()
p.add_argument('--dataset_path', required=True)
p.add_argument('--dataset', choices=datasets, default=datasets[0])
View create_data_loaders.py
def create_data_loaders(
dataset_root: str,
dataset_name: str,
sample_transformer: Callable = None,
target_transformer: Callable = None,
num_workers: int = cpu_count(),
batch_size: int = 32,
download: bool = True,
pin_memory: bool = False,
) -> Tuple[OrderedDict, Dict]:
View error_messages.py
import numpy as np
class MathErrors:
ZeroDivision = 'division failed: argument {argument} is zero.'
NaN = 'multiplication failed: argument {argument} is not a number.'
OutOfRange = 'value {argument} should be in range [{a}, {b}), but {value} received'
def divide(a: float, b: float) -> float:
@devforfu
devforfu / traceback_decorator.py
Last active Jun 23, 2020
Traceback logging decorator
View traceback_decorator.py
import logging
import traceback
from typing import Callable
def report_failure_traceback(logger: logging.Logger, exc: Exception) -> None:
logger.critical('*** Unexpected error ***')
logger.critical('')
lines = traceback.format_tb(exc.__traceback__)
lines = ''.join(lines).split('\n')
@devforfu
devforfu / dicom.txt
Created Nov 20, 2019
Example of DICOM file content
View dicom.txt
(0008, 0018) SOP Instance UID UI: ID_3d2ea4e22
(0008, 0060) Modality CS: 'CT'
(0010, 0020) Patient ID LO: 'ID_e758d530'
(0020, 000d) Study Instance UID UI: ID_ebb91b8154
(0020, 000e) Series Instance UID UI: ID_57e1457f93
(0020, 0010) Study ID SH: ''
(0020, 0032) Image Position (Patient) DS: ['-125', '26', '272.300049']
(0020, 0037) Image Orientation (Patient) DS: ['1', '0', '0', '0', '1', '0']
(0028, 0002) Samples per Pixel US: 1
(0028, 0004) Photometric Interpretation CS: 'MONOCHROME2'
@devforfu
devforfu / example.py
Last active Aug 25, 2019
Catalyst example. (Doesn't work as expected).
View example.py
import os
import re
from pdb import set_trace
from multiprocessing import cpu_count
from pprint import pprint as pp
from imageio import imread
import numpy as np
import pandas as pd
import PIL.Image
@devforfu
devforfu / concat_and_make_cli.py
Last active Jul 2, 2019
Converting function signature into CLI
View concat_and_make_cli.py
import argparse
from pathlib import Path
import signature
def concat(folder: str, output: str = 'concat.txt', include_names: bool = True):
"""Concatenates a list of files from a folder and saves them into a single file."""
p = Path(folder)
files = []
for filename in p.iterdir():
@devforfu
devforfu / parsing_signature.py
Last active Jun 30, 2019
Construct CLI from a signature
View parsing_signature.py
import argparse
import signature
def make_cli(func: callable):
parser = argparse.ArgumentParser()
sig = inspect.signature(func)
empty = inspect._empty
for param in sig.parameters.values():
annot = param.annotation