Kaggle competition: What's Cooking?
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?<choose_model_class>
warnings.filterwarnings("ignore")
 pd.set_option('max_colwidth',400)
 tqdm.pandas()
learn = Learner(data, get_model() ,
metrics=[AUROC() , FBeta() , accuracy],
callback_fns=[partial(SaveModelCallback)],
wd=0.1,
path = '.' )<find_best_params>
train_set = pd.read_json('.. /input/whats-cooking/train.json', orient='columns')
 test_set = pd.read_json('.. /input/whats-cooking/test.json', orient='columns' )
min_grad_lr = learn.recorder.min_grad_lr
min_grad_lr<train_model>
ingredients_individual = Counter([ingredient for ingredient_list in train_set.ingredients for ingredient in ingredient_list])
 ingredients_individual = pd.DataFrame.from_dict(ingredients_individual,orient='index' ).reset_index()
ingredients_individual = ingredients_individual.rename(columns={'index':'Ingredient', 0:'Count'} )
learn.fit_one_cycle(10, slice(min_grad_lr))<train_model>
print(len(train_set.cuisine.unique()))
print(len(ingredients_individual.Ingredient.unique()))
validation = learn.validate()
print("Final model validation loss: {0}".format(validation[0]))<save_model>
cat = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
num_class = len(cat )
learn.save('efficientnet-cactus', return_path=True )<find_best_params>
lb = LabelEncoder()
enc = OneHotEncoder(sparse=False)
 enc.fit(np.array(cat ).reshape(-1, 1))
y_cat = lb.fit_transform(train_set.cuisine)
 y_ohe = enc.transform(y_cat.reshape(-1, 1))
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<choose_model_class>
def preprocessing(titles_array):
processed_array = []
for title in tqdm(titles_array):
ingredients = []
for ingredient in title:
ingredient = ingredient.strip().lower()
ingredient = re.sub('[^a-zA-Z ]', '', ingredient)
 ingredient = re.sub(' ', ' ', ingredient)
 ingredient = re.sub(' ', ' ', ingredient)
 words = ingredient.split()
ingredients.append('_'.join([word for word in words if len(word)> 1]))
processed_array.append(' '.join(ingredients))
return processed_array
train_set['processed'] = preprocessing(train_set['ingredients'])
 test_set['processed'] = preprocessing(test_set['ingredients'])
 print(train_set['processed'][0] )
probability, _ = learn.TTA(ds_type=DatasetType.Test )<find_best_params>
sentences = pd.concat([train_set['processed'], test_set['processed']],axis=0)
 train_sentences = list(sentences.progress_apply(str.split ).values)
 model = Word2Vec(sentences=train_sentences,
sg=1,
min_count=1,
size=300,
workers=4)
 model.wv.save_word2vec_format('custom_glove_300d.txt' )
probability.argmin(dim=1)[:10]<filter>
from sklearn.feature_extraction.text import CountVectorizer
test_df.has_cactus = probability.numpy() [:, 0]<save_to_csv>
max_len = max(np.max(train_set.ingredients.map(lambda l: len(l))), np.max(test_set.ingredients.map(lambda l: len(l))))
max_features = len(model.wv.vocab.keys())
 tk = Tokenizer(lower = True, filters='', num_words=max_features)
 tk.fit_on_texts(train_sentences)
 embedding_path = "custom_glove_300d.txt"
embed_size = 300
train_set1 = tk.texts_to_sequences(train_set['processed'])
 train_set1 = pad_sequences(train_set1, maxlen = max_len)
 test_set1 = tk.texts_to_sequences(test_set['processed'])
 test_set1 = pad_sequences(test_set1, maxlen = max_len)
 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embedding_index = dict(get_coefs(*o.strip().split(" ")) for o in open(embedding_path))
word_index = tk.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words+1, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embedding_index.get(word)
 if embedding_vector is not None:embedding_matrix[i] = embedding_vector
max_features = nb_words
print('No.of Features is {0:.0f}.'.format(max_features))
test_df.to_csv('submission.csv', index=False )<import_modules>
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) , K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim
import os
import time
import copy
import pathlib
from collections import defaultdict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from PIL import Image<import_modules>
def textv1() :
inp = Input(shape =(max_len,))
x = Embedding(max_features + 1, embed_size, weights = [embedding_matrix], trainable = True )(inp)
 x = SpatialDropout1D(0.3 )(x)
 x = Bidirectional(CuDNNGRU(256, return_sequences = True))(x)
 x = Attention(max_len )(x)
 x = BatchNormalization()(x)
 x = Dense(128, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = Dense(num_class, activation = "softmax" )(x)
 model = Model(inputs = inp, outputs = x)
 return model
model1 = textv1()
model1.summary()
import torch
import torchvision
import torch.nn as nn
import torch.optim as optmim
from torch.optim import lr_scheduler
from torchvision import models, datasets, transforms
from torch.utils.data import Dataset
from torch.utils.data.sampler import SubsetRandomSampler<set_options>
def textv2() :
inp = Input(shape =(max_len,))
x1 = Embedding(max_features + 1, embed_size, weights = [embedding_matrix], trainable = True )(inp)
 x1 = SpatialDropout1D(0.3 )(x1)
 x_gru = Bidirectional(CuDNNGRU(128, return_sequences = True))(x1)
 x_conv1 = Conv1D(32, kernel_size=4, padding='valid', kernel_initializer='he_uniform' )(x_gru)
 avg_pool1_gru = GlobalAveragePooling1D()(x_conv1)
 max_pool1_gru = GlobalMaxPooling1D()(x_conv1)
 x_conv2 = Conv1D(32, kernel_size=3, padding='valid', kernel_initializer='he_uniform' )(x_gru)
 avg_pool2_gru = GlobalAveragePooling1D()(x_conv2)
 max_pool2_gru = GlobalMaxPooling1D()(x_conv2)
 x_lstm = Bidirectional(CuDNNLSTM(128, return_sequences = True))(x1)
 x_conv3 = Conv1D(32, kernel_size=4, padding='valid', kernel_initializer='he_uniform' )(x_lstm)
 avg_pool1_lstm = GlobalAveragePooling1D()(x_conv3)
 max_pool1_lstm = GlobalMaxPooling1D()(x_conv3)
 x_conv4 = Conv1D(32, kernel_size=3, padding='valid', kernel_initializer='he_uniform' )(x_lstm)
 avg_pool2_lstm = GlobalAveragePooling1D()(x_conv4)
 max_pool2_lstm = GlobalMaxPooling1D()(x_conv4)
 x = concatenate([avg_pool1_gru, max_pool1_gru, avg_pool2_gru, max_pool2_gru,
avg_pool1_lstm, max_pool1_lstm, avg_pool2_lstm, max_pool2_lstm])
 x = BatchNormalization()(x)
 x = Dense(128, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = Dense(num_class, activation = "softmax" )(x)
 model = Model(inputs = inp, outputs = x)
 return model
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<define_variables>
def textv3() :
inp = Input(shape =(max_len,))
x = Embedding(max_features + 1, embed_size, weights = [embedding_matrix], trainable = True )(inp)
 x = SpatialDropout1D(0.3 )(x)
 x = Masking()(x)
 x = LSTM(256 )(x)
 x = BatchNormalization()(x)
 x = Dense(128, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = Dense(num_class, activation = "softmax" )(x)
 model = Model(inputs = inp, outputs = x)
 return model
base_path = pathlib.Path(".. /input/" )<load_from_csv>
n_folds=20
epochs=10
verbose=2
batch_size = 64
for i in range(n_folds):
print("Training on Fold: ", i + 1)
 x_train, x_valid, y_train, y_valid = train_test_split(train_set1, y_ohe, test_size = 0.1, random_state = i)
 model1 = textv1()
callbacks = [EarlyStopping(monitor='val_loss', patience=3, verbose=1, min_delta=1e-4),
ModelCheckpoint(filepath='temp1.hdf5', verbose=1,save_best_only=True, mode='auto')]
model1.compile(loss = "categorical_crossentropy", optimizer='adam', metrics = ["accuracy"])
 model1.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, verbose = verbose,
validation_data =(x_valid, y_valid), callbacks = callbacks, shuffle=True)
 model1.load_weights('temp1.hdf5')
 model1.compile(loss = "categorical_crossentropy", optimizer='adam', metrics = ["accuracy"])


 if(i == 0):
train_pred = model1.predict(train_set1, verbose=2)
 test_pred = model1.predict(test_set1, verbose=2)
 else:
train_pred += model1.predict(train_set1, verbose=2)
 test_pred += model1.predict(test_set1, verbose=2)

print("======="*12, end="
")
 train_pred /= n_folds
test_pred /= n_folds
df_data = pd.read_csv(base_path/'train.csv' )<categorify>
predictions = np.round(np.argmax(test_pred, axis=1)).astype(int)
 test_set.drop(['ingredients', 'processed'], axis=1)
 sub = pd.DataFrame({'id': test_set['id'], 'cuisine': lb.inverse_transform(predictions)}, columns=['id', 'cuisine'])
 sub.to_csv('predicitions.csv', index=False )
class CactusDataset(Dataset):

 def __init__(self, df_data, image_path, image_size, transform=None):
self.data = df_data
self.image_path = image_path
self.transform = transform
def __len__(self):

 return len(self.data)
 def __getitem__(self, index):

 image_name = self.data.iloc[index, 0]
image = Image.open(str(self.image_path)+ '/' +image_name)
 image = image.convert('RGB')
 image = image.resize(image_size, Image.ANTIALIAS)
 if self.transform is not None:
image = self.transform(image)
 label = self.data.iloc[index, 1]
label = torch.from_numpy(np.asarray(label))
return image, label<normalization>
def create_download_link(df, filename = "submission.csv", title = "Download CSV file"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(sub )