Kaggle competition: Rossmann Store Sales
<SOS> metric: RootMeanSquarePercentageError Kaggle data source: rossmann-store-sales<choose_model_class>
num_sample_stores=1115
valid_days=0
s= Namespace(**{
"l1":4497,
"l2":2328,
"ps1":0.2771132028380148,
"ps2":0.15631474446268287,
"emb_drop":0.14301109844119272,
"batchsize":64,
"lrate":0.0660858230905056,
"lrate_ratio":9,
"wd":0.17305139150930285,
"l1epoch":4,
"l2epoch":3,
"l3epoch":8,
})
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB5(
 input_shape=(512, 512, 3),
weights='noisy-student',
include_top=False 
),
L.GlobalMaxPooling2D() ,
L.Dense(3**3),
L.LayerNormalization() ,
L.LeakyReLU(0.1),
L.Dropout(0.5),
L.Dense(3, activation='sigmoid'),
SoftProbField()
])
 model.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 model.summary()<load_pretrained>
from pathlib import Path
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
from fastai import *
from fastai.tabular import *
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf
weights = model.get_weights()<normalization>
plotly.offline.init_notebook_mode(connected=False)
 cf.go_offline()
%matplotlib inline
%reload_ext autoreload
%autoreload 2
pd.set_option('display.max_columns', 0)
 pd.set_option('display.max_rows', 500 )
IMAGE_SIZE = [512, 512]
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
def transform(image,label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 45.* tf.random.normal([1],dtype='float32')
 shr = 32.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/5.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/5.
 h_shift = 32.* tf.random.normal([1],dtype='float32')
 w_shift = 32.* tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label<categorify>
path=Path(".. /input/rossmann-data-engineering/")
 traindf=pd.read_feather(path/"train.feather")
 testdf=pd.read_feather(path/"test.feather" )
def get_datasets(tr_idx, vl_idx):
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths[tr_idx], train_labels[tr_idx])) 
.map(decode_image, num_parallel_calls=AUTO)
.cache() 
.map(data_augment, num_parallel_calls=AUTO)
.map(transform, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(64)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths[vl_idx], train_labels[vl_idx])) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(64)
.cache() 
.prefetch(AUTO)
)
 return train_dataset, valid_dataset<train_model>
traindata=traindf[traindf["Sales"]!=0]
sample_stores=list(range(1,num_sample_stores+1))
traindata=traindata[traindata.Store.isin(sample_stores)].sample(frac=1,random_state=0 ).reset_index(drop=True )
gkf = StratifiedKFold(n_splits=5)
 tta = TTACallback(test_dataset, 0.96)
 STEPS_PER_EPOCH = train_labels.shape[0] // 64
for i,(tr_idx, vl_idx)in enumerate(gkf.split(train_paths, train_labels.argmax(-1))):
print(f'Start {i} fold')
 tf.keras.backend.clear_session()
model.set_weights(weights)
 train_dataset, valid_dataset = get_datasets(tr_idx, vl_idx)
 lr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=1)
 history = model.fit(
 train_dataset,
epochs=20,
callbacks=[lr_schedule, tta],
steps_per_epoch=STEPS_PER_EPOCH * 6,
validation_data=valid_dataset
)<save_to_csv>
valid_idx=traindata[traindata.Date>=(traindata.Date.max() - timedelta(days=valid_days)) ].index.tolist()
probs = np.mean(tta.test_pred, axis=0)
 sub.loc[:, 'healthy':] = probs
sub.to_csv('submission.csv', index=False)
 sub.head()<install_modules>
datecols=traindata.select_dtypes(include="datetime" ).columns.tolist()
traindata[datecols]=traindata[datecols].astype("int64")
 testdf[datecols]=testdf[datecols].astype("int64" )
!pip install efficientnet<import_modules>
procs = [FillMissing, Categorify, Normalize]
dep_var = 'Sales'
cont_names=[
'CompetitionDistance',
'Week',
'Day',
'Dayofyear',
'Elapsed',
'ratio-sales-customer',
'ratio-saturday-week',
'ratio-sunday-week',
'ratio-promo-nopromo',
'Promo_thisweek',
'Open_thisweek',
'StateHolidayBool_thisweek',
'SchoolHoliday_thisweek',
'Promo_prevweek',
'Open_prevweek',
'StateHolidayBool_prevweek',
'SchoolHoliday_prevweek',
'Promo_nextweek',
'Open_nextweek',
'StateHolidayBool_nextweek',
'SchoolHoliday_nextweek',
'Promo2Days',
'CompetitionDaysOpen',
'trend',
'trend_DE',
'Max_Humidity',
'Max_Wind_SpeedKm_h',
'Mean_Humidity',
'Mean_TemperatureC',
'Max_TemperatureC_chnage',
'Month_Sales_mean',
'Year_Sales_mean',
'Dayofweek_Sales_mean',
'Dayofweek_promo_Sales_mean',
'BeforeSchoolHoliday',
'AfterSchoolHoliday',
'BeforeClosed',
'AfterClosed',
'BeforePromo',
'AfterPromo',
'BeforeStateHolidayBool',
'AfterStateHolidayBool',
'Promo2ActiveMonthBool',
'BeforePromo2ActiveMonthBool',
'AfterPromo2ActiveMonthBool',
'SchoolHoliday_fw',
'StateHolidayBool_fw',
'Promo_fw',
'Closed_fw',
'Promo2ActiveMonthBool_fw',
'CompetitionOpenSince', 'Promo2Since'
]
cat_names=[
'Store',
'DayOfWeek',
'Open',
'Promo',
'StateHoliday',
'SchoolHoliday',
'StoreType',
'Assortment',
'Promo2',
'PromoInterval',
'Year',
'Month',
'Dayofweek',
'Is_month_end',
'Is_month_start',
'Is_quarter_end',
'Is_quarter_start',
'Is_year_end',
'Is_year_start',
'Promo2SinceYear',
'Promo2Na',
'Events',
'Fog',
'Hail',
'Rain',
'Snow',
'Thunderstorm',
'Quarter',
'CompetitionOpenNA',
'CompetitionDistanceNA',
'CompetitionOpenSinceYear',
'State'
]
import math, re, os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
import efficientnet.tfkeras as efn
from keras.applications.densenet import DenseNet201
from sklearn import metrics
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint<set_options>
max_log_y = np.log(np.max(traindata['Sales']))
y_range = torch.tensor([0, max_log_y], device=defaults.device )
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
databunch =(TabularList.from_df(traindata, path="", cat_names=cat_names, cont_names=cont_names, procs=procs,)
.split_by_idx(valid_idx)
.label_from_df(cols=dep_var, label_cls=FloatList, log=True)
.add_test(TabularList.from_df(testdf, path=path, cat_names=cat_names, cont_names=cont_names)) 
.databunch())
 databunch.batch_size=s.batchsize
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 40
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
IM_Z = 768<define_variables>
learn = tabular_learner(databunch, layers=[s.l1,s.l2], ps=[s.ps1,s.ps2], emb_drop=s.emb_drop, y_range=y_range, metrics=exp_rmspe )
def format_path(st):
return GCS_DS_PATH + '/images/' + st + '.jpg'<load_from_csv>
learn.model
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.1, random_state=2020 )<normalization>
learn.lr_find()
def decode_image(filename, label=None, image_size=(IM_Z, IM_Z)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 if label is None:
return image
else:
return image, label<categorify>
learn.fit_one_cycle(s.l1epoch, s.lrate, wd=s.wd )
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.cache() 
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<choose_model_class>
learn.fit_one_cycle(s.l2epoch, s.lrate/s.lrate_ratio, wd=s.wd )
def build_lrfn(lr_start=0.00001, lr_max=0.000075,
lr_min=0.000001, lr_rampup_epochs=20,
lr_sustain_epochs=0, lr_exp_decay=.8):
lr_max = lr_max * strategy.num_replicas_in_sync
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs)+ lr_min
return lr
return lrfn
ch_p = ModelCheckpoint(filepath="model_ef.h5", monitor='val_loss', save_weights_only=True,
verbose=1 )<choose_model_class>
learn.fit_one_cycle(s.l3epoch, s.lrate/(s.lrate_ratio*s.lrate_ratio), wd=s.wd )
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB7(
 input_shape=(IM_Z, IM_Z, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
<choose_model_class>
valid_preds=learn.get_preds(DatasetType.Valid)
 traindata["SalesPreds"]=pd.Series(index=traindata.iloc[valid_idx].index,data=np.exp(valid_preds[0].numpy().T[0]))
lrfn = build_lrfn()
lr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=1)
 STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE<train_model>
def rmspe_metric(act,pred):
return np.sqrt(np.mean(((act-pred)/act)**2))
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule, ch_p],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset
)<choose_model_class>
rmspe_metric(traindata.Sales,traindata.SalesPreds )
with strategy.scope() :
model1 = tf.keras.Sequential([
tf.keras.applications.DenseNet201(
 input_shape=(IM_Z, IM_Z, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax')
 ])
 model1.compile(
 optimizer='adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
<choose_model_class>
store_rmspe=traindata.groupby(["Store"] ).apply(lambda x:rmspe_metric(x.Sales,x.SalesPreds)).sort_values(ascending=False )
ch_p_den = ModelCheckpoint(filepath="model_den.h5", monitor='val_loss', save_weights_only=True,
verbose=1 )<train_model>
t=traindata.set_index("Date" )
<save_to_csv><EOS> 
test_preds=learn.get_preds(DatasetType.Test)
 testdf["Sales"]=np.exp(test_preds[0].data ).numpy().T[0]
testdf[["Id","Sales"]]=testdf[["Id","Sales"]].astype("int")
 testdf[["Id","Sales"]].to_csv("rossmann_submission.csv",index=False )