def save_model(model, tort = 'Val'): images_so_far = 0 if tort == 'Train': dataloader = dataloader_train else: dataloader = dataloader_val for i, data in enumerate(dataloader): samples = data if use_gpu: inputs = Variable(samples['image'].cuda()) trajectories = Variable(samples['trajectories'].cuda()) else: inputs, trajectories = Variable(samples['image']), Variable(samples['trajectories']) outputs = model(inputs) for j in range(inputs.size()[0]): outData = GMR(outputs[j], trajectories[j]) images_so_far += 1 file_path = 'predictedTraj' + tort + '/' + str(images_so_far) + '.txt' np.savetxt(file_path, outData.cpu().data.numpy())
def trajPredictor(model, image): mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) transform_data = transforms.Compose([ Rescale((240, 320)), #Normalize(mean, std), ToTensor() ]) if use_gpu: inputs = Variable(transform_data(image).cuda()) else: inputs = Variable(transform_data(image)) inputs = inputs.unsqueeze(0) outputs = model(inputs) traj = GMR(outputs[0]) if use_gpu: nptraj = traj.data.cpu().numpy() else: nptraj = traj.data.numpy() return nptraj
def visualize_model(model, num_images=4, tort = 'Val'): images_so_far = 0 fig = plt.figure() model.train(False) traj_len = 200 if use_gpu: torchType = torch.cuda.FloatTensor else: torchType = torch.FloatTensor time = Variable(torch.arange(0, (traj_len - 1) / traj_len, 1 / traj_len).type(torchType) , requires_grad=False) if tort == 'Train': dataloader = dataloader_train else: dataloader = dataloader_val for i, data in enumerate(dataloader): samples = data if use_gpu: inputs = Variable(samples['image'].cuda()) trajectories = Variable(samples['trajectories'].cuda()) else: inputs, trajectories = Variable(samples['image']), Variable(samples['trajectories']) outputs = model(inputs) for j in range(outputs.size()[0]): outData = GMR(outputs[j]) loss = (trajectories[j] - outData).pow(2).sum().sqrt() / 200 print(loss) images_so_far += 1 ax = plt.subplot(num_images//2, 2, images_so_far) ax.axis('off') inp = inputs[j].cpu().data.numpy().transpose((1, 2, 0)) #mean = np.array([0.485, 0.456, 0.406]) #std = np.array([0.229, 0.224, 0.225]) #inp = std * inp + mean #inp = np.clip(inp, 0, 1) ax.imshow(inp, origin='upper') img_traj = trajectories[j].cpu().data.numpy() img_out_data = outData.cpu().data.numpy() # img_traj[:, 1] = (img_traj[:, 1] + 0.75) * 240 # img_traj[:, 0] = (img_traj[:, 0] + 1.0) * 240 # img_out_data[:, 1] = (img_out_data[:, 1] + 0.75) * 240 # img_out_data[:, 0] = (img_out_data[:, 0] + 1.0) * 240 img_traj[:, 0] = (img_traj[:, 0] + 1.0) * 240 img_traj[:, 1] = (img_traj[:, 1] + (2.0 / 3.0)) * 240 img_out_data[:, 0] = (img_out_data[:, 0] + 1.0) * 240 img_out_data[:, 1] = (img_out_data[:, 1] + (2.0 / 3.0)) * 240 #imshow(inputs.cpu().data[j]) plt.scatter(img_traj[:, 1], img_traj[:, 0], s=10, marker='.', color='red') plt.scatter(img_out_data[:, 1], img_out_data[:, 0], s=10, marker='.', color='blue') # plt.scatter(time.cpu().data.numpy(), trajectories[j][:, 0].cpu().data.numpy(), s=10, marker='.', color='red') # plt.scatter(time.cpu().data.numpy(), outData[:, 0].cpu().data.numpy(), s=10, marker='.', color='blue') # plot_results(outputs[j], ax) # plot_results_time(outputs[j], ax) plt.pause(0.001) if images_so_far == num_images: return
image_torch = torch.from_numpy(image).float() if use_gpu: inputs = Variable(image_torch.cuda()) else: inputs = Variable(image_torch) inputs = inputs.unsqueeze(0) outputs = model(inputs) fig = plt.figure() nameFile = save_dir + str(i) + '.txt' np.savetxt(nameFile, outputs[0].cpu().data.numpy()) outData = GMR(outputs[0]) inp = inputs[0].cpu().data.numpy().transpose((1, 2, 0)) plt.imshow(inp, origin='upper') plt.axis('off') img_out_data = outData.cpu().data.numpy() img_out_data[:, 0] = (img_out_data[:, 0] + 1.0) * 240 img_out_data[:, 1] = (img_out_data[:, 1] + (2 / 3)) * 240 plt.scatter(img_out_data[:, 1], img_out_data[:, 0], s=10, marker='.', color='blue')
def __init__(self): self.gmr = GMR() self.launch_gmr()
class SHO_APP: def __init__(self): self.gmr = GMR() self.launch_gmr() def set_gmr_sleep_delay(self, sleep_delay): self.gmr.set_sleep_delay(sleep_delay) def set_gmr_time_for_push_notification(self, from_, to): self.gmr.set_time_for_push_notification(from_, to) def launch_gmr(self): self.gmr.set_mode(True) self.gmr.launch() def set_light_delay(self, delay): payload = { "datapoints": [{ "dataChnId": "4", "values": { "value": delay } }] } self.gmr.post_to_mcs(payload) def set_automation_button(self, value): payload = { "datapoints": [{ "dataChnId": "3", "values": { "value": value } }] } self.gmr.post_to_mcs(payload) def set_control_button(self, value): payload = { "datapoints": [{ "dataChnId": "2", "values": { "value": value } }] } self.gmr.post_to_mcs(payload) def get_light_delay(self): return int(self.gmr.get_from_mcs("4")) def get_automation_button(self): return int(self.gmr.get_from_mcs("3")) def get_control_button(self): return int(self.gmr.get_from_mcs("2"))