Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<load_pretrained>
%matplotlib inline
test_loaders = {
"test": DataLoader(dataset=test_dataset,
batch_size=BATCH_SIZE,
shuffle=False)
 }<find_best_params>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print(train_df.shape, test_df.shape )
runner.infer(
 model=model,
loaders=test_loaders,
callbacks=[
CheckpointCallback(
 resume=f"{LOG_DIR}/checkpoints/best.pth" 
),
InferCallback() ,
],
verbose=True
)<predict_on_test>
def clean_text(text):

 text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
 text = re.sub(r'\_', '', text)
 return text
predicted_probs = runner.callbacks[0].predictions['logits']<prepare_output>
train_df['text'] = train_df['text'].map(lambda x: clean_text(x))
train_df['text'] = train_df['text'].map(lambda x: x.strip().split())
 train_df.head()
submission['target'] = predicted_probs.argmax(axis=1)
 submission.head()<save_to_csv>
test_df['text'] = test_df['text'].map(lambda x: clean_text(x))
test_df['text'] = test_df['text'].map(lambda x: x.strip().split())
 test_df.head()
submission.to_csv('submission.csv', index=False )<save_to_csv>
data = []
for i in range(len(train_df)) :
data.append(train_df['text'][i])
 for j in range(len(test_df)) :
data.append(test_df['text'][j] )
submission.to_csv('submission.csv', index=False )<import_modules>
embedding = gensim.models.Word2Vec(data, size=50, window=10, min_count=1, sg=0 )
import tensorflow as tf
from keras.layers import *
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.utils import plot_model, to_categorical
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt<load_from_csv>
embedding.train(data,total_examples=len(data),epochs=30 )
def load_dataset() :
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 label = train.pop('label')
 return train, label, label.nunique()
X_train_orig, Y_train_orig, classes = load_dataset()
m = X_train_orig.shape[0]
def load_quizset() :
quiz = pd.read_csv('.. /input/digit-recognizer/test.csv')
 return quiz
X_quiz_orig = load_quizset()
mq = X_quiz_orig.shape[0]
X_train = X_train_orig.to_numpy().reshape(( -1,28,28,1)) /255.0
X_quiz = X_quiz_orig.to_numpy().reshape(( -1,28,28,1)) /255.0
Y_train = to_categorical(Y_train_orig.to_numpy())
 print("Training examples = ", str(m))
print("X_train shape: " + str(X_train.shape))
print("Y_train shape: " + str(Y_train.shape))
print("Quiz examples = ", str(mq))
print("X_quiz shape: " + str(X_quiz.shape)) 
<filter>
words = list(embedding.wv.vocab)
 print(len(words))
X_train[np.isnan(X_train)]<train_model>
print(embedding['capered'] )
train_image_gen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=5,
zoom_range=0.1,
validation_split=0.2 
)
 train_image_gen.fit(X_train)
 val_image_gen = ImageDataGenerator()
data_gen = train_image_gen.flow(X_train,seed=130)
 plt.figure(figsize=(10,10))
i=0
for data in data_gen[1]:
plt.subplot(5,5,i+1)
 plt.xticks([])
 plt.yticks([])
 plt.grid(False)
 plt.imshow(image.array_to_img(data_gen[0][i]))
i = i+1
if i > 24:
break 
<choose_model_class>
train_df['author'] = pd.Categorical(train_df['author'])
 df_Dummies = pd.get_dummies(train_df['author'], prefix='author')
 train_df = pd.concat([train_df, df_Dummies], axis=1)
 train_df.head()
def model(input_shape,classes):

 X_input = Input(input_shape)
 X = ZeroPadding2D(( 3,3))(X_input)
 X = Conv2D(32,(7,7), strides=(1,1), name='conv1a' )(X)
 X = Activation('relu' )(X)
 X = Conv2D(64,(7,7), strides=(1,1), name='conv1b' )(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(( 2,2))(X)
 X = Dropout(0.3 )(X)
 X = Conv2D(128,(5,5), strides=(1,1), name='conv2a' )(X)
 X = Activation('relu' )(X)
 X = Conv2D(256,(5,5), strides=(1,1), name='conv2b' )(X)
 X = Activation('relu' )(X)
 X = MaxPooling2D(( 2,2), strides=(2,2))(X)
 X = Dropout(0.3 )(X)
 X = Flatten()(X)
 X = Dense(classes, activation='softmax', name='fc'+str(classes))(X)
 model = Model(inputs = X_input, outputs = X, name='MyModel')
 return model
model(X_train.shape[1:],classes ).summary() 
<choose_model_class>
X = train_df['text'].str[:100]
Y = train_df[['author_EAP', 'author_HPL', 'author_MWS']].values
print(X.shape, X[0], Y.shape, Y[0] )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_categorical_accuracy',
patience=5,
verbose=1,
factor=0.3,
min_lr=1e-05 )<train_model>
def text_to_avg(text):

 avg = np.zeros(( 50,))
for w in text:
avg += embedding[w]
avg = avg/len(text)
 return avg
BATCH_SIZE=4096
EPOCHS=1000
mymodel = model(X_train.shape[1:],classes)
 mymodel.compile(optimizer=Adam() ,
loss='categorical_crossentropy',
metrics=['categorical_accuracy','categorical_crossentropy'])
 history = mymodel.fit(train_image_gen.flow(x=X_train,y=Y_train,batch_size=BATCH_SIZE),
epochs=EPOCHS, shuffle=True,
validation_data=val_image_gen.flow(x=X_train,y=Y_train,batch_size=BATCH_SIZE),
callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_categorical_crossentropy',patience=16,restore_best_weights=True),
learning_rate_reduction] )<predict_on_test>
X_avg = np.zeros(( X.shape[0], 50))
for i in range(X.shape[0]):
X_avg[i] = text_to_avg(X[i] )
Y_pred = mymodel.predict(X_train)
 Y_pred_classes = np.argmax(Y_pred,axis=1)
 Y_train_classes = np.argmax(Y_train,axis=1)
 errors =(Y_pred_classes - Y_train_classes !=0)
 Y_pred_errors = Y_pred_classes[errors]
Y_train_errors = Y_train_classes[errors]
X_train_errors = X_train[errors] 
<save_to_csv>
X_test_avg = np.zeros(( X_test.shape[0], 50))
for i in range(X_test.shape[0]):
X_test_avg[i] = text_to_avg(X_test[i] )
Y_quiz = mymodel.predict(X_quiz)
 Y_sub = np.argmax(Y_quiz,axis=1)
 Y_sub = pd.Series(Y_sub,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name='ImageId'),Y_sub],axis=1)
 submission.to_csv("submission.csv",index=False )<import_modules>
X_train, X_dev, Y_train, Y_dev = train_test_split(X_avg, Y, test_size=0.2, random_state=123)
 print(X_train.shape, Y_train.shape, X_dev.shape, Y_dev.shape )
print(torch.__version__)
<define_variables>
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(50,)))
 model.add(layers.Dense(3, activation='softmax'))
model.summary()
pt_models = ".. /input/pretrained-models/pretrained-models.pytorch-master/"
sys.path.insert(0, pt_models)
 ef_models = ".. /input/efficientnet-pytorch/"
sys.path.insert(0, ef_models)
<import_modules>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
import efficientnet_pytorch
from efficientnet_pytorch import EfficientNet<import_modules>
epochs = 50
history = model.fit(X_train, Y_train, epochs=epochs, batch_size=128, validation_data=(X_dev, Y_dev))
import glob
import torch
import albumentations
import pandas as pd
import numpy as np
from tqdm import tqdm
from PIL import Image
import joblib
import torch.nn as nn
from torch.nn import functional as F<define_search_space>
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(50,)))
 model.add(layers.Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
MODEL_MEAN =(0.485, 0.456, 0.406)
 MODEL_STD =(0.229, 0.224, 0.225)
 IMG_HEIGHT = 137
IMG_WIDTH = 236
DEVICE="cuda"<normalization>
epochs = 10
model.fit(X_avg, Y, epochs=epochs, batch_size=128 )
class Se_resnext50_32x4d(nn.Module):
def __init__(self, pretrained):
super(Se_resnext50_32x4d, self ).__init__()
if pretrained is True:
self.model = pretrainedmodels.__dict__["se_resnext50_32x4d"](pretrained="imagenet")
 else:
self.model = pretrainedmodels.__dict__["se_resnext50_32x4d"](pretrained=None)
 self.l0 = nn.Linear(2048, 168)
 self.l1 = nn.Linear(2048, 11)
 self.l2 = nn.Linear(2048, 7)
 def forward(self, x):
bs, _, _, _ = x.shape
x = self.model.features(x)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(bs, -1)
 l0 = self.l0(x)
 l1 = self.l1(x)
 l2 = self.l2(x)
 return l0, l1, l2<normalization>
preds = model.predict(X_test_avg)
 print(preds.shape)
 print(preds[7] )
class Senet50(nn.Module):
def __init__(self, pretrained):
super(Senet50, self ).__init__()
if pretrained is True:
self.model = pretrainedmodels.__dict__["se_resnet50"](pretrained="imagenet")
 else:
self.model = pretrainedmodels.__dict__["se_resnet50"](pretrained=None)
 self.l0 = nn.Linear(2048, 168)
 self.l1 = nn.Linear(2048, 11)
 self.l2 = nn.Linear(2048, 7)
 def forward(self, x):
bs, _, _, _ = x.shape
x = self.model.features(x)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(bs, -1)
 l0 = self.l0(x)
 l1 = self.l1(x)
 l2 = self.l2(x)
 return l0, l1, l2<choose_model_class>
pred_labels = []
for i in range(len(X_test_avg)) :
pred_label = np.argmax(preds[i])
 pred_labels.append(pred_label )
class EffNetB4(nn.Module):
def __init__(self, pretrained):
super(EffNetB4, self ).__init__()
if pretrained is True:
self.model = EfficientNet.from_pretrained('efficientnet-b4', in_channels=3)
 else:
self.model = EfficientNet.from_name('efficientnet-b4')
 self.l0 = nn.Linear(1792, 168)
 self.l1 = nn.Linear(1792, 11)
 self.l2 = nn.Linear(1792, 7)
 def forward(self, x):
bs, _, _, _ = x.shape
x = self.model.extract_features(x)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(bs, -1)
 l0 = self.l0(x)
 l1 = self.l1(x)
 l2 = self.l2(x)
 return l0, l1, l2<normalization>
result = pd.DataFrame(preds, columns=['EAP','HPL','MWS'])
 result.insert(0, 'id', test_df['id'])
 result.head()
<create_dataframe><EOS> 
result.to_csv('submission.csv', index=False, float_format='%.20f' )