Created
March 16, 2017 08:02
-
-
Save kentsommer/5403f78a9908efa30975b41215ac1eea to your computer and use it in GitHub Desktop.
Preprocess for kaggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
import numpy as np | |
from glob import glob | |
from tqdm import tqdm | |
from keras.preprocessing import image | |
from keras.applications.inception_v3 import preprocess_input | |
def preprocess_input(x): | |
x = np.divide(x, 255.0) | |
x = np.subtract(x, 0.5) | |
x = np.multiply(x, 2.0) | |
return x | |
def central_crop(image, central_fraction): | |
"""Crop the central region of the image. | |
Remove the outer parts of an image but retain the central region of the image | |
along each dimension. If we specify central_fraction = 0.5, this function | |
returns the region marked with "X" in the below diagram. | |
-------- | |
| | | |
| XXXX | | |
| XXXX | | |
| | where "X" is the central 50% of the image. | |
-------- | |
Args: | |
image: 3-D array of shape [height, width, depth] | |
central_fraction: float (0, 1], fraction of size to crop | |
Raises: | |
ValueError: if central_crop_fraction is not within (0, 1]. | |
Returns: | |
3-D array | |
""" | |
if central_fraction <= 0.0 or central_fraction > 1.0: | |
raise ValueError('central_fraction must be within (0, 1]') | |
if central_fraction == 1.0: | |
return image | |
img_shape = image.shape | |
depth = img_shape[2] | |
fraction_offset = int(1 / ((1 - central_fraction) / 2.0)) | |
bbox_h_start = np.divide(img_shape[0], fraction_offset) | |
bbox_w_start = np.divide(img_shape[1], fraction_offset) | |
bbox_h_size = img_shape[0] - bbox_h_start * 2 | |
bbox_w_size = img_shape[1] - bbox_w_start * 2 | |
image = image[bbox_h_start:bbox_h_start+bbox_h_size, bbox_w_start:bbox_w_start+bbox_w_size] | |
return image | |
def path_to_label(path): | |
current = np.zeros((3)) | |
if "Type_1" in path: | |
current[0] = 1 | |
return current | |
if "Type_2" in path: | |
current[1] = 1 | |
return current | |
if "Type_3" in path: | |
current[2] = 1 | |
return current | |
def get_processed_image(img_path): | |
# Load image and convert from BGR to RGB | |
im = cv2.imread(img_path) | |
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) | |
im = central_crop(im, 0.875) | |
im = cv2.resize(im, (299, 299)) | |
im = np.asarray(im) | |
im = preprocess_input(im) | |
return im | |
def get_training_data(PATH=os.getcwd() + '/train'): | |
images = [] | |
labels = [] | |
for x in tqdm(os.walk(PATH)): | |
for cpath in tqdm(glob(os.path.join(x[0], '*.jpg'))): | |
current_label = path_to_label(cpath) | |
current_image = get_processed_image(cpath) | |
images.append(current_image) | |
labels.append(current_label) | |
# print(current_image.shape) | |
return np.asarray(images), np.asarray(labels) | |
if __name__ == '__main__': | |
[images, labels] = get_training_data() | |
print(images.shape) | |
print(labels.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment