1

I have an assignment that leverages tf-slim for inception. We are supposed to train a new model using a predefined architecture and pretrained weights. The example code looks like the following.

from tensorflow.contrib.slim.nets import inception
import tensorflow.contrib.slim as slim

I'd like to use mobilenet, but it appears that it will not import like it's not available.

import tensorflow.contrib.slim.nets
from tensorflow.contrib.slim.nets import mobilenet

input_tensor = tf.placeholder(tf.float32, shape=[None, height, width, channels], name="input_tensor")
with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
    logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

ImportError: cannot import name 'mobilenet'

Joey Carson
  • 2,973
  • 7
  • 36
  • 60

1 Answers1

3

Answering my own question as this was not immediately obvious to me as a student of Tensorflow.

This github page says that the models int the research directory (where mobilenet is) are not officially supported in the release branches of tensorflow and thus are unavailable in production installations.

"The research models. Links to an external site. are a large collection of models implemented in TensorFlow by researchers. They are not officially supported or available in release branches; it is up to the individual researchers to maintain the models and/or provide support on issues and pull requests."

You can however hack this together by including the directory in your project. In fact I came across a discussion that stated it was officially necessary to copy the code into your project. I'll update the answer if I can find that same forum.

import tensorflow as tf
from models.research.slim.nets.mobilenet import mobilenet_v2
input_tensor = tf.placeholder(tf.float32, shape=[None, 224, 224, 3], name="input_tensor")
with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
       logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

Additionally, inside mobilenet_v2.py, there are a couple of relative python import statements. They are simply assuming they are being run from within the models/research/slim directory and are thus trying to import nets.mobilenet. Just change those to use the fully qualified package name, e.g. prepend nets with models.research.slim.

from models.research.slim.nets.mobilenet import conv_blocks as ops
from models.research.slim.nets.mobilenet import mobilenet as lib
Joey Carson
  • 2,973
  • 7
  • 36
  • 60