Skip to content

Commit

Permalink
Added training utilities, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent Sommer committed May 23, 2017
1 parent 4d66d3a commit 4a48f0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
10 changes: 4 additions & 6 deletions evaluate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import cv2
import os


# If you want to use a GPU set its index here
os.environ['CUDA_VISIBLE_DEVICES'] = ''

def preprocess_input(x):
x = np.divide(x, 255.0)
x = np.subtract(x, 1.0)
x = np.multiply(x, 2.0)
return x

# This function comes from Google's ImageNet Preprocessing Script
def central_crop(image, central_fraction):
Expand Down Expand Up @@ -49,12 +46,13 @@ def central_crop(image, central_fraction):
image = image[bbox_h_start:bbox_h_start+bbox_h_size, bbox_w_start:bbox_w_start+bbox_w_size]
return image


def get_processed_image(img_path):
# Load image and convert from BGR to RGB
im = np.asarray(cv2.imread(img_path))[:,:,::-1]
im = central_crop(im, 0.875)
im = cv2.resize(im, (299, 299))
im = preprocess_input(im)
im = inception_v4.preprocess_input(im)
if K.image_data_format() == "channels_first":
im = np.transpose(im, (2,0,1))
im = im.reshape(-1,3,299,299)
Expand Down
26 changes: 14 additions & 12 deletions inception_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from keras.layers import Input, Dropout, Dense, Flatten, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.merge import concatenate
from keras import regularizers
from keras import initializers
from keras.models import Model
# Backend
from keras import backend as K
Expand All @@ -19,16 +21,17 @@
# Implements the Inception Network v4 (http://arxiv.org/pdf/1602.07261v1.pdf) in Keras. #
#########################################################################################

WEIGHTS_PATH = 'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5'


def preprocess_input(x):
x = np.divide(x, 255.0)
x = np.subtract(x, 1.0)
x = np.subtract(x, 0.5)
x = np.multiply(x, 2.0)
return x


WEIGHTS_PATH = 'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5'

def conv2d_bn(x, nb_filter, num_row, num_col,
padding='same', strides=(1, 1), use_bias=False):
"""
Expand All @@ -42,11 +45,14 @@ def conv2d_bn(x, nb_filter, num_row, num_col,
x = Convolution2D(nb_filter, (num_row, num_col),
strides=strides,
padding=padding,
use_bias=use_bias)(x)
x = BatchNormalization(axis=channel_axis, scale=False)(x)
use_bias=use_bias,
kernel_regularizer=regularizers.l2(0.00004),
kernel_initializer=initializers.VarianceScaling(scale=2.0, mode='fan_in', distribution='normal', seed=None))(x)
x = BatchNormalization(axis=channel_axis, momentum=0.9997, scale=False)(x)
x = Activation('relu')(x)
return x


def block_inception_a(input):
if K.image_data_format() == 'channels_first':
channel_axis = 1
Expand Down Expand Up @@ -241,7 +247,6 @@ def inception_v4(num_classes, dropout_keep_prob, weights, include_top):


# Final pooling and prediction

if include_top:
# 1 x 1 x 1536
x = AveragePooling2D((8,8), padding='valid')(x)
Expand Down Expand Up @@ -277,11 +282,8 @@ def inception_v4(num_classes, dropout_keep_prob, weights, include_top):
cache_subdir='models',
md5_hash='9296b46b5971573064d12e4669110969')
model.load_weights(weights_path, by_name=True)
if K.backend() == 'theano':
warnings.warn('The Theano backend is not currently supported for '
'this model on Keras 2. Please use the TensorFlow '
'backend or you will get bad results!')
return model


def create_model(num_classes=1001, dropout_prob=0.2, weights=None, include_top=True):
return inception_v4(num_classes, dropout_prob, weights, include_top)
return inception_v4(num_classes, dropout_prob, weights, include_top)

0 comments on commit 4a48f0d

Please sign in to comment.