Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<choose_model_class>
import numpy as np
import pandas as pd
import os
import json
from sklearn.cross_validation import train_test_split
def Le_Conv() :
model1 = Sequential()
model1.add(Conv2D(6, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=(32, 32, 3), padding="same"))
model1.add(AveragePooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid'))
model1.add(Conv2D(16, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model1.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
model1.add(Conv2D(120, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model1.add(Flatten())
 model1.add(Dense(84, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))
model1.compile(optimizer = Adamax(lr =0.001), loss = "binary_crossentropy", metrics=["acc"])
 return model1
def VggNet_Model() :
model = Sequential()
model.add(Conv2D(16,(2,2), activation="relu", input_shape=(32, 32, 3)))
 model.add(Conv2D(16,(2,2), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Conv2D(32,(3,3), activation="relu"))
model.add(Conv2D(32,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(units=1024, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(units=1, activation='sigmoid'))
model.summary()
adam = Adam(lr=0.001)
 model.compile(optimizer=adam,
loss='binary_crossentropy',
metrics=['acc'])
 return model
def Custom_Conv() :
model = Sequential()
model.add(Conv2D(32,(5,5), activation="relu", input_shape=(32, 32, 3)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,(5,5), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=1, activation='sigmoid'))
adam = Adam(lr=0.001)
 model.compile(optimizer=adam,
loss='binary_crossentropy',
metrics=['acc'])
 return model
def Complex_model() :
model = Sequential()
model.add(Conv2D(3, kernel_size = 3, activation = 'relu', input_shape =(32, 32, 3)))
 model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 32, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 64, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 128, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 2048, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(470, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(128, activation = 'tanh'))
model.add(Dense(1, activation = 'sigmoid'))
opt=Adam(0.001)
 model.compile(optimizer = opt, loss = 'mean_squared_error', metrics = ['accuracy'])
 return model 
<train_on_grid>
import gensim
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy.sparse import csr_matrix, hstack, vstack
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
models = []
histories =[]
for i in range(len(model)) :
ckpt_path = 'aerial_cactus_detection_'+str(i)+'.hdf5'
earlystop = EarlyStopping(monitor='val_acc', patience=25, verbose=1, restore_best_weights=False)
 reducelr = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=3, verbose=1, min_lr=1e-6)
 modelckpt_cb = ModelCheckpoint(ckpt_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 tb = TensorBoard()
callbacks = [earlystop, reducelr, modelckpt_cb, tb]
history = model[i].fit_generator(train_generator,
validation_data=validation_generator,
steps_per_epoch=train_size//batch_size,
validation_steps=validation_size//batch_size,
epochs=80, verbose=1,
shuffle=True,
callbacks=callbacks)
 models.append(model[i])
 histories.append(history )<load_from_csv>
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO )
test_df = pd.read_csv(os.path.join(base_dir, "sample_submission.csv"))
print(test_df.head())
 test_images = []
images = test_df['id'].values
for image_id in images:
test_images.append(cv2.imread(os.path.join(test_dir, image_id)))
 test_images = np.asarray(test_images)
 test_images = test_images / 255.0
print("Number of Test set images: " + str(len(test_images)) )<predict_on_test>
print("Read Dataset...")
 def read_dataset(path):
	return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 print("Prepare text data of Train and Test...")
 def generate_text(data):
	text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
	return text_data
def getSentences(data):
text_data= []
for doc in data:
text_data.append(doc['ingredients'])
 return text_data
train_text = generate_text(train)
 test_text = generate_text(test)
 Y = [doc['cuisine'] for doc in train]
_Id = [doc['id'] for doc in test]
train_sentences = getSentences(train)
 test_sentences = getSentences(test)
 sentences = train_sentences + test_sentences
final_pred =[]
predictcustom = models[0].predict(test_images)
 predictle = models[1].predict(test_images)
 predictcomplex = models[2].predict(test_images)
 stacked_arrays = np.hstack(( predictcustom, predictle, predictcomplex))
pred =np.mean(stacked_arrays, axis=1)
 predweighted = 0.3*predictcustom +0.2*predictle + 0.5*predictcomplex<save_to_csv>
tfidf = TfidfVectorizer(max_df=0.9, min_df=2 )
test_df['has_cactus'] = predweighted
test_df.to_csv('aerial-cactus-submission.csv', index = False )<import_modules>
X = tfidf.fit_transform(train_text )
print(tf.__version__ )<set_options>
X_train_source = tfidf.transform(( ' '.join(i)for i in train_sentences))
X_test_source = tfidf.transform(( ' '.join(i)for i in test_sentences))
sns.set(font_scale=1.5 )<define_variables>
from sklearn.model_selection import StratifiedKFold
MAIN_DIR = '.. /input/'
TRAIN_DIR = os.path.join(MAIN_DIR,"train/train/")
 TEST_DIR = os.path.join(MAIN_DIR, "test/test/")
 IMAGE_ROWS = 32
IMAGE_COLS = 32
CHANNELS = 3
BATCH_SIZE = 256
EPOCHS = 300
IMG_SHAPE =(IMAGE_ROWS, IMAGE_COLS,CHANNELS )<load_from_csv>
probaFeature = np.zeros(( X.shape[0], 40))
probaTest = []
df_train = pd.read_csv(MAIN_DIR + "train.csv" )<count_values>
lg = LogisticRegression(
 penalty='l2',
C=10,
n_jobs=-1, verbose=1,
solver='sag', multi_class='multinomial',
max_iter=300
)
df_train['has_cactus'].value_counts()<count_missing_values>
X = csr_matrix(X ).toarray()
df_train.isnull().any()<choose_model_class>
params = {
'multi_class': 'ovr',
'solver': 'lbfgs'
}
lgbm_params = {
'n_estimators': 250,
'max_depth': 25,
'learning_rate': 0.2,
'objective': 'multiclass',
'n_jobs': 7
}
model = LGBMClassifier(**lgbm_params)
def cnn_graph() :
main_input = tf.keras.Input(shape=(IMAGE_ROWS,IMAGE_COLS,CHANNELS), name="main_input")
 x = tf.keras.layers.Conv2D(16,(3,3), padding='same', use_bias=False, name="conv_1" )(main_input)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(32,(3,3), padding='same', use_bias=False, name='conv_2' )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(64,(3,3), padding='same', use_bias=False, name="conv_3" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(64,(3,3), padding='same', use_bias=False, name="conv_4" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(128,(3,3), padding='same', use_bias=False, name="conv_5" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(128,(3,3), padding='same', use_bias=False, name="conv_6" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(256,(3,3), padding='same', use_bias=False, name="conv_7" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(256,(3,3), padding='same', use_bias=False, name="conv_8" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(512,(3,3), padding='same', use_bias=False, name="conv_9" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(512,(3,3), padding='same', use_bias=False, name='conv_10' )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Flatten()(x)
 x = tf.keras.layers.Dense(256, activation='relu', use_bias=True )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 outputs = tf.keras.layers.Dense(1, activation='sigmoid', name="output_sigmoid" )(x)
 model = tf.keras.Model(inputs=main_input, outputs=outputs)
 return model<load_from_csv>
skf = StratifiedKFold(n_splits=5, random_state=2, shuffle=False)
 for train_index, test_index in skf.split(X, Y):
print("TRAIN:", train_index, "TEST:", test_index)
 X_train, X_test = X[train_index], X[test_index]
Y_train, Y_test = Y[train_index], Y[test_index]
lg.fit(X_train, Y_train)
 probaLg = lg.predict_proba(X_test)
 score = lg.score(X_test, Y_test)
 print(score)
 model.fit(X_train, Y_train)
 probaLGBM = model.predict_proba(X_test)
 score = model.score(X_test, Y_test)
 print(score)
 probaLg_test = lg.predict_proba(X_test_source)
 probaLGBM_test = model.predict_proba(X_test_source)
 probaStack = np.hstack(( probaLg, probaLGBM))
probaTestStack = np.hstack(( probaLg_test, probaLGBM_test))
probaTest.append(probaTestStack)
 probaFeature[test_index] = probaStack
df = pd.read_csv(MAIN_DIR+'train.csv')
 df['has_cactus'] = df['has_cactus'].apply(str )<choose_model_class>
clfKNN = KNeighborsClassifier(
 n_neighbors=10,
p=2,
metric='minkowski',
)
model = cnn_graph()
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'binary_crossentropy',
metrics=['accuracy'] )<load_pretrained>
outTest = []
def load_data(dataframe=None, batch_size=16, class_mode='binary', target_size=(IMAGE_ROWS, IMAGE_COLS)) :
k_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255,
zoom_range=0.1,
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 data_train = k_gen.flow_from_dataframe(dataframe,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=target_size,
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
subset="training")
 data_test = k_gen.flow_from_dataframe(dataframe,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(target_size),
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
subset='validation')
 return data_train, data_test<create_dataframe>
for train_index, test_index in skf.split(probaFeature, Y):
print("TRAIN:", train_index, "TEST:", test_index)
 X_train, X_test = probaFeature[train_index], probaFeature[test_index]
Y_train, Y_test = Y[train_index], Y[test_index]
clfKNN.fit(X_train, Y_train)
 score = clfKNN.score(X_test, Y_test)
 print(score)
 predict = clfKNN.predict_proba(probaFeatureTest)
 outTest.append(predict )
train_k_gen, val_k_gen = load_data(dataframe=df,batch_size=BATCH_SIZE, target_size=(IMAGE_ROWS, IMAGE_COLS))<train_model>
outTest = np.array(outTest)
 outTestMean = np.mean(outTest, axis=0)
 indexClass = outTestMean.argmax(axis=1 )
history = model.fit_generator(train_k_gen,
steps_per_epoch=(train_k_gen.n//train_k_gen.batch_size),
epochs=EPOCHS,
validation_data=val_k_gen,
validation_steps=len(val_k_gen),
shuffle=True )<import_modules>
results = []
import tqdm
from tqdm import tqdm, tqdm_notebook<predict_on_test>
for i in indexClass:
results.append(clfKNN.classes_[i] )
def predict(model, sample_submission):
pred = np.empty(( sample_submission.shape[0],))
for n in tqdm(range(sample_submission.shape[0])) :
data = np.array(Image.open(TEST_DIR + sample_submission.id[n]))
pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)) /255)[0]
sample_submission['has_cactus'] = pred
return sample_submission<save_to_csv>
with open('tfidf_lg_lgbm_stack4.csv', 'w')as f:
f.write('id,cuisine
')
 for _id, y in zip(_Id, results):
f.write('%s,%s
' %(_id, y))