demo.py程式問題
1.demo.py 問題
請問我在執行Real Time的時候遇到了如下的問題:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
3 while True:
4 _, frame = video_capture.read()
----> 5 canvas = detect(frame, net, transform)
6 cv2.imshow('Video', canvas)
7 if cv2.waitKey(1) & 0xFF == ord('q'):
in detect(frame, net, transform)
8 x = torch.from_numpy(frame_t).permute(2, 0, 1)
9 x = Variable(x.unsqueeze(0))
---> 10 y = net(x)
11 detections = y.data
12 scale = torch.Tensor([width, height, width, height])
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
545 result = self._slow_forward(*input, **kwargs)
546 else:
--> 547 result = self.forward(*input, **kwargs)
548 for hook in self._forward_hooks.values():
549 hook_result = hook(self, input, result)
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\ssd.py in forward(self, x)
73 # apply vgg up to conv4_3 relu
74 for k in range(23):
---> 75 x = self.vgg[k](x)
76
77 s = self.L2Norm(x)
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
545 result = self._slow_forward(*input, **kwargs)
546 else:
--> 547 result = self.forward(*input, **kwargs)
548 for hook in self._forward_hooks.values():
549 hook_result = hook(self, input, result)
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
341
342 def forward(self, input):
--> 343 return self.conv2d_forward(input, self.weight)
344
345 class Conv3d(_ConvNd):
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)
338 _pair(0), self.dilation, self.groups)
339 return F.conv2d(input, weight, self.bias, self.stride,
--> 340 self.padding, self.dilation, self.groups)
341
342 def forward(self, input):
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'
2.輸入資料型態.ipynb 問題
我在loading 完圖片後執行images, targets = next(batch_iterator) 遇到問題如下:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
----> 1 images, targets = next(batch_iterator)
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
344 def __next__(self):
345 index = self._next_index() # may raise StopIteration
--> 346 data = self.dataset_fetcher.fetch(index) # may raise StopIteration
347 if self.pin_memory:
348 data = _utils.pin_memory.pin_memory(data)
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
D:\program\anaconda\envs\tf_gpu_1.13\lib\site-packages\torch\utils\data\_utils\fetch.py in (.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
d:\freshman\python\Object Detection 程式導讀\data\coco.py in __getitem__(self, index)
108 target is the object returned by ``coco.loadAnns``.
109 """
--> 110 im, gt, h, w = self.pull_item(index)
111 return im, gt
112
d:\freshman\python\Object Detection 程式導讀\data\coco.py in pull_item(self, index)
130 assert osp.exists(path), 'Image path does not exist: {}'.format(path)
131 img = cv2.imread(osp.join(self.root, path))
--> 132 height, width, _ = img.shape
133 if self.target_transform is not None:
134 target = self.target_transform(target, width, height)
AttributeError: 'NoneType' object has no attribute 'shape'
3.Build RetinaNet.ipynb、Build SSD_VGG.ipynb 問題
在執行 基本設定 時,遇到了:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in
4 '''tensor type會依照cpu或gpu有所不同'''
5 if torch.cuda.is_available():
----> 6 if args.cuda:
7 torch.set_default_tensor_type('torch.cuda.FloatTensor')
8 if not args.cuda:
NameError: name 'args' is not defined
請問該如何解決?
4.檔案中出現很多 _DS_Store 的檔案,請問那是甚麼檔案?
回答列表
-
2020/01/31 上午 10:42楊哲寧贊同數:0不贊同數:0留言數:0
您好,針對第一題,可以把有cuda的部分移除,使用cpu來inference:
原本:
xx = Variable(x.unsqueeze(0)).type(torch.cuda.FloatTensor) # wrap tensor in Variable
if torch.cuda.is_available():
xx = xx.cuda()
net.cuda()
'''Forward Pass'''
y = net(xx)
移除cuda:
xx = Variable(x.unsqueeze(0)).type(torch.FloatTensor) # wrap tensor in Variable
y = net(xx)
並且可以在一開始設定:
torch.set_default_tensor_type('torch.FloatTensor')
針對第二題:應該是沒有讀到照片,可以確認一下照片路徑
針對第三題:
可以在上方增添這這段程式碼
class Parameter():
def __init__(self):
self.cuda=False ##(自己設置)
args=Parameter()
-
2020/02/11 下午 00:26Felix贊同數:0不贊同數:0留言數:0
cv2.VideoCapture是指從攝影機讀取影像,如果沒有連接攝影機,自然不會撈回影像資料,所以相關需要的資料參數都會是empty
-
2020/02/11 下午 01:03Felix贊同數:0不贊同數:0留言數:0
如果沒有USB攝影機或是不是在筆電執行,可以採用我的方式,直接讀取串流影片
把原本的
# 直接從攝影機撈取影像
# video_capture = cv2.VideoCapture(0)
替換成
# 採用線上影片串流方式
# 安裝套件
# !pip install -U pafy youtube_dl
import pafy
url = 'https://www.youtube.com/watch?v=MPh1p0zeieo'
video = pafy.new(url)
# 查看最好的畫質
# best = video.getbest()
# print(best.resolution, best.extension)
best = video.getbest(preftype="mp4")
video_capture = cv2.VideoCapture(best.url)