Comments (17)
I have managed to get it working for AlexNet() with Keras 2 with Tensorflow backend using suggestions from all the above/other threads. To summarize, here are the steps that I took:
- use @lunardog's modifications https://github.com/lunardog/convnets-keras
- in customlayers.py, line 22, change
(0,half)
to((0,0),(half,half)))
(by @erralves from issue#44) - in convnets.py add (from keras.utils.layer_utils import convert_all_kernels_in_model)
- then, add this at the end of AlexNet() method as suggested by @neil454
if K.backend() == 'tensorflow':
convert_all_kernels_in_model(model)
- modify your input to be channel first (eg. (3,227,227))
from convnets-keras.
@MrXu Take a look in the keras wiki:
https://github.com/fchollet/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa
from convnets-keras.
@GodOfProbability I got the AlexNet model up and running with the tf backend by carefully changing the dimension ordering both in convnets.py
and customlayers.py
. I also had to change X.shape
in customlayers
to [int(i) for i in X.get_shape()]
, and None
if casting to int is not possible. Let me know if you'd like to see my modified files, it shouldn't be too hard to rewrite this entire package in a backend-agnostic way.
from convnets-keras.
Here is my code with Tensorflow capability: https://github.com/lunardog/convnets-keras
Can't get heatmaps to work though.
from convnets-keras.
@mabirck Thank you 👍
from convnets-keras.
Many many thanks for replying me. I remove "image_data_format": "channels_last",
from /.keras/keras.json. Now it works fine with keras2.0.2.
.
from convnets-keras.
I'm facing the same problem also with tensorflow backend
i figure out that it's related to input shape as this model is trained using theano not TF so TH expecting input to Conv2d as (samples, channels, rows, cols) but TF expect as follow (samples, rows, cols, channels)
i tried to hardcoded fix the input dim but it through an error in custom layer
"""
File "/usr/local/lib/python2.7/dist-packages/convnetskeras/convnets.py", line 233, in AlexNet
conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 514, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 572, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 149, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 556, in call
return self.function(x, **arguments)
File "/usr/local/lib/python2.7/dist-packages/convnetskeras/customlayers.py", line 14, in f
b, ch, r, c = X.shape
AttributeError: 'Tensor' object has no attribute 'shape'
"""
from convnets-keras.
What is the lead on this topic?
from convnets-keras.
A link to your implementation will be helpful. Thanks.
from convnets-keras.
@lunardog Great work! Did you manage to convert the weight for tensorflow backend as well?
from convnets-keras.
Nope, @MrXu, using th
ordering.
from convnets-keras.
I tried https://github.com/lunardog/convnets-keras with Keras2 with tensor flow. This is not working. Can any one help what changes to do?
from convnets-keras.
Hi @Anabik. I cloned this project from @heuritech before Keras 2 was released and haven't updated it for months. You may have more luck with the original repo.
Also, if you need convnets on Keras, you may want to try the keras.applications module (part of keras). It has some convolutional models built in and ready to use.
from convnets-keras.
Thanks for reply. I tried with the built in convolutional models in Keras2.0. But I got error message.
My Code:
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
Error Message:
Traceback (most recent call last):
File "", line 1, in
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/applications/resnet50.py", line 264, in ResNet50
model.load_weights(weights_path)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.py", line 2495, in load_weights
load_weights_from_hdf5_group(f, self.layers)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.py", line 2908, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1992, in batch_set_value
assign_op = x.assign(assign_placeholder)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 522, in assign
return state_ops.assign(self._variable, value, use_locking=use_locking)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 47, in assign
use_locking=use_locking, name=name)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2329, in create_op
set_shapes_for_outputs(ret)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1717, in set_shapes_for_outputs
shapes = shape_func(op)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1667, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/home/cvpr/miniconda3/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimension 0 in both shapes must be equal, but are 7 and 3 for 'Assign' (op: 'Assign') with input shapes: [7,7,3,64], [3,64,7,7].
I tried same thing with VGG16 but it is not working. I can not find out the reason why the in-built models are not working. Please help me.
from convnets-keras.
@lunardog I have tried your modification to allow tensorflow support, as well as my own modification (you don't actually need if
statements for the two backends, just use X._keras_shape
instead of X.shape
).
In both situations, I can get the network to run, but the classifications are wrong (probability scores are all very low, no class sticks out). It outputs the correct class when using the theano backend.
This is with Keras 1.2.2 by the way.
from convnets-keras.
@lunardog Actually I have figured out the problem. Theano and Tensorflow use different convolutional kernels, but luckily keras has a function built-in to convert them. Just change the end of AlexNet() to the following...
if weights_path:
model.load_weights(weights_path)
if K.backend() == 'tensorflow':
convert_all_kernels_in_model(model)
return model
You'll have to import the function as well...
from keras.utils.layer_utils import convert_all_kernels_in_model
from convnets-keras.
I don't know if this is still an issue, but if I may do a bit of publicity for my repo, I used this one as based and improved on it. It's up to date, works with keras 2 and Theano, tensorflow and CNTK. You can also use other CNN like ResNet to get heatmaps.
https://github.com/gabrieldemarmiesse/heatmaps
from convnets-keras.
Related Issues (20)
- Heatmap has low resolution (43x73 in the dog example) HOT 1
- add license
- raise error when using process_image_batch (using sample code)
- Are you interested in doubling the heatmap resolution? (4x more pixels) HOT 2
- support more networks
- vgg networks dont't produce heatmaps HOT 1
- error while loading weights in case of heatmap=true
- Why don't you assign regularisation to the layers?
- I cannot reproduce your results. HOT 3
- Import Error HOT 21
- What is geometric norm?
- Alexnet issue with Keras 2
- why can't I reproduce your result? HOT 3
- Load weights error in theano HOT 2
- Share information about training
- error: Load weight file (ValueError: You are trying to load a weight file containing 11 layers into a model with 7 layers.)
- Dimensions must be equal, HOT 2
- convnets.py in 'module' object is not callable HOT 1
- Unable to install h5py and convnets-keras HOT 2
- Unable to download weights for AlexNet HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from convnets-keras.