Kaggle competition: Rainforest Connection Species Audio Detection
<SOS> metric: WeightedLabelRankingAveragePrecision Kaggle data source: rainforest-connection-species-audio-detection<load_from_csv>
!pip install --up.. /input/rfcx-species-audio-detection/he-torch
!ltt install torch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2
!pip install fastaudio==1.0.0
!pip install wandb --upgrade
test_df = pd.read_feather(root/'test.feather')
 test_df_csv = pd.read_csv(DATA_PATH + 'test.csv')
 test_df['timestamp'] = test_df_csv['timestamp'].copy()
row_ids = test_df['row_id']
test_df = test_df.drop('row_id', axis=1)
 weather_df = pd.read_feather(root/'weather_test.feather')
 weather_df_csv = pd.read_csv(DATA_PATH + 'weather_test.csv')
 weather_df['timestamp'] = weather_df_csv['timestamp'].copy()
weather_df = fill_weather(weather_df)
 weather_df = reduce_mem_usage(weather_df)
 test_df = reduce_mem_usage(test_df)
 test_df = test_df.merge(building_df,left_on='building_id',right_on='building_id',how='left')
 test_df = test_df.merge(weather_df,how='left',on=['timestamp','site_id'])
 del weather_df, building_df, weather_df_csv, test_df_csv
gc.collect()
test_df = feature_engineering(test_df )<predict_on_test>
def placeholder(x):
raise pkg_resources.DistributionNotFound
pkg_resources.get_distribution = placeholder
results = []
for model in models:
if results == []:
results = np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 else:
results += np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 del model
gc.collect()
del test_df, models
gc.collect()<save_to_csv>
torchaudio.set_audio_backend("sox_io")
 print(torch.cuda.is_available() )
submission = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(results, 0, a_max=None)})
 del row_ids,results
gc.collect()
submission.to_csv("submission.csv", index=False )<set_options>
print(f'torch version: {torch.__version__}')
 print(f'torchaudio version: {torchaudio.__version__}')
 print(f'fastaudio version: {fastaudio.__version__}')
 print(f'fastai version: {fastai.__version__}' )
py.init_notebook_mode(connected=True)
 pd.set_option('max_columns', 150)
 py.init_notebook_mode(connected=True)
 init_notebook_mode(connected=True)
<define_variables>
def _rebuild_from_type(func, type, args, dict):
ret = func(*args ).as_subclass(type)
 ret.__dict__ = dict
return ret
@patch
def __reduce_ex__(self: TensorBase, proto):
torch.utils.hooks.warn_if_has_hooks(self)
 args =(type(self), self.storage() , self.storage_offset() , tuple(self.size()), self.stride())
 if self.is_quantized: args = args +(self.q_scale() , self.q_zero_point())
 args = args +(self.requires_grad, OrderedDict())
 f = _fa_rebuild_qtensor if self.is_quantized else _fa_rebuild_tensor
return(_rebuild_from_type,(f, type(self), args, self.__dict__))
metadata_dtype = {'site_id':"uint8",'building_id':'uint16','square_feet':'float32','year_built':'float32','floor_count':"float16"}
weather_dtype = {"site_id":"uint8",'air_temperature':"float16",'cloud_coverage':"float16",'dew_temperature':"float16",'precip_depth_1_hr':"float16",
'sea_level_pressure':"float32",'wind_direction':"float16",'wind_speed':"float16"}
train_dtype = {'meter':"uint8",'building_id':'uint16'}<load_from_csv>
DISABLE_WB = False
WB_CONFIG = {
"project": "RFCX Experiment Tracker",
"name": "baseline with n_mels=256, masked bce with TPFP, monitor LWRAP(10+30)",
"notes": "rerun baseline again, without mixup and audio aug, n_mels=256, masked bce loss with TPFP, monitor LWRAP(10+30)",
"job_type": "train",
"tags": ['resnet34', "TPFP", "Masked BCE loss", "imagenet norm"]
}
DATA_DIR = Path('.. /input/rfcx-species-audio-detection')
 MODEL_DIR = Path('.. /input/fastiai-fastaudio-rainforest-starter')
 IS_PSEUDO_LABEL = False
PSEUDO_LABEL_CSV = Path('.. /input/rainforest-model-checkpoints/df_train_pseudo_3s.csv')
 NORMALIZER = Normalizer.IMAGENET_STATS.value
SR = 48000
INTERVAL = 10
RANDOM_INTERVAL = 8
MELSPEC_CONFIG = {
'n_fft': 2048,
'sample_rate': SR,
'n_mels': 256
}
IS_MIN_MAX_RESCALE = False
RESIZE_MEL = None
IS_AUDIO_AUG = False
IS_MIXUP = False
NUM_WORKERS = 0
CHANNEL = Channel.SINGLE.value
LOSS_FUNCTION = LossFunction.MASKED_BCE_WITH_TPFP.value
IS_MULTILABEL = True
MONITOR_METRIC = RecordMetric.LWRAP.value
FOLD_ID = 0
BATCH_SIZE = 128
LR = 5e-2
FREEZE_EPOCH = 10
UNFREEZE_EPOCH = 30
RANDOM_SEED = 144
%%time
weather_train = pd.read_csv(".. /input/ashrae-energy-prediction/weather_train.csv", parse_dates=['timestamp'], dtype=weather_dtype)
 weather_test = pd.read_csv(".. /input/ashrae-energy-prediction/weather_test.csv", parse_dates=['timestamp'], dtype=weather_dtype)
 metadata = pd.read_csv(".. /input/ashrae-energy-prediction/building_metadata.csv", dtype=metadata_dtype)
 train = pd.read_csv(".. /input/ashrae-energy-prediction/train.csv", parse_dates=['timestamp'], dtype=train_dtype)
 test = pd.read_csv(".. /input/ashrae-energy-prediction/test.csv", parse_dates=['timestamp'], usecols=['building_id','meter','timestamp'], dtype=train_dtype)
 print('Size of train_df data', train.shape)
 print('Size of weather_train_df data', weather_train.shape)
 print('Size of weather_test_df data', weather_test.shape)
 print('Size of building_meta_df data', metadata.shape )<categorify>
train_path = DATA_DIR/ 'train'
test_path = DATA_DIR/ 'test'
train_fns = get_audio_files(train_path)
 df_train_tp = pd.read_csv(DATA_DIR/ 'train_tp.csv')
 df_train_tp['recording_id'] = df_train_tp['recording_id'].map(lambda x: 'train/' + x)
 df_train_tp['species_id'] = df_train_tp['species_id'].astype(str)
 skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=RANDOM_SEED)
 for fold_id,(train_idxs, val_idxs)in enumerate(skf.split(df_train_tp.recording_id.values, df_train_tp.species_id.values)) :
kfold_col = f'fold_{fold_id}'
df_train_tp[kfold_col] = 0
df_train_tp.loc[val_idxs, kfold_col] = 1
target_fold = f'fold_{FOLD_ID}'
if LOSS_FUNCTION == LossFunction.MASKED_BCE_WITH_TPFP.value:
print('Concatenating TPFP when using Masked Loss')
 df_train_fp = pd.read_csv(DATA_DIR/ 'train_fp.csv')
 df_train_fp['recording_id'] = df_train_fp['recording_id'].map(lambda x: 'train/' + x)
 df_train_fp['species_id'] = df_train_fp['species_id'].astype(str)
 for fold_id in range(5):
df_train_fp[f'fold_{fold_id}'] = 0
df_train_tp['label_type'] = 'TP'
df_train_fp['label_type'] = 'FP'
df_train_tp = pd.concat([df_train_tp, df_train_fp] ).reset_index(drop=True)
 print(f'DataFrame size after concat TPFP: {df_train_tp.shape[0]}')
 print(f'
assert DATA_DIR.is_dir()
assert MODEL_DIR.is_dir()
train['meter'].replace({0:"Electricity",1:"ChilledWater",2:"Steam",3:"HotWater"},inplace=True)
 test['meter'].replace({0:"Electricity",1:"ChilledWater",2:"Steam",3:"HotWater"},inplace=True )<drop_column>
SAMPLE_IDX = 1
test_row = df_train_tp.loc[SAMPLE_IDX]
test_row
metadata.drop('floor_count',axis=1,inplace=True )<data_type_conversions>
def create_audio_tensor(row, is_truncate=True):
if is_truncate:
t_min, t_max = row.t_min, row.t_max
center =(t_max + t_min)/ 2
start_t = center -(INTERVAL/2.)
_frame_offset = int(max(0, start_t)* SR)
 _num_frames = int(INTERVAL * SR)
 else:
_frame_offset = 0
_num_frames = -1
audio_fn = DATA_DIR.resolve() /f'{row.recording_id}.flac'
audio = AudioTensor.create(audio_fn,
frame_offset=_frame_offset,
num_frames=_num_frames)
 return audio
for df in [train, test]:
df['Month'] = df['timestamp'].dt.month.astype("uint8")
 df['DayOfMonth'] = df['timestamp'].dt.day.astype("uint8")
 df['DayOfWeek'] = df['timestamp'].dt.dayofweek.astype("uint8")
 df['Hour'] = df['timestamp'].dt.hour.astype("uint8" )<feature_engineering>
truncated_audio = create_audio_tensor(test_row, is_truncate=True)
 orig_audio = create_audio_tensor(test_row, is_truncate=False)
 assert truncated_audio.shape[1] <= INTERVAL * SR
assert orig_audio.shape[1] == 60 * SR
train['meter_reading'] = np.log1p(train['meter_reading'] )<data_type_conversions>
if NORMALIZER == Normalizer.IMAGENET_STATS.value:
batch_tfms = IntToFloatTensor
elif NORMALIZER == Normalizer.SAMPLE_STATS.value:
class SampleNormalize(Transform):
order=99
def encodes(self, o:AudioSpectrogram):
means = o.mean(axis=(1,2,3), keepdim=True)
 stds = o.std(axis=(1,2,3), keepdim=True)
 return(o-means)/stds
batch_tfms = SampleNormalize
else:
raise NotImplementedError
class CustomAudioBlock(TransformBlock):
"A `TransformBlock` for audios"
@delegates(audio_item_tfms)
 def __init__(self, batch_tfms, cache_folder=None, **kwargs):
item_tfms = audio_item_tfms(**kwargs)
 type_tfm = None
return super().__init__(type_tfms=type_tfm,
item_tfms=item_tfms,
batch_tfms=batch_tfms)
 vocab = list(map(str, range(24)))
 if LOSS_FUNCTION == LossFunction.MASKED_BCE_WITH_TPFP.value:
def label_encodes_dict(self, d: dict):
o = d['label']
if not all(elem in self.vocab.o2i.keys() for elem in o):
diff = [elem for elem in o if elem not in self.vocab.o2i.keys() ]
diff_str = "', '".join(diff)
 raise KeyError(f"Labels '{diff_str}' were not included in the training dataset")
 t = TensorMultiCategory([self.vocab.o2i[o_] for o_ in o])
 t.label_type = d['type']
return t
def one_hot_with_metadata(self, o: TensorMultiCategory):
t = TensorMultiCategory(one_hot(o, self.c ).float())
 t.__dict__ = o.__dict__
return t
class NegateFP(Transform):
order = OneHotEncode.order + 1
def encodes(self, o: TensorMultiCategory):
assert getattr(o, 'label_type')
 assert o.label_type in('TP', 'FP')
 if o.label_type == 'FP':
o *= -1
return o
def decodes(self, o: TensorMultiCategory):
assert getattr(o, 'label_type')
 assert o.label_type in('TP', 'FP')
 if o.label_type == 'FP':
o *= -1
return o
def MultiCategoryBlockForMetadata(vocab):
tfm = [MultiCategorize(vocab=vocab), OneHotEncode, NegateFP]
return TransformBlock(type_tfms=tfm)
 MultiCategorize.encodes.add(label_encodes_dict)
 OneHotEncode.encodes.add(one_hot_with_metadata)
 blocks =(
 partial(CustomAudioBlock, batch_tfms=batch_tfms, sample_rate=SR),
partial(MultiCategoryBlockForMetadata, vocab=vocab)
)
 elif IS_MULTILABEL:
blocks =(
 partial(CustomAudioBlock, batch_tfms=batch_tfms, sample_rate=SR),
partial(MultiCategoryBlock, vocab=vocab)
)
 else:
blocks =(
 partial(CustomAudioBlock, batch_tfms=batch_tfms, sample_rate=SR),
partial(CategoryBlock, vocab=vocab)
)
 item_tfms = [ResizeSignal(duration=RANDOM_INTERVAL*1000, pad_mode=AudioPadType.Repeat)]
if IS_AUDIO_AUG:
data_augmentation = [
AddNoise(color=NoiseColor.White, noise_level=0.1),
SignalShifter(max_pct=0.3)
 ]
item_tfms += data_augmentation
db_batch_tfms = []
db_batch_tfms += [
AudioToSpec.from_cfg(
 AudioConfig.BasicMelSpectrogram(**MELSPEC_CONFIG)
)
 ]
if IS_MIN_MAX_RESCALE:
class MinMaxRescale(Transform):
def encodes(self, o: AudioSpectrogram):
BS, C, W, H = o.shape
_min = o.reshape(BS, C, W*H ).min(axis=2, keepdim=True)[0][:,:,:,None]
_max = o.reshape(BS, C, W*H ).max(axis=2, keepdim=True)[0][:,:,:,None]
return(o - _min)/ _max
db_batch_tfms += [MinMaxRescale() ]
if RESIZE_MEL is not None:
assert isinstance(RESIZE_MEL, tuple)
 resize_tfms = TfmResizeGPU(size=RESIZE_MEL)
 db_batch_tfms += [resize_tfms]
if CHANNEL == Channel.THREE.value:
class SpectrogramStacker(Transform):
def encodes(self, o: AudioSpectrogram):
return o.expand(-1, 3, -1, -1)
 def decodes(self, o: AudioSpectrogram):
return o[:, :1, :, :]
db_batch_tfms += [SpectrogramStacker() ]
if IS_PSEUDO_LABEL:
df_pseudo = pd.read_csv(PSEUDO_LABEL_CSV)
 df_pseudo.recording_id = df_pseudo.recording_id.apply(lambda _id: f'train/{_id}')
 for _idx in range(5):
df_pseudo[f'fold_{_idx}'] = None
df_train_tp = pd.concat([df_train_tp, df_pseudo])
 df_train_tp = df_train_tp.reset_index(drop=True)
 df_train_tp['species_id'] = df_train_tp['species_id'].astype(str)
 val_idxs = df_train_tp[df_train_tp[target_fold] == 1].index.tolist()
splitter=IndexSplitter(val_idxs)
 if LOSS_FUNCTION == LossFunction.MASKED_BCE_WITH_TPFP.value:
def label_to_list_v2(row):

 y_reader = ColReader('species_id')
 label = y_reader(row)
 try:
label_type = row.label_type
except:
label_type = 'TP'
return {'label': [label], 'type': label_type}
y_getter = label_to_list_v2
else:
y_reader = ColReader('species_id')
 def label_to_list(row):
label = y_reader(row)
 return [label]
y_getter = label_to_list if IS_MULTILABEL else y_reader
datablock = DataBlock(
 blocks=blocks,
item_tfms=item_tfms,
batch_tfms=db_batch_tfms,
get_x=create_audio_tensor,
get_y=y_getter,
splitter=splitter
)
metadata['primary_use'].replace({"Healthcare":"Other","Parking":"Other","Warehouse/storage":"Other","Manufacturing/industrial":"Other",
"Retail":"Other","Services":"Other","Technology/science":"Other","Food sales and service":"Other",
"Utility":"Other","Religious worship":"Other"},inplace=True)
 metadata['square_feet'] = np.log1p(metadata['square_feet'])
 metadata['year_built'].fillna(-999, inplace=True)
 metadata['year_built'] = metadata['year_built'].astype('int16' )<merge>
dls = datablock.dataloaders(source=df_train_tp,
bs=BATCH_SIZE,
num_workers=NUM_WORKERS)
 one_batch = dls.one_batch()
assert one_batch[0].shape[0] == BATCH_SIZE
for idx in [1, 3, 6]:
test_row = dls.train.items.loc[idx]
gt_category = int(test_row.species_id)
 tfms_category = dls.train.tfms[1](test_row)
 if IS_MULTILABEL:
assert tfms_category.sum().numpy() == 1.
 assert tfms_category[gt_category].numpy() == 1.
 else:
assert isinstance(tfms_category, TensorCategory)
 print(len(dls.train.items))
print(len(dls.valid.items))
print('DataLoader transform completed and checked' )
%%time
train = pd.merge(train,metadata,on='building_id',how='left')
 test = pd.merge(test,metadata,on='building_id',how='left')
 print("Training Data+Metadata Shape {}".format(train.shape))
print("Testing Data+Metadata Shape {}".format(test.shape))
gc.collect()
train = pd.merge(train,weather_train,on=['site_id','timestamp'],how='left')
 test = pd.merge(test,weather_test,on=['site_id','timestamp'],how='left')
 print("Training Data+Metadata+Weather Shape {}".format(train.shape))
print("Testing Data+Metadata+Weather Shape {}".format(test.shape))
gc.collect()<data_type_conversions>
def LWRAP(preds, labels):
if not IS_MULTILABEL:
labels = torch.nn.functional.one_hot(labels, 24)
 ranked_classes = torch.argsort(preds, dim=-1, descending=True)
 class_ranks = torch.zeros_like(ranked_classes)
 for i in range(ranked_classes.size(0)) :
for j in range(ranked_classes.size(1)) :
class_ranks[i, ranked_classes[i][j]] = j + 1
ground_truth_ranks = class_ranks * labels +(1e6)*(1 - labels)
 sorted_ground_truth_ranks, _ = torch.sort(ground_truth_ranks, dim=-1, descending=False)
 num_labels = labels.sum(-1)
 pos_matrix = torch.tensor(np.array([i+1 for i in range(labels.size(-1)) ])).unsqueeze(0)
 score_matrix = pos_matrix / sorted_ground_truth_ranks
score_mask_matrix, _ = torch.sort(labels, dim=-1, descending=True)
 scores = score_matrix * score_mask_matrix
score = scores.sum() / labels.sum()
return score.item()
activation_type = 'Softmax' if IS_MULTILABEL else 'Sigmoid'
lwrap_metric = AccumMetric(
 func=LWRAP, activation=activation_type,
to_np=False, flatten=False
)
for df in [train,test]:
df['square_feet'] = df['square_feet'].astype('float16')
 cols = ['air_temperature','cloud_coverage','dew_temperature','precip_depth_1_hr','sea_level_pressure','wind_direction','wind_speed']
for col in cols:
train[col].fillna(train[col].mean() ,inplace=True)
 test[col].fillna(test[col].mean() ,inplace=True)
 idx_to_drop = list(( train[(train['site_id'] == 0)&(train['timestamp'] < "2016-05-21 00:00:00")] ).index)
 print(len(idx_to_drop))
train.drop(idx_to_drop,axis='rows',inplace=True)
 idx_to_drop = list(train[(train['meter'] == "Electricity")&(train['meter_reading'] == 0)].index)
 print(len(idx_to_drop))
train.drop(idx_to_drop,axis='rows',inplace=True )<feature_engineering>
class BinaryFocalLoss(nn.Module):
def __init__(self, gamma, alpha=1.) :
super().__init__()
self.gamma = gamma
self.alpha = alpha
self.loss_func = nn.BCEWithLogitsLoss(reduction='none')
 def forward(self, preds, targets):
bce_loss = self.loss_func(preds, targets)
 probas = torch.sigmoid(preds)
 loss = torch.where(
 targets>=0.5,
self.alpha*(( 1.-probas)**self.gamma)*bce_loss, 
(probas**self.gamma)*bce_loss 
)
 loss = loss.mean()
return loss
class MaskedLossWithTPFP(BCEWithLogitsLossFlat):
def __init__(self, **kwargs):
super().__init__(reduction='sum', **kwargs)
 def __call__(self, inp, targ, **kwargs):
weight =(targ != 0.).float().to(targ.device)
 self.func.weight = weight.view(-1)
 targ[targ == -1] = 0.
 n = weight.sum()
masked_sum = super().__call__(inp, targ, **kwargs)
 return masked_sum / n
%%time
number_unique_meter_per_building = train.groupby('building_id')['meter'].nunique()
train['number_unique_meter_per_building'] = train['building_id'].map(number_unique_meter_per_building)
 mean_meter_reading_per_building = train.groupby('building_id')['meter_reading'].mean()
train['mean_meter_reading_per_building'] = train['building_id'].map(mean_meter_reading_per_building)
 median_meter_reading_per_building = train.groupby('building_id')['meter_reading'].median()
train['median_meter_reading_per_building'] = train['building_id'].map(median_meter_reading_per_building)
 std_meter_reading_per_building = train.groupby('building_id')['meter_reading'].std()
train['std_meter_reading_per_building'] = train['building_id'].map(std_meter_reading_per_building)
 mean_meter_reading_on_year_built = train.groupby('year_built')['meter_reading'].mean()
train['mean_meter_reading_on_year_built'] = train['year_built'].map(mean_meter_reading_on_year_built)
 median_meter_reading_on_year_built = train.groupby('year_built')['meter_reading'].median()
train['median_meter_reading_on_year_built'] = train['year_built'].map(median_meter_reading_on_year_built)
 std_meter_reading_on_year_built = train.groupby('year_built')['meter_reading'].std()
train['std_meter_reading_on_year_built'] = train['year_built'].map(std_meter_reading_on_year_built)
 mean_meter_reading_per_meter = train.groupby('meter')['meter_reading'].mean()
train['mean_meter_reading_per_meter'] = train['meter'].map(mean_meter_reading_per_meter)
 median_meter_reading_per_meter = train.groupby('meter')['meter_reading'].median()
train['median_meter_reading_per_meter'] = train['meter'].map(median_meter_reading_per_meter)
 std_meter_reading_per_meter = train.groupby('meter')['meter_reading'].std()
train['std_meter_reading_per_meter'] = train['meter'].map(std_meter_reading_per_meter)
 mean_meter_reading_per_primary_usage = train.groupby('primary_use')['meter_reading'].mean()
train['mean_meter_reading_per_primary_usage'] = train['primary_use'].map(mean_meter_reading_per_primary_usage)
 median_meter_reading_per_primary_usage = train.groupby('primary_use')['meter_reading'].median()
train['median_meter_reading_per_primary_usage'] = train['primary_use'].map(median_meter_reading_per_primary_usage)
 std_meter_reading_per_primary_usage = train.groupby('primary_use')['meter_reading'].std()
train['std_meter_reading_per_primary_usage'] = train['primary_use'].map(std_meter_reading_per_primary_usage)
 mean_meter_reading_per_site_id = train.groupby('site_id')['meter_reading'].mean()
train['mean_meter_reading_per_site_id'] = train['site_id'].map(mean_meter_reading_per_site_id)
 median_meter_reading_per_site_id = train.groupby('site_id')['meter_reading'].median()
train['median_meter_reading_per_site_id'] = train['site_id'].map(median_meter_reading_per_site_id)
 std_meter_reading_per_site_id = train.groupby('site_id')['meter_reading'].std()
train['std_meter_reading_per_site_id'] = train['site_id'].map(std_meter_reading_per_site_id)
 test['number_unique_meter_per_building'] = test['building_id'].map(number_unique_meter_per_building)
 test['mean_meter_reading_per_building'] = test['building_id'].map(mean_meter_reading_per_building)
 test['median_meter_reading_per_building'] = test['building_id'].map(median_meter_reading_per_building)
 test['std_meter_reading_per_building'] = test['building_id'].map(std_meter_reading_per_building)
 test['mean_meter_reading_on_year_built'] = test['year_built'].map(mean_meter_reading_on_year_built)
 test['median_meter_reading_on_year_built'] = test['year_built'].map(median_meter_reading_on_year_built)
 test['std_meter_reading_on_year_built'] = test['year_built'].map(std_meter_reading_on_year_built)
 test['mean_meter_reading_per_meter'] = test['meter'].map(mean_meter_reading_per_meter)
 test['median_meter_reading_per_meter'] = test['meter'].map(median_meter_reading_per_meter)
 test['std_meter_reading_per_meter'] = test['meter'].map(std_meter_reading_per_meter)
 test['mean_meter_reading_per_primary_usage'] = test['primary_use'].map(mean_meter_reading_per_primary_usage)
 test['median_meter_reading_per_primary_usage'] = test['primary_use'].map(median_meter_reading_per_primary_usage)
 test['std_meter_reading_per_primary_usage'] = test['primary_use'].map(std_meter_reading_per_primary_usage)
 test['mean_meter_reading_per_site_id'] = test['site_id'].map(mean_meter_reading_per_site_id)
 test['median_meter_reading_per_site_id'] = test['site_id'].map(median_meter_reading_per_site_id)
 test['std_meter_reading_per_site_id'] = test['site_id'].map(std_meter_reading_per_site_id )<data_type_conversions>
if LOSS_FUNCTION == LossFunction.FOCAL_LOSS.value:
loss_func = BinaryFocalLoss(gamma=2., alpha=1.)
elif LOSS_FUNCTION == LossFunction.BCE_LOGIT_LOSS.value:
loss_func = BCEWithLogitsLossFlat()
elif LOSS_FUNCTION == LossFunction.CE_SOFTMAX_LOSS.value:
loss_func = CrossEntropyLossFlat()
elif LOSS_FUNCTION == LossFunction.MASKED_BCE_WITH_TPFP.value:
loss_func = MaskedLossWithTPFP()
else:
raise ValueError('Invalid loss function')
 if CHANNEL == Channel.SINGLE.value:
config_dict = {'n_in': 1}
elif CHANNEL == Channel.THREE.value:
config_dict = {'n_in': 3}
else:
raise NotImplementedError
cbs = MixUp(1.) if IS_MIXUP else None
if NORMALIZER == Normalizer.IMAGENET_STATS.value:
def encode_tensorimage(self, x:TensorImage):
return(x-self.mean)/ self.std
def encode_audiospec(self, x:AudioSpectrogram):
return(x-self.mean)/ self.std
Normalize.encodes = TypeDispatch([encode_tensorimage, encode_audiospec])
 learner = cnn_learner(
 dls, resnet34,
pretrained=True,
normalize=True,
config=config_dict,
loss_func=loss_func,
metrics=[lwrap_metric],
cbs=cbs 
)
 elif NORMALIZER == Normalizer.SAMPLE_STATS.value:
learner = cnn_learner(
 dls, resnet34,
pretrained=True,
normalize=False,
config=config_dict,
loss_func=loss_func,
metrics=[lwrap_metric],
cbs=cbs 
)
 else:
raise NotImplementedError
print('Learner is set' )
%%time
for df in [train, test]:
df['mean_meter_reading_per_building'] = df['mean_meter_reading_per_building'].astype("float16")
 df['median_meter_reading_per_building'] = df['mean_meter_reading_per_building'].astype("float16")
 df['std_meter_reading_per_building'] = df['std_meter_reading_per_building'].astype("float16")
 df['mean_meter_reading_on_year_built'] = df['mean_meter_reading_on_year_built'].astype("float16")
 df['median_meter_reading_on_year_built'] = df['median_meter_reading_on_year_built'].astype("float16")
 df['std_meter_reading_on_year_built'] = df['std_meter_reading_on_year_built'].astype("float16")
 df['mean_meter_reading_per_meter'] = df['mean_meter_reading_per_meter'].astype("float16")
 df['median_meter_reading_per_meter'] = df['median_meter_reading_per_meter'].astype("float16")
 df['std_meter_reading_per_meter'] = df['std_meter_reading_per_meter'].astype("float16")
 df['mean_meter_reading_per_primary_usage'] = df['mean_meter_reading_per_primary_usage'].astype("float16")
 df['median_meter_reading_per_primary_usage'] = df['median_meter_reading_per_primary_usage'].astype("float16")
 df['std_meter_reading_per_primary_usage'] = df['std_meter_reading_per_primary_usage'].astype("float16")
 df['mean_meter_reading_per_site_id'] = df['mean_meter_reading_per_site_id'].astype("float16")
 df['median_meter_reading_per_site_id'] = df['median_meter_reading_per_site_id'].astype("float16")
 df['std_meter_reading_per_site_id'] = df['std_meter_reading_per_site_id'].astype("float16")
 df['number_unique_meter_per_building'] = df['number_unique_meter_per_building'].astype('uint8')
 gc.collect()<categorify>
if(NORMALIZER == Normalizer.IMAGENET_STATS.value):
if CHANNEL ==(Channel.SINGLE.value):
learner.dls.after_batch[-1].mean = learner.dls.after_batch[-1].mean[:, 0:1, :, :]
learner.dls.after_batch[-1].std = learner.dls.after_batch[-1].std[:, 0:1, :, :]
assert learner.dls.after_batch[-1].mean.shape[1] == 1
assert learner.dls.train.after_batch[-1].mean.shape[1] == 1
if CHANNEL ==(Channel.SINGLE.value):
mean=learner.dls.after_batch[-1].mean.cpu().numpy().flatten() [0]
std=learner.dls.after_batch[-1].std.cpu().numpy().flatten() [0]
elif CHANNEL ==(Channel.THREE.value):
mean=learner.dls.after_batch[-1].mean.cpu().numpy().flatten()
std=learner.dls.after_batch[-1].std.cpu().numpy().flatten()
else:
raise NotImplementedError
learner.dls.valid.after_batch.add(
 Normalize.from_stats(mean=mean, std=std),
'after_batch' 
)
 print(f'Train batch_tfms:
{learner.dls.train.after_batch}')
 print(f'Valid batch_tfms:
{learner.dls.valid.after_batch}' )
train.drop('timestamp',axis=1,inplace=True)
 test.drop('timestamp',axis=1,inplace=True)
 le = LabelEncoder()
train['meter']= le.fit_transform(train['meter'] ).astype("uint8")
 test['meter']= le.fit_transform(test['meter'] ).astype("uint8")
 train['primary_use']= le.fit_transform(train['primary_use'] ).astype("uint8")
 test['primary_use']= le.fit_transform(test['primary_use'] ).astype("uint8")
 print(train.shape, test.shape )<prepare_x_and_y>
learner.loss_func
%%time
y = train['meter_reading']
train.drop('meter_reading',axis=1,inplace=True)
 categorical_cols = ['building_id','Month','meter','Hour','primary_use','DayOfWeek','DayOfMonth']<split>
learner.save('model_last_epoch')
 print('Model checkpoint saved' )
x_train,x_test,y_train,y_test = train_test_split(train,y,test_size=0.2,random_state=0, stratify=meter_cut)
 print(x_train.shape)
 print(y_train.shape)
 print(x_test.shape)
 print(y_test.shape )<import_modules>
try:
learner.remove_cbs(FetchPredsCallback)
 except:
print('Failed to remove FetchPredsCallback, probably its absent')
 learner.cbs
from sklearn.ensemble import RandomForestRegressor as RF
import lightgbm as lgb<create_dataframe>
preds, targs, loss = learner.get_preds(with_loss=True)
 l = loss.mean().cpu().numpy()
print(f'Loss on validation: {l:.2f}' )
lgb_train = lgb.Dataset(x_train, y_train,categorical_feature=categorical_cols)
 lgb_test = lgb.Dataset(x_test, y_test,categorical_feature=categorical_cols)
<init_hyperparams>
def sample_one_subclips(row, i):
length = int(RANDOM_INTERVAL * SR)
 total_length = int(60 * SR)
 if(i + 1)* length > total_length:
_frame_offset = total_length - length
else:
_frame_offset = i * length
audio_fn = test_path.resolve() /f'{row.recording_id}.flac'
audio_subclip = AudioTensor.create(audio_fn,
frame_offset=_frame_offset,
num_frames=length)
 return audio_subclip
del x_train, y_train
params = {'feature_fraction': 0.75,
'bagging_fraction': 0.75,
'objective': 'regression_l2',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'rmse',
"verbosity": -1,
'reg_alpha': 0.5,
'reg_lambda': 0.5,
'random_state': 47
}
reg = lgb.train(params, lgb_train, num_boost_round=3000, valid_sets=[lgb_train, lgb_test], early_stopping_rounds=100, verbose_eval = 100 )<drop_column>
test_df = pd.read_csv(DATA_DIR/'sample_submission.csv' )
del train<predict_on_test>
dls.train_ds.tls[0].tfms
predictions = []
step = 50000
for i in range(0, len(x_test), step):
predictions.extend(np.expm1(reg.predict(x_test.iloc[i: min(i+step, len(x_test)) , :], num_iteration=reg.best_iteration)) )<compute_test_metric>
dls.valid_ds.tls[0].tfms = Pipeline(partial(sample_one_subclips, i=0))
sample_test_dl = dls.test_dl(test_df, with_labels=False)
 print(sample_test_dl.after_batch )
def RMSLE_Func(actual, predicted):
actual = np.array(actual)
 predicted = np.array(predicted)
 return(np.sqrt(np.sum(np.power(np.log(predicted+1)-np.log(actual+1),2)) /actual.shape[0]))<compute_test_metric>
subclips_preds = []
segment_n = ceil(( 60*SR)/(RANDOM_INTERVAL*SR))
cp_pipeline = learner.dls.valid_ds.tls[0].tfms
for crt_i in range(segment_n):
print(f'Running prediction on subclip {crt_i} for all test samples...')
 crt_test_dl = learner.dls.test_dl(test_df, with_labels=False)
 crt_test_dl.tls[0].tfms = Pipeline(partial(sample_one_subclips, i=crt_i))
_preds = learner.get_preds(dl=crt_test_dl)
 if IS_MULTILABEL:
_preds = _preds[0]
else:
_preds = torch.nn.Softmax(dim=-1 )(_preds[0])
 subclips_preds.append(_preds )
RMSLE = RMSLE_Func(y_test, predictions)
 print(RMSLE)
 del x_test, y_test, predictions<predict_on_test>
crt_test_dl.after_batch
%%time
predictions = []
step = 50000
for i in range(0, len(test), step):
predictions.extend(np.expm1(reg.predict(test.iloc[i: min(i+step, len(test)) , :], num_iteration=reg.best_iteration)) )<save_to_csv>
all_preds = torch.stack(subclips_preds, dim=1)
 final_preds = all_preds.max(dim=1)[0]
test_df.iloc[:, 1:] = final_preds
test_df.to_csv('my_submission.csv', index=False)
 print('Submission CSV written' )
%%time
Submission = pd.DataFrame(test.index,columns=['row_id'])
 Submission['meter_reading'] = predictions
Submission['meter_reading'].clip(lower=0,upper=None,inplace=True)
 Submission.to_csv("lgbm.csv",index=None )<load_pretrained>
learner.dls.valid_ds.tls[0].tfms
<import_modules><EOS> 
learner = learner.load('model')
 learner.dls.valid_ds.tls[0].tfms = cp_pipeline
preds, targs, loss = learner.get_preds(with_loss=True)
 l = loss.mean().cpu().numpy()
print(f'Loss on validation after model load: {l:.2f}' )