def __init__(self, db_path, db_type, kmeans_file): # initialize the renderedImages dataset first super().__init__(db_path, db_type) # add the kmeans part self.kmeans = pickle.load(open(kmeans_file, 'rb')) self.num_clusters = self.kmeans.n_clusters self.rotations_dict = np.stack([get_R(self.kmeans.cluster_centers_[i]) for i in range(self.num_clusters)])
def testing(): model.eval() ypred = [] ytrue = [] labels = [] for i, sample in enumerate(test_loader): xdata = Variable(sample['xdata'].cuda()) label = Variable(sample['label'].cuda()) output = model(xdata, label) ypred_bin = np.argmax(output[0].data.cpu().numpy(), axis=1) ypred_res = output[1].data.cpu().numpy() y = [ get_y(np.dot(rotations_dict[ypred_bin[j]], get_R(ypred_res[j]))) for j in range(ypred_bin.shape[0]) ] ypred.append(y) ytrue.append(sample['ydata'].numpy()) labels.append(sample['label'].numpy()) del xdata, label, output, sample gc.collect() ypred = np.concatenate(ypred) ytrue = np.concatenate(ytrue) labels = np.concatenate(labels) model.train() return ytrue, ypred, labels
def get_residuals(ydata, key_rotations): ydata_res = np.zeros((ydata.shape[0], len(key_rotations), 3)) for i in range(ydata.shape[0]): for j in range(len(key_rotations)): ydata_res[i, j, :] = get_y(np.dot(key_rotations[j].T, get_R(ydata[i]))) return ydata_res
def __init__(self, db_path, db_type, problem_type, kmeans_file): # initialize the renderedImages dataset first super().__init__(db_path, db_type) self.problem_type = problem_type # add the kmeans part self.kmeans = pickle.load(open(kmeans_file, 'rb')) self.num_clusters = self.kmeans.n_clusters #pdb.set_trace() if self.problem_type == 'm2': self.key_rotations = [get_R(y) for y in self.kmeans.cluster_centers_]
def __getitem__(self, idx): # run the item handler of the renderedImages dataset sample = super().__getitem__(idx) # update the ydata target using kmeans dictionary ydata = sample['ydata'].numpy() # rotation matrix ydata_rot = np.stack([get_R(ydata[i]) for i in range(ydata.shape[0])]) sample['ydata_rot'] = torch.from_numpy(ydata_rot).float() # bin part ydata_bin = self.kmeans.predict(ydata) sample['ydata_bin'] = torch.from_numpy(ydata_bin).long() # residual part ydata_res = np.stack([get_y(np.dot(self.rotations_dict[ydata_bin[i]].T, ydata_rot[i])) for i in range(ydata.shape[0])]) sample['ydata_res'] = torch.from_numpy(ydata_res).float() return sample
def get_residuals(ydata, ydata_bin): ydata_res = np.zeros((ydata.shape[0], 3)) for i in range(ydata.shape[0]): ydata_res[i, :] = get_y( np.dot(rotations_dict[ydata_bin[i]].T, get_R(ydata[i]))) return ydata_res
# assign GPU os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id # save stuff here results_file = os.path.join('results', args.save_str) model_file = os.path.join('models', args.save_str + '.tar') plots_file = os.path.join('plots', args.save_str) log_dir = os.path.join('logs', args.save_str) # kmeans data kmeans_file = 'data/kmeans_dictionary_axis_angle_' + str( args.dict_size) + '.pkl' kmeans = pickle.load(open(kmeans_file, 'rb')) num_clusters = kmeans.n_clusters rotations_dict = np.stack( [get_R(kmeans.cluster_centers_[i]) for i in range(kmeans.n_clusters)]) # relevant variables ndim = 3 num_classes = len(classes) # loss mse_loss = nn.MSELoss().cuda() ce_loss = nn.CrossEntropyLoss().cuda() # DATA # datasets real_data = GBDGenerator(args.augmented_path, 'real', kmeans_file) render_data = GBDGenerator(args.render_path, 'render', kmeans_file) test_data = TestImages(args.pascal3d_path) # setup data loaders