Kaggle competition: iWildCam 2019 - FGVC6
<SOS> metric: MacroFScore Kaggle data source: iwildcam-2019-fgvc6<concatenate>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
submission = pd.concat([rowid,loc],axis = 1 )<save_to_csv>
from fastai import *
from fastai.vision import *
import pandas as pd
submission.to_csv('Utkarsh.csv',index = False )<load_from_csv>
print('Make sure cuda is installed:', torch.cuda.is_available())
 print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )
FTRAIN = '.. /input/training/training.csv'
FTEST = '.. /input/test/test.csv'
def load(test=False, cols=None):

 fname = FTEST if test else FTRAIN
df = read_csv(os.path.expanduser(fname))
df['Image'] = df['Image'].apply(lambda im: np.fromstring(im, sep=' '))
if cols:
df = df[list(cols)+ ['Image']]
print(df.count())
 df = df.dropna()
X = np.vstack(df['Image'].values)/ 255.
 X = X.astype(np.float32)
 if not test:
y = df[df.columns[:-1]].values
y =(y - 48)/ 48
X, y = shuffle(X, y, random_state=42)
 y = y.astype(np.float32)
 else:
y = None
return X, y
X, y = load()
print("X.shape == {}; X.min == {:.3f}; X.max == {:.3f}".format(
 X.shape, X.min() , X.max()))
print("y.shape == {}; y.min == {:.3f}; y.max == {:.3f}".format(
 y.shape, y.min() , y.max()))<prepare_x_and_y>
path = Path('.. /input' )
def load2d(test=False,cols=None):
re = load(test, cols)
 X = re[0].reshape(-1,96,96,1)
 y = re[1]
return X, y<choose_model_class>
train_df = pd.read_csv(path/'train.csv')
 train_df = pd.concat([train_df['id'],train_df['category_id']],axis=1,keys=['id','category_id'])
 train_df.head()
%%time
model = Sequential()
model.add(Dense(128,input_dim=X.shape[1]))
model.add(Activation('relu'))
model.add(Dropout(0.1))
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(30))
model.summary()<train_model>
test_df = pd.read_csv(path/'test.csv')
 test_df = pd.DataFrame(test_df['id'])
 test_df['predicted'] = 0
test_df.head()
sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)
 model.compile(loss='mean_squared_error', optimizer=sgd)
 hist = model.fit(X, y, nb_epoch=50,batch_size=128, validation_split=0.2,verbose=False )<predict_on_test>
data.show_batch()
X_test, _ = load(test=True)
 y_test = model.predict(X_test )<save_model>
learn = cnn_learner(data, base_arch=models.densenet121, metrics=[FBeta() ,accuracy], wd=1e-5 ).mixup()
model.save('my_model.h5')
 model = load_model('my_model.h5' )<drop_column>
lr = 2e-2
learn.fit_one_cycle(2, slice(lr))
<prepare_x_and_y>
learn.save('stage-1-sz32' )
X,y = load2d()<import_modules>
lr = 1e-3
learn.fit_one_cycle(4, slice(lr/100, lr))
from keras.layers import MaxPooling2D, Conv2D , Flatten, Dropout
from keras.layers.normalization import BatchNormalization<choose_model_class>
learn.save('stage-2-sz32' )
def CNN() :
model2 = Sequential()
model2.add(Conv2D(filters=16,kernel_size=2,padding="same",activation="relu",input_shape=(96,96,1)))
 model2.add(Dropout(0.1))
model2.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode="valid"))
model2.add(BatchNormalization())
 model2.add(Conv2D(32, 5, 5,activation="relu"))
model2.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode="valid"))
model2.add(Dropout(0.2))
model2.add(BatchNormalization())
 model2.add(Conv2D(64, 5, 5,activation="relu"))
model2.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode="valid"))
model2.add(BatchNormalization())
 model2.add(Conv2D(128, 3, 3,activation="relu"))
model2.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode="valid"))
model2.add(Dropout(0.4))
model2.add(BatchNormalization())
 model2.add(Flatten())
 model2.add(Dense(500, activation="relu"))
model2.add(Dropout(0.1))
model2.add(Dense(128, activation="relu"))
model2.add(Dropout(0.1))
model2.add(Dense(30))
model2.summary()
model2.compile(optimizer='adam',
loss='mse',
metrics=['mae','accuracy'])
 return(model2 )<train_on_grid>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
model2 = CNN()
hist2 = model2.fit(X, y, nb_epoch=500,batch_size=128, validation_split=0.2,verbose=False )<predict_on_test>
test_preds = learn.TTA(DatasetType.Test)
 test_df['predicted'] = test_preds[0].argmax(dim=1 )
sample1,_ = load(test=True)
 sample2,_ = load2d(test=True)
 y_pred1 = model.predict(sample1)
 y_pred2 = model2.predict(sample2 )<load_pretrained>
test_df.to_csv('submission.csv', index=False )