joeyearsley / efficient_densenet_tensorflow Goto Github PK
View Code? Open in Web Editor NEWA memory efficient implementation of densenet.
A memory efficient implementation of densenet.
can this efficient denseNet implementation reduce inference time?
I have learn so much from your project, It's a good working. Thank you~
I am writing project in tf2.0 now. I haven't been able to implement it. So sad.....
Do you know how to implement in tf2.0. Thank you!
class Back_Recompute(Layer):
def __init__(self, nb_filter, bottleneck=False, dropout_rate=None, weight_decay=1e-4, **kwargs):
self.nb_filter = nb_filter
self.weight_decay = weight_decay
self.bottleneck = bottleneck
self.dropout_rate = dropout_rate
super(Back_Recompute, self).__init__(**kwargs)
def call(self, ip):
global brcount
concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
with tf.variable_scope('denseblock_{}'.format(brcount), use_resource=True):
def _x(inner_ip):
x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(inner_ip)
x = Activation('relu')(x)
if self.bottleneck:
inter_channel = self.nb_filter * 4 # Obtained from https://github.com/liuzhuang13/DenseNet/blob/master/densenet.lua
x = Conv2D(inter_channel, (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False,
kernel_regularizer=l2(self.weight_decay))(x)
x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
x = Activation('relu')(x)
x = Conv2D(self.nb_filter, (3, 3), kernel_initializer='he_normal', padding='same', use_bias=False)(x)
if self.dropout_rate:
x = Dropout(self.dropout_rate)(x)
return x
brcount = brcount + 1
_x = tf.contrib.layers.recompute_grad(_x)
return _x(ip)
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1], input_shape[2], self.nb_filter)
Then WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass.
I found in model.summary, the paras of Back_Recompute is 0,Why?
The method you propose for using recompute_grad
is not working, except for the simplest case where all layers in the model are recomputed except the input and output layers. All other cases (e.g. when every-other layer is recomputed) cause the following error:
ValueError: The variables used on recompute were different than the variables originally
used. The function wrapped with @recompute_grad likley creates its own variable
scope with a default name and has been called twice in the same enclosing scope.
To fix, ensure each call to the function happens in its own unique variable
scope.
Can you please advise how to fix this error?
My current method is to (1) create a memory efficient layer, e.g.:
def Conv2D_mem_eff( input_tensor,
filters,
kernel_size,
kernel_regularizer,
bias_regularizer,
padding,
name ):
with tf.variable_scope( name,
use_resource = True ):
def _x( inner_input_tensor ):
x = Conv2D( filters = filters,
kernel_size = kernel_size,
padding = padding,
kernel_regularizer = kernel_regularizer,
bias_regularizer = bias_regularizer,
name = name )(inner_input_tensor)
return x
_x = tf.contrib.layers.recompute_grad( _x )
return _x( input_tensor )
then (2) use this within a Lambda
layer when defining my model:
x = Lambda( Conv2D_mem_eff,
arguments = {'filters' : 24,
'kernel_size' : (5,5),
'kernel_regularizer' : l2,
'bias_regularizer' : l2,
'padding' : 'same',
'name' : 'conv02'},
name= 'conv02' )(x)
I give unique names for each layer I use.
the argument parse of boolean varibles is supposed to passed in the version1 format,
otherwise it will not accept the False argument, it is been discussed here
https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
import argparse
parser = argparse.ArgumentParser()
# version 1
parser.add_argument('--efficient', type = lambda x: (str(x).lower() == 'true'), default=False,
help='Whether to run with gradient checkpointing or not.')
# version2
parser.add_argument('--fp16', type=bool, default=False,
help='Whether to run with FP16 or not.')
args = parser.parse_args()
print('args.efficient', args.efficient)
print('args.fp16', args.fp16)
python param_tmp.py --efficient=False --fp16=False
Great work! I am using Keras to bulid my model and I want to reduce the memory of densenet. Can this project be directly used on Keras? Could you show some Keras examples or user guide in Keras?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.