def makeIntoVariables(dat): input_ = np.concatenate( [(dat["obs"] - means['o']) / std['o'], dat["actions"], (dat["s_transition_obs"] - means['s']) / std['s']], axis=2) x, y = Variable(torch.from_numpy(input_).cuda(), requires_grad=False), Variable(torch.from_numpy( dat["r_transition_obs"][:, :, :6]).cuda(), requires_grad=False) return x, y
def makeIntoVariables(dat): input_ = np.concatenate([dat["obs"][:,:,:6], dat["actions"], dat["s_transition_obs"][:,:,:6]], axis=2) x, y = autograd.Variable( # Don't predict palet and goal position torch.from_numpy(input_).cuda(), requires_grad=False ), autograd.Variable( torch.from_numpy(dat["r_transition_obs"][:,:,:6]).cuda(), requires_grad=False ) return x, y
def makeIntoVariables(dat): input_ = np.concatenate( [(dat["obs"][:, :, :10] - means['o']) / std['o'], (dat["actions"] / std['a']), (dat["s_transition_obs"][:, :, :10] - means['s']) / std['s']], axis=2) x, y, theta_r = Variable( torch.from_numpy(input_).cuda(), requires_grad=False), Variable( torch.from_numpy(dat["r_transition_obs"][:, :, 6:8]).cuda(), requires_grad=False), Variable(torch.from_numpy( np.arccos(dat["r_transition_obs"][:, :, :2]) * np.sign(dat["r_transition_obs"][:, :, 2:4])).cuda(), requires_grad=False) return x, y, theta_r
def detect(self, img): device = self.device prior_data, scale, scale1 = self.decode_params(*img.shape[:2]) # REF: test_fddb.py img = np.float32(img) img -= (104, 117, 123) img = img.transpose(2, 0, 1) img = torch.from_numpy(img).unsqueeze(0) img = img.to(device, dtype=torch.float32) loc, conf, landms = self.net(img) loc = loc.cpu() conf = conf.cpu() landms = landms.cpu() # Decode results boxes = decode(loc.squeeze(0), prior_data, self.variance) boxes = boxes * scale scores = conf.squeeze(0)[:, 1] landms = decode_landm(landms.squeeze(0), prior_data, self.variance) landms = landms * scale1 inds = scores > self.confidence_threshold boxes = boxes[inds] landms = landms[inds] return boxes, landms
def __getitem__(self, idx): """ Method to retrieve an item from the dataset based on the index given. :param idx: index of the flux, and label we want to read. """ X = torch.from_numpy(np.expand_dims(self.fluxs[idx], 0)) y = torch.Tensor([self.zs[idx]]) return X, y
def __getitem__(self, idx): """ Constructs an item of the dataset as a tuple in the same order as the keys given on dataset construction. Assumes a numpy array is stored at each index of the different h5 datasets. Does this so from_numpy can be used to generate tensors for the data. Applies given transformations to datasets by key. :param idx: index of the datasets to retrieve. """ if self.transforms: items = [] for k in self.keys: k_trans = self.transforms.get(k, self._i) items.append(k_trans(torch.from_numpy(getattr(self, k)[idx]))) return tuple(items) else: return tuple( torch.from_numpy(getattr(self, k)[idx]) for k in self.keys)
def get_test_input(input_dim, CUDA): img = cv2.imread("imgs/messi.jpg") img = cv2.resize(img, (input_dim, input_dim)) img_ = img[:,:,::-1].transpose((2,0,1)) img_ = img_[np.newaxis,:,:,:]/255.0 img_ = torch.from_numpy(img_).float() img_ = Variable(img_) if CUDA: img_ = img_.cuda() return img_
def prep_image(img, inp_dim): """ Prepare image for inputting to the neural network. Returns a Variable img_是适配后的图像 orig_im是原始图像 """ orig_im = img dim = orig_im.shape[1], orig_im.shape[0] img = cv2.resize(orig_im, (inp_dim, inp_dim)) img_ = img[:,:,::-1].transpose((2,0,1)).copy() img_ = torch.from_numpy(img_).float().div(255.0).unsqueeze(0) return img_, orig_im, dim
def process_image(self, image_path): ''' Scales, crops, and normalizes a PIL image for a PyTorch model, and returns an Numpy array. INPUTS: 1. Relative image path and file name: <str> RETURNS: 1. Numpy array ''' # Process a PIL image for use in a PyTorch model img = Image.open(image_path) width = img.size[0] height = img.size[1] # scale the image resize_dim = 256 if width > height: percent = float(resize_dim) / float(height) resize_width = int(width * percent) img = img.resize((resize_width, resize_dim), Image.BILINEAR) else: percent = float(resize_dim) / float(width) resize_height = int(height * percent) img = img.resize((resize_dim, resize_height), Image.BILINEAR) # crop the image object crop_size = 224 left = (img.size[0] - crop_size) / 2 upper = (img.size[1] - crop_size) / 2 right = left + crop_size lower = upper + crop_size img = img.crop((left, upper, right, lower)) # normalize the pixel values # adjust values to be between 0 - 1 instead of 0 - 255 img = np.array(img) / 255 mean = np.array([0.485, 0.456, 0.406]) # mean as provided above with Transform std = np.array([0.229, 0.224, 0.225]) # std dev as provided above with Transform img = (img - mean) / std # normalize # PyTorch expects the color channel to be the first dimension but it's the third dimension # moving the third index to the first, and shifting the other two indices img = img.transpose((2, 0, 1)) img = torch.from_numpy(img).type(torch.FloatTensor) # return the Pytorch tensor (image) return img
def makeIntoVariables(episode): _input = np.concatenate([ episode["state_next_sim"], episode["state_current_real"], episode["actions"] ], axis=2) _input = torch.from_numpy(_input) _output = episode["state_next_real"] if CUDA: _input = _input.cuda() _output = _output.cuda() x, y = Variable(_input, requires_grad=False), Variable(_output, requires_grad=False) return x, y
def init(state, im_x, total_num): for i in range(len(state)): target_pos.append(state[i][:2]) target_sz.append(state[i][2:4]) tracking_index.append(index * 100 + total_num + i) im_z_crop, _ = get_crop(im_x, target_pos[i], target_sz[i], z_size, avg_chans=avg_chans, context_amount=context_amount, func_get_subwindow=get_subwindow_tracking) im_z_crops.append(im_z_crop) array = torch.from_numpy( np.ascontiguousarray( im_z_crops[i].transpose(2, 0, 1)[np.newaxis, ...], np.float32)).to(torch.device("cuda")) lost.append(0) with torch.no_grad(): features.append(Model(array, phase=phase))
def to_variables(X, C, POS, Y): if cfg.BATCH_TYPE == "multi": x_var = X c_var = C pos_var = POS y_var = list(chain.from_iterable(list(Y))) lm_X = [[ cfg.LM_MAX_VOCAB_SIZE - 1 if (x >= cfg.LM_MAX_VOCAB_SIZE) else x for x in x1d ] for x1d in X] else: x_var = Variable(cuda.LongTensor([X])) c_var = C # f_var = Variable(torch.from_numpy(f)).float().unsqueeze(dim=0).cuda() pos_var = Variable(torch.from_numpy(POS).cuda()).unsqueeze(dim=0) lm_X = [ cfg.LM_MAX_VOCAB_SIZE - 1 if (x >= cfg.LM_MAX_VOCAB_SIZE) else x for x in X ] y_var = Variable(cuda.LongTensor(Y)) return x_var, c_var, pos_var, y_var, lm_X
def train_model(): num_epochs = 200 learning_rate = 0.000001 batch_size = 10 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) model = ConvNet().to(device) """ dataxy=get_data() with open("psd_score.txt","wb") as f: pickle.dump(dataxy,f) """ dataxy = [] with open('psd_score.txt', 'rb') as file: dataxy = pickle.load(file) x = np.array(dataxy[0]) y = np.array(dataxy[1]) train_x_origin, test_x_origin, train_y_origin, test_y_origin = train_test_split( x, y) traindataset = ds(train_x_origin, train_y_origin, len(train_x_origin)) testdataset = ds(test_x_origin, test_y_origin, len(test_x_origin)) loss = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) train_loader = DataLoader(dataset=traindataset, batch_size=batch_size, shuffle=True, num_workers=0) test_loader = DataLoader(dataset=testdataset, batch_size=batch_size, shuffle=True, num_workers=0) train_loss = [] test_loss = [] for epoch in tqdm(range(num_epochs)): cur_train_loss = [] cur_test_loss = [] for i, (data, labels) in enumerate(train_loader): data = data.to(device).reshape(-1, 1, 5, 31) data = data.type(torch.FloatTensor).to(device) labels = labels.to(device) y_pred = model(data).type(torch.FloatTensor).to(device) labels = labels.type(torch.FloatTensor).to(device) l = loss(y_pred, labels) l.backward() optimizer.step() optimizer.zero_grad() cur_train_loss.append(l.item()) train_loss.append(np.array(cur_train_loss).mean()) print("train loss ", np.array(cur_train_loss).mean()) for i, (test_x, test_y) in enumerate(test_loader): test_x = test_x.reshape(-1, 1, 5, 31).type(torch.FloatTensor).to(device) test_y = test_y.to(device) out = model(test_x) l = loss(out, test_y) cur_test_loss.append(l.item()) test_loss.append(np.array(cur_test_loss).mean()) print("test loss ", np.array(cur_test_loss).mean()) plt.plot(train_loss, label="train_loss") plt.plot(test_loss, label="test_loss") plt.legend() plt.show() predict = [] torch.save(model.state_dict(), "model1.pt") for i in range(len(test_x_origin)): predict.append( model( torch.from_numpy(test_x_origin[i]).reshape(-1, 1, 5, 31).type( torch.FloatTensor).to(device)).item()) return test_y_origin, predict
def detect(save_img=False): out, source, weights, half, view_img, save_txt, imgsz = \ opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt, opt.img_size webcam = source == '0' or source.startswith('rtsp') or source.startswith( 'http') or source.endswith('.txt') # Initialize #device = torch_utils.select_device(opt.device) device = select_device(opt.device) if os.path.exists(out): shutil.rmtree(out) # delete output folder os.makedirs(out) # make new output folder # Load model ''' The original method needs exactly the same folder structure and imports it was trained on this is a pickle limitation in the torch model. The alternative would be loading from github, which is not working Another issue is when training and detection have different devices (CPU/GPU) ''' # DB 20201018 = Original method #google_utils.attempt_download(weights) attempt_download(weights) model = torch.load(weights, map_location=device)['model'] # ORIGINAL # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning # model.fuse() model.to(device).eval( ) # ATTENTION! UMCOMMENT THIS IF YOU UNCOMMENT model = torch.load(weights, map_location=device)['model'] # ORIGINAL #model.to(device).float().eval() # DB 20201018: detect on CPU using GPU trained model # DB 20201018: Load from github method #model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device).eval() # DB 20201016 MODEL IMPORT FIX #model = torch.hub.load('danfbento/SIB2', 'mod5_test_weight', pretrained=True).to(device).eval() # DB 20201016 MODEL IMPORT FIX # Second-stage classifier classify = False if classify: #modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize modelc = load_classifier(name='resnet101', n=2) # initialize modelc.load_state_dict( torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights modelc.to(device).eval() # Half precision half = half and device.type != 'cpu' # half precision only supported on CUDA if half: model.half() # Set Dataloader vid_path, vid_writer = None, None if webcam: view_img = True torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference dataset = LoadStreams(source, img_size=imgsz) else: save_img = True dataset = LoadImages(source, img_size=imgsz) # Get names and colors names = model.names if hasattr(model, 'names') else model.modules.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] # Run inference t0 = time.time() img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img _ = model(img.half() if half else img.float() ) if device.type != 'cpu' else None # run once for path, img, im0s, vid_cap in dataset: img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0) # Inference #t1 = torch_utils.time_synchronized() t1 = time_synchronized() pred = model(img, augment=opt.augment)[0] #t2 = torch_utils.time_synchronized() t2 = time_synchronized() # to float if half: pred = pred.float() # Apply NMS pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, fast=True, classes=opt.classes, agnostic=opt.agnostic_nms) # Apply Classifier if classify: pred = apply_classifier(pred, modelc, img, im0s) # Process detections for i, det in enumerate(pred): # detections per image if webcam: # batch_size >= 1 p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() else: p, s, im0 = path, '', im0s save_path = str(Path(out) / Path(p).name) s += '%gx%g ' % img.shape[2:] # print string gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh if det is not None and len(det): # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() # Print results for c in det[:, -1].unique(): n = (det[:, -1] == c).sum() # detections per class s += '%g %ss, ' % (n, names[int(c)]) # add to string # Write results for *xyxy, conf, cls in det: if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format if save_img or view_img: # Add bbox to image label = '%s %.2f' % (names[int(cls)], conf) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) # Print time (inference + NMS) print('%sDone. (%.3fs)' % (s, t2 - t1)) # Stream results if view_img: cv2.imshow(p, im0) if cv2.waitKey(1) == ord('q'): # q to quit raise StopIteration # Save results (image with detections) if save_img: if dataset.mode == 'images': cv2.imwrite(save_path, im0) else: if vid_path != save_path: # new video vid_path = save_path if isinstance(vid_writer, cv2.VideoWriter): vid_writer.release( ) # release previous video writer fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) vid_writer = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) vid_writer.write(im0) if save_txt or save_img: print('Results saved to %s' % os.getcwd() + os.sep + out) if platform == 'darwin': # MacOS os.system('open ' + save_path) print('Done. (%.3fs)' % (time.time() - t0))
raise Exception("model couldn't be found:", MODEL_PATH_BEST) loss_function = nn.MSELoss().cuda() if TRAIN: optimizer = optim.Adam(net.parameters(), lr=0.001) scheduler = MultiStepLR(optimizer, milestones=[60,85,110], gamma=0.5) if CONTINUE: old_model_string = loadModel(optional=True) print(old_model_string) else: old_model_string = loadModel(optional=False) loss_min = [float('inf')] m_c = torch.from_numpy(means["c"]) s_c = torch.from_numpy(std["c"]) mean_c, std_c = Variable(m_c, requires_grad=False).cuda(), Variable(s_c, requires_grad=False).cuda() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True) net.train() for epi, data in enumerate(iterator): x, y = makeIntoVariables(data) # reset hidden lstm units net.zero_grad() net.zero_hidden()
raise Exception("model couldn't be found:", MODEL_PATH_BEST) loss_function = nn.MSELoss().cuda() if TRAIN: optimizer = optim.Adam(net.parameters(), lr=0.001) scheduler = MultiStepLR(optimizer, milestones=[60, 85, 110], gamma=0.5) if CONTINUE: old_model_string = loadModel(optional=True) print(old_model_string) else: old_model_string = loadModel(optional=False) loss_min = [float('inf')] m_c = torch.from_numpy(means["c"]) s_c = torch.from_numpy(std["c"]) mean_c, std_c = Variable(m_c, requires_grad=False).cuda(), Variable( s_c, requires_grad=False).cuda() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True) net.train() for epi, data in enumerate(iterator): x, y = makeIntoVariables(data) # reset hidden lstm units net.zero_grad()
def init_embeddings(self, weights, trainable): self.embedding.weight = nn.Parameter(torch.from_numpy(weights), requires_grad=trainable)
raise Exception("model couldn't be found:", MODEL_PATH_BEST) loss_function = nn.MSELoss().cuda() if TRAIN: optimizer = optim.Adam(net.parameters(), lr=0.002) scheduler = MultiStepLR(optimizer, milestones=[20, 60, 85, 110], gamma=0.5) if CONTINUE: old_model_string = loadModel(optional=True) print(old_model_string) else: old_model_string = loadModel(optional=False) loss_min = [float('inf')] m_c = torch.from_numpy(means["c"]) s_c = torch.from_numpy(std["c"]) mean_c, std_c = Variable(m_c, requires_grad=False).cuda(), Variable( s_c, requires_grad=False).cuda() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True) net.train() for epi, data in enumerate(iterator): x, y = makeIntoVariables(data) if x.shape[0] != batch_size: continue
raise Exception("model couldn't be found:", MODEL_PATH_BEST) loss_function = nn.MSELoss() if TRAIN: optimizer = optim.Adam(net.parameters(), lr=0.001) scheduler = MultiStepLR(optimizer, milestones=[50, 75, 100], gamma=0.5) if CONTINUE: old_model_string = loadModel(optional=True) print(old_model_string) else: old_model_string = loadModel(optional=False) loss_min = [float('inf')] m_c = torch.from_numpy(means["c"]) s_c = torch.from_numpy(std["c"]) m_t = torch.from_numpy(means["theta"]) s_t = torch.from_numpy(std["theta"]) # if CUDA: # m_c.cuda(), s_c.cuda(), m_t.cuda(), s_t.cuda() mean_c, std_c = Variable(m_c, requires_grad=False).cuda(), Variable( s_c, requires_grad=False).cuda() mean_t, std_t = Variable(m_t, requires_grad=False).cuda(), Variable( s_t, requires_grad=False).cuda() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True)
def multiprocessing_update(task, task_cfg, index, im, dataqueue, resultqueue): # build model Model = model_builder.build_model(task, task_cfg.model).to(torch.device("cuda")) Model.eval() target_pos = [] target_sz = [] im_z_crops = [] lost = [] features = [] tracking_index = [] total_num = 0 avg_chans = np.mean(im, axis=(0, 1)) im_h, im_w = im.shape[0], im.shape[1] z_size = hyper_params['z_size'] x_size = hyper_params['x_size'] context_amount = hyper_params['context_amount'] phase = hyper_params['phase_init'] phase_track = hyper_params['phase_track'] score_size = ( hyper_params['x_size'] - hyper_params['z_size'] ) // hyper_params['total_stride'] + 1 - hyper_params['num_conv3x3'] * 2 if hyper_params['windowing'] == 'cosine': window = np.outer(np.hanning(score_size), np.hanning(score_size)) window = window.reshape(-1) elif hyper_params['windowing'] == 'uniform': window = np.ones((score_size, score_size)) else: window = np.ones((score_size, score_size)) def init(state, im_x, total_num): for i in range(len(state)): target_pos.append(state[i][:2]) target_sz.append(state[i][2:4]) tracking_index.append(index * 100 + total_num + i) im_z_crop, _ = get_crop(im_x, target_pos[i], target_sz[i], z_size, avg_chans=avg_chans, context_amount=context_amount, func_get_subwindow=get_subwindow_tracking) im_z_crops.append(im_z_crop) array = torch.from_numpy( np.ascontiguousarray( im_z_crops[i].transpose(2, 0, 1)[np.newaxis, ...], np.float32)).to(torch.device("cuda")) lost.append(0) with torch.no_grad(): features.append(Model(array, phase=phase)) def delete_node(j): try: del target_pos[j] del target_sz[j] del features[j] del tracking_index[j] del lost[j] except Exception as error: print("delete error", error) while True: try: im_x, state, delete = dataqueue.get() except Exception as error: print(error) continue else: if len(state) > 0: init(state, im_x, total_num) total_num += len(state) continue if len(delete) > 0: delete_list = [] for i in delete: if i in tracking_index: # print("delete",i) node = tracking_index.index(i) delete_node(node) result = [] im = im_x.copy() del im_x, state, delete for i in range(len(features)): im_x_crop, scale_x = get_crop( im, target_pos[i], target_sz[i], z_size, x_size=x_size, avg_chans=avg_chans, context_amount=context_amount, func_get_subwindow=get_subwindow_tracking) array = torch.from_numpy( np.ascontiguousarray( im_x_crop.transpose(2, 0, 1)[np.newaxis, ...], np.float32)).to(torch.device("cuda")) with torch.no_grad(): score, box, cls, ctr, *args = Model(array, *features[i], phase=phase_track) box = tensor_to_numpy(box[0]) score = tensor_to_numpy(score[0])[:, 0] cls = tensor_to_numpy(cls[0]) ctr = tensor_to_numpy(ctr[0]) box_wh = xyxy2cxywh(box) # #lost goal if score.max() < 0.2: lost[i] += 1 continue elif lost[i] > 0: lost[i] -= 1 best_pscore_id, pscore, penalty = postprocess_score( score, box_wh, target_sz[i], scale_x, window) # box post-processing new_target_pos, new_target_sz = postprocess_box( best_pscore_id, score, box_wh, target_pos[i], target_sz[i], scale_x, x_size, penalty) new_target_pos, new_target_sz = restrict_box( im_h, im_w, new_target_pos, new_target_sz) # save underlying state target_pos[i], target_sz[i] = new_target_pos, new_target_sz # return rect format track_rect = cxywh2xywh( np.concatenate([target_pos[i], target_sz[i]], axis=-1)) result.append(track_rect) delete_list = [] for i in range(len(features)): if lost[i] > 10: delete_list = [] delete_list.append(i) for i in delete_list: delete_node(i) # print(index, len(features)) resultqueue.put([result, tracking_index])
batch = 1 name = "/data/lisa/data/sim2real/mujoco_data4.h5" reacher_data = H5PYDataset(name, which_sets=('valid', ), sources=('s_transition_img', 'r_transition_img')) stream = DataStream(reacher_data, iteration_scheme=ShuffledScheme(reacher_data.num_examples, batch)) i = 0 for data in stream.get_epoch_iterator(as_dict=True): s_trans_img = data['s_transition_img'][0] r_trans_img = data['r_transition_img'][0] in_ = torch.from_numpy(s_trans_img).float().cuda() in_ = in_.permute(0, 3, 1, 2) in_ = transform(in_) out = model.netG_A.forward(Variable(in_, volatile=True)) out = out.data.permute(0, 2, 3, 1).cpu() * 128 + 128 out = out.byte().numpy() images_translated = [out[j, :, :, :] for j in range(150)] imageio.mimsave('env1_{}.gif'.format(i), [s_trans_img[j, :, :, :] for j in range(150)]) imageio.mimsave('env2_{}.gif'.format(i), [r_trans_img[j, :, :, :] for j in range(150)]) imageio.mimsave('translated_{}.gif'.format(i), images_translated) i = i + 1 if i == 15:
loss_history = [9999999 ] # very high loss because loss can't be empty for min() # h0 = Variable(torch.randn(, 3, 20)) # c0 = Variable(torch.randn(2, 3, 20)) for epoch_idx in range(EPOCHS): loss_epoch = 0 diff_epoch = 0 for episode_idx, data in enumerate(dataloader): x, y = makeIntoVariables(data) #diff_episode = F.mse_loss(x.data, y.data).data.cpu()[0] # use random images before feeding real images images = autograd.Variable(torch.from_numpy( np.random.rand(x.size()[0], 150, 3, 128, 128).astype('float32')), requires_grad=False).cuda() # reset hidden lstm units net.zero_hidden() loss_episode = 0 diff_episode = 0 if TRAIN: optimizer.zero_grad() # iterate over episode frames for frame_idx in np.arange(len(x)): prediction = net.forward(x[frame_idx]) loss = loss_function(prediction, y[frame_idx].view(1, -1))
ipdb.set_trace() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True) for epi, _ in enumerate(iterator): x, y = makeIntoVariables(data) # reset hidden lstm units net.zero_grad() net.zero_hidden() optimizer.zero_grad() correction = net.forward(x) sim_prediction = Variable(torch.from_numpy( data["s_transition_obs"][:, :, :6]), requires_grad=False).cuda() loss = loss_function(sim_prediction + correction, y).mean() loss.backward() optimizer.step() loss_episode = loss.clone().cpu().data.numpy()[0] diff_episode = F.mse_loss(sim_prediction, y).clone().cpu().data.numpy()[0] loss_epoch.append(loss_episode) diff_epoch.append(diff_episode) loss.detach_() net.hidden[0].detach_() net.hidden[1].detach_()
if hyperdash_support: exp = Experiment("simple lstm - pusher") exp.param("layers", LSTM_LAYERS) exp.param("nodes", HIDDEN_NODES) if TRAIN: optimizer = optim.Adam(net.parameters(), lr=0.001) scheduler = MultiStepLR(optimizer, milestones=[100, 175, 200], gamma=0.5) if CONTINUE: old_model_string = loadModel(optional=True) print(old_model_string) else: old_model_string = loadModel(optional=False) loss_min = [float('inf')] mean_c = Variable(torch.from_numpy(means["c"]), requires_grad=False).cuda() std_c = Variable(torch.from_numpy(std["c"]), requires_grad=False).cuda() for epoch in np.arange(EPOCHS): loss_epoch = [] diff_epoch = [] iterator = stream_train.get_epoch_iterator(as_dict=True) for epi, data in enumerate(iterator): x, y = makeIntoVariables(data) # reset hidden lstm units net.zero_grad() net.zero_hidden() optimizer.zero_grad()