From c0bcf2e44ffd213c0a7fbf18994eff5f98b2624c Mon Sep 17 00:00:00 2001 From: Somshubra Majumdar Date: Tue, 14 Feb 2017 11:35:47 -0600 Subject: [PATCH] Made changes according to review --- ...densenet_cifar10.py => cifar10_densenet.py} | 18 +++++++++--------- keras_contrib/applications/densenet.py | 16 ++++++---------- 2 files changed, 15 insertions(+), 19 deletions(-) rename examples/{densenet_cifar10.py => cifar10_densenet.py} (91%) diff --git a/examples/densenet_cifar10.py b/examples/cifar10_densenet.py similarity index 91% rename from examples/densenet_cifar10.py rename to examples/cifar10_densenet.py index fcb9f24..c3b3325 100644 --- a/examples/densenet_cifar10.py +++ b/examples/cifar10_densenet.py @@ -13,6 +13,12 @@ from keras.preprocessing.image import ImageDataGenerator from keras.utils import np_utils from keras_contrib.applications.densenet import DenseNet +''' +Trains a DenseNet-40-12 model on the CIFAR-10 Dataset. + +Gets a 99.84% accuracy score after 300 epochs. +''' + batch_size = 64 nb_classes = 10 nb_epoch = 300 @@ -72,12 +78,6 @@ model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size), samp validation_data=(testX, Y_test), nb_val_samples=testX.shape[0], verbose=2) -yPreds = model.predict(testX) -yPred = np.argmax(yPreds, axis=1) -print(yPred) -yTrue = testY - -accuracy = metrics.accuracy_score(yTrue, yPred) * 100 -error = 100 - accuracy -print("Accuracy : ", accuracy) -print("Error : ", error) +scores = model.evaluate(testX, Y_test, batch_size=batch_size) +print("Test loss : ", scores[0]) +print("Test accuracy : ", scores[1]) diff --git a/keras_contrib/applications/densenet.py b/keras_contrib/applications/densenet.py index 42192e8..51acfa7 100644 --- a/keras_contrib/applications/densenet.py +++ b/keras_contrib/applications/densenet.py @@ -165,7 +165,7 @@ def DenseNet(depth=40, nb_dense_block=3, growth_rate=12, nb_filter=16, def __conv_block(ip, nb_filter, bottleneck=False, dropout_rate=None, weight_decay=1E-4): - ''' Apply BatchNorm, Relu 3x3, Conv2D, optional bottleneck block and dropout + ''' Apply BatchNorm, Relu, 3x3 Conv2D, optional bottleneck block and dropout Args: ip: Input keras tensor @@ -175,7 +175,6 @@ def __conv_block(ip, nb_filter, bottleneck=False, dropout_rate=None, weight_deca weight_decay: weight decay factor Returns: keras tensor with batch_norm, relu and convolution2d added (optional bottleneck) - ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 @@ -215,7 +214,6 @@ def __transition_block(ip, nb_filter, compression=1.0, dropout_rate=None, weight weight_decay: weight decay factor Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool - ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 @@ -233,7 +231,7 @@ def __transition_block(ip, nb_filter, compression=1.0, dropout_rate=None, weight def __dense_block(x, nb_layers, nb_filter, growth_rate, bottleneck=False, dropout_rate=None, weight_decay=1E-4): - ''' Build a __dense_block where the output of each __conv_block is fed to subsequent ones + ''' Build a dense_block where the output of each conv_block is fed to subsequent ones Args: x: keras tensor @@ -245,17 +243,16 @@ def __dense_block(x, nb_layers, nb_filter, growth_rate, bottleneck=False, dropou weight_decay: weight decay factor Returns: keras tensor with nb_layers of __conv_block appended - ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 - feature_list = [x] + x_list = [x] for i in range(nb_layers): x = __conv_block(x, growth_rate, bottleneck, dropout_rate, weight_decay) - feature_list.append(x) - x = merge(feature_list, mode='concat', concat_axis=concat_axis) + x_list.append(x) + x = merge(x_list, mode='concat', concat_axis=concat_axis) nb_filter += growth_rate return x, nb_filter @@ -263,7 +260,7 @@ def __dense_block(x, nb_layers, nb_filter, growth_rate, bottleneck=False, dropou def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1E-4): - ''' Build the __create_dense_net model + ''' Build the DenseNet model Args: nb_classes: number of classes @@ -279,7 +276,6 @@ def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_bl weight_decay: weight decay Returns: keras tensor with nb_layers of __conv_block appended - ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1