cyberzhg / keras-drop-block Goto Github PK
View Code? Open in Web Editor NEWDropBlock implemented in Keras
Home Page: https://pypi.org/project/keras-drop-block/
License: MIT License
DropBlock implemented in Keras
Home Page: https://pypi.org/project/keras-drop-block/
License: MIT License
18 utils = keras.utils
19 activations = keras.activations
---> 20 applications = keras.applications
21 backend = keras.backend
22 datasets = keras.datasets
AttributeError: module 'keras' has no attribute 'applications'
Could anyone please help out a bit?
I make some small change in your implementation when call the dropblock and it will work when inference.
def call(self, inputs, training=None):
def dropped_inputs():
outputs = inputs
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 2, 3, 1])
shape = K.shape(outputs)
if self.sync_channels:
mask = self._compute_drop_mask([shape[0], shape[1], shape[2], 1])
else:
mask = self._compute_drop_mask(shape)
outputs = outputs * mask *\
(K.cast(K.prod(shape), dtype=K.floatx()) / K.sum(mask))
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 3, 1, 2])
return outputs
return dropped_inputs()
# return K.in_train_phase(dropped_inputs, inputs, training=training)
But when loading my model with your keras dropblock implementation, I met unpredicted bug. When I train my model with dropblock and save it, all is good. But when I load the model and want to attack it. The bug merged.
Traceback (most recent call last):
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value sequential_1/Dropblock1/Variable
[[{{node sequential_1/Dropblock1/Variable/read}} = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](sequential_1/Dropblock1/Variable)]]
[[{{node sequential_1/dense_3/BiasAdd/_103}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_319_sequential_1/dense_3/BiasAdd", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_attack.py", line 79, in <module>
adv = attack.attack(inputs, targets)
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/l2_attack.py", line 137, in attack
r.extend(self.attack_batch(imgs[i:i+self.batch_size], targets[i:i+self.batch_size]))
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/l2_attack.py", line 196, in attack_batch
self.newimg])
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value sequential_1/Dropblock1/Variable
[[node sequential_1/Dropblock1/Variable/read (defined at /home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:402) = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](sequential_1/Dropblock1/Variable)]]
[[{{node sequential_1/dense_3/BiasAdd/_103}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_319_sequential_1/dense_3/BiasAdd", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'sequential_1/Dropblock1/Variable/read', defined at:
File "test_attack.py", line 72, in <module>
attack = CarliniL2(sess, model, batch_size=9, max_iterations=1000, confidence=0)
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/l2_attack.py", line 90, in __init__
self.output = model.predict(self.newimg)
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/setup_cifar.py", line 122, in predict
return self.model(data)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/engine/base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/engine/network.py", line 564, in call
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/engine/network.py", line 721, in run_internal_graph
layer.call(computed_tensor, **kwargs))
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/keras_drop_block/drop_block.py", line 199, in call
return dropped_inputs()
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/keras_drop_block/drop_block.py", line 193, in dropped_inputs
mask = self._compute_drop_mask(shape)
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/keras_drop_block/drop_block.py", line 174, in _compute_drop_mask
mask *= self._compute_valid_seed_region(height, width)
File "/home/weitian/code/tf/new_cw/nn_robust_attacks/keras_drop_block/drop_block.py", line 166, in _compute_valid_seed_region
K.ones((height, width)),
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 736, in ones
return variable(v, dtype=dtype, name=name)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 402, in variable
v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 183, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 146, in _variable_v1_call
aggregation=aggregation)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 125, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2444, in default_variable_creator
expected_shape=expected_shape, import_scope=import_scope)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 187, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1329, in __init__
constraint=constraint)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1491, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 81, in identity
return gen_array_ops.identity(input, name=name)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3454, in identity
"Identity", input=input, name=name)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
op_def=op_def)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value sequential_1/Dropblock1/Variable
[[node sequential_1/Dropblock1/Variable/read (defined at /home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:402) = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](sequential_1/Dropblock1/Variable)]]
[[{{node sequential_1/dense_3/BiasAdd/_103}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_319_sequential_1/dense_3/BiasAdd", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
The performance on my net is poor.
Running the mnist.py demo, receive the following error
Traceback (most recent call last):
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2953, in ones
tensor_shape.TensorShape(shape))
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 771, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 771, in <listcomp>
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 716, in as_dimension
return Dimension(value)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 200, in __init__
None)
File "<string>", line 3, in raise_from
TypeError: Dimension value must be integer or None or have an __index__ method, got <tf.Tensor 'strided_slice:0' shape=() dtype=int32>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 6328, in pack
values, "axis", axis)
tensorflow.python.eager.core._FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/ma7555/Documents/Kaggle/bengaliai-cv19/mnist.py", line 75, in <module>
drop_block_model = get_drop_block_model()
File "c:/Users/ma7555/Documents/Kaggle/bengaliai-cv19/mnist.py", line 56, in get_drop_block_model
model.add(DropBlock2D(input_shape=(28, 28, 1), block_size=7, keep_prob=0.8, name='Input-Dropout'))
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 166, in add
layer(x)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 489, in __call__
output = self.call(inputs, **kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras_drop_block\drop_block.py", line 200, in call
return K.in_train_phase(dropped_inputs, inputs, training=training)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 3214, in in_train_phase
x = switch(training, x, alt)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 3147, in switch
else_expression_fn)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1392, in cond_for_tf_v2
return cond(pred, true_fn=true_fn, false_fn=false_fn, strict=True, name=name)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1177, in cond
return cond_v2.cond_v2(pred, true_fn, false_fn, name)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\cond_v2.py", line 83, in cond_v2
op_return_value=pred)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras_drop_block\drop_block.py", line 193, in dropped_inputs
mask = self._compute_drop_mask(shape)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras_drop_block\drop_block.py", line 174, in _compute_drop_mask
mask *= self._compute_valid_seed_region(height, width)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras_drop_block\drop_block.py", line 166, in _compute_valid_seed_region
K.ones((height, width)),
File "C:\Users\ma7555\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 996, in ones
v = tf.ones(shape=shape, dtype=dtype, name=name)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2956, in ones
shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1341, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1449, in _autopacking_conversion_function
return _autopacking_helper(v, dtype, name or "packed")
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1355, in _autopacking_helper
return gen_array_ops.pack(list_or_tuple, name=name)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 6333, in pack
values, axis=axis, name=name, ctx=_ctx)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 6375, in pack_eager_fallback
ctx=ctx, name=name)
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 75, in quick_execute
raise e
File "C:\Users\ma7555\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
inputs, attrs, num_outputs)
TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
@tf.function
def has_init_scope():
my_constant = tf.constant(1.)
with tf.init_scope():
added = my_constant * 2
The graph tensor has name: strided_slice:0
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.