Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
warnings.filterwarnings('ignore')
 %matplotlib inline
def cutmix(images, labels, batch_size, image_size):
DIM = image_size[0]
new_image_indices = tf.cast(tf.random.uniform([batch_size], 0, batch_size), tf.int32)
 new_x = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 new_y = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 b = tf.random.uniform([batch_size], 0, 1)
 new_width = tf.cast(DIM * tf.math.sqrt(1-b), tf.int32)
 new_y0 = tf.math.maximum(0, new_y - new_width // 2)
 new_y1 = tf.math.minimum(DIM, new_y + new_width // 2)
 new_x0 = tf.math.maximum(0, new_x - new_width // 2)
 new_x1 = tf.math.minimum(DIM, new_x + new_width // 2)
 target = tf.broadcast_to(tf.range(DIM), shape=(batch_size, DIM))
mask_y = tf.math.logical_and(new_y0[:, tf.newaxis] <= target, target <= new_y1[:, tf.newaxis])
 mask_x = tf.math.logical_and(new_x0[:, tf.newaxis] <= target, target <= new_x1[:, tf.newaxis])
 mask = tf.cast(tf.math.logical_and(mask_y[:, :, tf.newaxis], mask_x[:, tf.newaxis, :]), tf.float32)
 new_images = images * tf.broadcast_to(1 - mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])+ \
tf.gather(images, new_image_indices)* tf.broadcast_to(mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])
 a = tf.cast(new_width ** 2 / DIM ** 2, tf.float32)
 for c in ['root', 'vowel', 'consonant', 'unique', 'root2', 'vowel2', 'consonant2', 'unique2', 'root3', 'vowel3', 'consonant3', 'unique3']:
y1, y2 = labels[c], tf.gather(labels[c], new_image_indices)
 labels[c] = y1 *(1-a)[:, tf.newaxis] + y2 * a[:, tf.newaxis]
return new_images, labels
def transform(image, inv_mat, image_shape):
h, w, c = image_shape
cx, cy = w//2, h//2
new_xs = tf.repeat(tf.range(-cx, cx, 1), h)
 new_ys = tf.tile(tf.range(-cy, cy, 1), [w])
 new_zs = tf.ones([h*w], dtype=tf.int32)
 old_coords = tf.matmul(inv_mat, tf.cast(tf.stack([new_xs, new_ys, new_zs]), tf.float32))
old_coords_x, old_coords_y = tf.round(old_coords[0, :] + w//2), tf.round(old_coords[1, :] + h//2)
 clip_mask_x = tf.logical_or(old_coords_x<0, old_coords_x>w-1)
 clip_mask_y = tf.logical_or(old_coords_y<0, old_coords_y>h-1)
 clip_mask = tf.logical_or(clip_mask_x, clip_mask_y)
 old_coords_x = tf.boolean_mask(old_coords_x, tf.logical_not(clip_mask))
old_coords_y = tf.boolean_mask(old_coords_y, tf.logical_not(clip_mask))
new_coords_x = tf.boolean_mask(new_xs+cx, tf.logical_not(clip_mask))
new_coords_y = tf.boolean_mask(new_ys+cy, tf.logical_not(clip_mask))
old_coords = tf.cast(tf.stack([old_coords_y, old_coords_x]), tf.int32)
 new_coords = tf.cast(tf.stack([new_coords_y, new_coords_x]), tf.int64)
 rotated_image_values = tf.gather_nd(image, tf.transpose(old_coords))
rotated_image_channel = list()
for i in range(c):
vals = rotated_image_values[:,i]
sparse_channel = tf.SparseTensor(tf.transpose(new_coords), vals, [h, w])
 rotated_image_channel.append(tf.sparse.to_dense(sparse_channel, default_value=0, validate_indices=False))
return tf.transpose(tf.stack(rotated_image_channel), [1,2,0])
 def random_rotate(image, angle, image_shape):
def get_rotation_mat_inv(angle):
angle = math.pi * angle / 180
cos_val = tf.math.cos(angle)
 sin_val = tf.math.sin(angle)
 one = tf.constant([1], tf.float32)
 zero = tf.constant([0], tf.float32)
 rot_mat_inv = tf.concat([cos_val, sin_val, zero,
-sin_val, cos_val, zero,
zero, zero, one], axis=0)
 rot_mat_inv = tf.reshape(rot_mat_inv, [3,3])
 return rot_mat_inv
angle = float(angle)* tf.random.normal([1],dtype='float32')
 rot_mat_inv = get_rotation_mat_inv(angle)
 return transform(image, rot_mat_inv, image_shape)
 def GridMask(image_height, image_width, d1, d2, rotate_angle=1, ratio=0.5):
h, w = image_height, image_width
hh = int(np.ceil(np.sqrt(h*h+w*w)))
 hh = hh+1 if hh%2==1 else hh
d = tf.random.uniform(shape=[], minval=d1, maxval=d2, dtype=tf.int32)
 l = tf.cast(tf.cast(d,tf.float32)*ratio+0.5, tf.int32)
 st_h = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 st_w = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 y_ranges = tf.range(-1 * d + st_h, -1 * d + st_h + l)
 x_ranges = tf.range(-1 * d + st_w, -1 * d + st_w + l)
 for i in range(0, hh//d+1):
s1 = i * d + st_h
s2 = i * d + st_w
y_ranges = tf.concat([y_ranges, tf.range(s1,s1+l)], axis=0)
 x_ranges = tf.concat([x_ranges, tf.range(s2,s2+l)], axis=0)
 x_clip_mask = tf.logical_or(x_ranges <0 , x_ranges > hh-1)
 y_clip_mask = tf.logical_or(y_ranges <0 , y_ranges > hh-1)
 clip_mask = tf.logical_or(x_clip_mask, y_clip_mask)
 x_ranges = tf.boolean_mask(x_ranges, tf.logical_not(clip_mask))
y_ranges = tf.boolean_mask(y_ranges, tf.logical_not(clip_mask))
hh_ranges = tf.tile(tf.range(0,hh), [tf.cast(tf.reduce_sum(tf.ones_like(x_ranges)) , tf.int32)])
 x_ranges = tf.repeat(x_ranges, hh)
 y_ranges = tf.repeat(y_ranges, hh)
 y_hh_indices = tf.transpose(tf.stack([y_ranges, hh_ranges]))
x_hh_indices = tf.transpose(tf.stack([hh_ranges, x_ranges]))
y_mask_sparse = tf.SparseTensor(tf.cast(y_hh_indices, tf.int64), tf.zeros_like(y_ranges), [hh, hh])
 y_mask = tf.sparse.to_dense(y_mask_sparse, 1, False)
 x_mask_sparse = tf.SparseTensor(tf.cast(x_hh_indices, tf.int64), tf.zeros_like(x_ranges), [hh, hh])
 x_mask = tf.sparse.to_dense(x_mask_sparse, 1, False)
 mask = tf.expand_dims(tf.clip_by_value(x_mask + y_mask, 0, 1), axis=-1)
 mask = random_rotate(mask, rotate_angle, [hh, hh, 1])
 mask = tf.image.crop_to_bounding_box(mask,(hh-h)//2,(hh-w)//2, image_height, image_width)
 return mask
def apply_grid_mask(image, image_shape):
AugParams = {
'd1' : 10,
'd2': 100,
'rotate' : 15,
'ratio' : 0.5
}
mask = GridMask(image_shape[0],
image_shape[1],
AugParams['d1'],
AugParams['d2'],
AugParams['rotate'],
tf.random.uniform(shape=[], minval=0.3, maxval=0.6)
)
 if image_shape[-1] == 3:
mask = tf.concat([mask, mask, mask], axis=-1)
 return image * tf.cast(mask, tf.float32)
 def augmentation(image, label, input_size):
image = apply_grid_mask(image,(*input_size,3))
return image, label<train_model>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print('Train size:\t
Test size:\t 
.format(train_df.shape[0], train_df.shape[1], test_df.shape[0], test_df.shape[1]))
def normalize_image(img, org_width, org_height, new_width, new_height):
img = 255 - img
img =(img *(255.0 / img.max())).astype(np.uint8)
 img = img.reshape(org_height, org_width)
 image_resized = cv2.resize(img,(new_width, new_height), interpolation=cv2.INTER_LINEAR)
 return image_resized
def predict_batch(img_batch, model, all_preds, unseen_th=0.5):
img_batch = np.float32(img_batch)
 bs = img_batch.shape[0]
r,v,c,u = np.zeros(( bs,168)) , np.zeros(( bs,11)) , np.zeros(( bs,7)) , np.zeros(( bs,1292))
if img_batch.ndim != 4:
img_batch = np.expand_dims(img_batch, 0)
 y_pred = [
model[0].predict([img_batch, r,v,c,u]),
model[1].predict([img_batch, r]),
model[2].predict([img_batch, v]),
model[3].predict([img_batch, c]),
]
for k in range(len(y_pred[0][0])) :
uu_p = y_pred[0][3][k].max(-1)
 if uu_p > unseen_th:
rr=y_pred[0][0][k].astype('float16')
 vv=y_pred[0][1][k].astype('float16')
 cc=y_pred[0][2][k].astype('float16')
 else:
rr=y_pred[1][0][k].astype('float16')
 vv=y_pred[2][0][k].astype('float16')
 cc=y_pred[3][0][k].astype('float16')
 all_preds['r'].append(rr)
 all_preds['v'].append(vv)
 all_preds['c'].append(cc)
 def postprocess(preds, num_classes, EXP = -1.2):
p0 = np.argmax(preds,axis=1)
 s = pd.Series(p0)
 vc = s.value_counts().sort_index()
df = pd.DataFrame({'a':np.arange(num_classes),'b':np.ones(num_classes)})
 df.b = df.a.map(vc)
 df.fillna(df.b.min() ,inplace=True)
 mat1 = np.diag(df.b.astype('float32')**EXP)
 p0 = np.argmax(preds.dot(mat1), axis=1)
 return p0
def main() :
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=123)
 parser.add_argument('--input_size', type=str, default='224,224')
 parser.add_argument('--batch_size', type=int, default=32)
 parser.add_argument('--backbone', type=str, default='efficientnet-b3')
 parser.add_argument('--weights', type=str, default='.. /input/efnet-b3-cosface-val9-3/best.h5')
 parser.add_argument('--r_weights', type=str, default='.. /input/efnet-b3-rcv-models/r_best.h5')
 parser.add_argument('--v_weights', type=str, default='.. /input/efnet-b3-rcv-models/v_best.h5')
 parser.add_argument('--c_weights', type=str, default='.. /input/efnet-b3-rcv-models/c_best.h5')
 parser.add_argument('--mode',type=str, default='all')
 args, _ = parser.parse_known_args()
org_height = 137
org_width = 236
args.input_size = tuple(int(x)for x in args.input_size.split(','))
np.random.seed(args.seed)
 tf.random.set_seed(args.seed)
 u_model = get_model(input_size=args.input_size +(3,), backbone=args.backbone,weights=None)
 u_model.load_weights(args.weights)
 r_model = get_r_model(input_size=args.input_size +(3,), backbone=args.backbone,weights=None)
 r_model.load_weights(args.r_weights)
 v_model = get_v_model(input_size=args.input_size +(3,), backbone=args.backbone,weights=None)
 v_model.load_weights(args.v_weights)
 c_model = get_c_model(input_size=args.input_size +(3,), backbone=args.backbone,weights=None)
 c_model.load_weights(args.c_weights)
 model = [u_model, r_model, v_model, c_model]
all_preds = dict(r = [], v = [], c = [])
 all_image_ids = []
img_batch = []
for i in tqdm(range(4)) :
parquet_fn = f'.. /input/bengaliai-cv19/test_image_data_{i}.parquet'
all_images = pd.read_parquet(parquet_fn)
 image_ids = all_images['image_id'].values
all_images = all_images.iloc[:, 1:].values
for k in tqdm(range(len(image_ids))):
all_image_ids.append(image_ids[k])
 img = all_images[k]
img = normalize_image(img, org_width, org_height, args.input_size[1], args.input_size[0])
 img_batch.append(np.dstack([img] * 3))
if len(img_batch)>= args.batch_size:
predict_batch(img_batch, model, all_preds)
 img_batch = []
if len(img_batch)> 0:
predict_batch(img_batch, model, all_preds)
 img_batch = []
np.save(f'r_preds{i}', all_preds['r'])
 np.save(f'v_preds{i}', all_preds['v'])
 np.save(f'c_preds{i}', all_preds['c'])
 all_preds = dict(r = [], v = [], c = [])
 del all_images
gc.collect()
del u_model, r_model, v_model, c_model, model
gc.collect()
r_preds = np.concatenate([np.load(f'r_preds{i}.npy')for i in range(4)] ,axis=0)
 r_preds = postprocess(r_preds, 168, -1.2)
 v_preds = np.concatenate([np.load(f'v_preds{i}.npy')for i in range(4)] ,axis=0)
 v_preds = postprocess(v_preds, 11, -1.2)
 c_preds = np.concatenate([np.load(f'c_preds{i}.npy')for i in range(4)] ,axis=0)
 c_preds = postprocess(c_preds, 7, -0.5)
 row_id, target = [], []
for iid, r, v, c in zip(all_image_ids, r_preds, v_preds, c_preds):
row_id.append(iid + '_grapheme_root')
 target.append(r)
 row_id.append(iid + '_vowel_diacritic')
 target.append(v)
 row_id.append(iid + '_consonant_diacritic')
 target.append(c)
 sub_fn = 'submission.csv'
sub = pd.DataFrame({'row_id': row_id, 'target': target})
 sub.to_csv(sub_fn, index=False)
 print(f'Done wrote to {sub_fn}')
 main()<set_options>
train_df['train_set'] = 1
test_df['train_set'] = 0
train_ids = train_df.pop('Id')
 test_ids = test_df.pop('Id')
 y = train_df.pop('SalePrice')
 df = train_df.append(test_df ).reset_index(drop=True)
 df.head()
%matplotlib inline 
<set_options>
cols_with_nulls, null_perc = [], []
for col in df.columns:
if sum(df[col].isnull())> 0:
cols_with_nulls.append(col)
 null_perc.append(100 * sum(df[col].isnull())/ len(df))
null_counts = pd.DataFrame({'column': cols_with_nulls, 'null_perc': null_perc} ).sort_values(by='null_perc',
ascending=False )
torch.device("cuda" if torch.cuda.is_available() else "cpu" )<define_variables>
null_threshold =.5
for col in df.columns:
if(sum(df[col].isnull())/ len(df)> null_threshold)&(col != 'SalePrice'):
df.drop(col, axis=1, inplace=True )
INFERENCE = True
KAGGLE = True
PATH = ".. /input/bengaliai-cv19" if KAGGLE else ".. /.. /data/bengali"
MODELS_PATH = ".. /input/bengali" if KAGGLE else ".. /.. /data/bengali"
CSV_PATH = "" if KAGGLE else ".. /.. /data/bengali/"
!dir $PATH
PREFIX = "test" if INFERENCE else "train"
file = PATH + "/{0}_image_data_{1}.parquet"
WIDTH=236
HEIGHT=137
FILES_NUM = 4
FILE_RECORDS = 50210
TRAIN_RECORDS_TOTAL = FILES_NUM * FILE_RECORDS
BS = 8 if INFERENCE else 8
TRAIN_SIZE = int(FILE_RECORDS * 0.9)
 VALID_SIZE = int(FILE_RECORDS * 0.1)
 np.random.seed(42 )<feature_engineering>
ordinal_numericals = ['BsmtFullBath', 'BsmtHalfBath', 'Fireplaces', 'FullBath', 'GarageCars',
'HalfBath', 'MoSold', 'OverallCond', 'OverallQual', 'TotRmsAbvGrd']
date_numericals = ['GarageYrBlt', 'YearBuilt', 'YearRemodAdd', 'YrSold']
categorical_numericals = ['MSSubClass']
dfs = None
if(not INFERENCE):
t1 = time.time()
dfs = [pd.read_parquet(file.format(PREFIX,i)) for i in range(4)]
print(time.time() - t1)
<load_from_csv>
continuous_feats = [col for col in numerical_cols if col not in ordinal_numericals and\
col not in date_numericals and\
col not in categorical_numericals and\
col != 'SalePrice' and col != 'train_set']
def get_data(ind):
df = pd.read_parquet(file.format(PREFIX,ind)) if INFERENCE else dfs[ind]
ids = df.iloc[:,0] if INFERENCE else None
data = df.iloc[:,1:].values.reshape(-1,HEIGHT, WIDTH ).astype(np.uint8)
 return data, ids
train_csv=None
if(not INFERENCE):
train_data, ids = get_data(0)
 train_csv = pd.read_csv(PATH + "/train.csv")
 print(train_csv.head())
 print(train_csv['grapheme_root'])
 print(train_csv.max())
 print(train_data.shape )<define_search_space>
categorical_strings = ['BldgType', 'Condition1', 'Condition2', 'Electrical', 'Exterior1st',
'Exterior2nd', 'Foundation', 'GarageType', 'Heating', 'HouseStyle',
'LandContour', 'LotConfig', 'LotShape', 'MSZoning', 'MasVnrType',
'Neighborhood', 'PavedDrive', 'RoofMatl', 'RoofStyle', 'SaleCondition',
'SaleType', 'Street', 'Utilities']
ordinal_strings = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual', 'ExterCond',
'ExterQual', 'FireplaceQu', 'Functional', 'GarageCond', 'GarageFinish', 'GarageQual',
'HeatingQC', 'KitchenQual', 'LandSlope']
boolean_strings = ['CentralAir']
bc=[255,255,255]
def get_aug(vert_flip = False):
p_vert_flip = 1 if vert_flip else 0
train_aug = A.Compose([
A.OneOf([A.Blur(blur_limit=10,p=1.0),
A.GaussianBlur(blur_limit=15,p=1.0),
A.GaussNoise(var_limit=200,p=1.0),
],p=0.4),
A.OneOf([A.Cutout(num_holes=9, max_h_size=20, max_w_size=20, p=1.0, fill_value=255),
A.CoarseDropout(max_holes=8, max_height=20, max_width=20, p=1.0, fill_value=255),
A.GridDistortion(border_mode=cv2.BORDER_CONSTANT,value=bc, p=0.1)
 ],p=0.4),
A.OneOf([A.RandomBrightness(p=1.0),
A.RandomContrast(p=1.0),
A.RandomBrightnessContrast(p=1.0)
 ],p=0.4),
A.OneOf([A.IAAPiecewiseAffine(p=1.0),
A.ElasticTransform(sigma=30, alpha=1, alpha_affine=30,
border_mode=cv2.BORDER_CONSTANT,value=bc, p=1.0)
 ],p=0.4),
A.RandomGamma(p=0.8),
A.ShiftScaleRotate(shift_limit=0.0625,scale_limit=0.1,
rotate_limit=30,p=1.0,value=bc,border_mode=cv2.BORDER_CONSTANT),
])
 inf_aug = A.VerticalFlip(p=p_vert_flip)
 return inf_aug if INFERENCE else train_aug
if(not INFERENCE and False):
n_imgs = 8
fig, axs = plt.subplots(n_imgs, 2, figsize=(10, 5*n_imgs))
for idx in range(n_imgs):
img = get_image(train_data, idx)
 img0 = train_data[idx]
axs[idx,0].imshow(img)
 axs[idx,0].axis('off')
 axs[idx,1].imshow(get_aug(True )(image=img)['image'])
 axs[idx,1].axis('off')
 plt.show()<categorify>
all_categoricals = categorical_numericals + categorical_strings
df[all_categoricals] = df[all_categoricals].fillna('NA')
 all_ordinals = ordinal_numericals + ordinal_strings
mode_df = pd.DataFrame(data=df[all_ordinals].mode().values.tolist() [0], index=all_ordinals)
 df[all_ordinals] = df[all_ordinals].fillna(mode_df[0])
 df[continuous_feats] = df[continuous_feats].fillna(df[continuous_feats].median())
 df[date_numericals] = df[date_numericals].fillna(df[date_numericals].median() )
def my() :
def __init__(self):
self.mean = m
self.std = s
self.n = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 def __call__(self, sample):
print("!!!called")
 return n(sample)
 data_transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 ])
 class GraphemeDataset(Dataset):
def __init__(self, labels_csv, data, file_num = 0, transform=data_transform, rev = True):
self.labels_csv = labels_csv
self.data = data
self.transform = data_transform
self.offset = file_num * FILE_RECORDS
self.aug = get_aug(rev)
 def __len__(self):
return self.data.shape[0]
def prepare_image(self,img, do_aug=True):
result = img
if do_aug:
result = self.aug(image=img)['image']
result =(result/255 ).astype(np.float32)
 result = self.transform(result)
 return result
def __getitem__(self,idx):
image_pure = get_image(self.data, idx)
 img0 = self.prepare_image(img = image_pure, do_aug=not INFERENCE)
 if(INFERENCE):
img1 = self.prepare_image(img = image_pure, do_aug=True)
 return img0,img1
else :
labels = self.labels_csv.iloc[idx + self.offset][1:4]
root = labels[0]
vowel = labels[1]
consonant = labels[2]
return img0, root, vowel, consonant
def get_dls(ind):
data, ids = get_data(ind)
 rev_ds = GraphemeDataset(train_csv, data, ind, rev = True)
 if(INFERENCE):
rev_dl = DataLoader(rev_ds, batch_size = BS)
 return rev_dl, None, ids
else:
train_ds, valid_ds = torch.utils.data.random_split(rev_ds, [TRAIN_SIZE, VALID_SIZE])
 train_dl = DataLoader(train_ds, batch_size=BS)
 valid_dl = DataLoader(valid_ds, batch_size=BS)
 return train_dl, valid_dl, ids
if(INFERENCE):
check_dl, some_dl, ids = get_dls(0)
 print(ids[0])
 x1, x2 = next(iter(check_dl))
print(x1.shape, x2.shape)
 else:
check_dl, valid_dl, ids = get_dls(0)
 x, l1, l2, l3 = next(iter(check_dl))
print(x.shape, l1.shape, l2.shape, l3.shape, type(x[0][0][0][0])) 
<set_options>
print('Shape before one-hot encoding of categoricals =\t{}'.format(df.shape))
df = pd.get_dummies(df, columns=all_categoricals)
 print('Shape after one-hot encoding of categoricals =\t{}'.format(df.shape))
def requires_grad(m:nn.Module, b:Optional[bool]=None)->Optional[bool]:
"If `b` is not set return `requires_grad` of first param, else set `requires_grad` on all params as `b`"
ps = list(m.parameters())
 if not ps: return None
if b is None: return ps[0].requires_grad
for p in ps:
p.requires_grad=b
def is_pool_type(l): return re.search(r'Pool[123]d$', l.__class__.__name__)
 def has_pool_type(m):
if is_pool_type(m): return True
for l in m.children() :
if has_pool_type(l): return True
return False
def create_body(model):
ll = list(enumerate(model.children()))
cut = next(i for i,o in reversed(ll)if has_pool_type(o))
return nn.Sequential(*list(model.children())[:cut])
 class AdaptiveConcatPool2d(nn.Module):
def __init__(self, sz=None):
super().__init__()
sz = sz or(1,1)
 self.ap = nn.AdaptiveAvgPool2d(sz)
 self.mp = nn.AdaptiveMaxPool2d(sz)
 def forward(self, x): return torch.cat([self.mp(x), self.ap(x)], 1)
 class Flatten(nn.Module):
def forward(self, x): return x.view(x.size(0), -1)
 def bn_drop_lin(n_in:int, n_out:int, bn:bool=True, p:float=0., actn:Optional[nn.Module]=None):
"Sequence of batchnorm(if `bn`), dropout(with `p`)and linear(`n_in`,`n_out`)layers followed by `actn`."
layers = [nn.BatchNorm1d(n_in)] if bn else []
if p != 0: layers.append(nn.Dropout(p))
layers.append(nn.Linear(n_in, n_out))
if actn is not None: layers.append(actn)
 return layers
def create_head(is_densenet):
lin_ftrs = [4416, 256, 168 + 11 + 7] if is_densenet else [4096, 256, 168 + 11 + 7]
ps = [0.5]
bn_final=False
concat_pool=True
if len(ps)== 1: ps = [ps[0]/2] *(len(lin_ftrs)-2)+ ps
actns = [nn.ReLU(inplace=True)] *(len(lin_ftrs)-2)+ [None]
pool = AdaptiveConcatPool2d() if concat_pool else nn.AdaptiveAvgPool2d(1)
 layers = [pool, Flatten() ]
for ni,no,p,actn in zip(lin_ftrs[:-1], lin_ftrs[1:], ps, actns):
layers += bn_drop_lin(ni, no, True, p, actn)
 if bn_final: layers.append(nn.BatchNorm1d(lin_ftrs[-1], momentum=0.01))
head = nn.Sequential(*layers)
 return head
def cond_init(m:nn.Module):
"Initialize the non-batchnorm layers of `m` with `init_func`."
if(not isinstance(m, nn.BatchNorm1d)) and requires_grad(m):
if hasattr(m, 'weight'): nn.init.kaiming_normal_(m.weight)
 if hasattr(m, 'bias')and hasattr(m.bias, 'data'): m.bias.data.fill_(0.)
def apply_leaf(m:nn.Module, f):
"Apply `f` to children of `m`."
c = list(m.children())
 if isinstance(m, nn.Module): f(m)
 for l in c: apply_leaf(l,f)
 def apply_init(m):
"Initialize all non-batchnorm layers of `m` with `init_func`."
apply_leaf(m, cond_init)
 def get_resnet() :
resnet101 = models.resnet101(pretrained=not INFERENCE)
 body = create_body(resnet101)
 head = create_head(False)
 apply_init(head)
 return nn.Sequential(body, head),body
def get_densenet() :
densenet= models.densenet161(pretrained= not INFERENCE)
 head = nn.Linear(2208, 168 + 11 + 7)
 apply_init(head)
 children = densenet.children()
body = next(children)
 head = create_head(True)
 return nn.Sequential(body, head),body
m_resnet,body = get_resnet()
m_densenet,body = get_densenet()
print(m_densenet 
)
<find_best_params>
ordinal_dict_1 = {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
ordinal_dict_2 = {'NA': 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}
ordinal_dict_3 = {'NA': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}
ordinal_dict_4 = {'Sal': 0, 'Sev': 1, 'Maj2': 2, 'Maj1': 3, 'Mod': 4, 'Min2': 5, 'Min1': 6, 'Typ': 7}
ordinal_dict_5 = {'Na': 0, 'Unf': 1, 'RFn': 2, 'Fin': 3}
ordinal_dict_6 = {'Sev': 0, 'Mod': 1, 'Gtl': 2}
m = m_resnet
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 epochs = 1
optimizer = torch.optim.Adam(m.parameters() , lr=2e-3)
 m.to(device)
 criterion = nn.CrossEntropyLoss()
def split(a):
return a[:, 0:168], a[:, 168:168+11], a[:, 168+11:]
def loss_func(roots168, vowels11, consonants7, outputs):
r,v,c = split(outputs)
 root_loss = criterion(r, roots168)
 vowel_loss = criterion(v, vowels11)
 consonant_loss = criterion(c, consonants7)
 total_loss = root_loss*2 + vowel_loss + consonant_loss
return total_loss
def acc_func(roots168, vowels11, consonants7, outputs, acc):
r,v,c = split(outputs)
 acc_c = [0,0,0,0]
acc_c[0] =(r.argmax(1)==roots168 ).float().mean()
acc_c[1] =(v.argmax(1)==vowels11 ).float().mean()
acc_c[2] =(c.argmax(1)==consonants7 ).float().mean()
acc_c[3] =(2*acc_c[0] + acc_c[1] + acc_c[2])/4
return np.add(acc, acc_c)
 def getv(v, length):
return round(( v/length ).item() ,4)
 def getv_list(v, length):
return [getv(e,length)for e in v]
def to_d(i, r, v,c):
return i.to(device), r.to(device), v.to(device), c.to(device)
 def train_int(train, dl, m, pbar):
acc = [0,0,0,0]
loss = 0
m.train(train)
 for(idx,(inp, r, v, c)) in(enumerate(dl)) :
inp, r, v, c = inp.to(device), r.to(device), v.to(device), c.to(device)
 outputs = m(inp)
 acc = acc_func(r, v, c, outputs, acc)
 if(train):
total_loss = loss_func(r, v, c, outputs)
 loss += total_loss
total_loss.backward()
optimizer.step()
optimizer.zero_grad()
pbar.update(BS)
 return acc, loss
def train_epoch(num, start3):
pbar = tqdm_notebook(total = TRAIN_RECORDS_TOTAL)
 print("acc train\tloss train\tacc valid")
 if(start3 != 0):
m.load_state_dict(torch.load(MODELS_PATH + "/densenet162.2e-3.4.tmp"))
for file_num in range(start3, FILES_NUM):
train_dl, valid_dl, ids = get_dls(file_num)
 acc_train, loss_train = train_int(True, train_dl, m, pbar)
 acc_valid, l = train_int(False, valid_dl, m, pbar)
 train_length = len(train_dl)
 acc_p = getv(acc_train[3], train_length)
 loss_p = getv(loss_train, train_length)
 acc_v_p = getv_list(acc_valid, len(valid_dl))
print(acc_p,"\t\t", loss_p, "\t\t",acc_v_p[3],
"(",acc_v_p[0], acc_v_p[1], acc_v_p[2],")")
 torch.save(m.state_dict() , MODELS_PATH + "/densenet162.2e-3.4.tmp")
 pbar.close()
def train() :
print("Will train a frozen-body model...")
 requires_grad(body, False)
 start1 = 5
start3 = 2
m.load_state_dict(torch.load(MODELS_PATH + "/densenet162.2e-3.5.0"))
for num in range(start1, 5):
print("Frozen, number " + str(num))
train_epoch(num, start3)
 start3 = 0
torch.save(m.state_dict() , MODELS_PATH + "/densenet162.2e-3." + str(num))
print("Will train an unfrozen-body model...")
 start2 = 5
start3 = 2
requires_grad(body, True)
 for num in range(start2,30):
print("Unfrozen, number " + str(num))
train_epoch(num, start3)
 start3 = 0
torch.save(m.state_dict() , MODELS_PATH + "/densenet162.2e-3.5." + str(num))
if(not INFERENCE):
train() 
<load_from_csv>
for cat in ['ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'HeatingQC', 'KitchenQual', 'FireplaceQu',
'GarageQual', 'GarageCond', ]:
df[cat] = df[cat].map(ordinal_dict_1)
 for cat in ['BsmtExposure']:
df[cat] = df[cat].map(ordinal_dict_2)
 for cat in ['BsmtFinType1', 'BsmtFinType2']:
df[cat] = df[cat].map(ordinal_dict_3)
 for cat in ['Functional']:
df[cat] = df[cat].map(ordinal_dict_4)
 for cat in ['GarageFinish']:
df[cat] = df[cat].map(ordinal_dict_5)
 for cat in ['LandSlope']:
df[cat] = df[cat].map(ordinal_dict_6)
 null_counts.set_index('column', inplace=True)
 for col in ordinal_strings:
if sum(df[col].isnull())> 0:
null_counts.loc[col, 'perc_after_encoding'] = 100 * sum(df[col].isnull())/ len(df)
 assert null_counts.loc[col, 'perc_after_encoding'] == null_counts.loc[col, 'null_perc'],\
'Nulls introduced in feature {}, check corresponding dictionary'.format(col )
def handle_single_file(file_num, subm_csv):
print("file",file_num)
 test_dl, something, ids = get_dls(file_num)
 m_densenet.train(False)
 m_resnet.train(False)
 index = 0
for(idx,(img, img_flipped)) in(enumerate(test_dl)) :
img = img.to(device)
 outputs1 = m_resnet(img)
 outputs = outputs1
r,v,c = split(outputs)
 roots = r.argmax(1 ).tolist()
vowels = v.argmax(1 ).tolist()
consonants = c.argmax(1 ).tolist()
current_bs = img.shape[0]
for i in range(current_bs):
test_name = ids[index]
subm_csv.write(test_name + "_consonant_diacritic," + str(consonants[i])+ "
")
 subm_csv.write(test_name + "_grapheme_root," + str(roots[i])+ "
")
 subm_csv.write(test_name + "_vowel_diacritic," + str(vowels[i])+ "
")
 index += 1
def inference() :
m_resnet.load_state_dict(torch.load(MODELS_PATH + "/resnet101.2e-4.5.21"))
m_densenet.load_state_dict(torch.load(MODELS_PATH + "/densenet162.2e-3.5.9"))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 m_resnet.to(device)
 m_densenet.to(device)
 print("Inference.... ")
 total_index = 0
subm_csv = open(CSV_PATH + "submission.csv", 'w')
 subm_csv.write('row_id,target
')
 for file_num in range(FILES_NUM):
handle_single_file(file_num,subm_csv)
 subm_csv.flush()
subm_csv.close()
if(INFERENCE):
inference()<install_modules>
for cat in boolean_strings:
encoder = LabelEncoder()
df[cat] = encoder.fit_transform(df[cat] )
!pip install.. /input/kaggle-efficientnet-repo/efficientnet-1.0.0-py3-none-any.whl<set_options>
df['TotalSF'] = df['1stFlrSF'] + df['2ndFlrSF']
df['AltTotalSF'] = df['TotalBsmtSF'] + df['ScreenPorch'] + df['WoodDeckSF'] + df['OpenPorchSF'] + df['EnclosedPorch']\
+ df['GarageArea'] + df['LowQualFinSF']
%matplotlib inline
start_time = time.time()<define_variables>
date_numericals.remove('YrSold')
 for date_col in date_numericals:
df[date_col] = df['YrSold'] - df[date_col]
df['RemodelledFlag'] =(df['YearBuilt'] != df['YearRemodAdd'] ).astype(int )
dataset = '/kaggle/input/bengaliai-cv19'
pretrained = '.. /input/bangla-graphemes-pretrained-weights'<train_model>
outlier_mask =(df['TotalSF'] > 4000)&(df['train_set'] == 1)
 outlier_mask_target = outlier_mask[df['train_set'] == 1]
df = df[~outlier_mask]
y = y[~outlier_mask_target.values]
if os.path.isfile(os.path.join(pretrained,"efficientNetB3.h5")) \
and os.path.isfile(os.path.join(pretrained,"enet.csv")) :
print('Model is present')
 else:
print("Error.No Model Found" )<define_variables>
skew_array = skew(df[continuous_feats].fillna(df[continuous_feats].median()), axis=0)
 skew_df = pd.DataFrame({'column': continuous_feats, 'skew': skew_array} ).sort_values(by='skew', ascending=False )
SIZE = 80
N_ch = 1<load_pretrained>
df[continuous_feats] = np.log1p(df[continuous_feats].fillna(df[continuous_feats].median()))
log_y = np.log1p(y )
model = load_model(os.path.join(pretrained, 'efficientNetB3.h5'))<normalization>
scaler = MinMaxScaler()
scaled_df = pd.DataFrame(data=scaler.fit_transform(df),
columns=df.columns)
 scaled_df.head()
def resize(df, size=80):
resized = {}
resize_size=80
angle=0
for i in range(df.shape[0]):
image=df.loc[df.index[i]].values.reshape(137,236)
 image_center = tuple(np.array(image.shape[1::-1])/ 2)
 matrix = cv2.getRotationMatrix2D(image_center, angle, 1.0)
 image = cv2.warpAffine(image, matrix, image.shape[1::-1], flags=cv2.INTER_AREA,
borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
matrix = cv2.getRotationMatrix2D(image_center, 0, 1.0)
 image = cv2.warpAffine(image, matrix, image.shape[1::-1], flags=cv2.INTER_AREA,
borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
augBright=A.RandomBrightnessContrast(p=1.0)
 image = augBright(image=image)['image']
_, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx = 0
ls_xmin = []
ls_ymin = []
ls_xmax = []
ls_ymax = []
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
 ls_xmin.append(x)
 ls_ymin.append(y)
 ls_xmax.append(x + w)
 ls_ymax.append(y + h)
 xmin = min(ls_xmin)
 ymin = min(ls_ymin)
 xmax = max(ls_xmax)
 ymax = max(ls_ymax)
 roi = image[ymin:ymax,xmin:xmax]
resized_roi = cv2.resize(roi,(resize_size, resize_size),interpolation=cv2.INTER_AREA)
 resized[df.index[i]] = resized_roi.reshape(-1)
 resized_df = pd.DataFrame(resized ).T
return resized_df<define_variables>
train_df = df[df['train_set'] == df['train_set'].max() ].copy()
test_df = df[df['train_set'] == df['train_set'].min() ].copy()
for dataset in [train_df, test_df]:
dataset.drop('train_set', axis=1, inplace=True )
tgt_cols = ['grapheme_root','vowel_diacritic','consonant_diacritic']<predict_on_test>
def rmsle(y_true, y_pred):
assert len(y_pred)== len(y_true), 'Input arrays different lengths'
return np.sqrt(np.mean(np.power(y_pred - y_true, 2)) )
row_ids = []
targets = []
id = 0
for i in range(4):
img_df = pd.read_parquet(os.path.join(
 dataset, 'test_image_data_'+str(i)+'.parquet'))
img_df = img_df.drop('image_id', axis = 1)
 img_df = resize(img_df, SIZE)/ 255
X_test = img_df.values.reshape(-1, SIZE, SIZE, N_ch)
 preds = model.predict(X_test)
 for j in range(len(X_test)) :
for k in range(3):
row_ids.append('Test_'+str(id)+'_'+tgt_cols[k])
 targets.append(np.argmax(preds[k][j]))
id += 1<save_to_csv>
def rmsle_for_lgbm(preds, train_data):
labels = train_data.get_label()
return 'RMSLE', np.sqrt(np.mean(np.power(np.log1p(preds)- np.log1p(labels), 2))), False
df_submit = pd.DataFrame({'row_id':row_ids,'target':targets},
columns = ['row_id','target'])
 df_submit.to_csv('submission.csv',index=False)
 df_submit.head(10 )<install_modules>
rmsle_score = make_scorer(rmsle )
!pip install.. /input/kaggle-efficientnet-repo/efficientnet-1.0.0-py3-none-any.whl<init_hyperparams>
regressions_to_try = [LinearRegression() , Ridge() , Lasso() , ElasticNet() ]
preds_list = []
for model in regressions_to_try:
scores = cross_validate(model, train_df, log_y, cv=3, scoring=rmsle_score, return_train_score=False)
 preds_list.append(cross_val_predict(model, train_df, log_y, cv=3))
print('{} RSMLE loss = {:.4f}'.format(type(model), scores['test_score'].mean()))
def normalize(image):
image -= tf.constant([0.485 * 255, 0.456 * 255, 0.406 * 255])
 image /= tf.constant([0.229 * 255, 0.224 * 255, 0.225 * 255])
 return image
def get_model_grapheme(input_size, backbone='efficientnet-b7', weights='imagenet', tta=False):
print(f'Using backbone {backbone} and weights {weights}')
 x = L.Input(shape=input_size, name='imgs', dtype='float32')
 y = normalize(x)
 if backbone.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone[-1]}')
 y = model_fn(input_shape=input_size, weights=weights, include_top=False )(y)
 y = L.GlobalAveragePooling2D()(y)
 l1 = L.Dense(168, activation='softmax', name='l1' )(y)
 model = tf.keras.Model(x, outputs=[l1])
 return model
def get_model_consonant(input_size, backbone='efficientnet-b3', weights='imagenet', tta=False):
print(f'Using backbone {backbone} and weights {weights}')
 x = L.Input(shape=input_size, name='imgs', dtype='float32')
 y = normalize(x)
 if backbone.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone[-1]}')
 y = model_fn(input_shape=input_size, weights=weights, include_top=False )(y)
 y = L.GlobalAveragePooling2D()(y)
 y= L.Dense(7, activation='softmax' )(y)
 model = tf.keras.Model(x, y)
 return model
def get_model_vowel(input_size, backbone='efficientnet-b3', weights='imagenet', tta=False):
print(f'Using backbone {backbone} and weights {weights}')
 x = L.Input(shape=input_size, name='imgs', dtype='float32')
 y = normalize(x)
 if backbone.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone[-1]}')
 y = model_fn(input_shape=input_size, weights=weights, include_top=False )(y)
 y = L.GlobalAveragePooling2D()(y)
 y= L.Dense(11, activation='softmax' )(y)
 model = tf.keras.Model(x,y)
 return model
def normalize_image(img, org_width, org_height, new_width, new_height):
img = 255 - img
img =(img *(255.0 / img.max())).astype(np.uint8)
 img = img.reshape(org_height, org_width)
 image_resized = cv2.resize(img,(new_width, new_height), interpolation=cv2.INTER_LINEAR)
 return image_resized
def dump_images(args, org_width, org_height, new_width, new_height):
labels = pd.read_csv(args.labels)
 iids = labels['image_id']
root = labels['grapheme_root']
vowel = labels['vowel_diacritic']
consonant = labels['consonant_diacritic']
labels = {a:(b, c, d)for a, b, c, d in zip(iids, root, vowel, consonant)}
tuples = sorted(set(labels.values()))
tuples_to_int = {v: k for k, v in enumerate(tuples)}
print(f'Got {len(tuples)} unique combinations')
 for i in tqdm(range(0, 4)) :
df = pd.read_parquet(args.data_template % i)
 image_ids = df['image_id'].values
df = df.drop(['image_id'], axis=1)
 for image_id, index in tqdm(zip(image_ids, range(df.shape[0])) , total=df.shape[0]):
normalized = normalize_image(df.loc[df.index[index]].values,
org_width, org_height, new_width, new_height)
 r, v, c = labels[image_id]
tuple_int = tuples_to_int[(r, v, c)]
out_fn = os.path.join(args.image_dir, f'{image_id}_rt_{r}_vl_{v}_ct_{c}_ti_{tuple_int}.png')
 cv2.imwrite(out_fn, normalized)
 def decode_predictions(y_pred, inv_tuple_map):
y_argmax = np.argmax(y_pred, -1)
 decoded = []
for yy in y_argmax:
decoded.append(inv_tuple_map[int(yy)])
 return decoded
def decode_predictions_v2(y_pred1,y_pred2,y_pred3):
decoded = []
for k in range(y_pred1.shape[0]):
rr=y_pred1[k]
vv=y_pred2[k]
cc=y_pred3[k]
rr = rr.argmax(-1)
 vv = vv.argmax(-1)
 cc = cc.argmax(-1)
 decoded.append(( rr, vv, cc))
return decoded
def process_batch(image_id_batch, img_batch, row_id, target, model1, model2, model3):
img_batch = np.float32(img_batch)
 if img_batch.ndim != 4:
img_batch = np.expand_dims(img_batch, 0)
 y_pred1 = model1.predict(img_batch)
 y_pred2 = model2.predict(img_batch)
 y_pred3 = model3.predict(img_batch)
 decoded = decode_predictions_v2(y_pred1,y_pred2,y_pred3)
 for iid, dd in zip(image_id_batch, decoded):
row_id.append(iid + '_grapheme_root')
 target.append(dd[0])
 row_id.append(iid + '_vowel_diacritic')
 target.append(dd[1])
 row_id.append(iid + '_consonant_diacritic')
 target.append(dd[2])
 def main() :
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=123)
 parser.add_argument('--input_size', type=str, default='224,224')
 parser.add_argument('--batch_size', type=int, default=32)
 parser.add_argument('--backbone', type=str, default='efficientnet-b7')
 parser.add_argument('--weights', type=str, default='.. /input/b3out3/efficientb3_weights00000040.h5')
 args, _ = parser.parse_known_args()
org_height = 137
org_width = 236
args.input_size = tuple(int(x)for x in args.input_size.split(','))
np.random.seed(args.seed)
 tf.random.set_seed(args.seed)
 model1 = get_model_grapheme(input_size=args.input_size +(3,),
weights=None)
 model2 = get_model_vowel(input_size=args.input_size +(3,),
weights=None)
 model3 = get_model_consonant(input_size=args.input_size +(3,),
weights=None)
 print(f'Loading weights {args.weights}')
 model1.load_weights('.. /input/separatemodels/graphemeb7.h5')
 model2.load_weights('.. /input/vowelconsonant/weights00000096.h5')
 model3.load_weights('.. /input/vowelconsonant/weights00000080.h5')
 row_id, target = [], []
image_id_batch, img_batch = [], []
for i in tqdm(range(4)) :
parquet_fn = f'.. /input/bengaliai-cv19/test_image_data_{i}.parquet'
df = pd.read_parquet(parquet_fn)
 image_ids = df['image_id'].values
df = df.drop(['image_id'], axis=1)
 for k in range(len(image_ids)) :
image_id = image_ids[k]
img = df.iloc[k].values
img = normalize_image(img, org_width, org_height, args.input_size[1], args.input_size[0])
 img_batch.append(np.dstack([img] * 3))
image_id_batch.append(image_id)
 if len(img_batch)>= args.batch_size:
process_batch(image_id_batch, img_batch, row_id, target, model1, model2, model3)
 image_id_batch, img_batch = [], []
if len(img_batch)> 0:
process_batch(image_id_batch, img_batch, row_id, target, model1, model2, model3)
 image_id_batch, img_batch = [], []
sub_fn = 'submission.csv'
sub = pd.DataFrame({'row_id': row_id, 'target': target})
 sub.to_csv(sub_fn, index=False)
 print(f'Done wrote to {sub_fn}')
 main() 
<data_type_conversions>
for alph in [1, 3, 6, 9, 12, 15]:
test_model = Ridge(alpha=alph)
 scores = cross_validate(test_model, train_df, log_y, cv=3, scoring=rmsle_score, return_train_score=False)
 print('Alpha = {}, Ridge RSMLE loss = {:.4f}'.format(alph, scores['test_score'].mean()))
IMAGE_SIZE = 137, 236
class DatasetRetriever(Dataset):
def __init__(self, df):
self.image_ids = df.iloc[:, 0].tolist()
self.images = torch.from_numpy(255 - df[[str(x)for x in range(32332)]].values)
 def __len__(self):
return self.images.shape[0]
def __getitem__(self, idx):
img = self.images[idx]
img = img.view(*IMAGE_SIZE)
 img = img.to(torch.float32)/ 255.
 img = img.unsqueeze(0)
 img = img.repeat(3, 1, 1)
 img_id = self.image_ids[idx]
return img_id, img
class Predictor:
def __init__(self, model):
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 self.model = model.to(self.device, dtype=torch.float32);
self.model.eval()
print(f'Model prepared.Device is {self.device}')
 def predict(self, inputs):
inputs = inputs.to(self.device, dtype=torch.float32)
 with torch.no_grad() :
outputs = self.model(inputs)
 return outputs
def load(self, path):
checkpoint = torch.load(path, map_location=self.device)
 self.model.load_state_dict(checkpoint['model_state_dict'] )<predict_on_test>
lin = Ridge(alpha=9)
 lin.fit(train_df, log_y)
 lin_preds = lin.predict(test_df )
predictors = []
for i,(model_path, net, coef)in enumerate([ 
('/kaggle/input/ghostnetbengali/checkpoint.pt', ghost_net(num_classes=168+11+7), 0.5), 
('/kaggle/input/ghostnetbengali/densenet121-checkpoint.pt', densenet121(num_classes=168+11+7), 0.5),
]):
predictor = Predictor(net)
 predictor.load(model_path)
 predictors.append(( predictor, coef))
def predict_to_numpy(predict):
return torch.nn.functional.softmax(predict, dim=1 ).data.cpu().numpy().argmax(axis=1)
 def make_prediction(images):
outputs = 0
for predictor, coef in predictors:
outputs += predictor.predict(images)* coef
roots = predict_to_numpy(outputs[:,:168])
 vowels = predict_to_numpy(outputs[:,168:168+11])
 consonants = predict_to_numpy(outputs[:,168+11:])
 return roots, vowels, consonants<load_from_csv>
lgbm_model = lgbm.LGBMRegressor(
 boosting_type='gbdt',
objective='huber',
learning_rate=0.2,
min_child_samples=30,
colsample_bytree=0.9,
max_depth=-1,
num_leaves=31,
reg_lambda=0,
n_estimators=1000 
)
 skgbm_model = GradientBoostingRegressor(
 loss='huber',
n_estimators=700,
alpha=.5,
max_depth=3,
subsample=.6
)
%%time
target = []
row_id = []
for i in range(4):
data = pd.read_parquet(f'.. /input/bengaliai-cv19/test_image_data_{i}.parquet')
 dataset = DatasetRetriever(data)
 data_loader = DataLoader(dataset, batch_size=256, num_workers=4, shuffle=False, sampler=SequentialSampler(dataset))
for idx,(image_ids, images)in tqdm(enumerate(data_loader), total=len(data_loader)) :
roots, vowels, consonants = make_prediction(images)
 for image_id, root, vowel, consonant in zip(image_ids, roots, vowels, consonants):
row_id.append(image_id + '_consonant_diacritic')
 target.append(consonant)
 row_id.append(image_id + '_grapheme_root')
 target.append(root)
 row_id.append(image_id + '_vowel_diacritic')
 target.append(vowel )<save_to_csv>
ensembles_to_try = [lgbm_model, skgbm_model]
preds_list = []
for i in range(len(ensembles_to_try)) :
scores = cross_validate(ensembles_to_try[i], train_df, log_y,
cv=3,
scoring=rmsle_score,
return_train_score=False)
 preds_list.append(cross_val_predict(ensembles_to_try[i], train_df, log_y, cv=3))
print('{} RSMLE loss = {:.4f}'.format(type(ensembles_to_try[i]), scores['test_score'].mean()))
df_submission = pd.DataFrame(
 {
'row_id': row_id,
'target': target
},
columns=['row_id','target'] 
)
 df_submission.to_csv('submission.csv', index=False)
 df_submission.head(10 )<set_options>
gbm = skgbm_model.fit(train_df, log_y)
 gbm_preds = gbm.predict(test_df )
%pylab inline 
<import_modules>
linear_ratio =.6
averaged_preds =(1-linear_ratio)*gbm_preds + linear_ratio*lin_preds
<import_modules><EOS> 
output = pd.DataFrame({'Id': test_ids, 'SalePrice': np.expm1(averaged_preds)})
 output.to_csv('submission.csv', index=False )