Models: 'module' object has no attribute 'convert_collection_to_dict'

Created on 3 Nov 2016  路  8Comments  路  Source: tensorflow/models

I want to start using TensorFlow-Slim. But in the first step, when I try to create a VGG16 model, it gives me the following error:

File ".../Tensoflow/Test.py", line 35, in
logits, _ = vgg.vgg_16(processed_images, is_training=False)
File ".../tensorflow/models/models/slim/nets/vgg.py", line 176, in vgg_16
end_points = slim.utils.convert_collection_to_dict(end_points_collection)
AttributeError: 'module' object has no attribute 'convert_collection_to_dict'

and here is my code:

import numpy as np
import os
import tensorflow as tf
import urllib2
import matplotlib.pyplot as plt

from datasets import imagenet
from nets import vgg
from preprocessing import vgg_preprocessing
from datasets import dataset_utils

slim = tf.contrib.slim

batch_size = 3
image_size = vgg.vgg_16.default_image_size

checkpoints_dir = '/tmp/checkpoints'
with tf.Graph().as_default():
    url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
    image_string = urllib2.urlopen(url).read()
    image = tf.image.decode_jpeg(image_string, channels=3)
    processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
    processed_images = tf.expand_dims(processed_image, 0)

    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits, _ = vgg.vgg_16(processed_images, is_training=False)
    probabilities = tf.nn.softmax(logits)

BTW, the atoucomplete of PyCharm shows the attribute. Any idea where is the issue?
Thank you,

builinstall

All 8 comments

You might try using

import tensorflow.contrib.slim.nets as vgg

I don't know where you are getting vgg_preprocessing. What 3rd party packages are you using?

Thank you @aselle,
I cloned the "TensorFlow-Slim image classification library" and then added it to my PYTHONPATH.

git clone https://github.com/tensorflow/models/

and I think it is an official one: https://github.com/tensorflow/models/tree/master/slim

The code is also provided by them in a file namely "slim_walktrough.ipynb" in its root directory (I just change inception to Vgg16).

I used your suggestion

import tensorflow.contrib.slim.nets as vgg

and it worked. However, I don't know what's wrong with the "TensorFlow-Slim image classification library". I am still using the same cloned vgg_preprocessing, so the problem should be with the vgg in this library.

@nathansilberman could you provide some insight into the problems @hadikazemi is having. Thanks!

I replaced

end_points = slim.utils.convert_collection_to_dict(end_points_collection)

with

end_points = dict(tf.get_collection(end_points_collection))

in the cloned file:

models/slim/nets/vgg.py

and my problem solved. I think there is no "slim.utils" available anymore as I have installed the latest version of Tensorflow from the source.
Thanks again,

As a warning, the solution end_points = dict(tf.get_collection(end_points_collection)) doesn't work with Python 3.

As far as I can tell, the convert_collection_to_dict function still exists in both the Master and 11rc2 branches of tensorflow/tensorflow:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/utils.py
https://github.com/tensorflow/tensorflow/blob/v0.11.0rc2/tensorflow/contrib/layers/python/layers/utils.py

In both branches, the slim module imports the layers.utils functions into slim:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/__init__.py
https://github.com/tensorflow/tensorflow/blob/v0.11.0rc2/tensorflow/contrib/slim/__init__.py

I'm not sure why your program cannot access that function. Maybe you can see which file python slim.utils is actually loading and check that file for convert_collection_to_dict:

import tensorflow as tf
slim = tf.contrib.slim
print(slim.utils.__file__)

@vonclites Thank you,
You were right... I had installed the latest version of Tensorflow in a wrong python directory. It's fixed after reinstalling the Tensorflow on the system PYTHONPATH.

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

Was this page helpful?
0 / 5 - 0 ratings