def extract_flat_features_preserve_current_room_and_nearest_room(X, room): # Grab Adjacent Rooms remap = reverse_map_location(room) nearest_room = devices.nearest[remap] # Assemble N = X[room].shape[0] C = conv_features_for_in_room(X, room) M = C.shape[1] * (len(nearest_room) + 1) feats = np.zeros((N, M)) # Make first channel = current room pos = 0 stride = C.shape[1] feats[:, pos:pos + stride] = C pos += stride for k in nearest_room: C = conv_features_for_in_room( X, reverse_map_location(k)) # conv_features_for_in_room(X, k) stride = C.shape[1] feats[:, pos:pos + stride] = C pos += stride return feats
def extract_flat_features_adjacent_rooms(X, room): # Grab Adjacent Rooms adjacent_rooms = devices.adjacencies[reverse_map_location(room)] # Assemble N = X[room].shape[0] C = conv_features_for_in_room(X, room) M = C.shape[1] * len(adjacent_rooms) feats = np.zeros((N, M)) # Make first channel = current room pos = 0 stride = C.shape[1] for k in adjacent_rooms: C = conv_features_for_in_room(X, k) stride = C.shape[1] feats[:, pos:pos + stride] = C pos += stride input = feats num_channels = len(adjacent_rooms) N = input.shape[0] M = int(input.shape[1] / num_channels) output = np.zeros((N, M * 2)) for n in range(N): folded = input[n, 0:].reshape(num_channels, -1) max_feats = np.max(folded, axis=0) mean_feats = np.mean(folded, axis=0) output[n, 0:M] = max_feats output[n, M:M * 2] = mean_feats return output
def create_subset(subset, dataset, method=None, room=None, feature_type='conv'): # Allocate Data num_set = 0 for k in subset: num_set += len(dataset[k]['labels']) # Assemble Dataset X_set = None Y_set = None label_mapping = None for k in subset: # Generate Features if (method == 'in-room'): X = conv_features_for_in_room(dataset[k]['data'], room) if (method == 'nearest-room'): X = extract_flat_features_preserve_current_room_and_nearest_room( dataset[k]['data'], room) if (method == 'nearest-room-only'): X = conv_features_for_in_room( dataset[k]['data'], devices.nearest[reverse_map_location(room)][0]) if (method == 'all-rooms'): X = extract_flat_features_preserve_current_room( dataset[k]['data'], 10, room) if (method == 'all-but-in-room'): X = conv_features_for_all_but_in_room(dataset[k]['data'], room) X = extract_flat_features(X, 9) # Filter Noise Y = dataset[k]['labels'] label_mapping = np.sort(np.unique(Y)) #print(np.sort(np.unique(Y))) filter = True if (filter): m = filter_mask(Y, 'noise') Y = np.array(Y)[m] X = X[m] # Encode Label Y_one_hot = one_hot(Y) X_set = np.concatenate((X_set, X), axis=0) if X_set is not None else X Y_set = np.concatenate( (Y_set, Y_one_hot), axis=0) if Y_set is not None else Y_one_hot return X_set, Y_set, label_mapping
def extract_flat_features_preserve_current_room_and_adjacent_room(X, room): keyset = list(X.keys()) sorted(keyset) # Grab Adjacent Rooms adjacent_rooms = devices.adjacencies[reverse_map_location(room)] # Assemble N = X[keyset[0]].shape[0] C = conv_features_for_in_room(X, room) M = C.shape[1] * (len(adjacent_rooms) + 1) feats = np.zeros((N, M)) # Make first channel = current room pos = 0 stride = C.shape[1] feats[:, pos:pos + stride] = C pos += stride # Remove current room in keyset keyset.remove(room) for k in adjacent_rooms: C = conv_features_for_in_room(X, k) stride = C.shape[1] feats[:, pos:pos + stride] = C pos += stride input = feats num_channels = len(adjacent_rooms) + 1 N = input.shape[0] M = int(input.shape[1] / num_channels) output = np.zeros((N, M * 3)) for n in range(N): output[n, 0:M] = input[n, 0:M] for n in range(N): folded = input[n, M:].reshape(num_channels - 1, -1) max_feats = np.max(folded, axis=0) mean_feats = np.mean(folded, axis=0) output[n, M:M * 2] = max_feats output[n, M * 2:M * 3] = mean_feats return output
Y_train = one_hot_to_zero_index(Y_train) Y_test = one_hot_to_zero_index(Y_test) #################### # Learn # Here's an example using a Random Forest #################### # Commented out for now # forest = NormedRandomForest() # ident = "%s-%s" % (room,day) # forest.train(X_train, Y_train) # Obtain Results log_entry = "%s\t%s\t%s\t%s-%s-%s\tn=%d" % ( features.reverse_map_location(room), method, day, features.reverse_map_location(room), method, day, X_train.shape[1]) # Log Room Accuracy print(log_entry) total = time.time() - et print("===== Execution time for %s-%s: %ds =====" % (dataset.replace(".pklz", ""), method, total)) # Stop Timer t1 = time.time() # Execution Time total = t1 - t0