Kaggle competition: Mechanisms of Action (MoA) Prediction
BASE_DIR=".. /input/cassava-leaf-disease-classification/"
TRAIN_IMAGES_DIR=os.path.join(BASE_DIR,'train_images')
 train_df=pd.read_csv(os.path.join(BASE_DIR,'train.csv'))<train_model>
train = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 sub = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 targets = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 targets1 = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 targetsnonzero=targets1[targets1.columns[targets1.sum() !=0]]
targetsnonzero= targetsnonzero.drop(['sig_id'], axis=1)
 targets = pd.concat([targets, targetsnonzero], axis=1, sort=False )
print("Count of training images {0}".format(len(os.listdir(TRAIN_IMAGES_DIR))))<categorify>
print('Train dataset',train.shape)
 print('Test dataset',test.shape)
 missing_train=(train.isnull().sum() ).sum()
missing_test=(test.isnull().sum() ).sum()
print('Missing values in train set:',missing_train,'Missing values in test set:',missing_test )
with open(f'{BASE_DIR}/label_num_to_disease_map.json', 'r')as f:
name_mapping = json.load(f)
 name_mapping = {int(k): v for k, v in name_mapping.items() }
train_df["class_id"]=train_df["label"].map(name_mapping )<import_modules>
combo = pd.concat([train, test], axis=0)
 cols=combo.columns.tolist()
catego = combo.iloc[:, 0:4]
thr = VarianceThreshold(0.95)
 VT_ALL = thr.fit_transform(combo[cols[4:]])
import torch
import torch.nn as nn
import torchvision.models as models
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold, GroupKFold, KFold, train_test_split
from albumentations.pytorch import ToTensorV2
import time
import datetime
import copy<train_on_grid>
QUA = QuantileTransformer(n_quantiles=100, output_distribution='normal')
 VT_ALL = QUA.fit_transform(VT_ALL)
class CassavaDataset(Dataset):
def __init__(self,df:pd.DataFrame,imfolder:str,train:bool = True, transforms=None):
self.df=df
self.imfolder=imfolder
self.train=train
self.transforms=transforms
def __getitem__(self,index):
im_path=os.path.join(self.imfolder,self.df.iloc[index]['image_id'])
 x=cv2.imread(im_path,cv2.IMREAD_COLOR)
 x=cv2.cvtColor(x,cv2.COLOR_BGR2RGB)
 if(self.transforms):
x=self.transforms(image=x)['image']
if(self.train):
y=self.df.iloc[index]['label']
return x,y
else:
return x
def __len__(self):
return len(self.df )<split>
all_ica = FastICA(n_components=300,max_iter=500)
 VT_ALL_ica=all_ica.fit_transform(VT_ALL)
 VT_ALL_ica_df = pd.DataFrame(VT_ALL_ica , columns=["VTICA" + str(i)for i in range(300)], index=combo.index)
 full= combo.iloc[:, 1:]
new_combo= pd.concat([full,VT_ALL_ica_df], axis=1)
 train = new_combo[ : train.shape[0]]
test = new_combo[-test.shape[0] : ]
train, valid = train_test_split(
 train_df,
test_size=0.1,
random_state=42,
stratify=train_df.label.values 
)
 train = train.reset_index(drop=True)
 valid = valid.reset_index(drop=True)
 train_targets = train.label.values
valid_targets = valid.label.values<create_dataframe>
train['cp_time'] = train['cp_time'].map({24: 1, 48: 2, 72: 3} ).astype(int)
 train = pd.get_dummies(train, columns = ["cp_type"], prefix="CPTP",drop_first=True)
 train = pd.get_dummies(train, columns = ["cp_dose"], prefix="CPD", drop_first=True)
 test['cp_time'] = test['cp_time'].map({24: 1, 48: 2, 72: 3} ).astype(int)
 test = pd.get_dummies(test, columns = ["cp_type"], prefix="CPTP",drop_first=True)
 test = pd.get_dummies(test, columns = ["cp_dose"], prefix="CPD",drop_first=True )
train_dataset=CassavaDataset(
 df=train,
imfolder=TRAIN_IMAGES_DIR,
train=True,
transforms=train_augs 
)
 valid_dataset=CassavaDataset(
 df=valid,
imfolder=TRAIN_IMAGES_DIR,
train=True,
transforms=valid_augs
)<load_pretrained>
y = targets.drop(['sig_id'], axis=1)
 X = train.values
Xtest = test.values
train_loader = DataLoader(
 train_dataset,
batch_size=16,
num_workers=4,
shuffle=True, 
)
 valid_loader = DataLoader(
 valid_dataset,
batch_size=16,
num_workers=4,
shuffle=False, 
)
<train_model>
def build_model() :
keras.backend.clear_session()
model = models.Sequential()
n_cols = X.shape[1]
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(2048,activation='relu', input_shape=(n_cols,)))
 model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(2048,activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(537, activation='sigmoid'))
return model
def train_model(datasets, dataloaders, model, criterion, optimizer, scheduler, num_epochs, device):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
 best_acc = 0.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs-1))
print('-' * 10)
 for phase in ['train', 'valid']:
if phase == 'train':
model.train()
else:
model.eval()
running_loss = 0.0
running_corrects = 0.0
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
model=model.to(device)
 outputs = model(inputs)
 _, preds = torch.max(outputs, 1)
 loss = criterion(outputs, labels)
 if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() *inputs.size(0)
 running_corrects += torch.sum(preds == labels.data)
 if phase == 'train':
scheduler.step()
epoch_loss = running_loss/len(datasets[phase])
 epoch_acc = running_corrects.double() /len(datasets[phase])
 print('{} Loss: {:.4f} Acc: {:.4f}'.format(
 phase, epoch_loss, epoch_acc))
if phase == 'valid' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
 print()
time_elapsed = time.time() -since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
model.load_state_dict(best_model_wts)
 return model<choose_model_class>
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_logloss', factor=0.3, patience=5, mode='min', min_lr=1E-5)
 early_stopping = keras.callbacks.EarlyStopping(monitor='val_logloss', min_delta=1E-5, patience=15, mode='min',restore_best_weights=True)
 n_labels = y.shape[1]
n_features = X.shape[1]
n_train = X.shape[0]
n_test = Xtest.shape[0]
def logloss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred,0.001,0.999)
 return -backend.mean(y_true*backend.log(y_pred)+(1-y_true)*backend.log(1-y_pred))
n_seeds = 6
np.random.seed(1)
 seeds = np.random.randint(0,100,size=n_seeds)
 n_folds = 5
y_pred = np.zeros(( n_test,n_labels))
oof = tf.constant(0.0)
 hists = []
for seed in seeds:
fold = 0
mltsplit = MultilabelStratifiedKFold(n_splits=n_folds,shuffle=True,random_state=seed)
 for train, test in mltsplit.split(X,y.values):
X_train = X[train]
X_test = X[test]
y_train = y.values[train]
y_test = y.values[test]
model=build_model()
model.compile(optimizer=keras.optimizers.Adam(lr=1e-3, decay=1e-3 / 200), loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=0.001), metrics=logloss)
 hist = model.fit(X_train,y_train, batch_size=128, epochs=50,verbose=0,validation_data =(X_test,y_test),callbacks=[reduce_lr, early_stopping])
 hists.append(hist)
 model.save('LabelSmoothed_seed_'+str(seed)+'_fold_'+str(fold))
y_val = model.predict(X_test)
 oof += logloss(tf.constant(y_test,dtype=tf.float32),tf.constant(y_val,dtype=tf.float32)) /(n_folds*n_seeds)
 y_pred += model.predict(Xtest)/(n_folds*n_seeds)
 print('seed=',seed,'fold=',fold)
 fold += 1
print('OOF score is ',oof)
 plt.figure(figsize=(12,8))
hist_train = np.zeros(40)
 hist_val = np.zeros(40)
 for i in range(n_folds):
hist_train += np.array(hists[i].history['loss'][:40])/n_folds
hist_val += np.array(hists[i].history['val_loss'][:40])/n_folds
plt.plot(hist_train)
 plt.plot(hist_val)
 plt.yscale('log')
 plt.yticks(ticks=[1,1E-1,1E-2])
 plt.xlabel('Epochs')
 plt.ylabel('Average Logloss')
 plt.legend(['Training','Validation'] )
<load_from_disk><EOS> 
test_preds = sub.copy()
test_preds[target_cols] = 0
test_preds.loc[:,target_cols] += y_pred[:,:206]
test2 = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test2 = test2.drop(['sig_id'], axis=1)
 test2 = pd.get_dummies(test2, columns = ["cp_type"], prefix="CPTP",drop_first=True)
 test_preds.iloc[test2['CPTP_trt_cp'] == 0,1:] = 0
test_preds.to_csv('submission.csv', index=False )