def get_saliency_for_classification(data, label, saliency_function, batch_size=50, balance_data=False, class_func='sum', reduce_func='sum', generator=None, generator_kwargs=None, generator_epochs=50, horizontal_flip=False): label_ids = np.unique(label) if label.ndim == 1 else list( range(label.shape[-1])) saliency_function = [] if isinstance(class_func, str): class_func = getattr(np, class_func) data_, label_ = utils.balance_data(data, label) if balance_data else data, label for label_id in label_ids: index = np.where( label_ == label_id)[0] if label_.ndim == 1 else np.where( label_[:, label_id] == 1)[0] label_saliency = get_saliency_func(data_[index], label_[index], saliency_function, batch_size, reduce_func, generator, generator_kwargs, generator_epochs) if horizontal_flip and len(data_.shape) > 2: label_saliency += get_saliency_func(data_[index][..., ::-1, :], label_[index], saliency_function, batch_size, reduce_func) label_saliency /= np.maximum(1e-6, np.sum(np.abs(label_saliency))) saliency_function.append(label_saliency) saliency = class_func(saliency_function, axis=0) # factor = np.sign(np.max(saliency_function, axis=0)) # print('bad values :', (factor < 0).sum()) saliency /= np.maximum(1e-6, np.abs(saliency).sum()) del data_, label_, saliency_function return saliency
dataset_name, max_features = dataset_stats print('loading dataset', dataset_name) dataset = load_dataset(dataset_name, directory=datasets_directory, normalize=dataset_name not in ['dexter', 'dorothea']) print('data loaded. labels =', dataset['train']['data'].shape) batch_size = min(len(dataset['train']['data']), b_size) input_shape = dataset['train']['data'].shape[-1:] nclasses = len(np.unique(dataset['train']['label'])) data = dataset['train']['data'] label = dataset['train']['label'] data, label = balance_data(data, label) valid_data = dataset['validation']['data'] valid_label = dataset['validation']['label'] label = kutils.to_categorical(label, 2) valid_label = kutils.to_categorical(valid_label, 2) total_features = data.shape[-1] fs_filename = root_directory + dataset_name + '_gamma_' + str( gamma) + '_ranks.json' output_filename = root_directory + dataset_name + '_gamma_' + str( gamma) + '_results.json' try: with open(fs_filename) as outfile: