Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensornets not working with threading.Threads #48

Open
hkgautam opened this issue Sep 10, 2019 · 0 comments
Open

Tensornets not working with threading.Threads #48

hkgautam opened this issue Sep 10, 2019 · 0 comments

Comments

@hkgautam
Copy link

Hi ! I was using tensornets with threading.Threads. It shows error with some tensorflow shapes. Please look after this and provide me with the solution.

Te code is

import threading
import tensorflow as tf
import tensornets as nets
import cv2
import numpy as np
import time
import os

import argparse

class MyThread(threading.Thread):

def run(self):

    # os.popen("start cmd /k python detection.py")

    print("------------------------------{} started!----------------------------------------".format(self.getName()))              # "Thread-x started!"


    inputs = tf.placeholder(tf.float32, [None, 416, 416, 3])
    model = nets.YOLOv3COCO(inputs, nets.Darknet19)
    #model = nets.YOLOv2(inputs, nets.Darknet19)

    #frame=cv2.imread("D://pyworks//yolo//truck.jpg",1)

    cap = cv2.VideoCapture('rtsp://root:[email protected]:554/axis-media/media.amp')

    classes={'0':'person','1':'bicycle','2':'car','3':'bike','5':'bus','7':'truck'}
    list_of_classes=[0,1,2,3,5,7]
    print("Done")

    config = tf.ConfigProto()
    config.intra_op_parallelism_threads = 44
    config.inter_op_parallelism_threads = 44
    # tf.session(config=config)
    
    with tf.Session(config=config) as sess:

        print("---------------------------{} session running-----------------------".format(self.getName()))
        sess.run(model.pretrained())
    # Create a VideoCapture object and read from input file
        # cap = cv2.VideoCapture('C://Users//essi//Downloads//vehicle.mp4')
        
        # Check if camera opened successfully
        if (cap.isOpened()== False):
            print("Error opening video  file")

        co=0
        # Read until video is completed
        while(cap.isOpened()):

        # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                
                co=co+1
                print(co)
                img=cv2.resize(frame,(416,416))
                    
                if (co%1==0):

                    imge=np.array(img).reshape(-1,416,416,3)
                    start_time=time.time()
                    preds = sess.run(model.preds, {inputs: model.preprocess(imge)})
                    # preds= nets.preprocess(model,imge)

                    print("--- %s seconds ---" % (time.time() - start_time))
                    boxes = model.get_boxes(preds, imge.shape[1:3])
                    cv2.namedWindow('video feed',cv2.WINDOW_NORMAL)

                    cv2.resizeWindow('video feed', 700,500)
                    #print("--- %s seconds ---" % (time.time() - start_time))
                    boxes1=np.array(boxes)
                    for j in list_of_classes:
                        count =0

                        if str(j) in classes:
                            lab=classes[str(j)]
                        if len(boxes1) !=0:
                            for i in range(len(boxes1[j])):
                                box=boxes1[j][i]

                                if boxes1[j][i][4]>=.40:

                                    count += 1

                                    cv2.rectangle(img,(box[0],box[1]),(box[2],box[3]),(0,255,0),1)
                                    cv2.putText(img, lab, (box[0],box[1]), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), lineType=cv2.LINE_AA)
                        print(lab,": ",count)
            # Display the resulting frame
                cv2.imshow('video feed', img)

            # Press Q on keyboard to  exit
                if cv2.waitKey(40) & 0xFF == ord('q'):
                    break
        
    # Break the loop
    #   else:
    #     break
    
    # When everything done, release
    # the video capture object
    cap.release()
    
    # Closes all the frames
    cv2.destroyAllWindows()

    
    
    print("------------------------------{} finished!----------------------------------------".format(self.getName()))             # "Thread-x finished!"

def main():

parser = argparse.ArgumentParser()
parser.add_argument('-t', help='help')
args = parser.parse_args()
args=vars(args)
print(args)

for x in range(int(args['t'])):                                     # Four times...
    mythread = MyThread(name = "Thread-{}".format(x+1))  # ...Instantiate a thread and pass a unique ID to it
    mythread.start()            

if name == 'main':main()

AND IT SHOWS THE OUTPUT AS

------------------------------Thread-1 started!----------------------------------------
WARNING: Logging before flag parsing goes to stderr.
------------------------------Thread-2 started!----------------------------------------
W0910 10:55:52.911941 5408 deprecation_wrapper.py:119] From .\threadwc.py:21: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0910 10:55:52.911941 5184 deprecation_wrapper.py:119] From .\threadwc.py:21: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0910 10:55:53.041654 5408 deprecation_wrapper.py:119] From C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py:238: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

W0910 10:55:53.041654 5184 deprecation_wrapper.py:119] From C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py:238: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

W0910 10:55:53.647777 5408 deprecation_wrapper.py:119] From C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py:279: The name tf.get_variable_scope is deprecated. Please use tf.compat.v1.get_variable_scope instead.

Done
2019-09-10 10:55:58.563571: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
---------------------------Thread-2 session running-----------------------
Done
---------------------------Thread-1 session running-----------------------
C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py:319: UserWarning: The sizes of symbolic and actual weights do not match. Never mind if you are trying to load stem layers only.
warnings.warn('The sizes of symbolic and actual weights do not match. '
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1864, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 4 and 1 for 'Assign_291' (op: 'Assign') with input shapes: [1,1,512,256], [255].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File ".\threadwc.py", line 41, in run
sess.run(model.pretrained())
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\pretrained.py", line 99, in _direct
return fun(scope, return_fn=pretrained_initializer)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\pretrained.py", line 695, in load_ref_yolo_v3_coco
return return_fn(scopes, values)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py", line 333, in pretrained_initializer
ops = [w.assign(v) for (w, v) in zip(weights[:-2], values[:-2])]
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py", line 333, in
ops = [w.assign(v) for (w, v) in zip(weights[:-2], values[:-2])]
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variables.py", line 1952, in assign
name=name)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\state_ops.py", line 227, in assign
validate_shape=validate_shape)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_state_ops.py", line 69, in assign
use_locking=use_locking, name=name)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in create_op
op_def=op_def)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2027, in init
control_input_ops)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1867, in _create_c_op
raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 4 and 1 for 'Assign_291' (op: 'Assign') with input shapes: [1,1,512,256], [255].

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1864, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 4 and 1 for 'Assign_291' (op: 'Assign') with input shapes: [1,1,512,256], [255].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File ".\threadwc.py", line 41, in run
sess.run(model.pretrained())
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\pretrained.py", line 99, in _direct
return fun(scope, return_fn=pretrained_initializer)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\pretrained.py", line 695, in load_ref_yolo_v3_coco
return return_fn(scopes, values)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py", line 333, in pretrained_initializer
ops = [w.assign(v) for (w, v) in zip(weights[:-2], values[:-2])]
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensornets\utils.py", line 333, in
ops = [w.assign(v) for (w, v) in zip(weights[:-2], values[:-2])]
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variables.py", line 1952, in assign
name=name)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\state_ops.py", line 227, in assign
validate_shape=validate_shape)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_state_ops.py", line 69, in assign
use_locking=use_locking, name=name)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in create_op
op_def=op_def)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2027, in init
control_input_ops)
File "C:\Users\essi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1867, in _create_c_op
raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 4 and 1 for 'Assign_291' (op: 'Assign') with input shapes: [1,1,512,256], [255].

threadwc - Copy.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant