Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading tf.contrib.slim manually to tf 2.0

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.

like image 924
Jonas G. Avatar asked Oct 30 '19 15:10

Jonas G.


People also ask

What is TensorFlow contrib slim?

TF-slim is a new lightweight high-level API of TensorFlow ( tensorflow. contrib. slim ) for defining, training and evaluating complex models. This directory contains code for training and evaluating several widely used Convolutional Neural Network (CNN) image classification models using TF-slim.

What is the difference between TensorFlow 1 and 2?

(As per the TensorFlow team) It is important to understand that there is no battle of TensorFlow 1.0 vs TensorFlow 2.0 as TensorFlow 2.0 is the updated version and hence clearly better and smarter. It was built keeping in mind the drawbacks of TensorFlow 1.0 which was particularly hard to use and understand.

What is Tf_slim?

TensorFlow-Slim (TF-Slim) is a TensorFlow wrapper library that allows you to build and train complex TensorFlow models in an easy, intuitive way by eliminating the boilerplate code that plagues many deep learning algorithms.


2 Answers

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

like image 183
Dmitry Grebenyuk Avatar answered Sep 21 '22 23:09

Dmitry Grebenyuk


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.

like image 30
Ohad Meir Avatar answered Sep 17 '22 23:09

Ohad Meir