Kaggle competition: Rossmann Store Sales
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 sample_sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )<split>
plt.style.use('ggplot')
 plt.rcParams['figure.figsize'] =(12,8)
 %matplotlib inline
X = train.drop(columns='label' ).values.reshape(-1, 28, 28, 1)
 y = train.label.values
train_X, valid_X, train_y, valid_y = train_test_split(X,
y,
test_size=0.2
)<prepare_x_and_y>
df_stores = pd.read_csv('.. /input/store.csv', sep=',')
 df_data = pd.read_csv('.. /input/train.csv', sep=',')
 df_test = pd.read_csv('.. /input/test.csv', sep=',' )
class MNISTDataset(Dataset):
def __init__(self, X, y=None, is_test=False, transforms=None):
self.X = X
self.y = y
self.is_test = is_test
self.transforms = transforms
def __len__(self):
return len(self.X)
 def __getitem__(self, index):
image = self.X[index]
if self.transforms:
image = self.transforms(image=image)['image']
if self.is_test:
return image
else:
return image, self.y[index]<normalization>
df_stores = df_stores.drop('CompetitionOpenSinceMonth', axis=1 ).drop('CompetitionOpenSinceYear', axis=1 ).drop('Promo2SinceWeek', axis=1 ).drop('Promo2SinceYear', axis=1 )
train_transforms = Compose([ToFloat(max_value=255),
ShiftScaleRotate(shift_limit=0.1,
scale_limit=0.1,
rotate_limit=10,
border_mode=cv2.BORDER_CONSTANT,
value=0,
p=1),
ToTensorV2()
])
 valid_transforms = Compose([ToFloat(max_value=255),
ToTensorV2()
] )<normalization>
closed_stores = df_test["Id"][df_test["Open"] == 0].values
df_test = df_test[df_test["Open"] != 0]
df_data = df_data[df_data["Open"] != 0]
example_transforms = Compose([ToFloat(max_value=255),
ShiftScaleRotate(shift_limit=0.3,
scale_limit=0.3,
rotate_limit=30,
p=1),
ToTensorV2()
] )<define_search_model>
df_data = df_data.drop('Open', axis=1 ).drop('Customers', axis=1)
 df_test = df_test.drop('Open', axis=1 )
class CNN(nn.Module):
def __init__(self):
super(CNN, self ).__init__()
self.dropout = nn.Dropout2d(0.5)
 self.bn1_1 = nn.BatchNorm2d(48)
 self.bn1_2 = nn.BatchNorm2d(48)
 self.bn2 = nn.BatchNorm1d(256)
 self.conv1 = nn.Conv2d(in_channels=1, out_channels=48, kernel_size=3, stride=1, padding=1)
 self.conv2 = nn.Conv2d(in_channels=48, out_channels=48, kernel_size=3, stride=2, padding=1)
 self.fc1 = nn.Linear(in_features=48 * 14 * 14, out_features=256)
 self.fc2 = nn.Linear(in_features=256, out_features=10)
 def forward(self, x):
x = F.relu(self.conv1(x))
x = self.bn1_1(x)
 x = F.relu(self.conv2(x))
x = self.bn1_2(x)
 x = self.dropout(x)
 x = torch.flatten(x, 1)
 x = F.relu(self.fc1(x))
x = self.bn2(x)
 x = self.dropout(x)
 x = self.fc2(x)
 return x<set_options>
df_data['Month'] = df_data['Date'].apply(lambda x: int(x[5:7]))
df_test['Month'] = df_test['Date'].apply(lambda x: int(x[5:7]))
df_data['Year'] = df_data['Date'].apply(lambda x: int(x[:4]))
df_test['Year'] = df_test['Date'].apply(lambda x: int(x[:4]))
df_test = df_test.drop('Date', axis=1)
 df_data = df_data.drop('Date', axis=1 )
class EarlyStopping:
def __init__(self, mode, path, patience=3, delta=0):
if mode not in {'min', 'max'}:
raise ValueError("Argument mode must be one of 'min' or 'max'.")
 if patience <= 0:
raise ValueError("Argument patience must be a postive integer.")
 if delta < 0:
raise ValueError("Argument delta must not be a negative number.")
 self.mode = mode
self.patience = patience
self.delta = delta
self.path = path
self.best_score = np.inf if mode == 'min' else -np.inf
self.counter = 0
def _is_improvement(self, val_score):

 if self.mode == 'max' and val_score > self.best_score + self.delta:
return True
elif self.mode == 'min' and val_score < self.best_score - self.delta:
return True
return False
def __call__(self, val_score, model):

 if self._is_improvement(val_score):
self.best_score = val_score
self.counter = 0
torch.save(model.state_dict() , self.path)
 print('Val loss improved.Saved model.')
 return False
else:
self.counter += 1
print(f'Early stopping counter: {self.counter}/{self.patience}')
 if self.counter >= self.patience:
print(f'Stopped early.Best val loss: {self.best_score:.4f}')
 return True<train_model>
df_stores = df_stores.join(df_data.groupby('Store')['Sales'].mean() , on='Store' ).rename(columns={'Sales': 'Av_sales'} )
def train_one_epoch(model, train_loader, optimizer, device, criterion):

 model.train()
running_loss_train = 0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
 optimizer.zero_grad()
outputs = model(inputs)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss_train += loss.item()
train_loss = running_loss_train / len(train_loader.dataset)
 return train_loss<train_model>
max_s = df_stores['Av_sales'].max()
df_stores['Av_sales']=df_stores['Av_sales']/max_s
def validate(model, valid_loader, device, criterion):

 model.eval()
correct = 0
running_loss_val = 0
with torch.no_grad() :
for inputs, labels in valid_loader:
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model(inputs)
 loss = criterion(outputs, labels)
 pred = outputs.argmax(dim=1)
 correct += pred.eq(labels ).sum().item()
running_loss_val += loss.item()
val_acc = correct / len(valid_loader.dataset)
 val_loss = running_loss_val / len(valid_loader.dataset)
 return val_acc, val_loss<train_model>
df_test = pd.merge(df_test, df_stores, left_index=True, on='Store')
 df_data = pd.merge(df_data, df_stores, left_index=True, on='Store' )
def fit(model, train_loader, valid_loader, learning_rate, num_epochs):
criterion = nn.CrossEntropyLoss(reduction='sum')
 optimizer = optim.Adam(model.parameters() , lr=learning_rate)
 es = EarlyStopping(mode='min', path='model.pth', patience=5)
 model = model.to(device)
 scheduler = ExponentialLR(optimizer, gamma=0.9)
 for epoch in range(1, num_epochs + 1):
train_loss = train_one_epoch(model, train_loader, optimizer, device, criterion)
 val_acc, val_loss = validate(model, valid_loader, device, criterion)
 scheduler.step()
print(f'Epoch {epoch:2}/{num_epochs}',
f'train loss: {train_loss:.4f}',
f'val loss: {val_loss:.4f}',
f'val acc: {val_acc:.2%}',
sep=' | ' 
)
 if es(val_loss, model):
break<train_model>
df_data['CompetitionDistance'].fillna(df_data['CompetitionDistance'].median() , inplace = True)
 df_test['CompetitionDistance'].fillna(df_data['CompetitionDistance'].median() , inplace = True )
TRAIN_BATCH_SIZE = 64
VALID_BATCH_SIZE = 512
NUM_EPOCHS = 50
LEARNING_RATE = 1e-3
NUM_WORKERS = 0
mnist_train = MNISTDataset(train_X, train_y, is_test=False, transforms=train_transforms)
 mnist_valid = MNISTDataset(valid_X, valid_y, is_test=False, transforms=valid_transforms)
 train_loader = DataLoader(mnist_train, batch_size=TRAIN_BATCH_SIZE, shuffle=True, drop_last=True)
 valid_loader = DataLoader(mnist_valid, batch_size=VALID_BATCH_SIZE, shuffle=False)
 model = CNN()
start = time.time()
fit(model, train_loader, valid_loader, learning_rate=LEARNING_RATE, num_epochs=NUM_EPOCHS)
 print(f'Total training time: {time.time() - start}')
 model.load_state_dict(torch.load('model.pth'))<create_dataframe>
max_dist = df_data['CompetitionDistance'].max()
df_data['CompetitionDistance']=df_data['CompetitionDistance']/max_dist
df_test['CompetitionDistance']=df_test['CompetitionDistance']/max_dist
TEST_BATCH_SIZE = 512
test_transforms = Compose([ToFloat(max_value=255),
ToTensorV2()
])
 test_X = test.values.reshape(-1, 28, 28, 1)
 mnist_test = MNISTDataset(test_X, is_test=True, transforms=test_transforms)
 test_loader = DataLoader(mnist_test, batch_size=TEST_BATCH_SIZE, shuffle=False )<save_to_csv>
df_data['PromoInterval'].fillna('n', inplace = True)
 df_test['PromoInterval'].fillna('n', inplace = True )
def predict(model, test_loader, device):

 model.eval()
predictions = sample_sub['Label'].values
with torch.no_grad() :
for i, inputs in enumerate(test_loader):
inputs = inputs.to(device)
 outputs = model(inputs)
 pred = outputs.argmax(dim=1 ).to('cpu' ).numpy()
predictions[i*TEST_BATCH_SIZE:i*TEST_BATCH_SIZE+len(inputs)] = pred
output = sample_sub.copy()
output['Label'] = predictions
output.to_csv('submission.csv', index=False)
 return output
output = predict(model, test_loader, device)
 output<import_modules>
df_data["StateHoliday"].loc[df_data["StateHoliday"] == 0] = "0"
df_test["StateHoliday"].loc[df_test["StateHoliday"] == 0] = "0"
import numpy as np
import pandas as pd
import os 
<import_modules>
df_test.isnull().any()
!pip install -q efficientnet 
<set_options>
features = ['DayOfWeek', 'Month', 'Promo', 'StateHoliday', 'SchoolHoliday',
'StoreType', 'Assortment', 'PromoInterval', 'Year']
for fea in features:
av_sales = df_data.groupby(fea)['Sales'].mean().values
num_dict = dict(df_data.groupby(fea)['Sales'].mean() /max(av_sales))
df_data[fea] = df_data[fea].map(num_dict)
 df_test[fea] = df_test[fea].map(num_dict)
 print(fea, num_dict, '
' )
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>
df_data = df_data[df_data['Sales']!=0]
GCS_DS_PATH = KaggleDatasets().get_gcs_path()<load_from_csv>
df_data = df_data[df_data['Sales']<34500]
def format_path(fn):
return GCS_DS_PATH + '/images/' + fn + '.jpg'
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
list_train_ds = tf.data.Dataset.from_tensor_slices(( train_paths, train_labels))
list_test_ds = tf.data.Dataset.from_tensor_slices(test_paths )<define_variables>
df_data = df_data.drop('Store', axis=1)
 df_test = df_test.drop('Store', axis=1 )
AUTOTUNE = tf.data.experimental.AUTOTUNE
EPOCHS = 40
BATCH_SIZE_1 = 16 * strategy.num_replicas_in_sync
BATCH_SIZE_2 = 64
image_size = 800<normalization>
df_train, df_mytest = train_test_split(df_data, test_size = 0.2 )
def decode_image(filename, label=None, image_size=(image_size, image_size)) :
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
def prepare_for_training(ds, batch_size, cache=True, shuffle_buffer_size=512):
if cache:
if isinstance(cache, str):
ds = ds.cache(cache)
 else:
ds = ds.cache()
ds = ds.map(data_augment, num_parallel_calls=AUTOTUNE)
 ds = ds.shuffle(buffer_size=shuffle_buffer_size)
 ds = ds.repeat()
ds = ds.batch(batch_size)
 ds = ds.prefetch(buffer_size=AUTOTUNE)
 return ds<categorify>
X_train, y_train = df_train.drop('Sales', axis=1 ).values, df_train['Sales'].values
train_ds1 = list_train_ds.map(decode_image, num_parallel_calls=AUTOTUNE)
 train_ds1 = prepare_for_training(train_ds1, BATCH_SIZE_1)
 train_ds2 = list_train_ds.map(decode_image, num_parallel_calls=AUTOTUNE)
 train_ds2 = prepare_for_training(train_ds2, BATCH_SIZE_2)
 test_ds = list_test_ds.map(decode_image, num_parallel_calls=AUTOTUNE)
 test_ds = test_ds.batch(BATCH_SIZE_1 )<choose_model_class>
model = LinearRegression(n_jobs=4 )
with strategy.scope() :
model1 = tf.keras.Sequential([
InceptionResNetV2(
 input_shape=(image_size, image_size, 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'] 
)
 model1.summary()<choose_model_class>
model.fit(X_train, y_train )
with strategy.scope() :
model2 = tf.keras.Sequential([
efn.EfficientNetB7(
 input_shape=(image_size, image_size, 3),
weights='imagenet',
include_top=False 
),
L.GlobalAveragePooling2D() ,
L.Dense(train_labels.shape[1], activation='softmax'),
])
 model2.compile(
 optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 model2.summary()<init_hyperparams>
model.fit(X_train, y_train )
LR_START = 0.0001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.0001
LR_RAMPUP_EPOCHS = 4
LR_SUSTAIN_EPOCHS = 6
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr = np.random.random_sample() * 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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True )<train_model>
def eval_rmspe(y_hat, y_true):
res = np.sqrt(sum(((y_true-y_hat)/y_true)**2)/len(y_true))
return res
STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE_1
history1 = model1.fit(
 train_ds1,
epochs=EPOCHS,
callbacks=[lr_callback],
steps_per_epoch=STEPS_PER_EPOCH,
)<train_model>
X_test, y_mytest = df_mytest.drop('Sales', axis=1 ).values, df_mytest['Sales'].values
STEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE_2
history2 = model2.fit(
 train_ds2,
epochs=EPOCHS,
callbacks=[lr_callback],
steps_per_epoch=STEPS_PER_EPOCH,
)<save_to_csv>
y_hat = model.predict(X_test )
probs1 = model1.predict(test_ds, verbose=1)
 probs2 = model2.predict(test_ds, verbose=1)
 probs_avg =(probs1 + probs2)/ 2
sub.loc[:, 'healthy':] = probs_avg
sub.to_csv('submission.csv', index=False)
 sub.head()<import_modules>
print(eval_rmspe(y_hat, y_mytest))
from IPython.display import clear_output<install_modules>
from sklearn.linear_model import Ridge
!pip install -U efficientnet
clear_output()<import_modules>
R_model = Ridge(alpha=1)
 R_model.fit(X_train, y_train )
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE<import_modules>
y_hat_R = R_model.predict(X_test)
 print(eval_rmspe(y_hat_R, y_mytest))
import efficientnet.tfkeras as efn
from matplotlib import pyplot as plt
from tensorflow.keras.applications import InceptionResNetV2<set_options>
from sklearn.ensemble import RandomForestRegressor
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)
<train_model>
X_train_full, y_train_full = df_data.drop('Sales', axis=1 ).values, df_data['Sales'].values
img = plt.imread('.. /input/plant-pathology-2020-fgvc7/images/Train_10.jpg')
 print(img.shape)
 plt.imshow(img )<load_from_csv>
X_test_full = df_test.drop('Id', axis=1 ).values
train_df = pd.read_csv("/kaggle/input/plant-pathology-2020-fgvc7/train.csv")
 test_df = pd.read_csv("/kaggle/input/plant-pathology-2020-fgvc7/test.csv")
 sub_df = pd.read_csv("/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv" )<define_variables>
forest = RandomForestRegressor(n_jobs=1, n_estimators=150, max_features=7, max_depth=100)
 forest.fit(X_train_full, y_train_full )
img_size = 896
EPOCHS = 40
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
n_classes = 4<init_hyperparams>
y_hat_full = forest.predict(X_test_full )
LR_START = 0.00001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1])) 
<define_variables>
store_ids = df_test['Id'].values
store_ids
train_tpu_paths = train_df.image_id.apply(lambda x: GCS_DS_PATH+"/images/"+x+".jpg" ).values
train_labels = train_df.iloc[:, 1:].values
test_tpu_paths = test_df.image_id.apply(lambda x: GCS_DS_PATH+"/images/"+x+".jpg" ).values<normalization>
submission = submission.append(pd.Series(y_hat_full, index=store_ids))
submission = pd.DataFrame({ "Id": submission.index, "Sales": submission.values})
 submission.to_csv('submission.csv', index=False )