Style fixes (#71)

This commit is contained in:
nzw
2017-04-16 01:41:28 +09:00
committed by Michael Oliver
parent 2afcddffd8
commit 88c60d2459
20 changed files with 93 additions and 139 deletions
+10 -11
View File
@@ -8,7 +8,6 @@ from __future__ import print_function
from __future__ import division
import numpy as np
import sklearn.metrics as metrics
from keras import backend as K
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
@@ -20,13 +19,13 @@ from keras_contrib.applications import DenseNet
batch_size = 64
nb_classes = 10
nb_epoch = 100
epochs = 100
img_rows, img_cols = 32, 32
img_channels = 3
# Parameters for the DenseNet model builder
img_dim = (img_channels, img_rows, img_cols) if K.image_data_format() == "channels_first" else (img_rows, img_cols, img_channels)
img_dim = (img_channels, img_rows, img_cols) if K.image_data_format() == 'channels_first' else (img_rows, img_cols, img_channels)
depth = 40
nb_dense_block = 3
growth_rate = 12
@@ -36,13 +35,13 @@ dropout_rate = 0.0 # 0.0 for data augmentation
# Create the model (without loading weights)
model = DenseNet(depth, nb_dense_block, growth_rate, nb_filter, dropout_rate=dropout_rate,
input_shape=img_dim, weights=None)
print("Model created")
print('Model created')
model.summary()
optimizer = Adam(lr=1e-3) # Using Adam instead of SGD to speed up training
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=["accuracy"])
print("Finished compiling")
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
print('Finished compiling')
(trainX, trainY), (testX, testY) = cifar10.load_data()
@@ -61,22 +60,22 @@ generator = ImageDataGenerator(rotation_range=15,
generator.fit(trainX, seed=0)
weights_file = "DenseNet-40-12-CIFAR-10.h5"
weights_file = 'DenseNet-40-12-CIFAR-10.h5'
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1),
cooldown=0, patience=10, min_lr=0.5e-6)
early_stopper = EarlyStopping(monitor='val_acc', min_delta=1e-4, patience=20)
model_checkpoint = ModelCheckpoint(weights_file, monitor="val_acc", save_best_only=True,
model_checkpoint = ModelCheckpoint(weights_file, monitor='val_acc', save_best_only=True,
save_weights_only=True, mode='auto')
callbacks = [lr_reducer, early_stopper, model_checkpoint]
model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size,
epochs=nb_epoch,
epochs=epochs,
callbacks=callbacks,
validation_data=(testX, Y_test),
verbose=2)
scores = model.evaluate(testX, Y_test, batch_size=batch_size)
print("Test loss : ", scores[0])
print("Test accuracy : ", scores[1])
print('Test loss : ', scores[0])
print('Test accuracy : ', scores[1])
+7 -9
View File
@@ -3,8 +3,6 @@ Trains a Residual-of-Residual Network (WRN-40-2) model on the CIFAR-10 Dataset.
Gets a 94.53% accuracy score after 150 epochs.
'''
import numpy as np
import sklearn.metrics as metrics
import keras.callbacks as callbacks
import keras.utils.np_utils as kutils
@@ -15,7 +13,7 @@ from keras.optimizers import Adam
from keras_contrib.applications import ResidualOfResidual
batch_size = 64
nb_epoch = 150
epochs = 150
img_rows, img_cols = 32, 32
(trainX, trainY), (testX, testY) = cifar10.load_data()
@@ -40,16 +38,16 @@ model = ResidualOfResidual(depth=40, width=2, dropout_rate=0.0, weights=None)
optimizer = Adam(lr=1e-3)
model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["acc"])
print("Finished compiling")
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
print('Finished compiling')
model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size,
epochs=nb_epoch,
callbacks=[callbacks.ModelCheckpoint("weights/RoR-WRN-40-2-Weights.h5", monitor="val_acc",
epochs=epochs,
callbacks=[callbacks.ModelCheckpoint('weights/RoR-WRN-40-2-Weights.h5', monitor='val_acc',
save_best_only=True, save_weights_only=True)],
validation_data=(testX, testY),
verbose=2)
scores = model.evaluate(testX, testY, batch_size)
print("Test loss : ", scores[0])
print("Test accuracy : ", scores[1])
print('Test loss : ', scores[0])
print('Test accuracy : ', scores[1])
+7 -7
View File
@@ -18,7 +18,7 @@ from keras.preprocessing.image import ImageDataGenerator
from keras_contrib.applications.wide_resnet import WideResidualNetwork
batch_size = 64
nb_epoch = 300
epochs = 300
img_rows, img_cols = 32, 32
(trainX, trainY), (testX, testY) = cifar10.load_data()
@@ -44,16 +44,16 @@ model = WideResidualNetwork(depth=28, width=8, dropout_rate=0.0, weights=None)
model.summary()
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
print("Finished compiling")
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
print('Finished compiling')
model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size,
epochs=nb_epoch,
epochs=epochs,
callbacks=[
callbacks.ModelCheckpoint("WRN-28-8 Weights.h5", monitor="val_acc", save_best_only=True,
callbacks.ModelCheckpoint('WRN-28-8 Weights.h5', monitor='val_acc', save_best_only=True,
save_weights_only=True)],
validation_data=(testX, testY))
scores = model.evaluate(testX, testY, batch_size)
print("Test loss : %0.5f" % (scores[0]))
print("Test accuracy = %0.5f" % (scores[1]))
print('Test loss : %0.5f' % (scores[0]))
print('Test accuracy = %0.5f' % (scores[1]))
+24 -24
View File
@@ -14,11 +14,11 @@ import warnings
from keras.models import Model
from keras.layers.core import Dense, Dropout, Activation, Reshape
from keras.layers import Convolution2D, Deconvolution2D, AtrousConvolution2D, UpSampling2D
from keras.layers import Deconvolution2D, AtrousConvolution2D, UpSampling2D
from keras.layers.merge import concatenate
from keras.layers.pooling import AveragePooling2D
from keras.layers.pooling import GlobalAveragePooling2D
from keras.layers import Input, merge, Conv2D
from keras.layers import Input, Conv2D
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras.utils.layer_utils import convert_all_kernels_in_model
@@ -295,7 +295,7 @@ def __conv_block(ip, nb_filter, bottleneck=False, dropout_rate=None, weight_deca
Returns: keras tensor with batch_norm, relu and convolution2d added (optional bottleneck)
'''
concat_axis = 1 if K.image_dim_ordering() == "th" else -1
concat_axis = 1 if K.image_dim_ordering() == 'th' else -1
x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(ip)
@@ -314,7 +314,7 @@ def __conv_block(ip, nb_filter, bottleneck=False, dropout_rate=None, weight_deca
beta_regularizer=l2(weight_decay))(x)
x = Activation('relu')(x)
x = Conv2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", use_bias=False,
x = Conv2D(nb_filter, (3, 3), kernel_initializer='he_uniform', padding='same', use_bias=False,
kernel_regularizer=l2(weight_decay))(x)
if dropout_rate:
x = Dropout(dropout_rate)(x)
@@ -336,12 +336,12 @@ def __transition_block(ip, nb_filter, compression=1.0, dropout_rate=None, weight
Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool
'''
concat_axis = 1 if K.image_dim_ordering() == "th" else -1
concat_axis = 1 if K.image_dim_ordering() == 'th' else -1
x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(ip)
x = Activation('relu')(x)
x = Conv2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", use_bias=False,
x = Conv2D(nb_filter, (3, 3), kernel_initializer='he_uniform', padding='same', use_bias=False,
kernel_regularizer=l2(weight_decay))(x)
if dropout_rate:
x = Dropout(dropout_rate)(x)
@@ -368,7 +368,7 @@ def __dense_block(x, nb_layers, nb_filter, growth_rate, bottleneck=False, dropou
Returns: keras tensor with nb_layers of conv_block appended
'''
concat_axis = 1 if K.image_dim_ordering() == "th" else -1
concat_axis = 1 if K.image_dim_ordering() == 'th' else -1
x_list = [x]
@@ -403,14 +403,14 @@ def __transition_up_block(ip, nb_filters, type='upsampling', output_shape=None,
if type == 'upsampling':
x = UpSampling2D()(ip)
elif type == 'subpixel':
x = Conv2D(nb_filters, (3, 3), padding="same", kernel_regularizer=l2(weight_decay), activation='relu',
x = Conv2D(nb_filters, (3, 3), padding='same', kernel_regularizer=l2(weight_decay), activation='relu',
use_bias=False, kernel_initializer='he_uniform')(ip)
x = SubPixelUpscaling(scale_factor=2)(x)
x = Conv2D(nb_filters, (3, 3), activation="relu", padding='same', kernel_regularizer=l2(weight_decay),
x = Conv2D(nb_filters, (3, 3), activation='relu', padding='same', kernel_regularizer=l2(weight_decay),
use_bias=False, kernel_initializer='he_uniform')(x)
elif type == 'atrous':
# waiting on https://github.com/fchollet/keras/issues/4018
x = AtrousConvolution2D(nb_filters, 3, 3, activation="relu", W_regularizer=l2(weight_decay),
x = AtrousConvolution2D(nb_filters, 3, 3, activation='relu', W_regularizer=l2(weight_decay),
bias=False, atrous_rate=(2, 2), init='he_uniform')(ip)
else:
x = Deconvolution2D(nb_filters, 3, 3, output_shape, activation='relu', border_mode='same',
@@ -445,18 +445,18 @@ def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_bl
Returns: keras tensor with nb_layers of conv_block appended
'''
concat_axis = 1 if K.image_dim_ordering() == "th" else -1
concat_axis = 1 if K.image_dim_ordering() == 'th' else -1
assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"
assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4'
if reduction != 0.0:
assert reduction <= 1.0 and reduction > 0.0, "reduction value must lie between 0.0 and 1.0"
assert reduction <= 1.0 and reduction > 0.0, 'reduction value must lie between 0.0 and 1.0'
# layers in each dense block
if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
nb_layers = list(nb_layers_per_block) # Convert tuple to list
assert len(nb_layers) == (nb_dense_block + 1), "If list, nb_layer is used as provided. " \
"Note that list size must be (nb_dense_block + 1)"
assert len(nb_layers) == (nb_dense_block + 1), 'If list, nb_layer is used as provided. ' \
'Note that list size must be (nb_dense_block + 1)'
final_nb_layer = nb_layers[-1]
nb_layers = nb_layers[:-1]
else:
@@ -479,7 +479,7 @@ def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_bl
compression = 1.0 - reduction
# Initial convolution
x = Conv2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", name="initial_conv2D", use_bias=False,
x = Conv2D(nb_filter, (3, 3), kernel_initializer='he_uniform', padding='same', name='initial_conv2D', use_bias=False,
kernel_regularizer=l2(weight_decay))(img_input)
# Add dense blocks
@@ -538,7 +538,7 @@ def __create_fcn_dense_net(nb_classes, img_input, include_top, nb_dense_block=5,
Returns: keras tensor with nb_layers of conv_block appended
'''
concat_axis = 1 if K.image_dim_ordering() == "th" else -1
concat_axis = 1 if K.image_dim_ordering() == 'th' else -1
if concat_axis == 1: # th dim ordering
_, rows, cols = input_shape
@@ -546,20 +546,20 @@ def __create_fcn_dense_net(nb_classes, img_input, include_top, nb_dense_block=5,
rows, cols, _ = input_shape
if reduction != 0.0:
assert reduction <= 1.0 and reduction > 0.0, "reduction value must lie between 0.0 and 1.0"
assert reduction <= 1.0 and reduction > 0.0, 'reduction value must lie between 0.0 and 1.0'
# check if upsampling_conv has minimum number of filters
# minimum is set to 12, as at least 3 color channels are needed for correct upsampling
assert nb_upsampling_conv > 12 and nb_upsampling_conv % 4 == 0, "Parameter `upsampling_conv` number of channels must " \
"be a positive number divisible by 4 and greater " \
"than 12"
assert nb_upsampling_conv > 12 and nb_upsampling_conv % 4 == 0, 'Parameter `upsampling_conv` number of channels must ' \
'be a positive number divisible by 4 and greater ' \
'than 12'
# layers in each dense block
if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
nb_layers = list(nb_layers_per_block) # Convert tuple to list
assert len(nb_layers) == (nb_dense_block + 1), "If list, nb_layer is used as provided. " \
"Note that list size must be (nb_dense_block + 1)"
assert len(nb_layers) == (nb_dense_block + 1), 'If list, nb_layer is used as provided. ' \
'Note that list size must be (nb_dense_block + 1)'
bottleneck_nb_layers = nb_layers[-1]
rev_layers = nb_layers[::-1]
@@ -572,7 +572,7 @@ def __create_fcn_dense_net(nb_classes, img_input, include_top, nb_dense_block=5,
compression = 1.0 - reduction
# Initial convolution
x = Conv2D(init_conv_filters, (3, 3), kernel_initializer="he_uniform", padding="same", name="initial_conv2D",
x = Conv2D(init_conv_filters, (3, 3), kernel_initializer='he_uniform', padding='same', name='initial_conv2D',
use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)
nb_filter = init_conv_filters
+5 -5
View File
@@ -158,11 +158,11 @@ def ResidualOfResidual(depth=40, width=2, dropout_rate=0.0,
def __initial_conv_block(input, k=1, dropout=0.0, initial=False):
init = input
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Check if input number of filters is same as 16 * k, else create convolution2d for this input
if initial:
if K.image_dim_ordering() == "th":
if K.image_dim_ordering() == 'th':
init = Conv2D(16 * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
else:
init = Conv2D(16 * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
@@ -185,10 +185,10 @@ def __initial_conv_block(input, k=1, dropout=0.0, initial=False):
def __conv_block(input, nb_filters=32, k=1, dropout=0.0):
init = input
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Check if input number of filters is same as 32 * k, else create convolution2d for this input
if K.image_dim_ordering() == "th":
if K.image_dim_ordering() == 'th':
if init._keras_shape[1] != nb_filters * k:
init = Conv2D(nb_filters * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
else:
@@ -234,7 +234,7 @@ def __create_pre_residual_of_residual(nb_classes, img_input, include_top, depth=
N = (depth - 4) // 6
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Initial convolution layer
x = Conv2D(16, (3, 3), padding='same', kernel_initializer='he_normal')(img_input)
+8 -8
View File
@@ -15,7 +15,7 @@ import warnings
from keras.models import Model
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.pooling import AveragePooling2D, MaxPooling2D
from keras.layers import Input, merge, Conv2D
from keras.layers import Input, Conv2D
from keras.layers.merge import add
from keras.layers.normalization import BatchNormalization
from keras.utils.layer_utils import convert_all_kernels_in_model
@@ -159,7 +159,7 @@ def WideResidualNetwork(depth=28, width=8, dropout_rate=0.0,
def __conv1_block(input):
x = Conv2D(16, (3, 3), padding='same')(input)
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
@@ -169,10 +169,10 @@ def __conv1_block(input):
def __conv2_block(input, k=1, dropout=0.0):
init = input
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Check if input number of filters is same as 16 * k, else create convolution2d for this input
if K.image_dim_ordering() == "th":
if K.image_dim_ordering() == 'th':
if init._keras_shape[1] != 16 * k:
init = Conv2D(16 * k, (1, 1), activation='linear', padding='same')(init)
else:
@@ -197,10 +197,10 @@ def __conv2_block(input, k=1, dropout=0.0):
def __conv3_block(input, k=1, dropout=0.0):
init = input
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Check if input number of filters is same as 32 * k, else create convolution2d for this input
if K.image_dim_ordering() == "th":
if K.image_dim_ordering() == 'th':
if init._keras_shape[1] != 32 * k:
init = Conv2D(32 * k, (1, 1), activation='linear', padding='same')(init)
else:
@@ -225,10 +225,10 @@ def __conv3_block(input, k=1, dropout=0.0):
def ___conv4_block(input, k=1, dropout=0.0):
init = input
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
channel_axis = 1 if K.image_dim_ordering() == 'th' else -1
# Check if input number of filters is same as 64 * k, else create convolution2d for this input
if K.image_dim_ordering() == "th":
if K.image_dim_ordering() == 'th':
if init._keras_shape[1] != 64 * k:
init = Conv2D(64 * k, (1, 1), activation='linear', padding='same')(init)
else:
+5 -14
View File
@@ -1,21 +1,12 @@
import tensorflow as tf
from tensorflow.python.training import moving_averages
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import control_flow_ops
try:
from tensorflow.python.ops import ctc_ops as ctc
except ImportError:
import tensorflow.contrib.ctc as ctc
from keras import backend as K
from keras.backend import tensorflow_backend as KTF
import numpy as np
import os
import warnings
from keras.backend.common import floatx, _EPSILON, image_data_format
from keras.backend.common import floatx, image_data_format
from keras.backend.tensorflow_backend import _preprocess_conv3d_input
from keras.backend.tensorflow_backend import _preprocess_conv3d_kernel
from keras.backend.tensorflow_backend import _preprocess_padding
from keras.backend.tensorflow_backend import _postprocess_conv3d_output
from keras.backend.tensorflow_backend import _preprocess_padding
from keras.backend.tensorflow_backend import _preprocess_conv2d_input
@@ -118,8 +109,8 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),
return _postprocess_conv3d_output(x, data_format)
def extract_image_patches(x, ksizes, ssizes, padding="same",
data_format="tf"):
def extract_image_patches(x, ksizes, ssizes, padding='same',
data_format='tf'):
'''
Extract the patches from an image
# Parameters
@@ -138,7 +129,7 @@ def extract_image_patches(x, ksizes, ssizes, padding="same",
kernel = [1, ksizes[0], ksizes[1], 1]
strides = [1, ssizes[0], ssizes[1], 1]
padding = _preprocess_padding(padding)
if data_format == "channels_first":
if data_format == 'channels_first':
x = KTF.permute_dimensions(x, (0, 2, 3, 1))
bs_i, w_i, h_i, ch_i = KTF.int_shape(x)
patches = tf.extract_image_patches(x, kernel, strides, [1, 1, 1, 1],
@@ -147,7 +138,7 @@ def extract_image_patches(x, ksizes, ssizes, padding="same",
bs, w, h, ch = KTF.int_shape(patches)
patches = tf.reshape(tf.transpose(tf.reshape(patches, [-1, w, h, tf.floordiv(ch, ch_i), ch_i]), [0, 1, 2, 4, 3]),
[-1, w, h, ch_i, ksizes[0], ksizes[1]])
if data_format == "channels_last":
if data_format == 'channels_last':
patches = KTF.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
return patches
+6 -15
View File
@@ -1,10 +1,5 @@
import theano
from theano import tensor as T
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
from theano.sandbox.neighbours import images2neibs
from theano.tensor.signal import pool
from theano.tensor.nnet import conv3d2d
from theano.printing import Print
try:
import theano.sparse as th_sparse_module
@@ -14,11 +9,8 @@ try:
from theano.tensor.nnet.nnet import softsign as T_softsign
except ImportError:
from theano.sandbox.softsign import softsign as T_softsign
from keras import backend as K
from keras.backend import theano_backend as KTH
import inspect
import numpy as np
from keras.backend.common import _FLOATX, floatx, _EPSILON, image_data_format
from keras.backend.common import image_data_format
from keras.backend.theano_backend import _preprocess_conv3d_input
from keras.backend.theano_backend import _preprocess_conv3d_kernel
from keras.backend.theano_backend import _preprocess_conv3d_filter_shape
@@ -27,7 +19,6 @@ from keras.backend.theano_backend import _postprocess_conv3d_output
from keras.backend.theano_backend import _preprocess_conv2d_input
from keras.backend.theano_backend import _postprocess_conv2d_output
import itertools
py_all = all
@@ -144,7 +135,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),
return conv_out
def extract_image_patches(X, ksizes, strides, padding="valid", data_format="channels_first"):
def extract_image_patches(X, ksizes, strides, padding='valid', data_format='channels_first'):
'''
Extract the patches from an image
Parameters
@@ -161,9 +152,9 @@ def extract_image_patches(X, ksizes, strides, padding="valid", data_format="chan
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
patch_size = ksizes[1]
if padding == "same":
padding = "ignore_borders"
if data_format == "channels_last":
if padding == 'same':
padding = 'ignore_borders'
if data_format == 'channels_last':
X = KTH.permute_dimensions(X, [0, 3, 1, 2])
# Thanks to https://github.com/awentzonline for the help!
batch, c, w, h = KTH.shape(X)
@@ -177,7 +168,7 @@ def extract_image_patches(X, ksizes, strides, padding="valid", data_format="chan
patches = KTH.permute_dimensions(patches, (0, 2, 1, 3, 4))
# arrange in a 2d-grid (rows, cols, channels, px, py)
patches = KTH.reshape(patches, (batch, num_rows, num_cols, num_channels, patch_size, patch_size))
if data_format == "channels_last":
if data_format == 'channels_last':
patches = KTH.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
return patches
+2 -13
View File
@@ -2,19 +2,8 @@ from __future__ import absolute_import
from __future__ import print_function
import os
import csv
import numpy as np
import time
import json
import warnings
from collections import deque
from collections import OrderedDict
from collections import Iterable
from keras.utils.generic_utils import Progbar
from keras import backend as K
from pkg_resources import parse_version
from keras.callbacks import Callback, ModelCheckpoint, LearningRateScheduler
@@ -47,7 +36,7 @@ class SnapshotModelCheckpoint(Callback):
def on_epoch_end(self, epoch, logs={}):
if epoch != 0 and (epoch + 1) % self.check == 0:
filepath = self.fn_prefix + "-%d.h5" % ((epoch + 1) // self.check)
filepath = self.fn_prefix + '-%d.h5' % ((epoch + 1) // self.check)
self.model.save_weights(filepath, overwrite=True)
# print("Saved snapshot at weights/%s_%d.h5" % (self.fn_prefix, epoch))
@@ -88,7 +77,7 @@ class SnapshotCallbackBuilder:
if not os.path.exists('weights/'):
os.makedirs('weights/')
callback_list = [ModelCheckpoint("weights/%s-Best.h5" % model_prefix, monitor="val_acc",
callback_list = [ModelCheckpoint('weights/%s-Best.h5' % model_prefix, monitor='val_acc',
save_best_only=True, save_weights_only=True),
LearningRateScheduler(schedule=self._cosine_anneal_schedule),
SnapshotModelCheckpoint(self.T, self.M, fn_prefix='weights/%s' % model_prefix)]
@@ -5,7 +5,6 @@ from keras.engine import Layer
from keras.engine import InputSpec
from .. import backend as K
from keras.utils.generic_utils import get_custom_objects
import numpy as np
class PELU(Layer):
+4 -5
View File
@@ -12,7 +12,6 @@ from keras.engine import InputSpec
from keras.layers.convolutional import Convolution3D
from keras.utils.generic_utils import get_custom_objects
from keras.utils.conv_utils import conv_output_length
from keras.utils.conv_utils import conv_input_length
from keras.utils.conv_utils import normalize_data_format
import numpy as np
@@ -227,8 +226,8 @@ class Deconvolution3D(Convolution3D):
Deconv3D = Deconvolution3D
get_custom_objects().update({"Deconvolution3D": Deconvolution3D})
get_custom_objects().update({"Deconv3D": Deconv3D})
get_custom_objects().update({'Deconvolution3D': Deconvolution3D})
get_custom_objects().update({'Deconv3D': Deconv3D})
class CosineConvolution2D(Layer):
@@ -454,8 +453,8 @@ class CosineConvolution2D(Layer):
CosineConv2D = CosineConvolution2D
get_custom_objects().update({"CosineConvolution2D": CosineConvolution2D})
get_custom_objects().update({"CosineConv2D": CosineConv2D})
get_custom_objects().update({'CosineConvolution2D': CosineConvolution2D})
get_custom_objects().update({'CosineConv2D': CosineConv2D})
class SubPixelUpscaling(Layer):
+1 -11
View File
@@ -2,14 +2,6 @@
from __future__ import absolute_import
from __future__ import division
import numpy as np
import keras
import copy
import inspect
import types as python_types
import warnings
from .. import backend as K
from .. import activations
from .. import initializers
@@ -17,8 +9,6 @@ from .. import regularizers
from .. import constraints
from keras.engine import InputSpec
from keras.engine import Layer
from keras.utils.generic_utils import func_dump
from keras.utils.generic_utils import func_load
from keras.utils.generic_utils import get_custom_objects
@@ -178,4 +168,4 @@ class CosineDense(Layer):
return dict(list(base_config.items()) + list(config.items()))
get_custom_objects().update({"CosineDense": CosineDense})
get_custom_objects().update({'CosineDense': CosineDense})
+4 -4
View File
@@ -15,7 +15,7 @@ class DSSIMObjective():
kernel_size: Size of the sliding window (default 3)
max_value: Max value of the output (default 1.0)
"""
self.__name__ = "DSSIMObjective"
self.__name__ = 'DSSIMObjective'
self.kernel_size = kernel_size
self.k1 = k1
self.k2 = k2
@@ -26,7 +26,7 @@ class DSSIMObjective():
self.backend = KC.backend()
def __int_shape(self, x):
return KC.int_shape(x) if self.backend == "tensorflow" else KC.shape(x)
return KC.int_shape(x) if self.backend == 'tensorflow' else KC.shape(x)
def __call__(self, y_true, y_pred):
# There are additional parameters for this function
@@ -36,8 +36,8 @@ class DSSIMObjective():
kernel = [self.kernel_size, self.kernel_size]
y_true = KC.reshape(y_true, [-1] + list(self.__int_shape(y_pred)[1:]))
y_pred = KC.reshape(y_pred, [-1] + list(self.__int_shape(y_pred)[1:]))
patches_pred = KC.extract_image_patches(y_pred, kernel, kernel, "valid", self.dim_ordering)
patches_true = KC.extract_image_patches(y_true, kernel, kernel, "valid", self.dim_ordering)
patches_pred = KC.extract_image_patches(y_pred, kernel, kernel, 'valid', self.dim_ordering)
patches_true = KC.extract_image_patches(y_true, kernel, kernel, 'valid', self.dim_ordering)
# Reshape to get the var in the cells
bs, w, h, c1, c2, c3 = self.__int_shape(patches_pred)
-1
View File
@@ -6,7 +6,6 @@ import six
from keras.engine import Model, Input
from keras.models import Sequential
from keras.models import model_from_json
from keras import backend as K
+4 -4
View File
@@ -82,8 +82,8 @@ class TestBackend(object):
strides = [kernel_shape, kernel_shape]
xth = KTH.variable(xval)
xtf = KTF.variable(xval)
ztf = KTF.eval(KCTF.extract_image_patches(xtf, kernel, strides, data_format='channels_first', padding="valid"))
zth = KTH.eval(KCTH.extract_image_patches(xth, kernel, strides, data_format='channels_first', padding="valid"))
ztf = KTF.eval(KCTF.extract_image_patches(xtf, kernel, strides, data_format='channels_first', padding='valid'))
zth = KTH.eval(KCTH.extract_image_patches(xth, kernel, strides, data_format='channels_first', padding='valid'))
assert zth.shape == ztf.shape
assert_allclose(zth, ztf, atol=1e-02)
@@ -95,8 +95,8 @@ class TestBackend(object):
strides = [kernel_shape, kernel_shape]
xth = KTH.variable(xval)
xtf = KTF.variable(xval)
ztf = KTF.eval(KCTF.extract_image_patches(xtf, kernel, strides, data_format='channels_last', padding="same"))
zth = KTH.eval(KCTH.extract_image_patches(xth, kernel, strides, data_format='channels_last', padding="same"))
ztf = KTF.eval(KCTF.extract_image_patches(xtf, kernel, strides, data_format='channels_last', padding='same'))
zth = KTH.eval(KCTH.extract_image_patches(xth, kernel, strides, data_format='channels_last', padding='same'))
assert zth.shape == ztf.shape
assert_allclose(zth, ztf, atol=1e-02)
+1 -1
View File
@@ -50,7 +50,7 @@ def test_uniform(tensor_shape):
'''
@pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', "CONV"])
@pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
def test_cai(tensor_shape):
# upper and lower bounds are proved in original paper
_runner(initializers.ConvolutionAware(), tensor_shape,
+1 -2
View File
@@ -6,7 +6,6 @@ from keras.utils.test_utils import layer_test
from keras_contrib.layers import recurrent
from keras.layers import embeddings
from keras.models import Sequential
from keras.layers.core import Masking
from keras import regularizers
from keras.utils.test_utils import keras_test
@@ -24,7 +23,7 @@ def rnn_test(f):
"""
f = keras_test(f)
# Example : return pytest.mark.parametrize("layer_class", [recurrent.JZ1, recurrent.NTM])(f)
return pytest.mark.parametrize("layer_class", [])(f)
return pytest.mark.parametrize('layer_class', [])(f)
@rnn_test
+1 -1
View File
@@ -18,5 +18,5 @@ def test_metrics():
assert K.eval(output).shape == ()
if __name__ == "__main__":
if __name__ == '__main__':
pytest.main([__file__])
+1 -1
View File
@@ -32,7 +32,7 @@ def _test_optimizer(optimizer, target=0.89):
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16,
history = model.fit(X_train, y_train, epochs=12, batch_size=16,
validation_data=(X_test, y_test), verbose=2)
config = optimizer.get_config()
assert type(config) == dict
+2 -2
View File
@@ -27,8 +27,8 @@ def get_data():
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)[:max_train_samples]
X_test = X_test.reshape(10000, 784)[:max_test_samples]
X_train = X_train.astype("float32") / 255
X_test = X_test.astype("float32") / 255
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
# convert class vectors to binary class matrices
y_train = y_train[:max_train_samples]