In [1]:
INPUT_DIR = '../../input/kaggle-sea-lion/02/'
INPUT_DIR2 = '../../input/kaggle-sea-lion/Train/'
OUTPUT_DIR = '../../output/kaggle-sea-lion/04/'
# IMAGE_DIMS = (42,42,3)
#IMAGE_DIMS = (84,84,3)
#IMAGE_DIMS = (32,32,3)
IMAGE_DIMS = (50,50,3)
INPUT_DATASET_NAME = 'lion-patches-0px'
RECREATE_OUTPUT_DIR = False
OUTPUT_WEIGHT = (1,1,1,1,1,5)
INPUT_RANGE = 1
BATCH_SIZE=64
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
import cv2
from sklearn import svm
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
import sklearn.metrics as metrics
import sklearn.cluster as cluster
import skimage
import random
import itertools
from modules.chog import CircularHOGExtractor
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras import models
from modules.logging import logger
import modules.utils as utils
from modules.utils import Timer
import modules.logging
import modules.lions as lions
import modules.objectdetect as objectdetect
In [3]:
utils.mkdirs(OUTPUT_DIR, dirs=['weights'], recreate=RECREATE_OUTPUT_DIR)
modules.logging.setup_file_logger(OUTPUT_DIR + 'out.log')
WEIGHTS_DIR = OUTPUT_DIR + 'weights/'
input_dataset_path = INPUT_DIR + utils.dataset_name(INPUT_DATASET_NAME, IMAGE_DIMS)
logger.info('Output dirs created')
In [4]:
logger.info('Using dataset ' + input_dataset_path + ' as input')
h5file = h5py.File(input_dataset_path, 'r')
logger.info('Using dataset ' + input_dataset_path + ' as input')
h5file = h5py.File(input_dataset_path, 'r')
#used for image augmentation (creating new images for balancing)
image_augmentation_generator = ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=359,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0,
horizontal_flip=True,
vertical_flip=True)
image_randomize_generator = image_augmentation_generator
logger.info('preparing train data')
train_batch_generator = utils.BatchGeneratorXYH5(h5file, start_ratio=0, end_ratio=INPUT_RANGE)
train_balance_generator = utils.ClassBalancerGeneratorXY(train_batch_generator,
image_augmentation=image_augmentation_generator,
output_weight=OUTPUT_WEIGHT,
max_augmentation_ratio=5,
max_undersampling_ratio=1,
enforce_max_ratios=False,
batch_size=BATCH_SIZE,
start_ratio=0, end_ratio=0.7)
train_generator = utils.image_augmentation_xy(train_balance_generator.flow(), image_randomize_generator)
logger.info('train size=' + str(train_balance_generator.size) + ' batches=' + str(train_balance_generator.nr_batches))
logger.info('preparing valid data')
valid_batch_generator = utils.BatchGeneratorXYH5(h5file, start_ratio=0, end_ratio=INPUT_RANGE)
valid_balance_generator = utils.ClassBalancerGeneratorXY(valid_batch_generator,
image_augmentation=image_augmentation_generator,
output_weight=OUTPUT_WEIGHT,
max_augmentation_ratio=5,
max_undersampling_ratio=1,
enforce_max_ratios=False,
batch_size=BATCH_SIZE,
start_ratio=0.7, end_ratio=0.85)
logger.info('valid size=' + str(valid_balance_generator.size) + ' batches=' + str(valid_balance_generator.nr_batches))
logger.info('preparing test data')
test_batch_generator = utils.BatchGeneratorXYH5(h5file, start_ratio=0, end_ratio=INPUT_RANGE)
test_balance_generator = utils.ClassBalancerGeneratorXY(test_batch_generator,
image_augmentation=image_augmentation_generator,
output_weight=OUTPUT_WEIGHT,
max_augmentation_ratio=5,
max_undersampling_ratio=1,
enforce_max_ratios=False,
batch_size=BATCH_SIZE,
start_ratio=0.85, end_ratio=1)
logger.info('test size=' + str(test_balance_generator.size) + ' batches=' + str(test_balance_generator.nr_batches))
In [5]:
logger.info('Loading train/valid data...')
def validate_sample(x,y):
#remove images with too much zeroed pixels
hist = cv2.calcHist([cv2.cvtColor(x,cv2.COLOR_BGR2GRAY).astype('u1')],[0],None,[256],[0,256])
return (hist[0][0]<200)
X_train, Y_train = utils.dump_xy_to_array(train_generator, train_balance_generator.size, x=True, y=True, filter_function=validate_sample)
X_valid, Y_valid = utils.dump_xy_to_array(valid_balance_generator.flow(), valid_balance_generator.size, x=True, y=True, filter_function=validate_sample)
X_test, Y_test = utils.dump_xy_to_array(test_balance_generator.flow(), test_balance_generator.size, x=True, y=True, filter_function=validate_sample)
Y_train = utils.onehot_to_label(Y_train)
Y_valid = utils.onehot_to_label(Y_valid)
Y_test = utils.onehot_to_label(Y_test)
print(X_train.shape)
print(Y_train.shape)
test_img = cv2.cvtColor(X_train[0],cv2.COLOR_BGR2GRAY).astype('u1')
test_img2 = cv2.cvtColor(X_train[1],cv2.COLOR_BGR2GRAY).astype('u1')
In [6]:
logger.info('Extracting chog for train...')
ch = CircularHOGExtractor()
X_chog_train = np.array([ch.extract(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY).astype('u1')) for im in X_train])
logger.info('Extracting chog for validation...')
X_chog_valid = np.array([ch.extract(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY).astype('u1')) for im in X_valid])
logger.info('Extracting chog for test...')
X_chog_test = np.array([ch.extract(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY).astype('u1')) for im in X_test])
logger.info('done')
In [7]:
#BATCH_SIZE = 10
#for batch in range(round(train_balance_generator.size/BATCH_SIZE)):
# logger.info('Batch ' + str(batch) + '...')
# X_train, Y_train = utils.dump_xy_to_array(train_generator, BATCH_SIZE, x=True, y=True)
#X = utils.extract_hogs(X_train)
#Y_true = utils.onehot_to_label(Y_train)
#Y_true = np.array([l if l==5 else 0 for l in Y_true])
logger.info('Training SVM...')
#ovr = OneVsRestClassifier(LinearSVC(random_state=0))
ovr = OneVsRestClassifier(SVC(probability=True))
#train just on two classes: 1-lion or 0-non-lion
Y_train_2 = np.array([0 if x==5 else 1 for x in Y_train])
Y_valid_2 = np.array([0 if x==5 else 1 for x in Y_valid])
Y_test_2 = np.array([0 if x==5 else 1 for x in Y_test])
svm_model = ovr.fit(X_chog_train, Y_train_2)
Y_train_pred = svm_model.predict(X_chog_train)
acc = metrics.accuracy_score(Y_train_2, Y_train_pred)
logger.info('Train accuracy: ' + str(acc))
Y_valid_pred = svm_model.predict(X_chog_valid)
acc = metrics.accuracy_score(Y_valid_2, Y_valid_pred)
logger.info('Validation accuracy: ' + str(acc))
Y_test_pred = svm_model.predict(X_chog_test)
Y_test_prob = svm_model.predict_proba(X_chog_test)
acc = metrics.accuracy_score(Y_test_2, Y_test_pred)
logger.info('Test accuracy: ' + str(acc))
logger.debug("Done")
utils.evaluate_predictions(Y_valid_2, Y_valid_pred)
In [8]:
Y_test_pred
Y_test_true0 = utils.onehot_to_label(Y_test_2)
il = []
for i,yp in enumerate(Y_test_pred):
il.append(str(yp) + '/' + str(Y_test_true0[i]))
utils.show_images(X_test[0:10], image_labels=il, cols=10, size=2)
In [9]:
import numpy as np
import skimage.feature as feature
import skimage.transform as transform
from sklearn.feature_extraction import image
from itertools import permutations
img_path = INPUT_DIR2 + '47.jpg'
img = cv2.imread(img_path)
#img = img[1800:4000,2000:3300]
#img = img[3000:4000,2500:3500]
#img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY).astype('u1')
def eval_region(region_img):
try:
region_chog = [ch.extract(cv2.cvtColor(region_img,cv2.COLOR_BGR2GRAY).astype('u1'))]
score = svm_model.predict_proba(region_chog)
except:
return 0
return score[0][1] * 255
img2 = img.copy()
print('preparing blur')
img2 = cv2.GaussianBlur(img2,(5,5),3)
region_generator = objectdetect.sliding_window_generator(img2, step=(30,30), window=IMAGE_DIMS, pyramid_max_layers=1)
good_regions, imgs = objectdetect.evaluate_regions(region_generator, eval_region, score_threshold=180, apply_non_max_suppression=True, supression_overlap_threshold=0.3, threads=None)
print('regions found:')
print(len(good_regions))
patches = []
for i, region in enumerate(good_regions):
score = int(good_regions[i][4])
p = img[region[0]:region[0]+region[2],region[1]:region[1]+region[3]]
patches.append(p)
cv2.rectangle(img2, (region[1],region[0]), (region[3]+region[1],region[2]+region[0]), color=(score,score,score), thickness=2)
utils.show_image(img2, size=40, is_bgr=True)
utils.show_images(patches[100:160], size=2, cols=10)