PythonでプログラムしていますがGLOBALなオブジェクトの扱いがよくわかりません。
以下のソースを見てください。
python
1import cv2 2import transformer 3import torch 4import utils 5from imutils import paths 6import argparse 7import itertools 8 9PRESERVE_COLOR = False 10 11 12 13net=None 14device=None 15 16def myinit(): 17 net=None 18 device=None 19 """ 20 Captures and saves an image, perform style transfer, and again saves the styled image. 21 Reads the styled image and show in window. 22 23 Saving and loading SHOULD BE eliminated, however this produces too much whitening in 24 the "generated styled image". This may be caused by the async nature of VideoCapture, 25 and I don't know how to fix it. 26 """ 27 mypath="C:\Users\oakoa\work\fast_neural_style_transfer\model_path" 28 29 modelPaths = paths.list_files(mypath, validExts=('.pth',)) 30 modelPaths = sorted(list(modelPaths)) 31 32 modelIter = itertools.cycle(modelPaths) 33 # Device 34 device = ("cuda" if torch.cuda.is_available() else "cpu") 35 36 # Load Transformer Network 37 print("Loading Transformer Network") 38 net = transformer.TransformerNetwork() 39 modelPath = next(modelIter) 40 net.load_state_dict(torch.load(modelPath)) 41 net = net.to(device) 42 print("Done Loading Transformer Network") 43 44 45 46 47def mainfunc(): 48 myinit() 49 50 # Set webcam settings 51 mycam = cv2.VideoCapture("rtsp://192.168.100.142:8554/in.mp4") 52 53 # Main loop 54 with torch.no_grad(): 55 count = 1 56 while True: 57 # Get webcam input 58 ret_val, img = mycam.read() 59 60 # Mirror 61 #img = cv2.flip(img, 1) 62 key=0 63 try: 64 height = img.shape[0] 65 width = img.shape[1] 66 d=1 67 img = cv2.resize(img , (int(width*d), int(height*d))) 68 69 print("P1") 70 # Free-up unneeded cuda memory 71 torch.cuda.empty_cache() 72 73 print("P2") 74 # Generate image 75 content_tensor = utils.itot(img).to(device) 76 print("P3") 77 generated_tensor = net(content_tensor) 78 print("P4") 79 generated_image = utils.ttoi(generated_tensor.detach()) 80 if (PRESERVE_COLOR): 81 generated_image = utils.transfer_color(content_image, generated_image) 82 img2 = cv2.imdecode(cv2.imencode(".png", generated_image)[1], cv2.IMREAD_UNCHANGED) 83 84 count += 2 85 # Show webcam 86 #img3 = cv2.flip(img2, 1) 87 d=1 88 img = cv2.resize(img , (int(width*d), int(height*d))) 89 cv2.imshow('real', img) 90 img2 = cv2.resize(img2 , (int(width*d), int(height*d))) 91 cv2.imshow('Demo webcam', img2) 92 key = cv2.waitKey(1) & 0xFF 93 except: 94 print('error occured') 95 96 if key == ord('n'): 97 modelPath = next(modelIter) 98 net.load_state_dict(torch.load(modelPath)) 99 net = net.to(device) 100 101 if key == ord('y'): #save on pressing 'y' 102 cv2.imwrite(f'images/capture/c{count}.png',img2) 103 104 105 elif key == ord('q'): 106 break # q to quit 107 108 # Free-up memories 109 mycam.release() 110 cv2.destroyAllWindows() 111 112# print(next(modelPaths)) 113mainfunc()
これなのですが実行すると
generated_tensor = net(content_tensor)
のところで落ちてしまいます。
しかし初期化のコードを関数でするのをやめて直接書くとうまく動きます。
python
1import cv2 2import transformer 3import torch 4import utils 5from imutils import paths 6import argparse 7import itertools 8 9PRESERVE_COLOR = False 10 11 12 13net=None 14device=None 15 16def mainfunc(): 17 net=None 18 device=None 19 """ 20 Captures and saves an image, perform style transfer, and again saves the styled image. 21 Reads the styled image and show in window. 22 23 Saving and loading SHOULD BE eliminated, however this produces too much whitening in 24 the "generated styled image". This may be caused by the async nature of VideoCapture, 25 and I don't know how to fix it. 26 """ 27 mypath="C:\Users\oakoa\work\fast_neural_style_transfer\model_path" 28 29 modelPaths = paths.list_files(mypath, validExts=('.pth',)) 30 modelPaths = sorted(list(modelPaths)) 31 32 modelIter = itertools.cycle(modelPaths) 33 # Device 34 device = ("cuda" if torch.cuda.is_available() else "cpu") 35 36 # Load Transformer Network 37 print("Loading Transformer Network") 38 net = transformer.TransformerNetwork() 39 modelPath = next(modelIter) 40 net.load_state_dict(torch.load(modelPath)) 41 net = net.to(device) 42 print("Done Loading Transformer Network") 43 # Set webcam settings 44 mycam = cv2.VideoCapture("rtsp://192.168.100.142:8554/in.mp4") 45 46 # Main loop 47 with torch.no_grad(): 48 count = 1 49 while True: 50 # Get webcam input 51 ret_val, img = mycam.read() 52 53 # Mirror 54 #img = cv2.flip(img, 1) 55 key=0 56 try: 57 height = img.shape[0] 58 width = img.shape[1] 59 d=1 60 img = cv2.resize(img , (int(width*d), int(height*d))) 61 62 print("P1") 63 # Free-up unneeded cuda memory 64 torch.cuda.empty_cache() 65 66 print("P2") 67 # Generate image 68 content_tensor = utils.itot(img).to(device) 69 print("P3") 70 generated_tensor = net(content_tensor) 71 print("P4") 72 generated_image = utils.ttoi(generated_tensor.detach()) 73 if (PRESERVE_COLOR): 74 generated_image = utils.transfer_color(content_image, generated_image) 75 img2 = cv2.imdecode(cv2.imencode(".png", generated_image)[1], cv2.IMREAD_UNCHANGED) 76 77 count += 2 78 # Show webcam 79 #img3 = cv2.flip(img2, 1) 80 d=1 81 img = cv2.resize(img , (int(width*d), int(height*d))) 82 cv2.imshow('real', img) 83 img2 = cv2.resize(img2 , (int(width*d), int(height*d))) 84 cv2.imshow('Demo webcam', img2) 85 key = cv2.waitKey(1) & 0xFF 86 except: 87 print('error occured') 88 89 if key == ord('n'): 90 modelPath = next(modelIter) 91 net.load_state_dict(torch.load(modelPath)) 92 net = net.to(device) 93 94 if key == ord('y'): #save on pressing 'y' 95 cv2.imwrite(f'images/capture/c{count}.png',img2) 96 97 98 elif key == ord('q'): 99 break # q to quit 100 101 # Free-up memories 102 mycam.release() 103 cv2.destroyAllWindows() 104 105# print(next(modelPaths)) 106mainfunc()
これから見るとpythonでGLOBALなオブジェクトを宣言するときはどうも=Noneではまずいらしいですが
どうすべきか知っている人はいませんか?
追記
GLOBALな変数についてしったのでこう直しても同じでした
python
1 2def myinit(): 3 global modelIter 4 global device 5 global mynet 6 7 mypath="C:\Users\oakoa\work\fast_neural_style_transfer\model_path" 8 9 modelPaths = paths.list_files(mypath, validExts=('.pth',)) 10 modelPaths = sorted(list(modelPaths)) 11 12 modelIter= itertools.cycle(modelPaths) 13 print(modelIter) 14 # Device 15 device = ("cuda" if torch.cuda.is_available() else "cpu") 16 # Load Transformer Network 17 print("Loading Transformer Network") 18 mynet = transformer.TransformerNetwork() 19 print(mynet) 20 modelPath = next(modelIter) 21 mynet.load_state_dict(torch.load(modelPath)) 22 mynet = mynet.to(device) 23 print("Done Loading Transformer Network") 24 25 26
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/03 06:21 編集
2019/08/03 06:25
2019/08/03 06:30
2019/08/03 06:35
2019/08/03 06:47
2019/08/03 06:49
2019/08/03 06:59
2019/08/03 07:00
2019/08/03 07:01