Kaggle competition: iWildCam 2019 - FGVC6
<SOS> metric: MacroFScore Kaggle data source: iwildcam-2019-fgvc6<load_from_csv>
warnings.filterwarnings('ignore' )
samplesubmission = pd.read_csv("/kaggle/input/SampleSubmission.csv")
 samplesubmission.head()<load_from_csv>
%%time
train_df = pd.read_csv(os.path.join(PATH, 'train.csv'))
test_df = pd.read_csv(os.path.join(PATH, 'test.csv'))
train = pd.read_csv("/kaggle/input/training/training.csv")
 train.head().T<count_missing_values>
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' )
train.isnull().sum()<count_missing_values>
classes_wild = {0: 'empty', 1: 'deer', 2: 'moose', 3: 'squirrel', 4: 'rodent', 5: 'small_mammal', \
6: 'elk', 7: 'pronghorn_antelope', 8: 'rabbit', 9: 'bighorn_sheep', 10: 'fox', 11: 'coyote', \
12: 'black_bear', 13: 'raccoon', 14: 'skunk', 15: 'wolf', 16: 'bobcat', 17: 'cat',\
18: 'dog', 19: 'opossum', 20: 'bison', 21: 'mountain_goat', 22: 'mountain_lion'}
train.isnull().sum()<correct_missing_values>
train_df['classes_wild'] = train_df['category_id'].apply(lambda cw: classes_wild[cw] )
train.fillna(method = 'ffill',inplace = True )<prepare_x_and_y>
train_image_files = list(os.listdir(os.path.join(PATH,'train_images')))
 test_image_files = list(os.listdir(os.path.join(PATH,'test_images')))
 print("Number of image files: train:{} test:{}".format(len(train_image_files), len(test_image_files)) )
X = train.Image.values
del train['Image']
Y = train.values<concatenate>
%%time
train_file_names = list(train_df['file_name'])
 print("Matching train image names: {}".format(len(set(train_file_names ).intersection(train_image_files))))
x = []
for i in tqdm(X):
q = [int(j)for j in i.split() ]
x.append(q)
 len(x )<split>
%%time
test_file_names = list(test_df['file_name'])
 print("Matching test image names: {}".format(len(set(test_file_names ).intersection(test_image_files))))
x_train,x_test,y_train,y_test = tts(x,Y,random_state = 69,test_size = 0.1 )<import_modules>
cnt_classes_images = train_df.classes_wild.nunique()
print("There are {} classes of images".format(cnt_classes_images))
pd.DataFrame(train_df.classes_wild.value_counts() ).transpose()
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.layers import Activation, Convolution2D, MaxPooling2D, BatchNormalization, Flatten, Dense, Dropout, Conv2D,MaxPool2D, ZeroPadding2D<choose_model_class>
try:
train_df['date_time'] = pd.to_datetime(train_df['date_captured'], errors='coerce')
 train_df["year"] = train_df['date_time'].dt.year
train_df["month"] = train_df['date_time'].dt.month
train_df["day"] = train_df['date_time'].dt.day
train_df["hour"] = train_df['date_time'].dt.hour
train_df["minute"] = train_df['date_time'].dt.minute
except Exception as ex:
print("Exception:".format(ex))
model = Sequential()
model.add(Convolution2D(32,(3,3), padding='same', use_bias=False, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(32,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()<compute_test_metric>
IMAGE_PATH = os.path.join(PATH,'train_images/')
 draw_category_images('classes_wild' )
<train_model>
x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .
model.compile(optimizer = 'adam',loss = 'mean_squared_error', metrics = ['mae','acc'])
 model.fit(x_train,y_train,batch_size=256, epochs=50,validation_data=(x_test,y_test))<train_model>
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_pred = self.model.predict(X_val)
 y_pred_cat = keras.utils.to_categorical(
 y_pred.argmax(axis=1),
num_classes=14 
)
 _val_f1 = f1_score(y_val, y_pred_cat, average='macro')
 _val_recall = recall_score(y_val, y_pred_cat, average='macro')
 _val_precision = precision_score(y_val, y_pred_cat, average='macro')
 self.val_f1s.append(_val_f1)
 self.val_recalls.append(_val_recall)
 self.val_precisions.append(_val_precision)
 print(( f"val_f1: {_val_f1:.4f}"
f" — val_precision: {_val_precision:.4f}"
f" — val_recall: {_val_recall:.4f}"))
return
f1_metrics = Metrics()
model.compile(optimizer = 'adam',loss = 'mean_squared_error', metrics = ['mae'])
 model.fit(x,Y,batch_size=64, epochs=100)
 model.fit(x,Y,batch_size=128, epochs=50)
 model.fit(x,Y,batch_size=256, epochs=50 )<load_from_csv>
model_densenet = DenseNet121(
 weights='.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5',
include_top=False,
input_shape=(32,32,3)
 )
test = pd.read_csv("/kaggle/input/test/test.csv")
 test.head()<count_missing_values>
model = Sequential()
model.add(model_densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dense(cnt_classes_images, activation='softmax'))
test.isnull().sum()<count_missing_values>
model.compile(
 loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
test.isnull().sum()<split>
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto'
)
test = test.Image.values
x_t = []
for i in tqdm(test):
q = [int(j)for j in i.split() ]
x_t.append(q)
 x_t = np.array(x_t)
 x_t = x_t.reshape(-1, 96,96,1)
 x_t = x_t/255.0
x_t.shape<predict_on_test>
BATCH_SIZE = 64
EPOCHS = 35
VALID_SPLIT = 0.1
history = model.fit(
 x=x_train,
y=y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=[checkpoint, f1_metrics],
validation_split=VALID_SPLIT
)
pred = model.predict(x_t)
 pred.shape<define_variables>
model.load_weights('model.h5')
 y_test = model.predict(x_test )
lookid_list = list(lookid_data['FeatureName'])
 imageID = list(lookid_data['ImageId']-1)
 pre_list = list(pred )<define_variables>
submission_df = pd.read_csv(os.path.join(PATH,'sample_submission.csv'))
submission_df['Predicted'] = y_test.argmax(axis=1)
 print(submission_df.shape)
 submission_df.head(3 )
<define_variables><EOS> 
submission_df.to_csv("submission.csv", index=False )