Skip to content

Commit

Permalink
Small Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent Sommer committed Mar 30, 2017
1 parent 5acfaca commit 32b58c4
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 96 deletions.
3 changes: 2 additions & 1 deletion convert_weights/convert_dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_layers(model):
try:
layer = model.layers[i]
if layer.trainable:
bad = ["pooling", "flatten", "dropout", "activation"]
bad = ["pooling", "flatten", "dropout", "activation", "concatenate"]
if not any(word in layer.name for word in bad):
result.append(layer)
except:
Expand Down Expand Up @@ -77,6 +77,7 @@ def get_layers(model):
# weights[0] = shuffle_rows(weights[0])
# th_layer.set_weights(weights)
# print('converted ', th_layer.name)
# elif th_layer.__class__.__name__ not in bad_classes:
else:
th_layer.set_weights(weights_list[index])
print('Set: ', th_layer.name)
Expand Down
4 changes: 2 additions & 2 deletions convert_weights/convert_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_layers(model):
try:
layer = model.layers[i]
if layer.trainable:
bad = ["pooling", "flatten", "dropout", "activation"]
bad = ["pooling", "flatten", "dropout", "activation", "concatenate"]
if not any(word in layer.name for word in bad):
result.append(layer)
except:
Expand All @@ -37,7 +37,7 @@ def get_layers(model):

K.set_image_dim_ordering('tf')

th_model = inception_v4.create_model(weights_path="../weights/inception-v4_weights_tf_dim_ordering_tf_kernels.h5")
th_model = inception_v4.create_model(weights="../weights/inception-v4_weights_tf_dim_ordering_tf_kernels.h5")

convert_all_kernels_in_model(th_model)
print("Converted all Kernels")
Expand Down
6 changes: 4 additions & 2 deletions convert_weights/convert_weights_to_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def natural_keys(myobject):

def setWeights(layers, weights):
for index, layer in enumerate(layers):
if "dense" in layer.name:
continue
layer.set_weights(weights[index])
print(layer.name + " weights have been set!")
print("Finished Setting Weights!")
Expand All @@ -37,7 +39,7 @@ def get_layers(model):
try:
layer = model.layers[i]
if layer.trainable:
bad = ["pooling", "flatten", "dropout", "activation"]
bad = ["pooling", "flatten", "dropout", "activation", "concatenate"]
if not any(word in layer.name for word in bad):
result.append(layer)
except:
Expand All @@ -63,7 +65,7 @@ def get_layers(model):
setWeights(layers, weights)

# Save model weights in h5 format
model.save_weights("../weights/inception-v4_weights_tf_dim_ordering_tf_kernels.h5")
model.save_weights("../weights/inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5")
print("Finished saving weights in h5 format")


Expand Down
1 change: 0 additions & 1 deletion convert_weights/get_inception_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def get_weights():
value = sess.run(key)
if y == 1:
current_bn = []
current_bn.append(np.ones(value.shape[-1]))
current_bn.append(value)
elif y == 2:
current_bn.append(value)
Expand Down
22 changes: 11 additions & 11 deletions convert_weights/helper_net/inception_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def block_inception_a(inputs, scope=None, reuse=None):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 96, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])


def block_reduction_a(inputs, scope=None, reuse=None):
Expand All @@ -69,7 +69,7 @@ def block_reduction_a(inputs, scope=None, reuse=None):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
return tf.concat(3, [branch_0, branch_1, branch_2])
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2])


def block_inception_b(inputs, scope=None, reuse=None):
Expand All @@ -93,7 +93,7 @@ def block_inception_b(inputs, scope=None, reuse=None):
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])


def block_reduction_b(inputs, scope=None, reuse=None):
Expand All @@ -115,7 +115,7 @@ def block_reduction_b(inputs, scope=None, reuse=None):
with tf.variable_scope('Branch_2'):
branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
return tf.concat(3, [branch_0, branch_1, branch_2])
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2])


def block_inception_c(inputs, scope=None, reuse=None):
Expand All @@ -128,20 +128,20 @@ def block_inception_c(inputs, scope=None, reuse=None):
branch_0 = slim.conv2d(inputs, 256, [1, 1], scope='Conv2d_0a_1x1')
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1')
branch_1 = tf.concat(3, [
branch_1 = tf.concat(axis=3, values=[
slim.conv2d(branch_1, 256, [1, 3], scope='Conv2d_0b_1x3'),
slim.conv2d(branch_1, 256, [3, 1], scope='Conv2d_0c_3x1')])
with tf.variable_scope('Branch_2'):
branch_2 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1')
branch_2 = slim.conv2d(branch_2, 448, [3, 1], scope='Conv2d_0b_3x1')
branch_2 = slim.conv2d(branch_2, 512, [1, 3], scope='Conv2d_0c_1x3')
branch_2 = tf.concat(3, [
branch_2 = tf.concat(axis=3, values=[
slim.conv2d(branch_2, 256, [1, 3], scope='Conv2d_0d_1x3'),
slim.conv2d(branch_2, 256, [3, 1], scope='Conv2d_0e_3x1')])
with tf.variable_scope('Branch_3'):
branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3')
branch_3 = slim.conv2d(branch_3, 256, [1, 1], scope='Conv2d_0b_1x1')
return tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])


def inception_v4_base(inputs, final_endpoint='Mixed_7d', scope=None):
Expand Down Expand Up @@ -192,7 +192,7 @@ def add_and_check_final(name, net):
with tf.variable_scope('Branch_1'):
branch_1 = slim.conv2d(net, 96, [3, 3], stride=2, padding='VALID',
scope='Conv2d_0a_3x3')
net = tf.concat(3, [branch_0, branch_1])
net = tf.concat(axis=3, values=[branch_0, branch_1])
if add_and_check_final('Mixed_3a', net): return net, end_points

# 73 x 73 x 160
Expand All @@ -207,7 +207,7 @@ def add_and_check_final(name, net):
branch_1 = slim.conv2d(branch_1, 64, [7, 1], scope='Conv2d_0c_7x1')
branch_1 = slim.conv2d(branch_1, 96, [3, 3], padding='VALID',
scope='Conv2d_1a_3x3')
net = tf.concat(3, [branch_0, branch_1])
net = tf.concat(axis=3, values=[branch_0, branch_1])
if add_and_check_final('Mixed_4a', net): return net, end_points

# 71 x 71 x 192
Expand All @@ -218,7 +218,7 @@ def add_and_check_final(name, net):
with tf.variable_scope('Branch_1'):
branch_1 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat(3, [branch_0, branch_1])
net = tf.concat(axis=3, values=[branch_0, branch_1])
if add_and_check_final('Mixed_5a', net): return net, end_points

# 35 x 35 x 384
Expand Down Expand Up @@ -269,7 +269,7 @@ def inception_v4(inputs, num_classes=1001, is_training=True,
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
create_aux_logits: Whether to include the auxilliary logits.
create_aux_logits: Whether to include the auxiliary logits.
Returns:
logits: the logits outputs of the model.
Expand Down
4 changes: 2 additions & 2 deletions evaluate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_processed_image(img_path):
im = central_crop(im, 0.875)
im = cv2.resize(im, (299, 299))
im = preprocess_input(im)
if K.image_dim_ordering() == "th":
if K.image_data_format() == "channels_first":
im = np.transpose(im, (2,0,1))
im = im.reshape(-1,3,299,299)
else:
Expand All @@ -64,7 +64,7 @@ def get_processed_image(img_path):

if __name__ == "__main__":
# Create model and load pre-trained weights
model = inception_v4.create_model(weights='imagenet')
model = inception_v4.create_model(weights='imagenet', include_top=True)

# Open Class labels dictionary. (human readable label given ID)
classes = eval(open('validation_utils/class_names.txt', 'r').read())
Expand Down
Loading

0 comments on commit 32b58c4

Please sign in to comment.