{"cells":[{"metadata":{"_cell_guid":"97ab7f65-6023-4a97-b40b-f404716c8c96","_uuid":"abc85ca22cd2b023575e3f8d35634e81eaa6b0f4","collapsed":true},"cell_type":"markdown","source":"#Intro\nFirst of all thanks to Kjetil Åmdal-Sævik for providing excellent code for data preparation.\nBeing a novice python programmer, my code may not be that much efficient but it may serve as a starting point for using TensorFlow."},{"metadata":{"_cell_guid":"c332549b-8d23-4bb5-8497-e7a8eb8b21d2","_uuid":"5c38504af3a84bee68c66d3cde74443c58df422f","trusted":true},"cell_type":"code","source":"import os\nimport sys\nimport numpy as np\nimport tensorflow as tf\nimport random\nimport math\nimport warnings\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nfrom itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\n\nwarnings.filterwarnings('ignore', category=UserWarning, module='skimage')\nseed = 42\nrandom.seed = seed\nnp.random.seed = seed","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5babc1df-c8e8-4037-9537-ee7409d338ef","_uuid":"5d84a2852337e5699520058221d974ac47e7204a","trusted":true},"cell_type":"code","source":"# Set some parameters\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3\nTRAIN_PATH = '../input/stage1_train/'\nTEST_PATH = '../input/stage1_test/'\n\nwarnings.filterwarnings('ignore', category=UserWarning, module='skimage')\nseed = 42\nrandom.seed = seed\nnp.random.seed = seed","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ffa0caf0-2d1b-40f2-865b-8e6db88526b6","_uuid":"3fb9d6530fbbd0e22e41fc4fd9fd9fc0bff027ac","trusted":true},"cell_type":"code","source":"# Get train and test IDs\ntrain_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ea59eaac4a31c1a9424357130671d5ff3ab4e2d"},"cell_type":"code","source":"images = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nimages","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ca0cc34b-c26f-41ee-88d7-975aebdb634e","scrolled":true,"_uuid":"9e389ba8bdb5b6fc03b231b6a6c84a8bde634053","trusted":true},"cell_type":"code","source":"# Get and resize train images and masks\nimages = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nlabels = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\nprint('Getting and resizing train images and masks ... ')\nsys.stdout.flush()\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n    path = TRAIN_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    images[n] = img\n    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\n    for mask_file in next(os.walk(path + '/masks/'))[2]:\n        mask_ = imread(path + '/masks/' + mask_file)\n        mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant', \n                                      preserve_range=True), axis=-1)\n        mask = np.maximum(mask, mask_)\n    labels[n] = mask\n\nX_train = images\nY_train = labels\n\n# Get and resize test images\nX_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nsizes_test = []\nprint('Getting and resizing test images ... ')\nsys.stdout.flush()\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TEST_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n    sizes_test.append([img.shape[0], img.shape[1]])\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X_test[n] = img\n\nprint('Done!')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d3a3382d66147c4349ce7c5546ac02c0cb69f77"},"cell_type":"code","source":"labels","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3f5e5a47-6133-4870-976a-a8e4fa7bf46c","_uuid":"2a83eab66bf55194f300953bea5534b6a043130f","trusted":true},"cell_type":"code","source":"def shuffle():\n    global images, labels\n    p = np.random.permutation(len(X_train))\n    images = X_train[p]\n    labels = Y_train[p]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3ca3becb-5613-431b-8a88-07d32ec751dc","_uuid":"5a3b79a7d9ee881c68e7f6147964fcd30f726b4a","trusted":true},"cell_type":"code","source":"def next_batch(batch_s, iters):\n    if(iters == 0):\n        shuffle()\n    count = batch_s * iters\n    return images[count:(count + batch_s)], labels[count:(count + batch_s)]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"10fd62e2-3b6a-4975-acb0-6d5759dce26f","_uuid":"3d9f26384e23e070783ec0f4e2b4ba25d800a440","trusted":true},"cell_type":"code","source":"def deconv2d(input_tensor, filter_size, output_size, out_channels, in_channels, name, strides = [1, 1, 1, 1]):\n    dyn_input_shape = tf.shape(input_tensor)\n    batch_size = dyn_input_shape[0]\n    out_shape = tf.stack([batch_size, output_size, output_size, out_channels])\n    filter_shape = [filter_size, filter_size, out_channels, in_channels]\n    w = tf.get_variable(name=name, shape=filter_shape)\n    h1 = tf.nn.conv2d_transpose(input_tensor, w, out_shape, strides, padding='SAME')\n    return h1","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5e2810f9-d1c6-4437-945f-e5356b7537b0","_uuid":"dad846d185af7745293da91e8700ea587fc5ba7d","trusted":true},"cell_type":"code","source":"def conv2d(input_tensor, depth, kernel, name, strides=(1, 1), padding=\"SAME\"):\n    return tf.layers.conv2d(input_tensor, filters=depth, kernel_size=kernel, strides=strides, padding=padding, activation=tf.nn.relu, name=name)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"22033858-fbe7-454e-9119-1f2a763070da","_uuid":"061fd3457e6fc5e69212887327d31163832e1153","trusted":true},"cell_type":"code","source":"X = tf.placeholder(tf.float32, [None, 128, 128, 3])\nY_ = tf.placeholder(tf.float32, [None, 128, 128, 1])\nlr = tf.placeholder(tf.float32)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"dedcf23a-9563-4d21-8383-b9ca8c39fc7a","_uuid":"0c5bffd335bcdfca6fe9b094eb1e64851c867477","trusted":true},"cell_type":"code","source":"net = conv2d(X, 32, 1, \"Y0\") #128\n\nnet = conv2d(net, 64, 3, \"Y2\", strides=(2, 2)) #64\n\nnet = conv2d(net, 128, 3, \"Y3\", strides=(2, 2)) #32\n\n\nnet = deconv2d(net, 1, 32, 128, 128, \"Y2_deconv\") # 32\nnet = tf.nn.relu(net)\n\nnet = deconv2d(net, 2, 64, 64, 128, \"Y1_deconv\", strides=[1, 2, 2, 1]) # 64\nnet = tf.nn.relu(net)\n\nnet = deconv2d(net, 2, 128, 32, 64, \"Y0_deconv\", strides=[1, 2, 2, 1]) # 128\nnet = tf.nn.relu(net)\n\nlogits = deconv2d(net, 1, 128, 1, 32, \"logits_deconv\") # 128\n\nloss = tf.losses.sigmoid_cross_entropy(Y_, logits)\noptimizer = tf.train.AdamOptimizer(lr).minimize(loss)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2d02dbe7-ff9b-4921-92d5-d5a8b412b429","scrolled":false,"_uuid":"803a9718ece3860da0f9fa4f3eeeead84f177e5e","trusted":true},"cell_type":"code","source":"# init\ninit = tf.global_variables_initializer()\nsess = tf.Session()\nsess.run(init)\n\nbatch_count = 0\ndisplay_count = 1\nfor i in range(10000):\n    # training on batches of 10 images with 10 mask images\n    if(batch_count > 67):\n        batch_count = 0    \n\n    batch_X, batch_Y = next_batch(10, batch_count)\n\n    batch_count += 1\n\n    feed_dict = {X: batch_X, Y_: batch_Y, lr: 0.0005}\n    loss_value, _ = sess.run([loss, optimizer], feed_dict=feed_dict)\n\n    if(i % 500 == 0):\n        print(str(display_count) + \" training loss:\", str(loss_value))\n        display_count +=1\n        \nprint(\"Done!\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e5b6c8c1-a848-45d0-bf94-4a5d403da697","_uuid":"e0ca515bd74d94605c0e4d47b2b5ccd84c7ac1ad"},"cell_type":"markdown","source":"**Test on the data that is not seen by the network during training:**"},{"metadata":{"_cell_guid":"73046a2f-f74a-47e9-bd61-917427d59426","_uuid":"ceb487c8c94b58011469b70821e30b24e9ffc611","trusted":true},"cell_type":"code","source":"ix = 3 #random.randint(0, 64) #len(X_test) - 1 = 64\ntest_image = X_test[ix].astype(float)\nimshow(test_image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"8128a55e-3131-42db-8695-3edd74c54c5e","_uuid":"5ebafc6ead8edfabca601a0fdea2fd1ee4e1bc88","trusted":true},"cell_type":"code","source":"def sigmoid(x):\n    return 1 / (1 + math.exp(-x))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"efdd905a-4955-4526-bb04-480cade00c01","_uuid":"5977edd0b667233e6c474e11ae61709999526d90","trusted":true},"cell_type":"code","source":"#print(ix)\ntest_image = np.reshape(test_image, [-1, 128 , 128, 3])\ntest_data = {X:test_image}\n\ntest_mask = sess.run([logits],feed_dict=test_data)\ntest_mask = np.reshape(np.squeeze(test_mask), [IMG_WIDTH , IMG_WIDTH, 1])\nfor i in range(IMG_WIDTH):\n    for j in range(IMG_HEIGHT):\n            test_mask[i][j] = int(sigmoid(test_mask[i][j])*255)\nimshow(test_mask.squeeze().astype(np.uint8))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"7139604f-9724-4621-a610-3ba851d20dbc","_uuid":"3113f21420b0215142b2914bffa725926eec0878","collapsed":true,"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}