Exemplo n.º 1
0
def test_generator_1():

    # ### CONSTANTS
    HEIGHT, WIDTH, CHANNELS, IMG_COLOR_MODE, MSK_COLOR_MODE, NUM_CLASS, \
        KS1, KS2, KS3, DL1, DL2, DL3, NF, NFL, NR1, NR2, DIL_MODE, W_MODE, LS, \
        TRAIN_SIZE, VAL_SIZE, TEST_SIZE, DR1, DR2, CLASSES, IMG_CLASS = _Params()

    TRAIN_IMG_PATH, TRAIN_MSK_PATH, TRAIN_MSK_CLASS, VAL_IMG_PATH, \
        VAL_MSK_PATH, VAL_MSK_CLASS, TEST_IMG_PATH, TEST_MSK_PATH, TEST_MSK_CLASS = _Paths()

    TRAIN_SEED, VAL_SEED, TEST_SEED = _Seeds()

    test_data_gen_img_args = dict(rescale=1. / 255)

    if MSK_COLOR_MODE == 'rgb':
        test_data_gen_msk_args = dict(rescale=1. / 255)

    elif MSK_COLOR_MODE == 'grayscale':
        test_data_gen_msk_args = dict(preprocessing_function=to_test_indices)

    test_image_datagen = ImageDataGenerator(**test_data_gen_img_args)
    test_mask_datagen = ImageDataGenerator(**test_data_gen_msk_args)

    test_image_generator = test_image_datagen.flow_from_directory(
        TEST_IMG_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=[IMG_CLASS],
        color_mode=IMG_COLOR_MODE,
        class_mode=None,
        batch_size=TEST_SIZE,
        shuffle=False,
        seed=TEST_SEED)

    test_mask_generator = test_mask_datagen.flow_from_directory(
        TEST_MSK_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=CLASSES,
        color_mode=MSK_COLOR_MODE,
        class_mode=None,
        batch_size=TEST_SIZE,
        shuffle=False,
        seed=TEST_SEED)

    while True:
        if MSK_COLOR_MODE == 'rgb':
            yield (test_image_generator.next(), test_mask_generator.next())
        elif MSK_COLOR_MODE == 'grayscale':
            yield(test_image_generator.next(), to_one_hot_val(test_mask_generator.next(), \
                TRAIN_SIZE,HEIGHT,WIDTH,NUM_CLASS))
Exemplo n.º 2
0
def test_generator_2():

    # ### CONSTANTS
    HEIGHT, WIDTH, CHANNELS, IMG_COLOR_MODE, MSK_COLOR_MODE, NUM_CLASS, \
        KS1, KS2, KS3, DL1, DL2, DL3, NF, NFL, NR1, NR2, DIL_MODE, W_MODE, LS, \
        TRAIN_SIZE, VAL_SIZE, TEST_SIZE, DR1, DR2, CLASSES, IMG_CLASS = _Params()

    TRAIN_IMG_PATH, TRAIN_MSK_PATH, TRAIN_MSK_CLASS, VAL_IMG_PATH, \
        VAL_MSK_PATH, VAL_MSK_CLASS, TEST_IMG_PATH, TEST_MSK_PATH, TEST_MSK_CLASS = _Paths()

    TRAIN_SEED, VAL_SEED, TEST_SEED = _Seeds()

    test_data_gen_img_args = dict(rescale=1. / 255)
    test_data_gen_msk_args = dict(rescale=1. / 255)

    test_image_datagen = ImageDataGenerator(**test_data_gen_img_args)
    test_mask_datagen = ImageDataGenerator(**test_data_gen_msk_args)

    test_image_generator = test_image_datagen.flow_from_directory(
        TEST_IMG_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=[IMG_CLASS],
        color_mode=IMG_COLOR_MODE,
        class_mode=None,
        batch_size=TEST_SIZE,
        shuffle=False,
        seed=TEST_SEED,
        # save_to_dir='dataset/val_local/images/save',
    )

    for i in range(NUM_CLASS):
        globals()['test_mask_gen_{}'.format(
            i)] = test_mask_datagen.flow_from_directory(
                TEST_MSK_PATH,
                target_size=(HEIGHT, WIDTH),
                classes=[CLASSES[i]],
                color_mode=MSK_COLOR_MODE,
                class_mode=None,
                batch_size=TEST_SIZE,
                shuffle=False,
                seed=TEST_SEED,
                # save_to_dir='dataset/val_local/masks/save',
            )

    while True:
        yield(test_image_generator.next(), \
              np.round(np.squeeze(np.stack([globals()['test_mask_gen_{}'.format(i)].next() for i in range(NUM_CLASS)],axis=3))))
Exemplo n.º 3
0
from MODULES.Losses import weighted_tani_loss
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Activation, SeparableConv2D
from tensorflow.keras.layers import Conv2D, Concatenate, Add, LeakyReLU
from tensorflow.keras.layers import Dropout, SpatialDropout2D
from tensorflow.keras.initializers import he_normal
from tensorflow.keras.optimizers import Adam

# In[2]

# ### CONSTANTS
 
HEIGHT, WIDTH, CHANNELS, IMG_COLOR_MODE, MSK_COLOR_MODE, NUM_CLASS, \
    KS1, KS2, KS3, DL1, DL2, DL3, NF, NFL, NR1, NR2, DIL_MODE, W_MODE, LS, \
    TRAIN_SIZE, VAL_SIZE, TEST_SIZE, DR1, DR2, CLASSES, IMG_CLASS = _Params()
        
TRAIN_IMG_PATH, TRAIN_MSK_PATH, TRAIN_MSK_CLASS, VAL_IMG_PATH, \
    VAL_MSK_PATH, VAL_MSK_CLASS, TEST_IMG_PATH, TEST_MSK_PATH, TEST_MSK_CLASS = _Paths()

# In[3]:    

# ### RESNET-ATROUS
    
def ResNet_Atrous(input_shape=(HEIGHT, WIDTH, CHANNELS),
                   num_class=NUM_CLASS,
                   ks1=KS1, ks2=KS2, ks3=KS3, 
                   dl1=DL1, dl2=DL2, dl3=DL3,
                   filters=NF,resblock1=NR1,
                   r_filters=NFL, resblock2=NR2,
                   dil_mode=DIL_MODE, 
Exemplo n.º 4
0
def train_generator_1():

    # ### CONSTANTS
    HEIGHT, WIDTH, CHANNELS, IMG_COLOR_MODE, MSK_COLOR_MODE, NUM_CLASS, \
        KS1, KS2, KS3, DL1, DL2, DL3, NF, NFL, NR1, NR2, DIL_MODE, W_MODE, LS, \
        TRAIN_SIZE, VAL_SIZE, TEST_SIZE, DR1, DR2, CLASSES, IMG_CLASS = _Params()

    TRAIN_IMG_PATH, TRAIN_MSK_PATH, TRAIN_MSK_CLASS, VAL_IMG_PATH, \
        VAL_MSK_PATH, VAL_MSK_CLASS, TEST_IMG_PATH, TEST_MSK_PATH, TEST_MSK_CLASS = _Paths()

    TRAIN_SEED, VAL_SEED, TEST_SEED = _Seeds()

    train_data_gen_img_args = dict(rescale=1. / 255,
                                   horizontal_flip=True,
                                   vertical_flip=True,
                                   rotation_range=90,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   shear_range=15,
                                   zoom_range=0.1,
                                   fill_mode='reflect')

    if MSK_COLOR_MODE == 'rgb':
        train_data_gen_msk_args = dict(rescale=1. / 255,
                                       horizontal_flip=True,
                                       vertical_flip=True,
                                       rotation_range=90,
                                       width_shift_range=0.1,
                                       height_shift_range=0.1,
                                       shear_range=15,
                                       zoom_range=0.1,
                                       fill_mode='reflect')

    elif MSK_COLOR_MODE == 'grayscale':
        train_data_gen_msk_args = dict(preprocessing_function=to_train_indices,
                                       horizontal_flip=True,
                                       vertical_flip=True,
                                       rotation_range=90,
                                       width_shift_range=0.1,
                                       height_shift_range=0.1,
                                       shear_range=15,
                                       zoom_range=0.1,
                                       fill_mode='reflect')

    train_image_datagen = ImageDataGenerator(**train_data_gen_img_args)
    train_mask_datagen = ImageDataGenerator(**train_data_gen_msk_args)

    train_image_generator = train_image_datagen.flow_from_directory(
        TRAIN_IMG_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=[IMG_CLASS],
        color_mode=IMG_COLOR_MODE,
        class_mode=None,
        batch_size=TRAIN_SIZE,
        shuffle=False,
        seed=TRAIN_SEED)

    train_mask_generator = train_mask_datagen.flow_from_directory(
        TRAIN_MSK_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=CLASSES,
        color_mode=MSK_COLOR_MODE,
        class_mode=None,
        batch_size=TRAIN_SIZE,
        shuffle=False,
        seed=TRAIN_SEED)

    while True:
        if MSK_COLOR_MODE == 'rgb':
            yield (train_image_generator.next(), train_mask_generator.next())
        elif MSK_COLOR_MODE == 'grayscale':
            yield(train_image_generator.next(), to_one_hot_train(train_mask_generator.next(), \
                TRAIN_SIZE,HEIGHT,WIDTH,NUM_CLASS))
Exemplo n.º 5
0
def train_generator_2():

    global mask

    # ### CONSTANTS
    HEIGHT, WIDTH, CHANNELS, IMG_COLOR_MODE, MSK_COLOR_MODE, NUM_CLASS, \
        KS1, KS2, KS3, DL1, DL2, DL3, NF, NFL, NR1, NR2, DIL_MODE, W_MODE, LS, \
        TRAIN_SIZE, VAL_SIZE, TEST_SIZE, DR1, DR2, CLASSES, IMG_CLASS = _Params()

    TRAIN_IMG_PATH, TRAIN_MSK_PATH, TRAIN_MSK_CLASS, VAL_IMG_PATH, \
        VAL_MSK_PATH, VAL_MSK_CLASS, TEST_IMG_PATH, TEST_MSK_PATH, TEST_MSK_CLASS = _Paths()

    TRAIN_SEED, VAL_SEED, TEST_SEED = _Seeds()

    train_data_gen_img_args = dict(rescale=1. / 255,
                                   horizontal_flip=True,
                                   vertical_flip=True,
                                   rotation_range=90,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   shear_range=15,
                                   zoom_range=0.1,
                                   fill_mode='reflect')

    train_data_gen_msk_args = dict(rescale=1. / 255,
                                   horizontal_flip=True,
                                   vertical_flip=True,
                                   rotation_range=90,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   shear_range=15,
                                   zoom_range=0.1,
                                   fill_mode='reflect')

    train_image_datagen = ImageDataGenerator(**train_data_gen_img_args)
    train_mask_datagen = ImageDataGenerator(**train_data_gen_msk_args)

    train_image_generator = train_image_datagen.flow_from_directory(
        TRAIN_IMG_PATH,
        target_size=(HEIGHT, WIDTH),
        classes=[IMG_CLASS],
        color_mode=IMG_COLOR_MODE,
        class_mode=None,
        batch_size=TRAIN_SIZE,
        shuffle=False,
        seed=TRAIN_SEED,
        # save_to_dir='dataset/train_local/images/save',
    )

    for i in range(NUM_CLASS):
        globals()['train_mask_gen_{}'.format(
            i)] = train_mask_datagen.flow_from_directory(
                TRAIN_MSK_PATH,
                target_size=(HEIGHT, WIDTH),
                classes=[CLASSES[i]],
                color_mode=MSK_COLOR_MODE,
                class_mode=None,
                batch_size=TRAIN_SIZE,
                shuffle=False,
                seed=TRAIN_SEED,
                # save_to_dir='dataset/train_local/masks/save',
            )

    while True:
        yield(train_image_generator.next(), \
              np.round(np.squeeze(np.stack([globals()['train_mask_gen_{}'.format(i)].next() for i in range(NUM_CLASS)],axis=3))))