In [1]:
INPUT_DIR = '../../output/kaggle-sea-lion/02/'
OUTPUT_DIR = '../../output/kaggle-sea-lion/03/'
IMAGE_DIMS = (148,148,3)
MODEL_FILE = OUTPUT_DIR + 'sea-lion-classifier'
In [2]:
%matplotlib inline
import numpy as np
import pandas as pd
import h5py
import matplotlib.pyplot as plt
import sklearn
import os
import glob
from modules.logging import logger
import modules.utils as utils
from modules.utils import Timer
import modules.logging
import modules.cnn as cnn
import modules.lions as lions
In [3]:
utils.mkdirs(OUTPUT_DIR, recreate=False)
modules.logging.setup_file_logger(OUTPUT_DIR + 'out.log')
logger.info('Dir ' + OUTPUT_DIR + ' created')
load_model_file = None
if(os.path.isfile(MODEL_FILE + '.index')):
load_model_file = MODEL_FILE
logger.info('Found previous model file. It will be loaded on cnn network as its initial state. ' + load_model_file)
In [4]:
logger.info('Prepare CNN for training')
network = lions.convnet_alexnet_lion(IMAGE_DIMS)
model = cnn.prepare_cnn_model(network, OUTPUT_DIR, model_file=load_model_file)
In [5]:
dataset_path = INPUT_DIR + utils.dataset_name('lion-patches', IMAGE_DIMS)
with h5py.File(dataset_path, 'r') as hdf5:
X,Y = utils.dataset_xy_range(hdf5, 0, 0.9)
logger.info('X shape ' + str(X.shape))
logger.info('Y shape ' + str(Y.shape))
logger.info('Starting CNN training...')
model.fit(X, Y,
validation_set=0.2,
shuffle=True,
batch_size=96,
n_epoch=10,
show_metric=True,
snapshot_epoch=False,
run_id='sea_lion_classifier')
model.save(MODEL_FILE)
logger.info("Network trained and saved as " + MODEL_FILE)
In [5]:
logger.info('Evaluate dataset')
dataset_path = INPUT_DIR + utils.dataset_name('lion-patches', IMAGE_DIMS)
with h5py.File(dataset_path, 'r') as hdf5:
X,Y = utils.dataset_xy_range(hdf5, 0.9, 1)
cnn.evaluate_dataset(X, Y, model, batch_size=24, detailed=True)
In [ ]:
In [ ]:
In [ ]:
In [ ]: