Reputation: 13
Here's my custom module stored in util.py
that wrapps up 3-steps what I always gone-through to read images when using tensorflow
#tf_load_image from path
def load_image(image_path, image_shape, clr_normalize = True ):
'''returns tensor-array on given image path and image_shape [height, width]'''
import tensorflow as tf
image = tf.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=3)
if clr_normalize == True:
image = tf.image.resize_images(image, image_shape) / 127.5 - 1
else:
image = tf.image.resize_images(image, image_shape)
return image
But this is quite inefficient to deal with lots of image load since it generally calls import tensorflow as tf
everytime I read "single image".
I'd like to let this function to inherit tf
command from the main.py
's tf where the load_image actually imported into.
Like..
import tensor flow as tf
from util import load_image
image = load_image("path_string") #where load_image no longer wraps the import tensorflow as tf in itself.
Upvotes: 0
Views: 183
Reputation: 77912
But this is quite inefficient (...) since it generally calls
import tensorflow as tf
everytime I read "single image".
imported modules are cached in sys.modules
on first import (I mean "the very first time a module is imported in a given process"), subsequent calls will retrieve already imported modules from this cache, so the overhead is way less than you might think. But anyway:
I'd like to let this function to inherit tf command from the main.py's tf where the load_image actually imported into.
The only way would be to explicitely pass the module to your function, ie:
# utils.py
def load_image(tf, ....):
# code here using `tf`
and then
# main.py
import tensorflow as tf
from utils import load_image
im = load_image(tf, ...)
BUT this is actually totally useless: all you have to do is, in utils.py, to move the import statement out of the function:
# utils.py
import tensorflow as tf
def load_image(image_path, image_shape, clr_normalize = True ):
'''returns tensor-array on given image path and image_shape [height, width]'''
import tensorflow as tf
image = tf.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=3)
if clr_normalize:
image = tf.image.resize_images(image, image_shape) / 127.5 - 1
else:
image = tf.image.resize_images(image, image_shape)
return image
FWIW, it IS officially recommanded to put all your imports at the top of the module, before any other definitions (functions, classes, whatever). Importing within a function should only be used as a Q&D last resort fix for circular imports (which are usually a sure sign you have a design issue and should actually be fixed by fixing the design itself).
Upvotes: 1
Reputation: 485
As @MatrixTai has suggested in the comments, you could make utils.py
like this:
import tensorflow as tf
def load_image(.........):
............
and in main.py
, you could import util
as you want to.
Upvotes: 0