def plot_AlexNet(data, demonstration, changepoints = None, plotter = None, labels = None, end_frame = 1000000, interactive_mode = False, plot_pca = False, plot_tsne = False): # list_of_layers = ["conv3", "conv4", "pool5"] list_of_layers = ["pool5"] PATH_TO_ANNOTATION = constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + str(constants.CAMERA) + ".p" annotations = pickle.load(open(PATH_TO_ANNOTATION, "rb")) for layer in list_of_layers: [X_pca, X_tsne, X_grp] = data[layer] X_pca = utils.sample_matrix(X_pca, 2) X_tsne = utils.sample_matrix(X_tsne, 2) print "AlexNet", layer, X_tsne.shape if plot_pca: plot_scatter_continous(X_pca, "plot_AlexNet_PCA" + layer+ "_" + str(start)+"_"+str(end), colormap_name = colormap_name, changepoints = changepoints, labels = labels, plotter = plotter, end_frame = end_frame, interactive_mode = interactive_mode) if plot_tsne: if interactive_mode: plot_scatter_continous(X_tsne, "plot_AlexNet_tSNE" + layer + "_" + str(end_frame), colormap_name = colormap_name, changepoints = changepoints, labels = labels, plotter = plotter, end_frame = end_frame, interactive_mode = interactive_mode) else: plot_scatter_continous(X_tsne, "plot_AlexNet_tSNE" + layer + "_" + str(start) + "_" + str(end), colormap_name = colormap_name, changepoints = changepoints, labels = labels, plotter = plotter, end_frame = end_frame, interactive_mode = interactive_mode)
def featurize_1(list_of_demonstrations, kinematics, sr): """ Given .p (pickle) files of sift features, this function concatenates Z with W vectors to produce X vector, dumped as pickle file. """ print "FEATURIZATION 1" data_X_1 = {} data_X_2 = {} for demonstration in list_of_demonstrations: print "SIFT for ", demonstration start, end = utils.get_start_end_annotations(constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + constants.CAMERA +".p") W = kinematics[demonstration] W_sampled = utils.sample_matrix(W, sampling_rate = sr) PATH_TO_SIFT = constants.PATH_TO_DATA + "sift_FCED/SIFT_"+ demonstration Z = pickle.load(open(PATH_TO_SIFT + "_1.p", "rb")) Z = Z[start:end + 1] Z_sampled_1 = utils.sample_matrix(Z, sampling_rate = sr) Z = pickle.load(open(PATH_TO_SIFT + "_2.p", "rb")) Z = Z[start:end + 1] Z_sampled_2 = utils.sample_matrix(Z, sampling_rate = sr) assert Z_sampled_1.shape[0] == W_sampled.shape[0] assert Z_sampled_2.shape[0] == W_sampled.shape[0] data_X_1[demonstration] = np.concatenate((W_sampled, Z_sampled_1), axis = 1) data_X_2[demonstration] = np.concatenate((W_sampled, Z_sampled_2), axis = 1) pickle.dump(data_X_1, open(PATH_TO_FEATURES + "SIFT_1.p", "wb")) pickle.dump(data_X_2, open(PATH_TO_FEATURES + "SIFT_2.p", "wb"))
def construct_features_kinematics(self): """ Independently loads/sets-up the kinematics in self.data_W. """ for demonstration in self.list_of_demonstrations: W = utils.sample_matrix(utils.get_kinematic_features(demonstration), sampling_rate = self.sr) scaler = preprocessing.StandardScaler().fit(W) self.data_W[demonstration] = scaler.transform(W)
def plot_SIFT(data, demonstration, changepoints = None, plotter = None, labels = None, end_frame = 1000000, interactive_mode = False, plot_pca = False, plot_tsne = False): [X_pca, X_tsne] = data X_pca = utils.sample_matrix(X_pca, 6) X_tsne = utils.sample_matrix(X_tsne, 6) print "SIFT", X_tsne.shape PATH_TO_ANNOTATION = constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + str(constants.CAMERA) + ".p" annotations = pickle.load(open(PATH_TO_ANNOTATION, "rb")) # labels = utils.get_chronological_sequences(annotations) if plot_pca: plot_scatter_continous(X_pca, "plot_SIFT_PCA" + "_" + str(start) + "_" + str(end), colormap_name = colormap_name, changepoints = changepoints, labels = labels, plotter = plotter) if plot_tsne: plot_scatter_continous(X_tsne, "plot_SIFT_tSNE" + "_" + str(start) + "_" + str(end), colormap_name = colormap_name, changepoints = changepoints, labels = labels, plotter = plotter, end_frame = end_frame, interactive_mode = interactive_mode)
def construct_features_kinematics(self): """ Independently loads/sets-up the kinematics in self.data_W. """ for demonstration in self.list_of_demonstrations: W = utils.sample_matrix( utils.get_kinematic_features(demonstration), sampling_rate=self.sr) scaler = preprocessing.StandardScaler().fit(W) self.data_W[demonstration] = scaler.transform(W)
def construct_features_kinematics(self): """ Loads kinematic features (saved in text files) and populates self.data_X dictionary """ for demonstration in self.list_of_demonstrations: W = utils.sample_matrix(utils.get_kinematic_features(demonstration), sampling_rate = self.sr) scaler = preprocessing.StandardScaler().fit(W) self.data_X[demonstration] = scaler.transform(W) print "Kinematics ", demonstration, self.data_X[demonstration].shape
def construct_features_kinematics(self): """ Loads kinematic features (saved in text files) and populates self.data_X dictionary """ for demonstration in self.list_of_demonstrations: W = utils.sample_matrix(parser.get_kinematic_features(demonstration), sampling_rate = self.sr) scaler = preprocessing.StandardScaler().fit(W) self.data_X[demonstration] = scaler.transform(W) print "Kinematics ", demonstration, self.data_X[demonstration].shape
def featurize_1(list_of_demonstrations, kinematics, sr): """ Given .p (pickle) files of sift features, this function concatenates Z with W vectors to produce X vector, dumped as pickle file. """ print "FEATURIZATION 1" data_X_1 = {} data_X_2 = {} for demonstration in list_of_demonstrations: print "SIFT for ", demonstration start, end = utils.get_start_end_annotations( constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + constants.CAMERA + ".p") W = kinematics[demonstration] W_sampled = utils.sample_matrix(W, sampling_rate=sr) PATH_TO_SIFT = constants.PATH_TO_DATA + "sift_FCED/SIFT_" + demonstration Z = pickle.load(open(PATH_TO_SIFT + "_1.p", "rb")) Z = Z[start:end + 1] Z_sampled_1 = utils.sample_matrix(Z, sampling_rate=sr) Z = pickle.load(open(PATH_TO_SIFT + "_2.p", "rb")) Z = Z[start:end + 1] Z_sampled_2 = utils.sample_matrix(Z, sampling_rate=sr) assert Z_sampled_1.shape[0] == W_sampled.shape[0] assert Z_sampled_2.shape[0] == W_sampled.shape[0] data_X_1[demonstration] = np.concatenate((W_sampled, Z_sampled_1), axis=1) data_X_2[demonstration] = np.concatenate((W_sampled, Z_sampled_2), axis=1) pickle.dump(data_X_1, open(PATH_TO_FEATURES + "SIFT_1.p", "wb")) pickle.dump(data_X_2, open(PATH_TO_FEATURES + "SIFT_2.p", "wb"))
def featurize_cnn_features_no_kinematics(list_of_demonstrations, layer, folder, feature_index, net, sr=3, config=[True, False, True]): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate=sr) demonstration_size[init_demonstration] = big_Z.shape[0] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate=sr) big_Z = np.concatenate((big_Z, Z_sampled), axis=0) demonstration_size[demonstration] = Z_sampled.shape[0] PC = min(100, min(demonstration_size.values())) if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC=PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] data_X_PCA[demonstration] = Z # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] data_X_GRP[demonstration] = Z start += size if config[0]: pickle.dump( data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[2]: pickle.dump( data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
def featurize_cnn_features(list_of_demonstrations, kinematics, layer, folder, feature_index, net, sr=3, config=[True, False, False], C=100): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_CCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate=sr) demonstration_size[init_demonstration] = big_Z.shape[0] kinematics_sampled = {} kinematics_sampled[init_demonstration] = utils.sample_matrix( kinematics[init_demonstration], sampling_rate=sr) dimensions_of_PCA = demonstration_size[init_demonstration] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate=sr) print "Sampled visual", Z.shape, Z_sampled.shape dimensions_of_PCA += Z_sampled.shape[0] big_Z = np.concatenate((big_Z, Z_sampled), axis=0) demonstration_size[demonstration] = Z_sampled.shape[0] kinematics_sampled[demonstration] = utils.sample_matrix( kinematics[demonstration], sampling_rate=sr) print "Sampled kinematics", kinematics_sampled[demonstration].shape print "PCA # of rows: ", dimensions_of_PCA PC = min(C, min(demonstration_size.values())) print "XXXX - PC:", str(PC) # Quick check to see if kinematics and visual features are aligned for demonstration in list_of_demonstrations: print demonstration_size[demonstration], kinematics_sampled[ demonstration].shape[0] assert demonstration_size[demonstration] == kinematics_sampled[ demonstration].shape[0] if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC=PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: W = kinematics_sampled[demonstration] size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] X = np.concatenate((W, Z), axis=1) data_X_PCA[demonstration] = X # ------------- CCA ------------- if config[1]: Z = big_Z[start:end] Z = utils.cca(W, Z) X = np.concatenate((W, Z), axis=1) data_X_CCA[demonstration] = X # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] X = np.concatenate((W, Z), axis=1) data_X_GRP[demonstration] = X start += size if config[0]: pickle.dump( data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[1]: pickle.dump( data_X_CCA, open(PATH_TO_FEATURES + str(feature_index) + "_CCA.p", "wb")) if config[2]: pickle.dump( data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
def featurize_LCD_VLAD(list_of_demonstrations, kinematics, layer, net_name, folder, dimensions, batch_size, fname, config=[True, True, True]): M = dimensions[0] a = dimensions[1] print "Featurizing LCD + VLAD: ", layer, net_name, folder, M, a, batch_size BATCH_SIZE = batch_size if constants.SIMULATION: BATCH_SIZE = 5 data_X_PCA = {} data_X_CCA = {} data_X_GRP = {} size_sampled_matrices = [ utils.sample_matrix(kinematics[demo], sampling_rate=BATCH_SIZE).shape[0] for demo in list_of_demonstrations ] PC = min(100, min(size_sampled_matrices)) print "PC: ", PC for demonstration in list_of_demonstrations: print demonstration W = kinematics[demonstration] Z = load_cnn_features(demonstration, layer, folder, net_name) W_new = utils.sample_matrix(W, sampling_rate=BATCH_SIZE) Z_batch = None W_batch = None j = 1 Z_new = None IPython.embed() PATH_TO_ANNOTATION = constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + str( constants.CAMERA) + ".p" start, end = utils.get_start_end_annotations(PATH_TO_ANNOTATION) Z = [] IPython.embed() for i in range(Z): vector_W = W[i] W_batch = utils.safe_concatenate(W_batch, vector_W) vector_Z = Z[i] vector_Z = vector_Z.reshape(M, a, a) vector_Z = lcd.LCD(vector_Z) Z_batch = utils.safe_concatenate(Z_batch, vector_Z) if (j == BATCH_SIZE): print "NEW BATCH", str(i) Z_batch_VLAD = encoding.encode_VLAD(Z_batch) Z_new = utils.safe_concatenate(Z_new, Z_batch_VLAD) # Re-initialize batch variables j = 0 Z_batch = None W_batch = None j += 1 # tail case if Z_batch is not None: print "TAIL CASE" print "NEW BATCH", str(i) Z_batch_VLAD = encoding.encode_VLAD(Z_batch) Z_new = utils.safe_concatenate(Z_new, Z_batch_VLAD) if config[0]: Z_new_pca = utils.pca_incremental(Z_new, PC=PC) print Z_new_pca.shape assert W_new.shape[0] == Z_new_pca.shape[0] X_PCA = np.concatenate((W_new, Z_new_pca), axis=1) data_X_PCA[demonstration] = X_PCA if config[1]: Z_new_cca = utils.cca(W_new, Z_new) print Z_new_cca.shape assert W_new.shape[0] == Z_new_cca.shape[0] X_CCA = np.concatenate((W_new, Z_new_cca), axis=1) data_X_CCA[demonstration] = X_CCA if config[2]: Z_new_grp = utils.grp(Z_new) print Z_new_grp.shape assert W_new.shape[0] == Z_new_grp.shape[0] X_GRP = np.concatenate((W_new, Z_new_grp), axis=1) data_X_GRP[demonstration] = X_GRP if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + fname + "_PCA" + ".p", "wb")) if config[1]: pickle.dump(data_X_CCA, open(PATH_TO_FEATURES + fname + "_CCA" + ".p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + fname + "_GRP" + ".p", "wb"))
def featurize_cnn_features_no_kinematics(list_of_demonstrations, layer, folder, feature_index, net, sr = 3, config = [True, False, True]): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate = sr) demonstration_size[init_demonstration] = big_Z.shape[0] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate = sr) big_Z = np.concatenate((big_Z, Z_sampled), axis = 0) demonstration_size[demonstration] = Z_sampled.shape[0] PC = min(100, min(demonstration_size.values())) if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC = PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] data_X_PCA[demonstration] = Z # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] data_X_GRP[demonstration] = Z start += size if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
def featurize_cnn_features(list_of_demonstrations, kinematics, layer, folder, feature_index, net, sr = 3, config = [True, False, False], C = 100): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_CCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate = sr) demonstration_size[init_demonstration] = big_Z.shape[0] kinematics_sampled = {} kinematics_sampled[init_demonstration] = utils.sample_matrix(kinematics[init_demonstration], sampling_rate = sr) dimensions_of_PCA = demonstration_size[init_demonstration] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate = sr) print "Sampled visual", Z.shape, Z_sampled.shape dimensions_of_PCA += Z_sampled.shape[0] big_Z = np.concatenate((big_Z, Z_sampled), axis = 0) demonstration_size[demonstration] = Z_sampled.shape[0] kinematics_sampled[demonstration] = utils.sample_matrix(kinematics[demonstration], sampling_rate = sr) print "Sampled kinematics", kinematics_sampled[demonstration].shape print "PCA # of rows: ", dimensions_of_PCA PC = min(C, min(demonstration_size.values())) print "XXXX - PC:", str(PC) # Quick check to see if kinematics and visual features are aligned for demonstration in list_of_demonstrations: print demonstration_size[demonstration], kinematics_sampled[demonstration].shape[0] assert demonstration_size[demonstration] == kinematics_sampled[demonstration].shape[0] if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC = PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: W = kinematics_sampled[demonstration] size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] X = np.concatenate((W, Z), axis = 1) data_X_PCA[demonstration] = X # ------------- CCA ------------- if config[1]: Z = big_Z[start:end] Z = utils.cca(W, Z) X = np.concatenate((W, Z), axis = 1) data_X_CCA[demonstration] = X # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] X = np.concatenate((W, Z), axis = 1) data_X_GRP[demonstration] = X start += size if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[1]: pickle.dump(data_X_CCA, open(PATH_TO_FEATURES + str(feature_index) + "_CCA.p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
def featurize_LCD_VLAD(list_of_demonstrations, kinematics, layer, net_name, folder, dimensions, batch_size, fname, config = [True, True, True]): M = dimensions[0] a = dimensions[1] print "Featurizing LCD + VLAD: ", layer, net_name, folder, M, a, batch_size BATCH_SIZE = batch_size if constants.SIMULATION: BATCH_SIZE = 5 data_X_PCA = {} data_X_CCA = {} data_X_GRP = {} size_sampled_matrices = [utils.sample_matrix(kinematics[demo], sampling_rate = BATCH_SIZE).shape[0] for demo in list_of_demonstrations] PC = min(100, min(size_sampled_matrices)) print "PC: ", PC for demonstration in list_of_demonstrations: print demonstration W = kinematics[demonstration] Z = load_cnn_features(demonstration, layer, folder, net_name) W_new = utils.sample_matrix(W, sampling_rate = BATCH_SIZE) Z_batch = None W_batch = None j = 1 Z_new = None IPython.embed() PATH_TO_ANNOTATION = constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER + demonstration + "_" + str(constants.CAMERA) + ".p" start, end = utils.get_start_end_annotations(PATH_TO_ANNOTATION) Z = [] IPython.embed() for i in range(Z): vector_W = W[i] W_batch = utils.safe_concatenate(W_batch, vector_W) vector_Z = Z[i] vector_Z = vector_Z.reshape(M, a, a) vector_Z = lcd.LCD(vector_Z) Z_batch = utils.safe_concatenate(Z_batch, vector_Z) if (j == BATCH_SIZE): print "NEW BATCH", str(i) Z_batch_VLAD = encoding.encode_VLAD(Z_batch) Z_new = utils.safe_concatenate(Z_new, Z_batch_VLAD) # Re-initialize batch variables j = 0 Z_batch = None W_batch = None j += 1 # tail case if Z_batch is not None: print "TAIL CASE" print "NEW BATCH", str(i) Z_batch_VLAD = encoding.encode_VLAD(Z_batch) Z_new = utils.safe_concatenate(Z_new, Z_batch_VLAD) if config[0]: Z_new_pca = utils.pca_incremental(Z_new, PC = PC) print Z_new_pca.shape assert W_new.shape[0] == Z_new_pca.shape[0] X_PCA = np.concatenate((W_new, Z_new_pca), axis = 1) data_X_PCA[demonstration] = X_PCA if config[1]: Z_new_cca = utils.cca(W_new, Z_new) print Z_new_cca.shape assert W_new.shape[0] == Z_new_cca.shape[0] X_CCA = np.concatenate((W_new, Z_new_cca), axis = 1) data_X_CCA[demonstration] = X_CCA if config[2]: Z_new_grp = utils.grp(Z_new) print Z_new_grp.shape assert W_new.shape[0] == Z_new_grp.shape[0] X_GRP = np.concatenate((W_new, Z_new_grp), axis = 1) data_X_GRP[demonstration] = X_GRP if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + fname + "_PCA" + ".p", "wb")) if config[1]: pickle.dump(data_X_CCA, open(PATH_TO_FEATURES + fname + "_CCA" + ".p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + fname + "_GRP" + ".p", "wb"))