Kaggle competition: Planet: Understanding the Amazon from Space
<SOS> metric: MeanFScoreBeta Kaggle data source: planet:-understanding-the-amazon-from-space<predict_on_test>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
def predict_test(model):
X_test = get_test_data()
y_test = model.predict(X_test)
 y_test = np.exp(y_test)
 y_test = pd.DataFrame(y_test,index=X_test.index)
 y_test.columns = ['SalePrice']
return y_test<save_to_csv>
import numpy as np
import pandas as pd
from fastai.vision import *
y_test = predict_test(en_int)
 y_test.to_csv('submission.csv')
 y_test.plot.hist(bins=20 )<import_modules>
df = pd.read_csv(path/"train_v2.csv")
 df.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split<load_from_csv>
tfms = get_transforms(flip_vert = True, max_lighting = 0.1, max_zoom = 1.05, max_warp = 0.)
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<prepare_x_and_y>
np.random.seed(7)
 source =(ImageList.from_csv(path, "train_v2.csv", folder = "train-jpg", suffix = ".jpg")
.split_by_rand_pct(0.2)
.label_from_df(cols = "tags", label_delim = " "))
X_train = df_train.drop('label', axis=1 ).values
y_train = df_train['label'].values.reshape(-1,1)
 X_test = df_test.values<data_type_conversions>
data =(source.transform(tfms, size = 128)
.databunch() 
.normalize(imagenet_stats))
X_train = X_train.astype('float32')/255.0
X_test = X_test.astype('float32')/255.0<categorify>
data.show_batch(rows = 4, figsize =(15,15))
X_train = X_train.reshape(-1,28,28,1)
 X_test = X_test.reshape(-1,28,28,1)
 y_train = to_categorical(y_train)
 target_count = y_train.shape[1]<train_model>
acc_thresh = partial(accuracy_thresh, thresh = 0.2)
 f2_score = partial(fbeta, beta = 2, thresh = 0.2 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<split>
learn = cnn_learner(data, models.resnet50, metrics = [acc_thresh, f2_score], model_dir = "/tmp/models" )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42 )<choose_model_class>
learn.lr_find()
learn.recorder.plot()
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
 model.add(Dense(target_count, activation='softmax'))
optimizer = RMSprop(learning_rate=0.001,rho=0.99)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, verbose=1,patience=2, min_lr=0.00000001)
 callback = EarlyStopping(monitor='loss', patience=5)
 history = model.fit(datagen.flow(X_train,y_train, batch_size=64), epochs = 50, validation_data=(X_val, y_val), verbose = 1, callbacks=[reduce_lr, callback] )<predict_on_test>
learn.fit_one_cycle(cyc_len = 5, max_lr = slice(1e-2))
y_val_m = y_val.argmax(axis=1)
 y_val_hat_prob = model.predict(X_val)
 y_val_hat = y_val_hat_prob.argmax(axis=1)
 X_val_inc = X_val[y_val_m != y_val_hat, :, :, :]
y_val_inc = y_val_m[y_val_m != y_val_hat]
y_val_hat_inc = y_val_hat[y_val_m != y_val_hat]
y_val_hat_prob_inc = y_val_hat_prob[y_val_m != y_val_hat]<compute_test_metric>
learn.save("baseline-rn50-128" )
for i in range(0,10):
act = y_val_inc[i]
pred = y_val_hat_inc[i]
print('Actual: {}; Confidence(act/pred): \t{} - {:.0f}% \t{} - {:.0f}%'.format(act, act, y_val_hat_prob_inc[i][act]*100, pred, y_val_hat_prob_inc[i][pred]*100))<save_to_csv>
np.random.seed(7)
 data2 =(source.transform(tfms, size = 256)
.databunch() 
.normalize(imagenet_stats))
y_test_hat = model.predict(X_test ).argmax(axis=1)
 df_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 df_submission['Label'] = y_test_hat.astype('int32')
 df_submission.to_csv('Submission.csv', index=False)
 print('Submission saved!' )<import_modules>
learn2 = cnn_learner(data2, models.resnet50, metrics = [acc_thresh, f2_score], model_dir = "/tmp/models" )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split<load_from_csv>
lr = 3e-2
learn2.fit_one_cycle(cyc_len = 5, max_lr = slice(lr))
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<prepare_x_and_y>
learn2.save("baseline-rn50-256" )
X_train = df_train.drop('label', axis=1 ).values
y_train = df_train['label'].values.reshape(-1,1)
 X_test = df_test.values<data_type_conversions>
learn2.fit_one_cycle(cyc_len = 10, max_lr = slice(1e-5, lr/10))
X_train = X_train.astype('float32')/255.0
X_test = X_test.astype('float32')/255.0<categorify>
learn2.save("stage-2-rn50-256" )
X_train = X_train.reshape(-1,28,28,1)
 X_test = X_test.reshape(-1,28,28,1)
 y_train = to_categorical(y_train)
 target_count = y_train.shape[1]<train_model>
learn2.export(file = ".. /working/export.pkl" )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<split>
test = ImageList.from_folder(path/"test-jpg-v2")
 len(test )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42 )<choose_model_class>
load_path = Path(".. /working/")
 learn = load_learner(load_path, test=test)
 predicts, _ = learn.get_preds(ds_type = DatasetType.Test )
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3,3), padding='valid', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
 model.add(Dense(target_count, activation='softmax'))
optimizer = RMSprop(learning_rate=0.001,rho=0.99)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, verbose=1,patience=2, min_lr=0.00000001)
 callback = EarlyStopping(monitor='loss', patience=5)
 history = model.fit(datagen.flow(X_train,y_train, batch_size=64), epochs = 50, validation_data=(X_val, y_val), verbose = 1, callbacks=[reduce_lr, callback] )<predict_on_test>
labels_pred = [" ".join([learn.data.classes[i] for i,p in enumerate(pred)if p > 0.2])for pred in predicts]
labels_pred[:5]
y_val_m = y_val.argmax(axis=1)
 y_val_hat_prob = model.predict(X_val)
 y_val_hat = y_val_hat_prob.argmax(axis=1)
 X_val_inc = X_val[y_val_m != y_val_hat, :, :, :]
y_val_inc = y_val_m[y_val_m != y_val_hat]
y_val_hat_inc = y_val_hat[y_val_m != y_val_hat]
y_val_hat_prob_inc = y_val_hat_prob[y_val_m != y_val_hat]<compute_test_metric>
image_names = [img.name[:-4] for img in learn.data.test_ds.items]
for i in range(0,10):
act = y_val_inc[i]
pred = y_val_hat_inc[i]
print('Actual: {}; Confidence(act/pred): \t{} - {:.0f}% \t{} - {:.0f}%'.format(act, act, y_val_hat_prob_inc[i][act]*100, pred, y_val_hat_prob_inc[i][pred]*100))<save_to_csv>
df2 = pd.DataFrame({"image_name":image_names, "tags":labels_pred})
 df2.head()
<set_options><EOS> 
df2.to_csv("submission.csv", index = False )