Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11.1 Detecting Multiple Images -- TypeError: add_weight() got multiple values for argument 'name' #73

Open
mikechen66 opened this issue May 6, 2020 · 3 comments

Comments

@mikechen66
Copy link

mikechen66 commented May 6, 2020

Hi Douwe:

I adopted the code of kbardool/keras-frcnn after yhenon/keras-frcnn was deprecated and deleted in the Github. While running the script of train_frcnn.py, the system raised TypeError: add_weight() got multiple values for argument 'name'.

I am pleased to provide the Snippet of the code, detailed TypeError Message and related environment code of lines for your information.

1. Snippet of the code

Since Keras was updated, I have changed the line of code from" if K.image_dim_ordering() == 'th':" to "if K.image_data_format() == 'channels_first':"

import keras_frcnn.resnet as nn

num_features = 1024

if K.image_data_format() == 'channels_first':
    input_shape_img = (3, None, None)
    input_shape_features = (num_features, None, None)
else:
    input_shape_img = (None, None, 3)
    input_shape_features = (None, None, num_features)

img_input = Input(shape=input_shape_img)
roi_input = Input(shape=(c.num_rois, 4))
feature_map_input = Input(shape=input_shape_features)

"""define the base network (resnet here, can be VGG, Inception, etc)"""
shared_layers = nn.nn_base(img_input, trainable=True)

"""define the RPN, built on the base layers"""
num_anchors = len(c.anchor_box_scales) * len(c.anchor_box_ratios)
rpn_layers = nn.rpn(shared_layers, num_anchors)

classifier = nn.classifier(feature_map_input, roi_input, c.num_rois, nb_classes=len(c.class_mapping), trainable=True)

model_rpn = Model(img_input, rpn_layers)
model_classifier_only = Model([feature_map_input, roi_input], classifier)

model_classifier = Model([feature_map_input, roi_input], classifier)

2. TypeError Message

TypeError Traceback (most recent call last)
in
17
18 # define the base network (resnet here, can be VGG, Inception, etc)
---> 19 shared_layers = nn.nn_base(img_input, trainable=True)
20
21 # define the RPN, built on the base layers

~/keras-frcnn/keras_frcnn/resnet.py in nn_base(input_tensor, trainable)
190 # Do not correct "trainable = trainable" to "trainable=True"
191 x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1', trainable=trainable)(x)
--> 192 x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
193 x = Activation('relu')(x)
194 x = MaxPooling2D((3, 3), strides=(2, 2))(x)

~/miniconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)

~/miniconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs)
461 'You can build it manually via: '
462 'layer.build(batch_input_shape)')
--> 463 self.build(unpack_singleton(input_shapes))
464 self.built = True
465

~/keras-frcnn/keras_frcnn/FixedBatchNormalization.py in build(self, input_shape)
28 regularizer=self.gamma_regularizer,
29 name='{}_gamma'.format(self.name),
---> 30 trainable=False)
31 self.beta = self.add_weight(shape,
32 initializer=self.beta_init,

TypeError: add_weight() got multiple values for argument 'name'

3. Related Snippet of FixedBatchNormalization.py

I checked the FRCNN code. It is related to FixedBatchNormalization.py.

def build(self, input_shape):
    self.input_spec = [InputSpec(shape=input_shape)]
    shape = (input_shape[self.axis],)

    self.gamma = self.add_weight(shape,
                                 initializer=self.gamma_init,
                                 regularizer=self.gamma_regularizer,
                                 name='{}_gamma'.format(self.name),
                                 trainable=False)
    self.beta = self.add_weight(shape,
                                initializer=self.beta_init,
                                regularizer=self.beta_regularizer,
                                name='{}_beta'.format(self.name),
                                trainable=False)
    self.running_mean = self.add_weight(shape, initializer='zero',
                                        name='{}_running_mean'.format(self.name),
                                        trainable=False)
    self.running_std = self.add_weight(shape, initializer='one',
                                       name='{}_running_std'.format(self.name),
                                       trainable=False)

    if self.initial_weights is not None:
        self.set_weights(self.initial_weights)
        del self.initial_weights

    self.built = True

Here is the website that I have referenced.
keras-frcnn: https://github.com/kbardool/keras-frcnn
deprecated yhenon: https://github.com/yhenon/keras-frcnn

Look forward to hearing from you,

Best regards,

Mike``

@mikechen66 mikechen66 changed the title 11.1 Detecting Multiple Images -- TypeError: add_weight() got multiple values for argument 11.1 Detecting Multiple Images -- TypeError: add_weight() got multiple values for argument 'name' May 7, 2020
@mikechen66
Copy link
Author

mikechen66 commented May 8, 2020

During running the example application, I found out the outstanding 'scipy.misc' issues. I am pleased to give the related solutions ad follows.

1. imread, imsave

Issue

ImportError: cannot import name 'imread' and 'imsave 'from 'scipy.misc'

Solution

First, install imageio

$ conda install imageio
or
$ pip install imageio

Second, change
from scipy.misc import imread, imsave
to:
from imageio import imread, imsave

imageio at conda: https://github.com/conda-forge/imageio-feedstock

2. imresize

Issue

ImportError: cannot import name 'imresize' from 'scipy.misc'

Solution

First, install scikit-image

$ conda install -c conda-forge scikit-image
or
$ pip install scikit-image

Second,
change
from scipy.misc import imresize
to:
from skimage.transform import resize

scikit-image: https://scikit-image.org/docs/dev/install.html
pypi: https://pypi.org/project/skimage/
example: https://scikit-image.org/docs/stable/auto_examples/transform/plot_rescale.html

3. fromimage, toimage

Issue

ImportError: cannot import name 'fromimage' from 'scipy.misc'
ImportError: cannot import name 'toimage' from 'scipy.misc'

Solution

Aopt the numpy method:
fromimage(im) -> np.asarray(im)

Adopt the pillow method Image.fromarray():
toimage() -> Image.fromarray()

stackoverflow: https://stackoverflow.com/questions/57796368/attributeerror-importerror-on-scipy-misc-image-functions-e-x-imread-imresize

@mikechen66
Copy link
Author

mikechen66 commented Apr 24, 2021

Hi Douwe:

It is a long journey.

A good news is that I have gotten through the error: TypeError: add_weight() got multiple values for argument 'name'
by correcting FixedBatchNormalization.py and other scripts related to the library of keras_frcnn.

However, I have not yet completed to run the application due to the function of format_img. It shows: NameError: name 'format_img' is not defined. I check both CV2 and PIL, there is no traceback for such an error.

Since keras_rfcnn is a classical case called by the application of 11.1 Detecting Multiple Images. I want to figure out it. Hope you can give a help.

Appreciate for your reply,


import cv2 
import PIL
from PIL import Image

img = cv2.imread('/home/mike/Documents/dl-cookbook/data/cat_dog.jpg')

X, ratio = format_img(img, c)

# -if K.image_dim_ordering() == 'tf':
if K.image_data_format() == 'channels_last':
    X = np.transpose(X, (0, 2, 3, 1))

y1, y2, f = model_rpn.predict(X)
# -r = keras_frcnn.roi_helpers.rpn_to_roi(y1, y2, c, K.image_dim_ordering(), overlap_thresh=0.7)
r = keras_frcnn.roi_helpers.rpn_to_roi(y1, y2, c, K.image_data_format(), overlap_thresh=0.7)
roi_count = R.shape[0] // c.num_rois
r2 = np.zeros((roi_count * c.num_rois, r.shape[1]))
r2 = r[:r2.shape[0],:r2.shape[1]]
r2 = np.reshape(r2, (roi_count, c.num_rois, r.shape[1]))

NameError Traceback (most recent call last)
in
5 img = cv2.imread('/home/mike/Documents/dl-cookbook/data/cat_dog.jpg')
6
----> 7 X, ratio = format_img(img, c)
8
9 # -if K.image_dim_ordering() == 'tf':

NameError: name 'format_img' is not defined_

@mikechen66
Copy link
Author

After adding the following code "from measure_map import format_img", there is no TypeError: add_weight() got multiple values for argument 'name'.

However, it has the TypeError: Object of type 'NoneType' has no len(). So I have added the issue 11.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant