Upgrading tf.contrib.slim manually to tf 2.0
Asked Answered
D

3

11

I have a problem with my python code that uses tf.contrib.slim functionalities and no longer works after upgrading to tensorflow to 2.0.

How can I upgrade the following to tf 2.0:

import tensorflow.contrib.slim as slim
import tensorflow.contrib.slim.nets

# ...

net = slim.conv2d(
    inp, 
    dim,
    [3, 3],
    rate=1,
    normalizer_fn=slim.layer_norm,
    activation_fn=lrelu,
    scope='g_' + str(width) + '_conv1') 

Thanks.

Disjunction answered 30/10, 2019 at 15:10 Comment(3)
Have you tried anything? There is literally an entire document devoted to this topic, including a section titled A note on Slim & contrib.layers.Acculturate
Yes, I looked into that, but found it not really helpful, as I am a newbie when it comes to tensorflow :/Disjunction
@gobrewers14 I agree with Jonas, it is not helpful at all.Beaird
O
11

Instal tf-slim first:

pip install --upgrade tf_slim

then instead of import tensorflow.contrib.slim as slim you need to write:

import tf_slim as slim. That fixed my very same problem. Hope this helps.

Ori answered 7/4, 2022 at 21:53 Comment(0)
R
10

You can use tf_slim package instead (https://github.com/google-research/tf-slim)

However, the push request updating the package to TF2 is still pending. So you should install from the branch pip install git+https://github.com/adrianc-a/tf-slim.git@remove_contrib

Rayfordrayle answered 19/11, 2019 at 5:21 Comment(1)
Stil not fully support on TF2. For example error on use tensorflow.contrib.training.python.training import evaluationTouched
C
1

That might do it:

net = tf.keras.layers.Conv2D(filters=dim, kernel_size=3, name='g_' + str(width) + '_conv1')(inp)
net = tf.keras.layers.BatchNormalization()(net)
net = tf.keras.layers.LeakyReLU()(net)

Using slim, there's an option to mention which batch normalization and activation you want to have after the convolution layer. You can't do it in 2.0, so you need to specify exactly which normalization and activation you want in 2 different layers.

Colligate answered 14/1, 2020 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.