Kaggle competition: iWildCam 2019 - FGVC6
BS = 24
train_loader = DataLoader(train_dataset, batch_size=BS, shuffle=True, num_workers=2, pin_memory=True)
 test_loader = DataLoader(test_dataset, batch_size=BS, shuffle=False, num_workers=2, pin_memory=True )<compute_test_metric>
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy')
 print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples')
 x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .
def f1_score(y_true, y_pred, threshold=0.5):
return fbeta_score(y_true, y_pred, 1, threshold)
 def fbeta_score(y_true, y_pred, beta, threshold, eps=1e-9):
beta2 = beta**2
y_pred = torch.ge(y_pred.float() , threshold ).float()
y_true = y_true.float()
true_positive =(y_pred * y_true ).sum(dim=1)
 precision = true_positive.div(y_pred.sum(dim=1 ).add(eps))
recall = true_positive.div(y_true.sum(dim=1 ).add(eps))
return torch.mean(
(precision*recall ).
 div(precision.mul(beta2)+ recall + eps ).
 mul(1 + beta2))<train_model>
datagen_train = ImageDataGenerator(
 width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
 datagen_train.fit(x_train )
def train_one_epoch(model, train_loader, criterion, optimizer, steps_upd_logging = 250):
model.train() ;
total_loss = 0.0
train_tqdm = tqdm_notebook(train_loader)
 for step,(features, targets)in enumerate(train_tqdm):
try:
features, targets = cuda(features), cuda(targets)
 optimizer.zero_grad()
logits = model(features)
 loss = criterion(logits, targets)
 loss.backward()
optimizer.step()
total_loss += loss.item()
if(step + 1)% steps_upd_logging == 0:
logstr = f'Train loss on step {step + 1} was {round(total_loss /(step + 1), 5)}'
train_tqdm.set_description(logstr)
 kaggle_commit_logger(logstr, need_print=False)
 except:
pass
return total_loss /(step + 1 )<choose_model_class>
from keras.applications import DenseNet121
from keras.layers import *
from keras.models import Sequential
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=0.0005)
 sheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.5, patience=3 )<train_model>
conv_base = DenseNet121(weights='imagenet',include_top=False,input_shape=(32,32,3))
%%time
TRAIN_LOGGING_EACH = 500
train_losses = []
valid_losses = []
valid_f1s = []
best_model_f1 = 0.0
best_model = None
best_model_ep = 0
for epoch in range(1, N_EPOCHS + 1):
ep_logstr = f"Starting {epoch} epoch..."
kaggle_commit_logger(ep_logstr)
 tr_loss = train_one_epoch(model, train_loader, criterion, optimizer, TRAIN_LOGGING_EACH)
 train_losses.append(tr_loss)
 tr_loss_logstr = f'Mean train loss: {round(tr_loss,5)}'
kaggle_commit_logger(tr_loss_logstr)
 valid_loss, valid_f1 = validate(model, test_loader, criterion)
 valid_losses.append(valid_loss)
 valid_f1s.append(valid_f1)
 val_loss_logstr = f'Mean valid loss: {round(valid_loss,5)}'
kaggle_commit_logger(val_loss_logstr)
 sheduler.step(valid_loss)
 if valid_f1 >= best_model_f1:
best_model = model
best_model_f1 = valid_f1
best_model_ep = epoch<save_model>
model = Sequential()
model.add(conv_base)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(14, activation='softmax'))
model.summary()
torch.save(best_model.state_dict() , 'model' )<find_best_params>
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'] )
bestmodel_logstr = f'Best f1 is {round(best_model_f1, 5)} on epoch {best_model_ep}'
kaggle_commit_logger(bestmodel_logstr )<load_from_csv>
str_ = 'Traning Started'
os.system('echo '+str_ )
SAMPLE_SUBMISSION_DF = pd.read_csv(r'.. /input/iwildcam-2020-fgvc7/sample_submission.csv')
 SAMPLE_SUBMISSION_DF.head()<feature_engineering>
batch_size = 128
epochs = 25
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto' 
)
 history = model.fit(
 x=x_train,
y=y_train,
batch_size=64,
epochs=10,
callbacks=[checkpoint],
validation_split=0.1
)
SAMPLE_SUBMISSION_DF.rename(columns={'Id':'file_name','Category':'category_id'}, inplace=True)
 SAMPLE_SUBMISSION_DF['file_name'] = SAMPLE_SUBMISSION_DF['file_name'] + '.jpg'
SAMPLE_SUBMISSION_DF.head()<create_dataframe>
model.load_weights('model.h5' )
subm_dataset = IMetDataset(SAMPLE_SUBMISSION_DF,
TEST_IMGS_DIR,
transforms = val_augmentation,
answer_colname=None
)<load_pretrained>
pred = model.predict_classes(x_test,verbose=1 )
SUMB_BS = 48
subm_dataloader = DataLoader(subm_dataset,
batch_size=SUMB_BS,
shuffle=False,
pin_memory=True )<find_best_params>
sam_sub = pd.read_csv('.. /input/iwildcam-2019-fgvc6/sample_submission.csv')
 sam_sub.head()
<find_best_params><EOS> 
output = np.array(np.concatenate(( _id, pred), 1))
output = pd.DataFrame(output,columns = ["Id","Predicted"])
 output.to_csv('submission.csv',index = False)