I created the cord which has function of prediction for labels.
Also, I created the cord of neural net based on pytorch.
So I need to convert to tensor type from numpy array.
First, the input data are acquired as numpy array and be put on the list format.
So, I used (Input = torch.FloatTensor(Input)) to convert to tensor from numpy list.
Next, I tried to the follow changes. I want to know the way to fix it.
response = np.argmax(np.bincount(temp))
↓
response = torch.argmax(torch.bincount(temp))
Then , the error occurred.
Error message
temp = torch.FloatTensor(temp)
ValueError: only one element tensors can be converted to Python scalars
cord
from collections import deque
from threading import Lock
import myo
import time
import numpy as np
from time import sleep
import torch
import torch.nn as nn
from bindsnet.network import Network
from including.models import LogisticRegression
model_path = "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/pytorchsession/snn.pth"
model = LogisticRegression(64,6)
class MyListener(myo.DeviceListener):
def __init__(self, queue_size=8):
self.lock = Lock()
self.emg_data_queue = deque(maxlen=queue_size)
self.orientation_data_queue = deque(maxlen=queue_size)
self.acceleration_data_queue = deque(maxlen=queue_size)
self.gyroscope_data_queue = deque(maxlen=queue_size)
self.rssi_data_queue = deque(maxlen=100)
self.pose = myo.Pose.rest
self.connected = False
self.battery_level = 100
self.emg_enabled = False
self.locked = False
self.rssi = None
self.emg = None
self.device_name = None
self.device = None
self.myo_firmware = None
self.arm = None
self.x_direction = None
self.sync = None
def on_paired(self, event):
if __name__ == '__main__':
print('paired')
else:
pass
def on_unpaired(self, event):
if __name__ == '__main__':
print('unpaired')
else:
pass
def on_connected(self, event):
self.device = event.device
event.device.unlock()
event.device.lock()
event.device.stream_emg(True)
self.connected = True
self.device_name = event.device_name
self.myo_firmware = '.'.join(map(str, event.firmware_version[:-1]))
def on_disconnected(self, event):
self.connected = False
def on_arm_synced(self, event):
self.sync = True
self.arm = event.arm
self.x_direction = event.x_direction
if __name__ == '__main__':
print(self.x_direction)
else:
pass
def on_arm_unsynced(self, event):
self.sync = False
if __name__ == '__main__':
print(f'arm unsynced : {event.arm}')
else:
pass
def on_unlocked(self, event):
self.locked = False
def on_locked(self, event):
self.locked = True
def on_pose(self, event):
self.pose = event.pose
def on_orientation(self, event):
with self.lock:
self.orientation_data_queue.append((event.timestamp,
event.orientation))
self.gyroscope_data_queue.append((event.timestamp,
event.gyroscope))
self.acceleration_data_queue.append((event.timestamp,
event.acceleration))
def on_rssi(self, event):
with self.lock:
self.rssi_data_queue.append(-event.rssi)
def on_battery_level(self, event):
self.battery_level = event.battery_level
def on_emg(self, event):
with self.lock:
self.emg_data_queue.append((event.timestamp,
event.emg))
def on_warmup_completed(self, event):
event.device.stream_emg(True)
self.emg_enabled = True
def get_emg_data(self):
with self.lock:
return list(self.emg_data_queue)
def get_orientation_data(self):
with self.lock:
return list(self.orientation_data_queue)
def get_gyroscope_data(self):
with self.lock:
return list(self.gyroscope_data_queue)
def get_accelerometor_data(self):
with self.lock:
return list(self.acceleration_data_queue)
if __name__ == '__main__':
myo.init(bin_path=r'C:\Users\田澤龍之介\Desktop\myo-sdk-win-0.9.0\bin')
HUB = myo.Hub()
model.eval()
listener = MyListener()
start = time.time()
temp = []
with HUB.run_in_background(listener.on_event):
while True:
data = listener.get_emg_data()
print(temp)
if time.time() - start >= 1:
#response = np.argmax(np.bincount(temp)) # conventionally numpy format
temp = torch.FloatTensor(temp) # I tried to convert to tensor from numpy list
response = torch.argmax(torch.bincount(temp))
#print(response)
print("Predicted gesture: {0}".format(response))
temp = []
start = time.time()
if len(data) > 0: # len(data) = 8
tmp = []
for v in listener.get_emg_data():
tmp.append(v[1])
tmp = list(np.stack(tmp).flatten())
tmp = torch.tensor(tmp)
if len(tmp) >= 64:
pred = model(tmp)
temp.append(pred[0])
sleep(0.01)