Last active
May 28, 2018 14:40
-
-
Save RomanSteinberg/8ee44a491fcb5e4f8a09f83356f9a173 to your computer and use it in GitHub Desktop.
CPU and GPU Tensorflow comparisons
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
# -*- coding: utf-8 -*- | |
import numpy as np | |
from glob import glob | |
import tensorflow.contrib.keras as K | |
from skimage.util import view_as_windows, pad | |
resnet = K.applications.resnet50 | |
image = K.preprocessing.image | |
def windows_count(image_side, window_side, stride): | |
"""Counts the number of windows, that image will be divided into. | |
Args: | |
image_side (int): length of an image side | |
window_side (int): length of a window side | |
stride (int): controls how the filter convolves around the input volume | |
Returns (int): | |
Number of windows. | |
""" | |
return (image_side - window_side) // stride + 1 | |
def border_size(image_side, window_side, stride): | |
"""Counts the size of required border for the correct image-to-windows conversion. | |
Args: | |
image_side (int): length of an image side | |
window_side (int): length of a window side | |
stride (int): controls how the filter convolves around the input volume | |
Returns (int): | |
Length of required border. | |
""" | |
windows = windows_count(image_side, window_side, stride) | |
cropped_side = (windows - 1) * stride + window_side | |
return 0 if cropped_side == image_side else windows * stride + window_side - image_side | |
def image_to_windows(img, window_shape, stride, resize_percent): | |
"""Divides image into multiple windows and prepares for the next resnet-usage. | |
Args: | |
img (image): image for preprocessing | |
window_shape ((int, int)): shape of one window | |
stride (int): controls how the filter convolves around the input volume | |
resize_percent (int): percentage of image compression | |
Returns (list): | |
List containing all windows, in which the source image was divided. | |
""" | |
resized = img.resize((round(img.size[0] * resize_percent), | |
round(img.size[1] * resize_percent))) | |
arr = image.img_to_array(resized) | |
border_h = border_size(arr.shape[0], window_shape[0], stride) | |
border_w = border_size(arr.shape[1], window_shape[1], stride) | |
bordered = pad(arr, ((0, border_h), (0, border_w), (0, 0)), mode='constant', constant_values=255) | |
wins = view_as_windows(bordered, (window_shape[0], window_shape[1], 3), stride) | |
wins = wins.reshape(wins.shape[0] * wins.shape[1] * wins.shape[2], | |
window_shape[0], window_shape[1], 3) | |
ready = resnet.preprocess_input(wins) | |
return ready | |
def load_pool(img_folder): | |
resnet_input_size = 224 | |
config = {'window_shape': (resnet_input_size, resnet_input_size), | |
'stride': resnet_input_size // 2, 'resize_percent': .5} | |
pool = [] | |
for fn in glob(img_folder + '*.jpg'): | |
img = image.load_img(fn) | |
pool.append(image_to_windows(img, config['window_shape'], config['stride'], config['resize_percent'])) | |
pool = np.concatenate(pool) | |
return pool |
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from time import clock | |
import tensorflow as tf | |
import tensorflow.contrib.keras as K | |
image = K.preprocessing.image | |
resnet = K.applications.resnet50 | |
from img_helpers import load_pool | |
def test(cpu=True): | |
# initialize Keras with mkl support | |
if cpu: | |
cfg = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1, | |
allow_soft_placement=True, device_count={'CPU': 1}) | |
session = tf.Session(config=cfg) | |
K.backend.set_session(session) | |
# initialize model | |
model_settings = {'include_top': False, 'weights': 'imagenet', 'pooling': 'max'} | |
model = resnet.ResNet50(**model_settings) | |
# initialize pool of images | |
pool = load_pool('./data/') | |
start = clock() | |
features = model.predict(pool, batch_size=4) | |
delta = clock() - start | |
print('Total time %10.2f, images %d, average speed %10.3f' % (delta, len(pool), delta / len(pool))) | |
if __name__ == '__main__': | |
test(False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment