Kaggle competition: Leaf Classification
lr = 3e-2
epochs = 10
device = torch.device("cuda:0" if torch.cuda.is_available else "cpu")
 criterion = nn.CrossEntropyLoss(reduction='mean')
 tensorboard = False
if tensorboard == True:
time = datetime.now().strftime("%d%m-%H%M")
 train_id = f"{time}_lr={lr}_epochs={epochs}_wd={wd}_Trans_difflr_Adam"
writer = SummaryWriter(log_dir=f"runs/{train_id}")
 model = TransCactusTron()
model.to(device)
 global_step = 0
print(f'
START TRAINING
')
 parameters = [{'params': model.body.parameters() },
{'params': model.head.parameters() }]
optimizer = torch.optim.Adam(parameters, lr=lr)
 scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=[lr/10, lr],
epochs=epochs, steps_per_epoch=len(train_loader))
for epoch in tqdm(range(epochs), desc='Epochs'):
train_loss = 0.0
valid_loss = 0.0
start_time = datetime.now()
i = 0
model.train()
for img, label in tqdm(train_loader, desc="Iteration"):
img, label = img.to(device), label.to(device ).squeeze()
optimizer.zero_grad()
predicted_label = model(img)
 loss = criterion(predicted_label, label)
 loss.backward()
optimizer.step()
train_loss += loss.item()
scheduler.step()
model.eval()
with torch.no_grad() :
for img, label in valid_loader:
img, label = img.to(device), label.to(device ).squeeze()
predicted_label = model(img)
 loss = criterion(predicted_label, label)
 valid_loss += loss.item()
avg_train_loss = train_loss/len(train_loader)
 avg_valid_loss = valid_loss/len(valid_loader)
 delta = datetime.now() -start_time
print(f"Epoch: {epoch}\tTrain Loss: {avg_train_loss:.6f}\tVal Loss: {avg_valid_loss:.6f}\t Time:{delta}")
 if tensorboard == True:
writer.add_scalar("Loss/Train", avg_train_loss, global_step=global_step)
 writer.add_scalar("Loss/Validation", avg_valid_loss, global_step=global_step)
 writer.add_scalars("Loss/Cross",{"Train": avg_train_loss,
"Validation": avg_valid_loss},
global_step=global_step)
 global_step += 1
torch.save(model.state_dict() , f'model.pt')
 print("FINISHED TRAINING")
 if tensorboard == True:
writer.close()<save_to_csv>
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedShuffleSplit
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Convolution1D, Dropout
from keras.optimizers import SGD
from keras.utils import np_utils
df = pd.read_csv(csv_path)
 y_pred = np.zeros(len(testset))
model.eval()
y_pred = model.predict(test_loader, device)
 df['has_cactus'] = y_pred
df.to_csv("submission.csv", index=False)
 df.head(15 )<drop_column>
train = pd.read_csv('.. /input/leaf-classification/train.csv.zip')
 test = pd.read_csv('.. /input/leaf-classification/test.csv.zip')
if data_from == "kaggle":
shutil.rmtree('/kaggle/working/train')
 shutil.rmtree('/kaggle/working/test')
<load_from_csv>
def encode(train, test):
label_encoder = LabelEncoder().fit(train.species)
 labels = label_encoder.transform(train.species)
 classes = list(label_encoder.classes_)
 train = train.drop(['species', 'id'], axis=1)
 test_ids=test.id
test = test.drop('id', axis=1)
 return train, labels, test, classes,test_ids
%%time
df_train = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 df_test = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 df_train.head(3 )<define_variables>
train, labels, test, classes,test_ids = encode(train, test)
TRAIN_DATA_PATH ='.. /input/aerial-cactus-identification/train/train'
sample = TRAIN_DATA_PATH +'/'+ os.listdir(TRAIN_DATA_PATH)[0]
TEST_DATA_PATH = '.. /input/aerial-cactus-identification/test/test'
np.array(Image.open(sample)).shape<filter>
scaler = StandardScaler().fit(train.values)
 scaled_train = scaler.transform(train.values )
print('target 0:1->',len(df_train[df_train.has_cactus==0])/len(df_train[df_train.has_cactus==1]))<prepare_x_and_y>
sss = StratifiedShuffleSplit(test_size=0.1, random_state=23)
 for train_index, valid_index in sss.split(scaled_train, labels):
X_train, X_valid = scaled_train[train_index], scaled_train[valid_index]
y_train, y_valid = labels[train_index], labels[valid_index]
%%time
tmp = []
for i in range(len(df_train)) :
tmp.append(np.array(Image.open(TRAIN_DATA_PATH +'/'+ df_train.iloc[i]['id'])))
 X_train, y_train = np.array(tmp)/255, df_train['has_cactus']
tmp = []
for i in range(len(df_test)) :
tmp.append(np.array(Image.open(TEST_DATA_PATH +'/'+ df_test.iloc[i]['id'])))
 X_test = np.array(tmp)/255
del tmp
print(X_train.shape, y_train.shape)
 print(X_test.shape )<choose_model_class>
nb_features = 64
nb_class = len(classes )
def build_model(input_shape):
model =Sequential()
model.add(Conv2D(64,(3,3),padding='same',input_shape=(input_shape)))
 model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(64,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(64,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(MaxPooling2D())
 model.add(Dropout(0.5))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(MaxPooling2D())
 model.add(Dropout(0.5))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(GlobalAveragePooling2D())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile('adam',loss='binary_crossentropy',metrics=['accuracy'])
 return model<filter>
X_train_r = np.zeros(( len(X_train), nb_features, 3))
X_train_r[:, :, 0] = X_train[:, :nb_features]
X_train_r[:, :, 1] = X_train[:, nb_features:128]
X_train_r[:, :, 2] = X_train[:, 128:]
X_valid_r = np.zeros(( len(X_valid), nb_features, 3))
X_valid_r[:, :, 0] = X_valid[:, :nb_features]
X_valid_r[:, :, 1] = X_valid[:, nb_features:128]
X_valid_r[:, :, 2] = X_valid[:, 128:]
1.0 / len(df_train[df_train.has_cactus==0])* len(df_train[df_train.has_cactus==1] )<init_hyperparams>
model = Sequential()
model.add(Convolution1D(512, 1, input_shape=(nb_features, 3)))
 model.add(Activation('relu'))
model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(2048, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(nb_class))
model.add(Activation('softmax'))
%%time
histories = []
oof_pred = np.zeros(len(df_train))
sub_pred = np.zeros(len(df_test))
class_weights = {}
weights = [3.010082493125573,
1.0]
len(df_train[df_train.has_cactus==0])
 for i in range(2):
class_weights[i] = weights[i]
print('class_weights:',class_weights)
 checkpoint_name = '/checkpoint.file'
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
 BATCH_SIZE = 64
EPOCHS = 128
for fold_id,(train_index, val_index)in enumerate(skf.split(X_train, y_train)) :
print(f'fold id: {fold_id}')
 X_tr, y_tr = X_train[train_index], y_train[train_index]
X_val, y_val = X_train[val_index], y_train[val_index]
callbacks=[
keras.callbacks.ModelCheckpoint(
 checkpoint_name, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.7, patience=5, verbose=1,min_delta=0.00005,),
keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)
 ]
datagen = ImageDataGenerator(
 height_shift_range=0.1,
horizontal_flip=True 
)
 K.clear_session()
model = build_model(X_train.shape[1:])
 model.summary()
histories.append(
 model.fit_generator(
 datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
steps_per_epoch=int(np.ceil(len(X_train)/ BATCH_SIZE)) , validation_data=(X_val, y_val),
epochs=EPOCHS, class_weight=class_weights,
callbacks=callbacks, verbose=2)
)
 model = load_model(checkpoint_name)
 oof_pred[val_index] = model.predict(X_val ).flatten()
sub_pred += model.predict(X_test ).flatten() / skf.n_splits
print(roc_auc_score(y_val, oof_pred[val_index]))
plot_roc_auc(y_val, oof_pred[val_index])
 del callbacks
sub_pred = np.clip(sub_pred,0.0,1.0 )<compute_test_metric>
y_train = np_utils.to_categorical(y_train, nb_class)
 y_valid = np_utils.to_categorical(y_valid, nb_class)
 sgd = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9)
 model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
 nb_epoch = 15
model.fit(X_train_r, y_train, epochs=nb_epoch, validation_data=(X_valid_r, y_valid), batch_size=16 )
print('Ambiguous image index:',np.where(( sub_pred<0.80)&(sub_pred>0.20)) [:32][0] )<save_to_csv>
scaler = StandardScaler().fit(test.values)
 scaled_test = scaler.transform(test.values )
submission = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 submission['has_cactus'] = sub_pred
submission.to_csv('submission.csv', index=False)
 submission.head(50 )<set_options>
test_dataset = np.zeros(( len(scaled_test), nb_features, 3))
test_dataset[:, :, 0] = scaled_test[:, :nb_features]
test_dataset[:, :, 1] = scaled_test[:, nb_features:128]
test_dataset[:, :, 2] = scaled_test[:, 128:]
tf.enable_eager_execution()
tf.set_random_seed(32 )<load_from_csv>
preds_test = model.predict_proba(test_dataset)
 preds_test
BASE_DIR = '/kaggle/input/aerial-cactus-identification'
IMAGE_PATH = '{}/train/train'.format(BASE_DIR)
 train_df = pd.read_csv("{}/train.csv".format(BASE_DIR))<normalization>
submission = pd.DataFrame(preds_test, columns=classes)
 submission.insert(0, 'id', test_ids)
 submission
def read_image(image_id, base_dir=IMAGE_PATH, transformation=False):
train_image = mplimg.imread("{}/{}".format(base_dir, image_id)) / 255.0
if not transformation:
return train_image
train_image = ndimage.rotate(train_image, np.random.choice([0, 1, 2, 3])* 90, mode='nearest')
 if np.random.rand() > 0.5:
train_image = np.flip(train_image, np.random.choice([(0),(1),(0, 1)]))
train_image +(np.random.rand() * 0.2)- 0.1
return train_image<define_variables>
submission.to_csv('submission.csv', index=False)
 print('done!' )