application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
from keras.layers import Input

from keras.layers.convolutional import Conv2D, Conv2DTranspose

from keras.layers.merge import concatenate

from keras.layers.pooling import MaxPooling2D

from keras.models import Model, load_model

from keras.optimizers import Adam

from skimage.io import imread

from skimage.transform import resize

from tqdm import tqdm

import numpy as np

import os

import random

TRAIN_PATH = '../input/train/'
TEST_PATH = '../input/test/'
seed = 42
random.seed = seed
np.random.seed = seed
tot_num = 5635
IMG_HEIGHT = 128
IMG_WIDTH = 128
files = os.listdir(TRAIN_PATH)
masks_list = []
imgs_list = []
for f in files:
    if 'mask' in f:
        masks_list.append(f)
    else:
        imgs_list.append(f)
masks_list = sorted(masks_list)
imgs_list = sorted(imgs_list)

X_train = np.zeros((tot_num, IMG_HEIGHT, IMG_WIDTH), dtype=np.float32)
Y_train = np.zeros((tot_num, IMG_HEIGHT, IMG_WIDTH), dtype=np.float32)

Y_train_one = []
X_train_one = []
Y_train_zero = []
X_train_zero = []
for i, file in tqdm(enumerate(imgs_list), total=len(imgs_list)):
    img_path = file
    mask_path = img_path[:-4] + '_mask.tif'
    mask = imread(TRAIN_PATH + mask_path)
    mask = resize(mask, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    img = imread(TRAIN_PATH + img_path)
    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    if mask.any() == False:
        Y_train_zero.append(mask)
        X_train_zero.append(img)
    else:
        Y_train_one.append(mask)
        X_train_one.append(img)

X_train_one = np.array(X_train_one)
Y_train_one = np.array(Y_train_one)
X_train_zero = np.array(X_train_zero)
Y_train_zero = np.array(Y_train_zero)

X_train = []
Y_train = []

def augmentation(imgs, masks):
    for img, mask in zip(imgs, masks):
        img_lr = np.fliplr(img)
        mask_lr = np.fliplr(mask)
        img_up = np.flipud(img)
        mask_up = np.flipud(mask)
        X_train.append(img)
        Y_train.append(mask)
        X_train.append(img_lr)
        Y_train.append(mask_lr)
        X_train.append(img_up)
        Y_train.append(mask_up)
augmentation(X_train_one, Y_train_one)

X_train = np.array(X_train)
Y_train = np.array(Y_train)

X_train_ax = X_train[:, :, :, np.newaxis] / 255.0
Y_train_ax = Y_train[:, :, :, np.newaxis] / 255.0

def to_one(x):
    if x == 0:
        return 0
    else:
        return 1
to_one = np.vectorize(to_one)
Y_train_ax = to_one(Y_train_ax)

smooth = 1.0
def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2.0 * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
    return -dice_coef(y_true, y_pred)

inputs = Input((IMG_HEIGHT, IMG_WIDTH, 1))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=Adam(lr=1e-05), loss=dice_coef_loss, metrics=[dice_coef])

results = model.fit(X_train_ax, Y_train_ax, validation_split=0.1, batch_size=8, epochs=18)