Code Monkey home page Code Monkey logo

Comments (17)

horngep avatar horngep commented on August 22, 2024 8

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:

if K.backend() == 'tensorflow':
       convert_all_kernels_in_model(model)
  • modify your input to be channel first (eg. (3,227,227))

from convnets-keras.

mabirck avatar mabirck commented on August 22, 2024 2

@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.

jkyl avatar jkyl commented on August 22, 2024 1

@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.

lunardog avatar lunardog commented on August 22, 2024 1

Here is my code with Tensorflow capability: https://github.com/lunardog/convnets-keras

Can't get heatmaps to work though.

from convnets-keras.

MrXu avatar MrXu commented on August 22, 2024 1

@mabirck Thank you 👍

from convnets-keras.

Anabik avatar Anabik commented on August 22, 2024 1

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.

mibrahimali avatar mibrahimali commented on August 22, 2024

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.

Hippogriff avatar Hippogriff commented on August 22, 2024

What is the lead on this topic?

from convnets-keras.

Hippogriff avatar Hippogriff commented on August 22, 2024

A link to your implementation will be helpful. Thanks.

from convnets-keras.

MrXu avatar MrXu commented on August 22, 2024

@lunardog Great work! Did you manage to convert the weight for tensorflow backend as well?

from convnets-keras.

lunardog avatar lunardog commented on August 22, 2024

Nope, @MrXu, using th ordering.

from convnets-keras.

Anabik avatar Anabik commented on August 22, 2024

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.

lunardog avatar lunardog commented on August 22, 2024

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.

Anabik avatar Anabik commented on August 22, 2024

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.

neil454 avatar neil454 commented on August 22, 2024

@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.

neil454 avatar neil454 commented on August 22, 2024

@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.

gabrieldemarmiesse avatar gabrieldemarmiesse commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.