Kaggle competition: What's Cooking? (Kernels Only)
model = keras.Sequential([
keras.layers.InputLayer(input_shape=(32, 32, 3), batch_size=None, name='input'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck1/conv1'),
keras.layers.Conv2D(16, 3, 1, 'SAME', activation=keras.activations.relu, name='bottleneck1/conv2'),
keras.layers.BatchNormalization(name='bottleneck1/bn1'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck2/conv1'),
keras.layers.Conv2D(32, 3, 2, 'SAME', activation=keras.activations.relu, name='bottleneck2/conv2'),
keras.layers.BatchNormalization(name='bottleneck2/bn1'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck3/conv1'),
keras.layers.Conv2D(64, 3, 2, 'SAME', activation=keras.activations.relu, name='bottleneck3/conv2'),
keras.layers.BatchNormalization(name='bottleneck3/bn1'),
keras.layers.Flatten(name='flatten'),
keras.layers.Dense(1, activation=keras.activations.sigmoid, name='prob')
 ], name='sequential0' )<choose_model_class>
train_df = pd.read_json(".. /input/train.json")
 train_df.head()
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=5, min_lr=1e-9, verbose=1)
 early_stop = keras.callbacks.EarlyStopping(monitor='loss', min_delta=1e-6, patience=5, verbose=True)
 model.compile(
 loss='binary_crossentropy',
optimizer=keras.optimizers.SGD(lr=0.0001, decay=0),
metrics=['accuracy'],
verbose=False
)<train_model>
test_df = pd.read_json(".. /input/test.json")
 test_df.head()
history = model.fit(
 x=train_X,
y=train_y,
validation_data=(val_X, val_y),
epochs=150, batch_size=50,
callbacks=[reduce_lr]
)<predict_on_test>
test_df["cuisine"] = "unknown"
df = pd.concat([train_df, test_df], ignore_index=True)
 df.head()
pred_y = model.predict(train_X )<compute_test_metric>
df.ingredients = df.ingredients.apply(lambda x:(" ".join(x)).lower())
 df.ingredients = df.ingredients.apply(lambda x: re.sub(r'[^\w\d ,]', '', x))
df.head()
print('validation accuracy: {}'.format(sklearn.metrics.roc_auc_score(train_y, pred_y)) )<load_from_csv>
train_set = df[df.cuisine != 'unknown']
test_set = df[df.cuisine == 'unknown']
output_df = pd.read_csv('.. /input/sample_submission.csv')
 test_X = []
for idx, row in output_df.iterrows() :
test_X.append(imread('.. /input/test/test/' + row['id'])/255.)<predict_on_test>
tfidf = TfidfVectorizer(binary=True)
 x = tfidf.fit_transform(train_set.ingredients ).todense()
x_test = tfidf.transform(test_set.ingredients ).todense()
test_y = model.predict(test_X )<feature_engineering>
lb = LabelEncoder()
y = lb.fit_transform(train_set.cuisine)
 y = np_utils.to_categorical(y)
 print(y.shape )
output_df['has_cactus'] = test_y<save_to_csv>
seed = 29
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=seed)
 print(x_train.shape, y_train.shape, x_val.shape, y_val.shape )
output_df.to_csv('submission.csv', index=False )<load_pretrained>
MLP = Sequential()
MLP.add(Dense(512, input_shape=(3073,), activation='relu'))
MLP.add(Dropout(0.5))
MLP.add(Dense(y_train.shape[1], activation='softmax'))
MLP.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 MLP.summary()
model.save_weights('weights.hdf' )<install_modules>
file_path = "mlp.hdf5"
check_point = ModelCheckpoint(file_path, monitor="val_acc", verbose=1, save_best_only=True, mode="max")
 early_stop = EarlyStopping(monitor="val_acc", mode="max", patience=5)
 mlp_history = MLP.fit(x_train,
y_train,
batch_size=128,
epochs=50,
validation_data=(x_val, y_val),
callbacks=[check_point, early_stop] )
!pip install -U efficientnet<load_from_csv>
mlp_best = load_model('mlp.hdf5' )
train_dir = '.. /input/train/train'
test_dir = '.. /input/test'
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()<load_pretrained>
inp = Input(shape=(3073,), dtype='float32')
 reshape = Reshape(target_shape=(7,439))(inp)
 stacks = []
for kernel_size in [2, 3, 4]:
conv = Conv1D(128, kernel_size, padding='same', activation='relu', strides=1 )(reshape)
 pool = MaxPooling1D(pool_size=3 )(conv)
 drop = Dropout(0.5 )(pool)
 stacks.append(drop)
 merged = Concatenate()(stacks)
 flatten = Flatten()(merged)
 drop = Dropout(0.5 )(flatten)
 outp = Dense(y_train.shape[1], activation='softmax' )(drop)
 TextCNN = Model(inputs=inp, outputs=outp)
 TextCNN.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 TextCNN.summary()
cactus = []
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][0]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][1]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][2]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][6]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][7]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][11]))
labels = ['cactus','cactus','cactus','no cactus','no cactus',' no cactus']
plt.figure(figsize=[10,10])
 for x in range(0,6):
plt.subplot(3, 3,x+1)
 plt.imshow(cactus[x])
 plt.title(labels[x])
 x += 1
plt.show()<import_modules>
file_path = "textcnn.hdf5"
check_point = ModelCheckpoint(file_path, monitor="val_acc", verbose=1, save_best_only=True, mode="max")
 early_stop = EarlyStopping(monitor="val_acc", mode="max", patience=5)
 textcnn_history = TextCNN.fit(x_train,
y_train,
batch_size=128,
epochs=50,
validation_data=(x_val, y_val),
callbacks=[check_point, early_stop] )
from keras import applications
from efficientnet import EfficientNetB3
from keras import callbacks
from keras.models import Sequential<data_type_conversions>
textcnn_best = load_model('textcnn.hdf5' )
train_df['has_cactus'] = train_df['has_cactus'].astype('str' )<define_variables>
mlp_pred = mlp_best.predict(x_test)
 textcnn_pred = textcnn_best.predict(x_test)
 ensemble_pred =(mlp_pred + textcnn_pred ).argmax(axis=1)
 final = lb.inverse_transform(ensemble_pred )
<choose_model_class><EOS> 
sub = pd.DataFrame({'id': test_df.id, 'cuisine': final})
 sub.to_csv('tfidf-textcnn.csv', index = False )