<jupyter_start><jupyter_text>[View in Colaboratory](https://colab.research.google.com/github/suryatej9195/kaggleCompetitions/blob/master/Kaggle_Comp_Salt_identification_demo.ipynb)<jupyter_code>print ('hai')
!pip install imageio
!pip install torch
!pip install kaggle
!pip install pywidgets
import os
import numpy as np
import imageio
import matplotlib.pyplot as plt
import pandas as pd
import torch
from torch.utils import data
from google.colab import files
files.upload()
ls -lha kaggle.json
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/

!chmod 600 ~/.kaggle/kaggle.json

!kaggle competitions download -c tgs-salt-identification-challenge
!ls
!unzip train.zip
class TGSSaltDataset(data.Dataset):
  def __init__(self, root_path, file_list):
    self.root_path = root_path
    self.file_list = file_list
  def __len__(self):
    return len(self.file_list)
  def __getitem__(self, index):
    file_id = self.file_list[index]
    # image folder + path
    image_folder = os.path.join(self.root_path, 'images')
    image_path = os.path.join(image_folder, file_id + '.png')
    # label folder + path
    mask_folder = os.path.join(self.root_path, 'masks')
    mask_path = os.path.join(mask_folder, file_id + '.png')
    # read it
    image = np.array(imageio.imread(image_path), dtype=np.uint8)
    mask = np.array(imageio.imread(mask_path), dtype=np.uint8)
    return image, mask
    
# init our new class
train_mask = pd.read_csv('train.csv')
depth = pd.read_csv('depths.csv') # feature to model
train_path = './'
file_list = list(train_mask['id'].values)
dataset = TGSSaltDataset(train_path, file_list)


def plot2x2array(image, mask):
  f, axarr = plt.subplots(1,2)
  axarr[0].imshow(image)
  axarr[1].imshow(mask)
  axarr[0].grid()
  axarr[1].grid()
  axarr[0].set_title('Image')
  axarr[1].set_title('Mask')
for i in range(5):
  image, mask = dataset[np.random.randint(0, len(dataset))]
  plot2x2array(image, mask)<jupyter_output><empty_output><jupyter_text>black region contains saltusing UNET<jupyter_code>plt.figure(figsize=(6,6))
plt.hist(depth['z'], bins=50)
plt.title('Depth Distribution')
# convert run length encoding to images
def rleToMask(rleString, height, width):
  rows, cols = height, width
  try:
    rleNumbers = [int(numstring) for numstring in rleString.split(' ')]
    rlePairs = np.array(rleNumbers).reshape(-1,2)
    img = np.zeros(row*cols, dtype=np.uint8)
    for index, length in rlePairs:
      index -=1
      img[index:index+length] = 255
    img = img.reshape(cols, row)
    img = img.T
  except:
    img = np.zeros((cols, rows))
  return img
    

    
# function for measuring how salty an image is
def salt_proportion(imgArray):
  try:
    unique, counts = np.unique(imgArray, return_counts = True)
    return counts[1]/10201
  except:
    return 0.0
train_mask['mask'] = train_mask['rle_mask'].apply(lambda x: rleToMask(x, 101, 101))
train_mask['salt_proportion'] = train_mask['mask'].apply(lambda x: salt_proportion(x))

#merge it
merged = train_mask.merge(depth, how='left')
merged.head()
plt.figure(figsize=(12,6))
plt.scatter(merged['salt_proportion'],merged['z'])
plt.title('Proporation of Salt Vs Depth')
# Just how
from keras.models import Model, load_model
from keras.layers import Input
from keras.layers.core import Lambda, RepeatVector, Reshape
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras import backend as K<jupyter_output><empty_output><jupyter_text>Using TensorFlow backend.<jupyter_code>im_width = 128
im_height = 128
border = 5
im_chan = 2
n_features = 1

input_img = Input((im_height, im_width, im_chan), name='img')
input_features = Input((n_features,), name='feat')

c1 = Conv2D(8, (3,3), activation='relu', padding='same') (input_img) 
c1 = Conv2D(8, (3,3), activation='relu', padding='same') (c1) 
p1 = MaxPooling2D((2,2)) (c1)

c2 = Conv2D(16, (3,3), activation='relu', padding='same') (p1) 
c2 = Conv2D(16, (3,3), activation='relu', padding='same') (c2) 
p2 = MaxPooling2D((2,2)) (c2)

c3 = Conv2D(32, (3,3), activation='relu', padding='same') (p2) 
c3 = Conv2D(32, (3,3), activation='relu', padding='same') (c3) 
p3 = MaxPooling2D((2,2)) (c3)

c4 = Conv2D(64, (3,3), activation='relu', padding='same') (p3) 
c4 = Conv2D(64, (3,3), activation='relu', padding='same') (c4) 
p4 = MaxPooling2D((2,2)) (c4)

# Join Features information in the depthest layer
f_repeat = RepeatVector(8*8)(input_features)
f_conv = Reshape((8,8, n_features))(f_repeat)
p4_feat = concatenate([p4, f_conv], -1)

# decoder layer
c5 = Conv2D(128, (3,3), activation='relu', padding='same') (p4_feat) 
c5 = Conv2D(128, (3,3), activation='relu', padding='same') (c5) 

u6 = Conv2DTranspose(64, (2,2), strides=(2,2), padding='same') (c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(64, (3,3), activation='relu', padding='same') (u6) 
c6 = Conv2D(64, (3,3), activation='relu', padding='same') (c6) 

u7 = Conv2DTranspose(32, (2,2), strides=(2,2), padding='same') (c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(32, (3,3), activation='relu', padding='same') (u7) 
c7 = Conv2D(32, (3,3), activation='relu', padding='same') (c7)

u8 = Conv2DTranspose(16, (2,2), strides=(2,2), padding='same') (c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(16, (3,3), activation='relu', padding='same') (u8) 
c8 = Conv2D(16, (3,3), activation='relu', padding='same') (c8)


u9 = Conv2DTranspose(8, (2,2), strides=(2,2), padding='same') (c8)
u9 = concatenate([u9, c1], axis = 3)
c9 = Conv2D(8, (3,3), activation='relu', padding='same') (u9) 
c9 = Conv2D(8, (3,3), activation='relu', padding='same') (c9)

outputs = Conv2D(1,(1,1), activation='sigmoid')(c9)

model = Model(inputs = [input_img, input_features], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy')
model.summary()
!pip install ipywidgets
import sys, os
from tqdm import tqdm
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from skimage.transform import resize


train_ids = next(os.walk(train_path+"images"))[2]


# Get and resize train images and masks
X = np.zeros((len(train_ids), im_height, im_width, im_chan), dtype=np.float32)
y = np.zeros((len(train_ids), im_height, im_width, 1), dtype=np.float32)
X_feat = np.zeros((len(train_ids), n_features), dtype=np.float32)
print('Getting and resizing train images and masks ... ')
sys.stdout.flush()
for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):
    path = train_path
    
    # Depth
    #X_feat[n] = depth.loc[id_.replace('.png', ''), 'z']
    
    # Load X
    img = load_img(path + '/images/' + id_, grayscale=True)
    x_img = img_to_array(img)
    x_img = resize(x_img, (128, 128, 1), mode='constant', preserve_range=True)
    
    # Create cumsum x
    x_center_mean = x_img[border:-border, border:-border].mean()
    x_csum = (np.float32(x_img)-x_center_mean).cumsum(axis=0)
    x_csum -= x_csum[border:-border, border:-border].mean()
    x_csum /= max(1e-3, x_csum[border:-border, border:-border].std())

    # Load Y
    mask = img_to_array(load_img(path + '/masks/' + id_, grayscale=True))
    mask = resize(mask, (128, 128, 1), mode='constant', preserve_range=True)

    # Save images
    X[n, ..., 0] = x_img.squeeze() / 255
    X[n, ..., 1] = x_csum.squeeze()
    y[n] = mask / 255

print('Done!')



from sklearn.model_selection import train_test_split

X_train, X_valid, X_feat_train, X_feat_valid, y_train, y_valid = train_test_split(X, X_feat, y, test_size=0.15, random_state=42)
callbacks = [
    EarlyStopping(patience=5, verbose=1),
    ReduceLROnPlateau(patience=3, verbose=1),
    ModelCheckpoint('model-tgs-salt-1.h5', verbose=1, save_best_only=True, save_weights_only=True)
]

results = model.fit({'img': X_train, 'feat': X_feat_train}, y_train, batch_size=16, epochs=50, callbacks=callbacks,
                    validation_data=({'img': X_valid, 'feat': X_feat_valid}, y_valid))
print('Hello')
from google.colab import files
files.download('model-tgs-salt-1.h5')
!ls
files.download('depths.csv')
files.download('sample_submission.csv')
files.download('train.csv')
files.download('test.zip')
files.download('train.zip')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16273727481562084, 'fraction_numerical': 0.038568492322572846, 'mean_word_length': 5.309687261632342, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '74753', 'license': 'Apache-2.0', 'n_tokens_mistral': 3360, 'n_tokens_neox': 3172, 'n_words': 672, 'path': 'Kaggle_Comp_Salt_identification_demo.ipynb', 'repo_name': 'suryatej9195/kaggleCompetitions', 'size': '8271'}