Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<train_model>
import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
from keras.layers import Dense, Embedding, Activation,Dropout
from keras.layers import LSTM
from keras.preprocessing import sequence
import keras as keras
import numpy as np
lr = 2e-02
learn.fit_one_cycle(4, slice(lr))<predict_on_test>
data=pd.read_json('.. /input/train.json' )
preds,_ = learn.get_preds(ds_type=DatasetType.Test )<filter>
cat_dict=[]
data_dict=[]
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
data=data.drop(['id'],axis=1 )
test_df.to_csv('samplesubmission.csv', index=False )<set_options>
top_len=0
for x in range(0,len(data)) :
l=len(data['ingredients'][x])
 if(l>top_len):
top_len=l
%matplotlib inline<load_from_csv>
m=[]
for x in range(0,len(data)) :
k=''
for f in data['ingredients'][x]:
k+=f.lower() +' '
m.append(k)
 data['ingredients']=m
data_path = Path(".. /input/")
 train_df = pd.read_csv(data_path/'train.csv')
 test_df = pd.read_csv(data_path/'sample_submission.csv' )<concatenate>
cuisine_dic_t=[]
df1 = train_df[train_df.has_cactus==0].copy()
df2 = df1.copy()
train_df = train_df.append([df1, df2], ignore_index=True )<feature_engineering>
for x in range(0,len(data)) :
if data['cuisine'][x] not in cuisine_dic_t:
cuisine_dic_t.append(data['cuisine'][x] )
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=20, max_lighting=0.3, max_warp=0.2, max_zoom=1.2 )<categorify>
cuisine_dic=pd.DataFrame(data=cuisine_dic_t )
test_images = ImageList.from_df(test_df, path=data_path/'test', folder='test')
 src =(ImageList.from_df(train_df, path=data_path/'train', folder='train')
.split_by_rand_pct(0.2)
.label_from_df() 
.add_test(test_images))<normalization>
cuisine=[]
data =(src.transform(tfms,
size=128,
resize_method=ResizeMethod.PAD,
padding_mode='reflection')
.databunch(bs=256)
.normalize(imagenet_stats))<find_best_params>
tokenizer = Tokenizer(filters='!"
tokenizer.fit_on_texts(data['ingredients'])
 ingridients = tokenizer.texts_to_sequences(data['ingredients'] )
data.classes, data.c<define_variables>
X_train=ingridients[:30000]
y_train=cuisine[:30000]
X_test=ingridients[30000:]
y_test=cuisine[30000:]
data.show_batch(rows=3, figsize=(9,9))<choose_model_class>
X_train = tokenizer.sequences_to_matrix(X_train,mode='binary')
 X_test = tokenizer.sequences_to_matrix(X_test,mode='binary')
 y_train = keras.utils.to_categorical(y_train,len(cuisine_dic))
y_test = keras.utils.to_categorical(y_test,len(cuisine_dic))
learn = cnn_learner(data,
models.resnet101,pretrained=True,
metrics=[accuracy, AUROC() ],path='',model_dir='work' )<find_best_params>
model = Sequential()
model.add(Dense(2048, input_shape=(len(tokenizer.word_index)+1,)))
 model.add(Activation('relu'))
model.add(Dropout(0.7))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(len(cuisine_dic)))
 model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
learn.lr_find(stop_div=True)
 learn.recorder.plot(suggestion=True )<train_model>
model.fit(X_train, y_train,
batch_size=128,
epochs=10,
validation_data=(X_test, y_test))
lr = 3.98E-04
learn.fit_one_cycle(5, lr )<save_model>
model.evaluate(X_test, y_test,
batch_size=32, verbose=1 )
learn.save('step-1' )<choose_model_class>
test_data=pd.read_json('.. /input/test.json' )
learn = cnn_learner(data,
models.resnet101, pretrained=True,
metrics=[accuracy, AUROC() ],
callback_fns=[partial(SaveModelCallback)],
wd=0.1,
ps=[0.9, 0.6, 0.4],
path = '')
 learn = learn.load('.. /work/step-1' )<find_best_params>
m=[]
for x in range(0,len(test_data)) :
k=''
for f in test_data['ingredients'][x]:
k+=f.lower() +' '
m.append(k)
 test_data['ingredients']=m
learn.freeze_to(4)
 learn.lr_find()
learn.recorder.plot()<train_model>
ingridients_test = tokenizer.texts_to_sequences(test_data['ingredients'] )
learn.fit_one_cycle(3 )<train_model>
ingridients_test=tokenizer.sequences_to_matrix(ingridients_test,mode='binary' )
learn.fit_one_cycle(7 )<predict_on_test>
s=model.predict_classes(ingridients_test )
probability, classification = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = probability.numpy() [:, 0]
test_df.head()<save_to_csv>
res_t=[]
for x in s:
res_t.append(cuisine_dic[0][x] )
test_df.to_csv("submission.csv", index=False )<import_modules>
test_data['ingredients']=res_t
print(os.listdir(".. /input"))
seed = 42
np.random.seed(seed )<load_from_csv>
test_data=test_data.rename(index=str, columns={'ingredients': "cuisine"} )
<data_type_conversions><EOS> 
test_data.to_csv('result.csv', index=False )