Beispiel #1
0
os.chdir(os.path.join(base_dir, 'src/training/'))
from sampling import DataLoader, CSVDataset
from sampling import transforms as tx
from models import create_unet_model3D

from keras import callbacks as cbks

# tx.Compose lets you string together multiple transforms
co_tx = tx.Compose([
    tx.TypeCast('float32'),
    tx.ExpandDims(axis=-1),
    tx.RandomAffine(
        rotation_range=(-15, 15),  # rotate btwn -15 & 15 degrees
        translation_range=(
            0.1, 0.1),  # translate btwn -10% and 10% horiz, -10% and 10% vert
        shear_range=(-10, 10),  # shear btwn -10 and 10 degrees
        zoom_range=(0.85, 1.15),  # between 15% zoom-in and 15% zoom-out
        turn_off_frequency=5,
        fill_value='min',
        target_fill_mode='constant',
        target_fill_value=0
    )  # how often to just turn off random affine transform (units=#samples)
])

input_tx = tx.MinMaxScaler((-1, 1))  # scale between -1 and 1

target_tx = tx.OneHot(
)  # convert segmentation image to One-Hot representation for cross-entropy loss

# use a co-transform, meaning the same transform will be applied to input+target images at the same time
# this is necessary since Affine transforms have random parameter draws which need to be shared
base_dir = '/users/ncullen/desktop/projects/unet-ants/'
os.chdir(base_dir + 'code/')

# local imports
from sampling import DataLoader, CSVDataset
from sampling import transforms as tx
from models import create_unet_model2D

data_dir = base_dir + 'data_2D/'
results_dir = base_dir + 'results_2D/'

# tx.Compose lets you string together multiple transforms
co_tx = tx.Compose([
    tx.MinMaxScaler((-1, 1)),
    tx.ExpandDims(
        axis=-1
    )  # expand from (128,128) to (128,128,1) -> RandomAffine and Keras expect that
])

# use a co-transform, meaning the same transform will be applied to input+target images at the same time
# this is necessary since Affine transforms have random parameter draws which need to be shared
dataset = CSVDataset(
    filepath=data_dir + 'image_filemap.csv',
    base_path=
    data_dir,  # this path will be appended to all of the filenames in the csv file
    input_cols=[
        'images'
    ],  # column in dataframe corresponding to inputs (can be an integer also)
    target_cols=[
        'images'
    ],  # column in dataframe corresponding to targets (can be an integer also)
from sampling import DataLoader, CSVDataset
from sampling import transforms as tx
from models import create_unet_model2D


data_dir = base_dir + 'data_2D/'
results_dir = base_dir+'results_2D/'

input_tx = tx.MinMaxScaler((0,1)) # scale input images between 0 and 1
target_tx = tx.BinaryMask(cutoff=0.5) # convert target segmentation to a binary mask

# tx.Compose lets you string together multiple transforms
co_tx = tx.Compose([tx.ExpandDims(axis=-1), # expand from (128,128) to (128,128,1) -> RandomAffine and Keras expect that
                    tx.RandomAffine(rotation_range=(-15,15), # rotate btwn -15 & 15 degrees
                                    translation_range=(0.1,0.1), # translate btwn -10% and 10% horiz, -10% and 10% vert
                                    shear_range=(-10,10), # shear btwn -10 and 10 degrees
                                    zoom_range=(0.85,1.15), # between 15% zoom-in and 15% zoom-out
                                    turn_off_frequency=5) # how often to just turn off random affine transform (units=#samples)
                    ])

# use a co-transform, meaning the same transform will be applied to input+target images at the same time 
# this is necessary since Affine transforms have random parameter draws which need to be shared
dataset = CSVDataset(filepath=data_dir+'image_filemap.csv', 
                    base_path=data_dir, # this path will be appended to all of the filenames in the csv file
                    input_cols=['images'], # column in dataframe corresponding to inputs (can be an integer also)
                    target_cols=['masks'],# column in dataframe corresponding to targets (can be an integer also)
                    input_transform=input_tx, target_transform=target_tx, co_transform=co_tx)


# split into train and test set based on the `train-test` column in the csv file
# this splits alphabetically by values, and since 'test' comes before 'train' thus val_data is returned before train_data

data_dir = base_dir + 'data_3D/'
results_dir = base_dir+'results_3D/'
try:
    os.mkdir(results_dir)
except:
    pass

# tx.Compose lets you string together multiple transforms
co_tx = tx.Compose([tx.TypeCast('float32'),
                    tx.MinMaxScaler((-1,1)), # scale between -1 and 1
                    tx.ExpandDims(axis=-1), # expand from (128,128,128) to (128,128,128,1) -> RandomAffine and Keras expect that
                    tx.RandomAffine(rotation_range=(-15,15), # rotate btwn -15 & 15 degrees
                                    translation_range=(0.1,0.1), # translate btwn -10% and 10% horiz, -10% and 10% vert
                                    shear_range=(-10,10), # shear btwn -10 and 10 degrees
                                    zoom_range=(0.85,1.15), # between 15% zoom-in and 15% zoom-out
                                    turn_off_frequency=5,
                                    fill_value='min',
                                    target_fill_mode='constant',
                                    target_fill_value='min') # how often to just turn off random affine transform (units=#samples)
                    ])

# use a co-transform, meaning the same transform will be applied to input+target images at the same time 
# this is necessary since Affine transforms have random parameter draws which need to be shared
dataset = CSVDataset(filepath=data_dir+'image_filemap.csv', 
                    base_path=os.path.join(data_dir,'images'), # this path will be appended to all of the filenames in the csv file
                    input_cols=['Images'], # column in dataframe corresponding to inputs (can be an integer also)
                    target_cols=['Images'],# column in dataframe corresponding to targets (can be an integer also)
                    co_transform=co_tx)