question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NameError: name 'tf' is not defined

See original GitHub issue

Hi,

I am trying to make a REST api on python where i put some AI models to predict, so i have to load the using tensorflow. I want to add some workers, so that i can call the server multiple times asynchronously.

I run the script and i get the error that tf name is not defined. I have that moduled imported as import tensorflow as tf but it seems that doesn’t reconigze it.

Does someone know what’s the problem? Thanks!

Below my code:

import uvicorn
from fastapi import FastAPI, Request ,Depends
import cv2
import uvicorn
from fastapi import FastAPI, Request ,Depends
import cv2
import io
from time import sleep
import PIL.Image as Image
import numpy as np
import base64
import tensorflow as tf
import nest_asyncio
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
import binascii
import json
from pathlib import Path

app = FastAPI()
nest_asyncio.apply()
CATEGORIES = ['neg', 'pos']
executor = ThreadPoolExecutor(5)
futures=[]
pred=[]
LIMIT=100


with open('DenseNet169/DenseNet169_model_architecture_contodo2.json', 'r') as f:
		DenseNetModel = tf.keras.models.model_from_json(f.read())
DenseNetModel.load_weights('DenseNet169/DenseNet169_model_weights_contodo2.h5')
print("DENSENET")
with open('MobileNet/MobileNet_model_architecture_contodo2.json', 'r') as f:
		MobileNetModel = tf.keras.models.model_from_json(f.read())
MobileNetModel.load_weights('MobileNet/MobileNet_model_weights_contodo2.h5')
print("MOBILENET")
with open('Xception/Xception_model_architecture_contodo3.json', 'r') as f:
		SqueezeNetModel = tf.keras.models.model_from_json(f.read())
SqueezeNetModel.load_weights('Xception/Xception_model_weights_contodo3.h5')
print("SQUEEZE")
with open('Xception_model_architecture_contodo2.json', 'r') as f:
		XceptionModel = tf.keras.models.model_from_json(f.read())
XceptionModel.load_weights('Xception/Xception_model_weights_contodo2.h5')
print("XCEPTION")
with open('InceptionResNetV2/InceptionResNetV2_model_architecture_contodo2.json', 'r') as f:
		InceptionResNetModel = tf.keras.models.model_from_json(f.read())
InceptionResNetModel.load_weights('InceptionResNetV2/InceptionResNetV2_model_weights_contodo2.h5')



@app.get('/')
def index():
    return {'message': 'Hello, stranger'}
@app.post('/transform')
async def get_name(request: Request):
    img = await request.json()
    img = img["bytes"]
    img = base64.b64decode(img)
    nparr = np.fromstring(img, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    
    lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(img)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(4,4))
    cl = clahe.apply(l)
    limg = cv2.merge((cl,a,b))
    gray = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
    # threshold 
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
    hh, ww = thresh.shape
    # make bottom 2 rows black where they are white the full width of the image
    thresh[hh-3:hh, 0:ww] = 0
    # get bounds of white pixels
    white = np.where(thresh>=200)
    xmin, ymin, xmax, ymax = np.min(white[1]), np.min(white[0]), np.max(white[1]), np.max(white[0])
    if (xmax - xmin) > LIMIT and (ymax-ymin) > LIMIT: 
    # crop the image at the bounds adding back the two blackened rows at the bottom
        crop = gray[ymin:ymax+3, xmin:xmax]
        dim = (224, 224)
        resized = cv2.resize(crop, dim, interpolation = cv2.INTER_AREA)
    else:
        dim = (224, 224)
        resized = cv2.resize(crop, dim, interpolation = cv2.INTER_AREA)
    lists = resized.tolist()
    ret = json.dumps(lists)
    return ret

def votePredict(pred):
    cont=0
    emaitza="neg"
    for i in range(0,len(pred)):
        if pred[i]=="pos":
            cont=cont+1
    if(cont>2):
        emaitza="pos"
    return emaitza

def predictImage(img):
    sleep(5)
    img=img["bytes"]
    img=json.loads(img)
    img=np.asarray(img)
    img=cv2.merge((img,img,img))
    img_np=np.float64(img)/255.0
    img = img_np.reshape((-1, 224, 224, 3))
    
    futures.append(executor.submit(DenseNetModel.predict, (img)))
    futures.append(executor.submit(MobileNetModel.predict, (img)))
    futures.append(executor.submit(SqueezeNetModel.predict, (img)))
    futures.append(executor.submit(XceptionModel.predict, (img)))
    futures.append(executor.submit(InceptionResNetModel.predict, (img)))
    
    for future in concurrent.futures.as_completed(futures):
            pred.append(CATEGORIES[np.argmax(future.result())])
    aciertos=votePredict(pred)
    return aciertos
    

@app.post('/predict')
async def predict(request:Request):
    futures.clear()
    pred.clear()
    img=await request.json()
    return predictImage(img)



if __name__ == '__main__':
    uvicorn.run("REST:app", host="127.0.0.1", port=8888 , workers=2)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
euri10commented, May 27, 2021

probably not uvicorn related but more an issue with sharing stuff between processes, since you use 2 workers, see https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes

will close this, happy to reopen if there is something tied to uvicorn that we missed.

0reactions
pablovallecommented, May 24, 2021

The code that gives error:

import uvicorn
from fastapi import FastAPI, Request ,Depends
import cv2
import io
from time import sleep
import PIL.Image as Image
import numpy as np
import base64
import tensorflow as tf
import nest_asyncio
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
import binascii
import json
from pathlib import Path
app = FastAPI()
nest_asyncio.apply()
CATEGORIES = ['neg', 'pos']
executor = ThreadPoolExecutor(5)
futures=[]
pred=[]
LIMIT=100



with open('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/DenseNet169/DenseNet169_model_architecture_contodo2.json', 'r') as f:
		DenseNetModel = tf.keras.models.model_from_json(f.read())
DenseNetModel.load_weights('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/DenseNet169/DenseNet169_model_weights_contodo2.h5')
print("DENSENET")
with open('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/MobileNet/MobileNet_model_architecture_contodo2.json', 'r') as f:
		MobileNetModel = tf.keras.models.model_from_json(f.read())
MobileNetModel.load_weights('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/MobileNet/MobileNet_model_weights_contodo2.h5')
print("MOBILENET")
with open('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/Xception/Xception_model_architecture_contodo3.json', 'r') as f:
		SqueezeNetModel = tf.keras.models.model_from_json(f.read())
SqueezeNetModel.load_weights('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/Xception/Xception_model_weights_contodo3.h5')
print("SQUEEZE")
with open('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/Xception/Xception_model_architecture_contodo2.json', 'r') as f:
		XceptionModel = tf.keras.models.model_from_json(f.read())
XceptionModel.load_weights('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/Xception/Xception_model_weights_contodo2.h5')
print("XCEPTION")
with open('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/InceptionResNetV2/InceptionResNetV2_model_architecture_contodo2.json', 'r') as f:
		InceptionResNetModel = tf.keras.models.model_from_json(f.read())
InceptionResNetModel.load_weights('C:/Users/pablo/OneDrive/Escritorio/3curso/PBL6/saved/InceptionResNetV2/InceptionResNetV2_model_weights_contodo2.h5')


@app.get('/')
def index():
    return {'message': 'Hello, stranger'}


@app.post('/transform')
async def get_name(request: Request):
    img = await request.json()
    img = img["bytes"]
    img = base64.b64decode(img)
    nparr = np.fromstring(img, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    
    lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(img)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(4,4))
    cl = clahe.apply(l)
    limg = cv2.merge((cl,a,b))
    gray = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
    hh, ww = thresh.shape
    thresh[hh-3:hh, 0:ww] = 0
    white = np.where(thresh>=200)
    xmin, ymin, xmax, ymax = np.min(white[1]), np.min(white[0]), np.max(white[1]), np.max(white[0])
    if (xmax - xmin) > LIMIT and (ymax-ymin) > LIMIT: 
    
        crop = gray[ymin:ymax+3, xmin:xmax]
        
        dim = (224, 224)
        resized = cv2.resize(crop, dim, interpolation = cv2.INTER_AREA)
    else:
        dim = (224, 224)
        resized = cv2.resize(crop, dim, interpolation = cv2.INTER_AREA)
    lists = resized.tolist()
    ret = json.dumps(lists)
    return ret

def votePredict(pred):
    cont=0
    emaitza="neg"
    for i in range(0,len(pred)):
        if pred[i]=="pos":
            cont=cont+1
    if(cont>2):
        emaitza="pos"
    return emaitza

def predictImage(img):
    sleep(5)
    img=img["bytes"]
    img=json.loads(img)
    img=np.asarray(img)
    img=cv2.merge((img,img,img))
    img_np=np.float64(img)/255.0
    img = img_np.reshape((-1, 224, 224, 3))

    futures.append(executor.submit(DenseNetModel.predict, (img)))
    futures.append(executor.submit(MobileNetModel.predict, (img)))
    futures.append(executor.submit(SqueezeNetModel.predict, (img)))
    futures.append(executor.submit(XceptionModel.predict, (img)))
    futures.append(executor.submit(InceptionResNetModel.predict, (img)))
  
    for future in concurrent.futures.as_completed(futures):
            pred.append(CATEGORIES[np.argmax(future.result())])
    aciertos=votePredict(pred)
    return aciertos
    

@app.post('/predict1')
async def predict(request:Request):
    futures.clear()
    pred.clear()
    img=await request.json()
    return predictImage(img)


uvicorn.run("REST-ekhi:app", host="127.0.0.1", port=8888 , workers=2)

what i do to run is:

  • Open the anaconda prompt
  • Go to the script path
  • Activate the environment
  • execute the script (python rest.py)

The error I get is:

2021-05-24 07:52:05.826816: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
DENSENET
MOBILENET
SQUEEZE
XCEPTION
←[32mINFO←[0m:     Uvicorn running on ←[1mhttp://127.0.0.1:8888←[0m (Press CTRL+C to quit)
←[32mINFO←[0m:     Started parent process [←[36m←[1m9500←[0m]
2021-05-24 07:52:32.744391: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-05-24 07:52:32.747416: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\pablo\OneDrive\Escritorio\3curso\REST-ekhi.py", line 30, in <module>
    DenseNetModel = tf.keras.models.model_from_json(f.read())
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 122, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 175, in deserialize
    printable_module_name='layer')
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 358, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 617, in from_config
    config, custom_objects)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1214, in reconstruct_from_config
    process_node(layer, node_data)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1162, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 926, in __call__
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    input_list)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 105, in spawn_main
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    exitcode = _main(fd)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 114, in _main
    outputs = call_fn(cast_inputs, *args, **kwargs)
    prepare(preparation_data)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\layers\core.py", line 903, in call
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    result = self.function(inputs, **kwargs)
  File "<ipython-input-3-a06f36a15934>", line 93, in <lambda>
    run_name="__mp_main__")
NameError: name 'tf' is not defined
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\pablo\OneDrive\Escritorio\3curso\REST-ekhi.py", line 30, in <module>
    DenseNetModel = tf.keras.models.model_from_json(f.read())
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 122, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 175, in deserialize
    printable_module_name='layer')
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 358, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 617, in from_config
    config, custom_objects)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1214, in reconstruct_from_config
    process_node(layer, node_data)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1162, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 926, in __call__
    input_list)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\pablo\anaconda3\envs\tensorenviron2\lib\site-packages\tensorflow\python\keras\layers\core.py", line 903, in call
    result = self.function(inputs, **kwargs)
  File "<ipython-input-3-a06f36a15934>", line 93, in <lambda>
NameError: name 'tf' is not defined

Technologies involved:

  • Windows 10
  • Anaconda 2.0.3
  • python 3.7.10
  • tensorflow 2.3.0
  • uvicorn 0.13.4
Read more comments on GitHub >

github_iconTop Results From Across the Web

'tf' is not defined on load_model() - using lambda
I have a Keras model that I am trying to export and use in a different python code.
Read more >
NameError: name 'tf' is not defined · Issue #14 - GitHub
Hi, I have run your jupyter notebook file succesfully. I have then saved nn4_small2 into a .h5 file. I'm now trying to convert...
Read more >
NameError: name 'tf' is not defined?? : r/learnpython - Reddit
Tried to run some entry code rank1_tensor = tf.Variable(["test", "ok", "tim"], tf.string) ... NameError: name 'tf' is not defined??
Read more >
tf.keras.Sequential | TensorFlow v2.11.0
Sequential groups a linear stack of layers into a tf.keras.Model. ... When using the delayed-build pattern (no input shape specified), you can
Read more >
'tf' is not defined on load_model() - using lambda - DevPress
I have a Keras model that I am trying to export and use in a different python code. Here is my code: from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found