<jupyter_start><jupyter_text>512 model<jupyter_code>model = get_unet(input_shape=(512,512,3), initial_filters=16)
model.load_weights(filepath='weights/best_weights_512_2.hdf5')
input_size=512
all_imgs=[]
for id in ids_list:
    img = cv2.imread('data/train/{}.jpg'.format(id))
    img = cv2.resize(img, (input_size, input_size))
    all_imgs.append(img)
preds = model.predict(np.array(all_imgs, np.float32)/255, batch_size=16)
preds = np.squeeze(preds, axis=3)
dice_loss_512=[]
for i in range(len(ids_list)):
    tmp = np.copy(preds[i,:,:])
    tmp = cv2.resize(tmp, (orig_width, orig_height))
    tmp = (tmp > 0.5).astype(int)
    mask = cv2.imread('data/train_masks/{}_mask.png'.format(ids_list[i]), cv2.IMREAD_GRAYSCALE)/255
    dice_loss_512.append(dice_loss_numpy(mask, tmp))
import pickle
pickle.dump( dice_loss_512, open( "dice_loss_512.pkl", "wb" ) )<jupyter_output><empty_output><jupyter_text>1024 model<jupyter_code>model = get_unet(input_shape=(1024,1024,3), initial_filters=8)
model.load_weights(filepath='weights/best_weights_1024_2.hdf5')
input_size=1024
val_batch_size   = 16
from tqdm import tqdm
dice_loss_1024=[]
for start in tqdm(range(0, len(ids_list), val_batch_size)):
    x_batch = []
    end = min(start + val_batch_size, len(ids_list))
    ids_test_batch = ids_list[start:end]
    for id in ids_test_batch:
        img = cv2.imread('data/train/{}.jpg'.format(id))
        img = cv2.resize(img, (input_size, input_size))
        x_batch.append(img)
    x_batch = np.array(x_batch, np.float32) / 255
    preds = model.predict_on_batch(x_batch)
    preds = np.squeeze(preds, axis=3)
    
    for i in range(len(ids_test_batch)):
        tmp = np.copy(preds[i,:,:])
        tmp = cv2.resize(tmp, (orig_width, orig_height))
        tmp = (tmp > 0.5).astype(int)
        mask = cv2.imread('data/train_masks/{}_mask.png'.format(ids_test_batch[i]), cv2.IMREAD_GRAYSCALE)/255
        dice_loss_1024.append(dice_loss_numpy(mask, tmp))

import pickle
pickle.dump( dice_loss_1024, open( "dice_loss_1024.pkl", "wb" ) )<jupyter_output><empty_output><jupyter_text>Full res model<jupyter_code>model = get_unet(input_shape=(1024,1024,3), initial_filters=8)
model.load_weights(filepath='weights/best_weights_fullres3.hdf5')
val_batch_size = 8
threshold = 0.5
import pickle
test_bboxes = pickle.load( open( "./train_bboxes.pkl", "rb" ) )
from tqdm import tqdm
dice_loss_fullres=[]
for start in tqdm(range(0, len(ids_list), val_batch_size)):
    
    x_batch = []
    end = min(start + val_batch_size, len(ids_list))
    ids_test_batch = ids_list[start:end]
    
    for id in ids_test_batch:
        img = cv2.imread('data/train/{}.jpg'.format(id))
        
        this_bbox = test_bboxes[id]
        img_1_box = this_bbox[0]
        img_2_box = this_bbox[1]
        
        img_1 = np.copy(img[img_1_box[2]:(img_1_box[3]+1), img_1_box[0]:(img_1_box[1]+1),:])
        img_2 = np.copy(img[img_2_box[2]:(img_2_box[3]+1), img_2_box[0]:(img_2_box[1]+1),:])
        img_2 = cv2.flip(img_2, 1)
        
        x_batch.append(img_1)
        x_batch.append(img_2)
    
    x_batch = np.array(x_batch, np.float32) / 255
    preds = model.predict_on_batch(x_batch)
    preds = np.squeeze(preds, axis=3)
    
    final_preds = []
    for i in range(len(ids_test_batch)):
        id = ids_test_batch[i]
        
        this_bbox = test_bboxes[id]
        img_1_box = this_bbox[0]
        img_2_box = this_bbox[1]
        
        pred_1 = np.copy(preds[(i*2), :, :])
        pred_2 = np.copy(preds[(i*2) + 1, :, :])
        pred_2 = cv2.flip(pred_2, 1)
        
        left_part   = np.copy(pred_1[:, 0:894])
        middle_1    = np.copy(pred_1[:, 894:1024])
        middle_2    = np.copy(pred_2[:, 0:130])
        middle_part = np.add(middle_1, middle_2)/2
        right_part  = np.copy(pred_2[:, 130:1024])
        all_parts   = np.concatenate((left_part, middle_part, right_part), axis=1)
        
        if img_1_box[2] != 0:
            top_part = np.zeros(shape = (img_1_box[2],1918), dtype = np.float32)
            all_parts = np.concatenate((top_part, all_parts), axis = 0)
            
        if img_1_box[3] != 1279:
            bottom_part = np.zeros(shape = (1279-img_1_box[3],1918), dtype = np.float32)
            all_parts = np.concatenate((all_parts, bottom_part), axis = 0)
        
        final_preds.append(all_parts)
        
    
    for i in range(len(ids_test_batch)):
        tmp = final_preds[i]
        tmp = (tmp > 0.5).astype(int)
        mask = cv2.imread('data/train_masks/{}_mask.png'.format(ids_test_batch[i]), cv2.IMREAD_GRAYSCALE)/255
        dice_loss_fullres.append(dice_loss_numpy(mask, tmp))
import pickle
pickle.dump( dice_loss_fullres, open( "dice_loss_fullres.pkl", "wb" ) )<jupyter_output><empty_output><jupyter_text>Comparison<jupyter_code>import pickle
df = pd.DataFrame({'id'    : ids_list,
                   'm512'  : pickle.load( open( "./dice_loss_512.pkl", "rb" ) ),
                   'm1024' : pickle.load( open( "./dice_loss_1024.pkl", "rb" ) ),
                   'mfull' : pickle.load( open( "./dice_loss_fullres.pkl", "rb" ) )
                  })
df.head()
df.m512.corr(df.m1024)
df.m512.corr(df.mfull)
df.m1024.corr(df.mfull)
((df.m512+df.mfull)/2).corr(df.m1024)
plt.figure(figsize=(10, 10))
plt.scatter(df.m512, df.mfull);
# Ensemble of 512 and full res models didn't increase the LB score<jupyter_output><empty_output><jupyter_text>Accuracy by angle<jupyter_code>df.head()
import re
angle_data = [re.search(r'[0-9]+$', x).group(0) for x in df.id.values]
df = df.assign(angle = angle_data)
df.head()
import seaborn as sns
plt.figure(figsize=(20, 5))
plt.subplot(1,3,1)
sns.boxplot(x='angle',y='m1024',data=df)
plt.ylim(0.990, 0.999)
plt.subplot(1,3,2)
sns.boxplot(x='angle',y='m512',data=df);
plt.ylim(0.990, 0.999)
plt.subplot(1,3,3)
sns.boxplot(x='angle',y='mfull',data=df);
plt.ylim(0.990, 0.999);<jupyter_output><empty_output><jupyter_text>Worst predictions of full res model<jupyter_code>df = df.set_index(['id'])
df.head()
worst_cases = df.groupby('angle')['mfull'].nsmallest(2).reset_index()
worst_cases.head()
ids_list = worst_cases.id.values
len(ids_list)
model = get_unet(input_shape=(1024,1024,3), initial_filters=8)
model.load_weights(filepath='weights/best_weights_fullres3.hdf5')
val_batch_size = 8
threshold = 0.5
import pickle
test_bboxes = pickle.load( open( "./train_bboxes.pkl", "rb" ) )
from tqdm import tqdm
predicted_masks = []
for start in tqdm(range(0, len(ids_list), val_batch_size)):
    
    x_batch = []
    end = min(start + val_batch_size, len(ids_list))
    ids_test_batch = ids_list[start:end]
    
    for id in ids_test_batch:
        img = cv2.imread('data/train/{}.jpg'.format(id))
        
        this_bbox = test_bboxes[id]
        img_1_box = this_bbox[0]
        img_2_box = this_bbox[1]
        
        img_1 = np.copy(img[img_1_box[2]:(img_1_box[3]+1), img_1_box[0]:(img_1_box[1]+1),:])
        img_2 = np.copy(img[img_2_box[2]:(img_2_box[3]+1), img_2_box[0]:(img_2_box[1]+1),:])
        img_2 = cv2.flip(img_2, 1)
        
        x_batch.append(img_1)
        x_batch.append(img_2)
    
    x_batch = np.array(x_batch, np.float32) / 255
    preds = model.predict_on_batch(x_batch)
    preds = np.squeeze(preds, axis=3)
    
    final_preds = []
    for i in range(len(ids_test_batch)):
        id = ids_test_batch[i]
        
        this_bbox = test_bboxes[id]
        img_1_box = this_bbox[0]
        img_2_box = this_bbox[1]
        
        pred_1 = np.copy(preds[(i*2), :, :])
        pred_2 = np.copy(preds[(i*2) + 1, :, :])
        pred_2 = cv2.flip(pred_2, 1)
        
        left_part   = np.copy(pred_1[:, 0:894])
        middle_1    = np.copy(pred_1[:, 894:1024])
        middle_2    = np.copy(pred_2[:, 0:130])
        middle_part = np.add(middle_1, middle_2)/2
        right_part  = np.copy(pred_2[:, 130:1024])
        all_parts   = np.concatenate((left_part, middle_part, right_part), axis=1)
        
        if img_1_box[2] != 0:
            top_part = np.zeros(shape = (img_1_box[2],1918), dtype = np.float32)
            all_parts = np.concatenate((top_part, all_parts), axis = 0)
            
        if img_1_box[3] != 1279:
            bottom_part = np.zeros(shape = (1279-img_1_box[3],1918), dtype = np.float32)
            all_parts = np.concatenate((all_parts, bottom_part), axis = 0)
        
        final_preds.append(all_parts)
        
    
    for i in range(len(ids_test_batch)):
        tmp = final_preds[i]
        tmp = (tmp > 0.5).astype(int)
        predicted_masks.append(tmp)
len(predicted_masks)
actual_masks = []
for id in ids_list:
    mask = mask = cv2.imread('data/train_masks/{}_mask.png'.format(id), cv2.IMREAD_GRAYSCALE)/255
    actual_masks.append(mask)
def plot_diff(i):
    pred = predicted_masks[i]
    act  = actual_masks[i]
    pred_minus_act = pred-act
    
    plt.figure(figsize=(20, 20))
    plt.subplot(1,3,1)
    plt.imshow(pred, cmap="PiYG")
    plt.subplot(1,3,2)
    plt.imshow(act, cmap="PiYG")
    plt.subplot(1,3,3)
    plt.imshow(pred_minus_act, cmap="PiYG")
    plt.show()
plot_diff(0);
plot_diff(1);
plot_diff(2);
plot_diff(3);
plot_diff(4);
plot_diff(5);
plot_diff(6);
plot_diff(7);
plot_diff(8);
plot_diff(9);
plot_diff(10);
plot_diff(11);
plot_diff(12);
plot_diff(13);
plot_diff(14);
plot_diff(15);
plot_diff(16);
plot_diff(17);
plot_diff(18);
plot_diff(19);
plot_diff(20);
plot_diff(21);
plot_diff(22);
plot_diff(23);
plot_diff(24);
plot_diff(25);
plot_diff(26);
plot_diff(27);
plot_diff(28);
plot_diff(29);
plot_diff(30);
plot_diff(31);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15664921465968587, 'fraction_numerical': 0.05706806282722513, 'mean_word_length': 3.450605778191985, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '274539', 'license': 'MIT', 'n_tokens_mistral': 4214, 'n_tokens_neox': 3947, 'n_words': 571, 'path': '10-model-predictions-viz.ipynb', 'repo_name': 'sainathadapa/kaggle-carvana-image-masking-challenge', 'size': '9550'}