Skip to content

Commit df5b759

Browse files
committed
Update DenseNet with ImageNet models and CIFAR 10 models
1 parent 92ab78d commit df5b759

File tree

9 files changed

+676
-156
lines changed

9 files changed

+676
-156
lines changed

‎cifar10.py‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from keras.utils import np_utils
1111
from keras.preprocessing.image import ImageDataGenerator
1212
from keras.optimizers import Adam
13-
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
13+
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
1414
from keras import backend as K
1515

16-
batch_size = 64
16+
batch_size = 100
1717
nb_classes = 10
1818
nb_epoch = 300
1919

@@ -24,15 +24,15 @@
2424
depth = 40
2525
nb_dense_block = 3
2626
growth_rate = 12
27-
nb_filter = 16
27+
nb_filter = -1
2828
dropout_rate = 0.0 # 0.0 for data augmentation
2929

3030
model = densenet.DenseNet(img_dim, classes=nb_classes, depth=depth, nb_dense_block=nb_dense_block,
31-
growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate)
31+
growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate, weights=None)
3232
print("Model created")
3333

3434
model.summary()
35-
optimizer = Adam(lr=1e-4) # Using Adam instead of SGD to speed up training
35+
optimizer = Adam(lr=1e-3) # Using Adam instead of SGD to speed up training
3636
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=["accuracy"])
3737
print("Finished compiling")
3838
print("Building model...")
@@ -42,38 +42,39 @@
4242
trainX = trainX.astype('float32')
4343
testX = testX.astype('float32')
4444

45-
trainX /= 255.
46-
testX /= 255.
45+
trainX = densenet.preprocess_input(trainX)
46+
testX = densenet.preprocess_input(testX)
4747

4848
Y_train = np_utils.to_categorical(trainY, nb_classes)
4949
Y_test = np_utils.to_categorical(testY, nb_classes)
5050

5151
generator = ImageDataGenerator(rotation_range=15,
5252
width_shift_range=5./32,
53-
height_shift_range=5./32)
53+
height_shift_range=5./32,
54+
horizontal_flip=True)
5455

5556
generator.fit(trainX, seed=0)
5657

5758
# Load model
58-
weights_file="weights/DenseNet-40-12CIFAR10-tf.h5"
59+
weights_file="weights/DenseNet-40-12-CIFAR10.h5"
5960
if os.path.exists(weights_file):
60-
model.load_weights(weights_file)
61+
#model.load_weights(weights_file, by_name=True)
6162
print("Model loaded.")
6263

6364
out_dir="weights/"
6465

65-
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1),
66-
cooldown=0, patience=10, min_lr=0.5e-6)
67-
early_stopper = EarlyStopping(monitor='val_acc', min_delta=0.0001, patience=20)
66+
lr_reducer = ReduceLROnPlateau(monitor='val_acc', factor=np.sqrt(0.1),
67+
cooldown=0, patience=5, min_lr=1e-5)
6868
model_checkpoint= ModelCheckpoint(weights_file, monitor="val_acc", save_best_only=True,
69-
save_weights_only=True,mode='auto')
69+
save_weights_only=True, verbose=1)
7070

71-
callbacks=[lr_reducer,early_stopper,model_checkpoint]
71+
callbacks=[lr_reducer, model_checkpoint]
7272

73-
model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size), samples_per_epoch=len(trainX), nb_epoch=nb_epoch,
74-
callbacks=callbacks,
75-
validation_data=(testX, Y_test),
76-
nb_val_samples=testX.shape[0], verbose=2)
73+
model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size),
74+
steps_per_epoch=len(trainX) // batch_size, epochs=nb_epoch,
75+
callbacks=callbacks,
76+
validation_data=(testX, Y_test),
77+
validation_steps=testX.shape[0] // batch_size, verbose=1)
7778

7879
yPreds = model.predict(testX)
7980
yPred = np.argmax(yPreds, axis=1)

‎densenet.py‎

Lines changed: 284 additions & 137 deletions
Large diffs are not rendered by default.

‎imagenet_inference.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
4+
from keras.preprocessing import image
5+
6+
from densenet import DenseNetImageNet121, DenseNetImageNet169, DenseNetImageNet161, preprocess_input, decode_predictions
7+
8+
import numpy as np
9+
10+
if __name__ == '__main__':
11+
size = 224
12+
13+
model = DenseNetImageNet121(input_shape=(size, size, 3))
14+
model.summary()
15+
16+
17+
img_path = 'images/elephant.jpg'
18+
img = image.load_img(img_path, target_size=(size, size))
19+
x = image.img_to_array(img)
20+
x = np.expand_dims(x, axis=0)
21+
22+
x = preprocess_input(x)
23+
24+
preds = model.predict(x)
25+
26+
print('Predicted:', decode_predictions(preds))
27+

‎images/cheetah.jpg‎

1010 KB
Loading

‎images/elephant.jpg‎

51.9 KB
Loading

‎weights/DenseNet-40-12-CIFAR10.h5‎

-4.21 MB
Binary file not shown.

‎weights/weight_translation_121.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import h5py
2+
import os
3+
4+
# Note : Weights obtained from https://github.com/flyyufelix/DenseNet-Keras
5+
f = h5py.File('densenet121_weights_tf.h5')
6+
7+
conv_weights = []
8+
bn_weights = []
9+
10+
dense_classifier_weights = None
11+
12+
for name in f.attrs['layer_names']:
13+
if 'data' in str(name):
14+
continue
15+
16+
if 'zeropadding' in str(name):
17+
continue
18+
19+
if 'relu' in str(name):
20+
continue
21+
22+
if 'prob' in str(name):
23+
continue
24+
25+
if 'pool' in str(name):
26+
continue
27+
28+
if 'concat' in str(name):
29+
continue
30+
31+
if 'fc' in str(name):
32+
v = f[name]
33+
v = [v[attr][:] for attr in v.attrs['weight_names']]
34+
dense_classifier_weights = v
35+
break
36+
37+
if 'bn' in str(name):
38+
v = f[name]
39+
v_w = [v[attr][:] for attr in v.attrs['weight_names']]
40+
bn_weights.append(v_w)
41+
continue
42+
43+
if 'scale' in str(name):
44+
v = f[name]
45+
v_w = [v[attr][:] for attr in v.attrs['weight_names']]
46+
bn_weights[-1][0] = v_w[0]
47+
bn_weights[-1][1] = v_w[1]
48+
continue
49+
50+
v = f[name]
51+
v_w = v[v.attrs['weight_names'][0]][:]
52+
conv_weights.append(v_w)
53+
54+
count_layers = 1 # for dense matrix
55+
count_layers += len(conv_weights)
56+
count_layers += len(bn_weights)
57+
58+
print('Copying %d weights. (%d layers)' % (count_layers, count_layers // 2))
59+
60+
import densenet
61+
62+
model = densenet.DenseNetImageNet121((224, 224, 3), weights=None)
63+
64+
conv_layer_ids = []
65+
bn_layer_ids = []
66+
67+
for i, layer in enumerate(model.layers):
68+
if layer.__class__.__name__ == 'Input':
69+
continue
70+
71+
if layer.__class__.__name__ == 'Activation':
72+
continue
73+
74+
if layer.__class__.__name__ == 'MaxPooling2D':
75+
continue
76+
77+
if layer.__class__.__name__ == 'AveragePooling2D':
78+
continue
79+
80+
if layer.__class__.__name__ == 'Concatenate':
81+
continue
82+
83+
if layer.__class__.__name__ == 'GlobalAveragePooling2D':
84+
continue
85+
86+
if layer.__class__.__name__ == 'Conv2D':
87+
conv_layer_ids.append(i)
88+
continue
89+
90+
if layer.__class__.__name__ == 'BatchNormalization':
91+
bn_layer_ids.append(i)
92+
continue
93+
94+
95+
count = 0
96+
for i, weights in enumerate(conv_weights):
97+
conv_idx = conv_layer_ids[i]
98+
model.layers[conv_idx].set_weights([weights])
99+
count += 1
100+
101+
for i, weights in enumerate(bn_weights):
102+
bn_idx = bn_layer_ids[i]
103+
104+
model.layers[bn_idx].set_weights(weights)
105+
count += 1
106+
107+
model.layers[-1].set_weights(dense_classifier_weights)
108+
count += 1
109+
110+
print("Sanity check : %d weights loaded" % count)
111+
112+
model.save_weights('DenseNet-BC-121-32.h5', overwrite=True)
113+
114+
print("Finished saving weights")
115+

‎weights/weight_translation_161.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import h5py
2+
import os
3+
4+
# Note : Weights obtained from https://github.com/flyyufelix/DenseNet-Keras
5+
f = h5py.File('densenet161_weights_tf.h5')
6+
7+
conv_weights = []
8+
bn_weights = []
9+
10+
dense_classifier_weights = None
11+
12+
for name in f.attrs['layer_names']:
13+
if 'data' in str(name):
14+
continue
15+
16+
if 'zeropadding' in str(name):
17+
continue
18+
19+
if 'relu' in str(name):
20+
continue
21+
22+
if 'prob' in str(name):
23+
continue
24+
25+
if 'pool' in str(name):
26+
continue
27+
28+
if 'concat' in str(name):
29+
continue
30+
31+
if 'fc' in str(name):
32+
v = f[name]
33+
v = [v[attr][:] for attr in v.attrs['weight_names']]
34+
dense_classifier_weights = v
35+
break
36+
37+
if 'bn' in str(name):
38+
v = f[name]
39+
v_w = [v[attr][:] for attr in v.attrs['weight_names']]
40+
bn_weights.append(v_w)
41+
continue
42+
43+
if 'scale' in str(name):
44+
v = f[name]
45+
v_w = [v[attr][:] for attr in v.attrs['weight_names']]
46+
bn_weights[-1][0] = v_w[0]
47+
bn_weights[-1][1] = v_w[1]
48+
continue
49+
50+
v = f[name]
51+
v_w = v[v.attrs['weight_names'][0]][:]
52+
conv_weights.append(v_w)
53+
54+
count_layers = 1 # for dense matrix
55+
count_layers += len(conv_weights)
56+
count_layers += len(bn_weights)
57+
58+
print('Copying %d weights. (%d layers)' % (count_layers, count_layers // 2))
59+
60+
import densenet
61+
62+
model = densenet.DenseNetImageNet161((224, 224, 3), weights=None)
63+
64+
conv_layer_ids = []
65+
bn_layer_ids = []
66+
67+
for i, layer in enumerate(model.layers):
68+
if layer.__class__.__name__ == 'Input':
69+
continue
70+
71+
if layer.__class__.__name__ == 'Activation':
72+
continue
73+
74+
if layer.__class__.__name__ == 'MaxPooling2D':
75+
continue
76+
77+
if layer.__class__.__name__ == 'AveragePooling2D':
78+
continue
79+
80+
if layer.__class__.__name__ == 'Concatenate':
81+
continue
82+
83+
if layer.__class__.__name__ == 'GlobalAveragePooling2D':
84+
continue
85+
86+
if layer.__class__.__name__ == 'Conv2D':
87+
conv_layer_ids.append(i)
88+
continue
89+
90+
if layer.__class__.__name__ == 'BatchNormalization':
91+
bn_layer_ids.append(i)
92+
continue
93+
94+
95+
count = 0
96+
for i, weights in enumerate(conv_weights):
97+
conv_idx = conv_layer_ids[i]
98+
model.layers[conv_idx].set_weights([weights])
99+
count += 1
100+
101+
for i, weights in enumerate(bn_weights):
102+
bn_idx = bn_layer_ids[i]
103+
104+
model.layers[bn_idx].set_weights(weights)
105+
count += 1
106+
107+
model.layers[-1].set_weights(dense_classifier_weights)
108+
count += 1
109+
110+
print("Sanity check : %d weights loaded" % count)
111+
112+
model.save_weights('DenseNet-BC-161-48.h5', overwrite=True)
113+
114+
print("Finished saving weights")
115+

0 commit comments

Comments
 (0)