mirror of
https://github.com/wassname/keras-contrib.git
synced 2026-06-27 16:10:11 +08:00
Fix examples and applications to keras-2
This commit is contained in:
@@ -36,19 +36,19 @@ generator = ImageDataGenerator(rotation_range=15,
|
||||
|
||||
generator.fit(trainX, seed=0)
|
||||
|
||||
model = ResidualOfResidual(depth=40, width=2, dropout_rate=0.0, weights='None')
|
||||
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.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), samples_per_epoch=len(trainX),
|
||||
nb_epoch=nb_epoch,
|
||||
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",
|
||||
save_best_only=True, save_weights_only=True)],
|
||||
validation_data=(testX, testY),
|
||||
nb_val_samples=testX.shape[0], verbose=2)
|
||||
verbose=2)
|
||||
|
||||
scores = model.evaluate(testX, testY, batch_size)
|
||||
print("Test loss : ", scores[0])
|
||||
|
||||
@@ -47,13 +47,12 @@ model.summary()
|
||||
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
|
||||
print("Finished compiling")
|
||||
|
||||
model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), samples_per_epoch=len(trainX),
|
||||
nb_epoch=nb_epoch,
|
||||
model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size,
|
||||
epochs=nb_epoch,
|
||||
callbacks=[
|
||||
callbacks.ModelCheckpoint("WRN-28-8 Weights.h5", monitor="val_acc", save_best_only=True,
|
||||
save_weights_only=True)],
|
||||
validation_data=(testX, testY),
|
||||
nb_val_samples=testX.shape[0], )
|
||||
validation_data=(testX, testY))
|
||||
|
||||
scores = model.evaluate(testX, testY, batch_size)
|
||||
print("Test loss : %0.5f" % (scores[0]))
|
||||
|
||||
@@ -405,8 +405,6 @@ def __transition_up_block(ip, nb_filters, type='upsampling', output_shape=None,
|
||||
elif type == 'subpixel':
|
||||
x = Conv2D(nb_filters, (3, 3), padding="same", kernel_regularizer=l2(weight_decay), activation='relu',
|
||||
use_bias=False, kernel_initializer='he_uniform')(ip)
|
||||
# x = Convolution2D(nb_filters, 3, 3, activation="relu", border_mode='same', W_regularizer=l2(weight_decay),
|
||||
# bias=False, init='he_uniform')(ip)
|
||||
x = SubPixelUpscaling(scale_factor=2)(x)
|
||||
x = Conv2D(nb_filters, (3, 3), activation="relu", padding='same', kernel_regularizer=l2(weight_decay),
|
||||
use_bias=False, kernel_initializer='he_uniform')(x)
|
||||
|
||||
@@ -14,9 +14,9 @@ import warnings
|
||||
|
||||
from keras.models import Model
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.convolutional import Convolution2D
|
||||
from keras.layers.pooling import AveragePooling2D, MaxPooling2D
|
||||
from keras.layers import Input, merge
|
||||
from keras.layers.merge import add
|
||||
from keras.layers import Input, Conv2D
|
||||
from keras.layers.normalization import BatchNormalization
|
||||
from keras.utils.layer_utils import convert_all_kernels_in_model
|
||||
from keras.utils.data_utils import get_file
|
||||
@@ -70,7 +70,6 @@ def ResidualOfResidual(depth=40, width=2, dropout_rate=0.0,
|
||||
# Returns
|
||||
A Keras model instance.
|
||||
"""
|
||||
|
||||
if weights not in {'cifar10', None}:
|
||||
raise ValueError('The `weights` argument should be either '
|
||||
'`None` (random initialization) or `cifar10` '
|
||||
@@ -88,7 +87,7 @@ def ResidualOfResidual(depth=40, width=2, dropout_rate=0.0,
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=32,
|
||||
min_size=8,
|
||||
dim_ordering=K.image_dim_ordering(),
|
||||
data_format=K.image_dim_ordering(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
@@ -164,22 +163,22 @@ def __initial_conv_block(input, k=1, dropout=0.0, initial=False):
|
||||
# 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":
|
||||
init = Convolution2D(16 * k, 1, 1, init='he_normal', border_mode='same')(init)
|
||||
init = Conv2D(16 * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
|
||||
else:
|
||||
init = Convolution2D(16 * k, 1, 1, init='he_normal', border_mode='same')(init)
|
||||
init = Conv2D(16 * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
|
||||
|
||||
x = BatchNormalization(axis=channel_axis)(input)
|
||||
x = Activation('relu')(x)
|
||||
x = Convolution2D(16 * k, 3, 3, border_mode='same', init='he_normal')(x)
|
||||
x = Conv2D(16 * k, (3, 3), padding='same', kernel_initializer='he_normal')(x)
|
||||
|
||||
if dropout > 0.0:
|
||||
x = Dropout(dropout)(x)
|
||||
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
x = Convolution2D(16 * k, 3, 3, border_mode='same', init='he_normal')(x)
|
||||
x = Conv2D(16 * k, (3, 3), padding='same', kernel_initializer='he_normal')(x)
|
||||
|
||||
m = merge([init, x], mode='sum')
|
||||
m = add([init, x])
|
||||
return m
|
||||
|
||||
|
||||
@@ -191,23 +190,23 @@ def __conv_block(input, nb_filters=32, k=1, dropout=0.0):
|
||||
# Check if input number of filters is same as 32 * k, else create convolution2d for this input
|
||||
if K.image_dim_ordering() == "th":
|
||||
if init._keras_shape[1] != nb_filters * k:
|
||||
init = Convolution2D(nb_filters * k, 1, 1, init='he_normal', border_mode='same')(init)
|
||||
init = Conv2D(nb_filters * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
|
||||
else:
|
||||
if init._keras_shape[-1] != nb_filters * k:
|
||||
init = Convolution2D(nb_filters * k, 1, 1, init='he_normal', border_mode='same')(init)
|
||||
init = Conv2D(nb_filters * k, (1, 1), kernel_initializer='he_normal', padding='same')(init)
|
||||
|
||||
x = BatchNormalization(axis=channel_axis)(input)
|
||||
x = Activation('relu')(x)
|
||||
x = Convolution2D(nb_filters * k, 3, 3, border_mode='same', init='he_normal')(x)
|
||||
x = Conv2D(nb_filters * k, (3, 3), padding='same', kernel_initializer='he_normal')(x)
|
||||
|
||||
if dropout > 0.0:
|
||||
x = Dropout(dropout)(x)
|
||||
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
x = Convolution2D(nb_filters * k, 3, 3, border_mode='same', init='he_normal')(x)
|
||||
x = Conv2D(nb_filters * k, (3, 3), padding='same', kernel_initializer='he_normal')(x)
|
||||
|
||||
m = merge([init, x], mode='sum')
|
||||
m = add([init, x])
|
||||
return m
|
||||
|
||||
|
||||
@@ -238,46 +237,46 @@ def __create_pre_residual_of_residual(nb_classes, img_input, include_top, depth=
|
||||
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
|
||||
|
||||
# Initial convolution layer
|
||||
x = Convolution2D(16, 3, 3, border_mode='same', init='he_normal')(img_input)
|
||||
x = Conv2D(16, (3, 3), padding='same', kernel_initializer='he_normal')(img_input)
|
||||
nb_conv = 4 # Dont count 4 long residual connections in WRN models
|
||||
|
||||
conv0_level1_shortcut = Convolution2D(64 * width, 1, 1, border_mode='same', subsample=(4, 4),
|
||||
name='conv0_level1_shortcut')(x)
|
||||
conv0_level1_shortcut = Conv2D(64 * width, (1, 1), padding='same', strides=(4, 4),
|
||||
name='conv0_level1_shortcut')(x)
|
||||
|
||||
conv1_level2_shortcut = Convolution2D(16 * width, 1, 1, border_mode='same',
|
||||
name='conv1_level2_shortcut')(x)
|
||||
conv1_level2_shortcut = Conv2D(16 * width, (1, 1), padding='same',
|
||||
name='conv1_level2_shortcut')(x)
|
||||
for i in range(N):
|
||||
initial = (i == 0)
|
||||
x = __initial_conv_block(x, k=width, dropout=dropout, initial=initial)
|
||||
nb_conv += 2
|
||||
|
||||
# Add Level 2 shortcut
|
||||
x = merge([x, conv1_level2_shortcut], mode='sum')
|
||||
x = add([x, conv1_level2_shortcut])
|
||||
|
||||
x = MaxPooling2D((2, 2))(x)
|
||||
|
||||
conv2_level2_shortcut = Convolution2D(32 * width, 1, 1, border_mode='same',
|
||||
name='conv2_level2_shortcut')(x)
|
||||
conv2_level2_shortcut = Conv2D(32 * width, (1, 1), padding='same',
|
||||
name='conv2_level2_shortcut')(x)
|
||||
for i in range(N):
|
||||
x = __conv_block(x, k=width, dropout=dropout)
|
||||
nb_conv += 2
|
||||
|
||||
# Add Level 2 shortcut
|
||||
x = merge([x, conv2_level2_shortcut], mode='sum')
|
||||
x = add([x, conv2_level2_shortcut])
|
||||
|
||||
x = MaxPooling2D((2, 2))(x)
|
||||
|
||||
conv3_level2_shortcut = Convolution2D(64 * width, 1, 1, border_mode='same',
|
||||
name='conv3_level2_shortcut')(x)
|
||||
conv3_level2_shortcut = Conv2D(64 * width, (1, 1), padding='same',
|
||||
name='conv3_level2_shortcut')(x)
|
||||
for i in range(N):
|
||||
x = __conv_block(x, nb_filters=64, k=width, dropout=dropout)
|
||||
nb_conv += 2
|
||||
|
||||
# Add Level 2 shortcut
|
||||
x = merge([x, conv3_level2_shortcut], mode='sum')
|
||||
x = add([x, conv3_level2_shortcut])
|
||||
|
||||
# Add Level 1 shortcut
|
||||
x = merge([x, conv0_level1_shortcut], mode='sum')
|
||||
x = add([x, conv0_level1_shortcut])
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ import warnings
|
||||
|
||||
from keras.models import Model
|
||||
from keras.layers.core import Dense, Dropout, Activation, Flatten
|
||||
from keras.layers.convolutional import Convolution2D
|
||||
from keras.layers.pooling import AveragePooling2D, MaxPooling2D
|
||||
from keras.layers import Input, merge
|
||||
from keras.layers import Input, merge, Conv2D
|
||||
from keras.layers.merge import add
|
||||
from keras.layers.normalization import BatchNormalization
|
||||
from keras.utils.layer_utils import convert_all_kernels_in_model
|
||||
from keras.utils.data_utils import get_file
|
||||
@@ -88,7 +88,7 @@ def WideResidualNetwork(depth=28, width=8, dropout_rate=0.0,
|
||||
input_shape = _obtain_input_shape(input_shape,
|
||||
default_size=32,
|
||||
min_size=8,
|
||||
dim_ordering=K.image_dim_ordering(),
|
||||
data_format=K.image_dim_ordering(),
|
||||
include_top=include_top)
|
||||
|
||||
if input_tensor is None:
|
||||
@@ -157,7 +157,7 @@ def WideResidualNetwork(depth=28, width=8, dropout_rate=0.0,
|
||||
|
||||
|
||||
def __conv1_block(input):
|
||||
x = Convolution2D(16, 3, 3, border_mode='same')(input)
|
||||
x = Conv2D(16, (3, 3), padding='same')(input)
|
||||
|
||||
channel_axis = 1 if K.image_dim_ordering() == "th" else -1
|
||||
|
||||
@@ -174,23 +174,23 @@ def __conv2_block(input, k=1, dropout=0.0):
|
||||
# Check if input number of filters is same as 16 * k, else create convolution2d for this input
|
||||
if K.image_dim_ordering() == "th":
|
||||
if init._keras_shape[1] != 16 * k:
|
||||
init = Convolution2D(16 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(16 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
else:
|
||||
if init._keras_shape[-1] != 16 * k:
|
||||
init = Convolution2D(16 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(16 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
|
||||
x = Convolution2D(16 * k, 3, 3, border_mode='same')(input)
|
||||
x = Conv2D(16 * k, (3, 3), padding='same')(input)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
if dropout > 0.0:
|
||||
x = Dropout(dropout)(x)
|
||||
|
||||
x = Convolution2D(16 * k, 3, 3, border_mode='same')(x)
|
||||
x = Conv2D(16 * k, (3, 3), padding='same')(x)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
m = merge([init, x], mode='sum')
|
||||
m = add([init, x])
|
||||
return m
|
||||
|
||||
|
||||
@@ -202,23 +202,23 @@ def __conv3_block(input, k=1, dropout=0.0):
|
||||
# Check if input number of filters is same as 32 * k, else create convolution2d for this input
|
||||
if K.image_dim_ordering() == "th":
|
||||
if init._keras_shape[1] != 32 * k:
|
||||
init = Convolution2D(32 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(32 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
else:
|
||||
if init._keras_shape[-1] != 32 * k:
|
||||
init = Convolution2D(32 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(32 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
|
||||
x = Convolution2D(32 * k, 3, 3, border_mode='same')(input)
|
||||
x = Conv2D(32 * k, (3, 3), padding='same')(input)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
if dropout > 0.0:
|
||||
x = Dropout(dropout)(x)
|
||||
|
||||
x = Convolution2D(32 * k, 3, 3, border_mode='same')(x)
|
||||
x = Conv2D(32 * k, (3, 3), padding='same')(x)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
m = merge([init, x], mode='sum')
|
||||
m = add([init, x])
|
||||
return m
|
||||
|
||||
|
||||
@@ -230,23 +230,23 @@ def ___conv4_block(input, k=1, dropout=0.0):
|
||||
# Check if input number of filters is same as 64 * k, else create convolution2d for this input
|
||||
if K.image_dim_ordering() == "th":
|
||||
if init._keras_shape[1] != 64 * k:
|
||||
init = Convolution2D(64 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(64 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
else:
|
||||
if init._keras_shape[-1] != 64 * k:
|
||||
init = Convolution2D(64 * k, 1, 1, activation='linear', border_mode='same')(init)
|
||||
init = Conv2D(64 * k, (1, 1), activation='linear', padding='same')(init)
|
||||
|
||||
x = Convolution2D(64 * k, 3, 3, border_mode='same')(input)
|
||||
x = Conv2D(64 * k, (3, 3), padding='same')(input)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
if dropout > 0.0:
|
||||
x = Dropout(dropout)(x)
|
||||
|
||||
x = Convolution2D(64 * k, 3, 3, border_mode='same')(x)
|
||||
x = Conv2D(64 * k, (3, 3), padding='same')(x)
|
||||
x = BatchNormalization(axis=channel_axis)(x)
|
||||
x = Activation('relu')(x)
|
||||
|
||||
m = merge([init, x], mode='sum')
|
||||
m = add([init, x])
|
||||
return m
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user