python报错:tf.image.per_image_whitening

本文解决了一个常见的TensorFlow预处理错误,当遇到'AttributeError: 'module' object has no attribute 'per_image_whitening''时,可以简单地将per_image_whitening替换为per_image_standardization来修复。
部署运行你感兴趣的模型镜像

在这里插入图片描述
AttributeError:‘module’对象没有属性’per_image_whitening’
解决方案:
用per_image_standardization替换per_image_whitening就行

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

import pickle import numpy as np import torch from torch.autograd import Variable # 封装了Tensor,并整合了反向传播的相关实现 import math import scipy.io as sio import time time_start = time.time() num_select = 1000 num_ratio = 0.8 num_train = int(num_select * num_ratio) num_test = int(num_select - num_train) # alpha固定 num_GSNR = int(45) num_alpha = int(1) # GSNR固定 # num_GSNR = int(1) # num_alpha = int(12) type_modulation1 = ['16QAM', '2PSK', '4PAM', '4PSK', '64QAM', '8PSK', 'AM_DSB', 'AM_SSB', 'FM', 'GMSK', 'MSK'] type_modulation2 = [] type_modulation = type_modulation1+type_modulation2 num_modulation = len(type_modulation) pred_mat = np.zeros([1, num_modulation*num_modulation*num_GSNR*num_alpha]) pred_mat.shape = [num_modulation, num_modulation, num_GSNR, num_alpha] pred_mat = np.floor(pred_mat) IMG_WIDTH = IMG_HEIGHT = 36 IMG_DEPTH = 1 def whitening_image(image_np): ''' Performs per_image_whitening :param image_np: a 4D numpy array representing a batch of images :return: the image numpy array after whitened ''' for i in range(len(image_np)): mean = np.mean(image_np[i, ...]) # Use adjusted standard deviation here, in case the std == 0. std = np.max([np.std(image_np[i, ...]), 1.0/np.sqrt(IMG_HEIGHT * IMG_WIDTH * IMG_DEPTH)]) image_np[i, ...] = (image_np[i, ...] - mean) / std return image_np def _read_one_batch(path, is_random_label): ''' The training data contains five data batches in total. The validation data has only one batch. This function takes the directory of one batch of data and returns the images and corresponding labels as numpy arrays :param path: the directory of one batch of data :param is_random_label: do you want to use random labels? :return: image numpy arrays and label numpy arrays ''' fo = open(path, 'rb') dicts = pickle.load(fo, encoding = 'iso-8859-1') # 将文件中的数据解析为一个python对象 fo.close() data = dicts[b'data'] filenames = dicts[b'filename'] if is_random_label is False: # label = np.array(dicts['labels']) label = np.array(dicts[b'labels']) else: labels = np.random.randint(low=0, high=10, size=10000) # 返回随机整型数 label = np.array(labels) return data, label, filenames def read_in_all_images(address_list, shuffle = True, is_random_label = False): """ This function reads all training or validation data, shuffles them if needed, and returns the images and the corresponding labels as numpy arrays :param address_list: a list of paths of cPickle files :return: concatenated numpy array of data and labels. Data are in 4D arrays: [num_images, image_height, image_width, image_depth] and labels are in 1D arrays: [num_images] """ data = np.array([]).reshape([0, IMG_WIDTH * IMG_HEIGHT * IMG_DEPTH]) label = np.array([]) filenames = np.array([]) for address in address_list: print('Reading images from ' + address) batch_data, batch_label, batch_filenames = _read_one_batch(address, is_random_label) # Concatenate along axis 0 by default data = np.concatenate((data, batch_data)) # numpy库数组拼接 label = np.concatenate((label, batch_label)) filenames = np.concatenate((filenames, batch_filenames)) num_data = len(label) # This reshape order is really important. Don't change # Reshape is correct. Double checked data = data.reshape((num_data, IMG_HEIGHT * IMG_WIDTH, IMG_DEPTH), order='F') # Fortran语言排列格式 data = data.reshape((num_data, IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH)) if shuffle is True: print('Shuffling') order = np.random.permutation(num_data) # 随机排列序列 data = data[order, ...] label = label[order] filenames= filenames[order] data = data.astype(np.float32) # 转换数据类型 return data, label, filenames def get_acc(output, label): total = output.shape[0] # 获取第一个维度 _, pred_label = output.max(1) # 返回每一行中最大值的那个元素,且返回其索引(返回最大元素在这一行的列索引) num_correct = (pred_label == label).sum().item() # 返回预测正确的数量 return num_correct / total # 准确率 test_data, test_label, test_filename = read_in_all_images([r"D:\shixun\data_train_test\data_test1"], shuffle=False) model = torch.load( './model/model0.4090909090909091.pkl', map_location=torch.device('cpu'), weights_only=False # 关闭权重仅加载模式,允许加载自定义模型类 ) print('load best model successfully') test_acc = 0 test_batch_size = 256 model = model.eval() num_test_data = len(test_data) num_batch_test = math.ceil(num_test_data / test_batch_size) # 向上取整 for cnt_test_batch in range(num_batch_test): # print(cnt_val_batch) test_batch_data = test_data[cnt_test_batch * test_batch_size:(cnt_test_batch + 1) * test_batch_size, ...] test_batch_data = whitening_image(test_batch_data) test_batch_tensor = torch.from_numpy(test_batch_data) # 转化为张量 test_data_tensor_cov = test_batch_tensor.permute(0, 3, 1, 2) # 交换维度 test_batch_label = test_label[cnt_test_batch * test_batch_size:(cnt_test_batch + 1) * test_batch_size, ...] name = test_filename[cnt_test_batch * test_batch_size:(cnt_test_batch + 1) * test_batch_size, ...] test_label_tensor = torch.from_numpy(test_batch_label) if torch.cuda.is_available(): with torch.no_grad(): test_data_tensor_cov = Variable(test_data_tensor_cov.cuda()) # (bs, 3, h, w)tensor变成variable test_label_tensor = Variable(test_label_tensor.cuda()) # (bs, else: with torch.no_grad(): test_data_tensor_cov = Variable(test_data_tensor_cov) test_label_tensor = Variable(test_label_tensor) out = model(test_data_tensor_cov) test_acc += get_acc(out, test_label_tensor) for cnt_modulation in range(0, num_modulation, 1): print('cnt_modulation={}'.format(int(cnt_modulation))) # path_tal = '/Test_Data_'+str(num_select)+'/'+type_modulation[cnt_modulation] # path_test = path_hed + path_mid + path_tal + '/' filnam_hed = 'b\"[\'SR_CONST_' filename_hed = filnam_hed + type_modulation[cnt_modulation] for GSNR in range(-20, 22, 2): print('GSNR={}'.format(int(GSNR))) cnt_GSNR = GSNR + 20 for alpha in range(15, 16, 1): print('alpha={}'.format(int(alpha))) cnt_alpha = alpha - 15 filename_mid = ' alpha = ' + str(int(alpha)) + ' GSNR = ' + str(int(GSNR)) # for cnt_cycle in range(1, num_test + 1, 1): filename_tal = '.mat\']\"' filename_test = filename_hed + filename_mid + filename_tal for i in range(len(test_batch_data)): filename = str(name[i]) # print(filename) true_label = int(test_batch_label[i]) if filename == filename_test: print(filename) print(cnt_modulation) print(true_label) _, pred_label = out.max(1) pre_labels = pred_label.cpu().numpy() pre_label = pre_labels[i] pred_mat[cnt_modulation, pre_label, cnt_GSNR, cnt_alpha] = pred_mat[cnt_modulation, pre_label, cnt_GSNR, cnt_alpha] + 1 print('Accuracy of the test set: %f%%' % (test_acc / num_batch_test*100)) sio.savemat('CONST2_3.mat', {'CONST2_3': pred_mat}) print('The confusion matrix has been successfully generated!') time_end = time.time() print('time cost', time_end-time_start, 's') :\Users\Lenovo\PyCharmMiscProject\.venv\Scripts\python.exe "D:\shixun\confusion matrix.py" Reading images from D:\shixun\data_train_test\data_test1 Traceback (most recent call last): File "D:\shixun\confusion matrix.py", line 118, in <module> test_data, test_label, test_filename = read_in_all_images([r"D:\shixun\data_train_test\data_test1"], shuffle=False) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\shixun\confusion matrix.py", line 91, in read_in_all_images data = np.concatenate((data, batch_data)) # numpy库数组拼接 ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1296 and the array at index 1 has size 10000 修正代码
最新发布
12-17
<think>好的,用户想要了解如何在Python中使用NumPy和scikit-learn实现PCA白化。首先,我需要回顾一下PCA白化的基本概念。白化的目的是对数据进行去相关和标准化,使得各维度具有单位方差。这在图像处理等场景中很常见,比如在训练神经网络前预处理数据。 接下来,用户可能已经知道PCA的基本步骤,但需要明确白化的具体实现。根据之前的引用,scikit-learn的PCA类提供了whiten参数,设为True即可实现白化。不过用户可能希望用NumPy手动实现,以更深入理解过程。手动实现的话,步骤应该是:标准化数据、计算协方差矩阵、特征分解、投影到主成分并缩放特征值。这里需要注意数据标准化的步骤,即减去均值并除以标准差。然后协方差矩阵的特征向量和特征值用于旋转数据,并缩放每个分量到单位方差。另外,用户可能关心优化建议,比如数值稳定性问题,处理零特征值的情况,或者使用SVD代替特征分解来提高效率。还要注意scikit-learn的PCA在应用白化时的参数设置是否正确,比如确保使用正确的n_components。用户可能遇到的问题包括数据维度的处理,白化后的数据是否保持原有结构,以及如何验证白化后的协方差矩阵是否为单位矩阵。需要提供代码示例和验证步骤,比如计算协方差矩阵是否为近似单位矩阵。 需要参考之前的引用,特别是引用[1]中的PCA使用案例,引用[3]的数据标准化方法,以及引用[4]的特征处理。确保代码中正确应用了标准化,并且在白化过程中正确缩放特征向量。 最后,生成相关问题时要围绕PCA白化的应用、验证方法、参数影响和与其他预处理方法的结合,帮助用户进一步学习。</think>### PCA白化的实现步骤与代码示例 PCA白化(Whitening)的目标是将数据投影到主成分空间后,对每个维度进行标准化,使其具有单位方差。以下是基于NumPy和scikit-learn的实现方法: --- #### **1. 使用scikit-learn的PCA类实现白化** scikit-learn的`PCA`类直接支持白化,通过设置`whiten=True`即可: ```python from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler import numpy as np # 生成示例数据 X = np.random.randn(100, 5) # 100个样本,5个特征 # 标准化数据(均值为0,方差为1) X_scaled = StandardScaler().fit_transform(X) # 应用PCA并启用白化 pca = PCA(n_components=5, whiten=True) X_whitened = pca.fit_transform(X_scaled) # 验证协方差矩阵是否接近单位矩阵 print("协方差矩阵:\n", np.cov(X_whitened.T)) ``` 输出应接近单位矩阵,例如: ``` 协方差矩阵: [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] ...] ``` --- #### **2. 手动实现(基于NumPy)** 手动实现分为以下步骤: 1. **标准化数据**:使每个特征均值为0,方差为1。 2. **计算协方差矩阵的特征分解**:获取特征向量和特征值。 3. **投影并缩放**:将数据投影到主成分后,用特征值的平方根倒数进行缩放。 ```python # 标准化数据 X_scaled = StandardScaler().fit_transform(X) # 计算协方差矩阵 cov_matrix = np.cov(X_scaled.T) # 特征分解 eigen_vals, eigen_vecs = np.linalg.eigh(cov_matrix) # 按特征值降序排序 sorted_idx = np.argsort(eigen_vals)[::-1] eigen_vals = eigen_vals[sorted_idx] eigen_vecs = eigen_vecs[:, sorted_idx] # 白化变换矩阵 epsilon = 1e-5 # 防止除以零 whitening_matrix = eigen_vecs @ np.diag(1.0 / np.sqrt(eigen_vals + epsilon)) # 应用白化 X_whitened_manual = X_scaled @ whitening_matrix # 验证协方差矩阵 print("手动实现协方差矩阵:\n", np.cov(X_whitened_manual.T)) ``` --- #### **优化建议** 1. **数值稳定性**:添加极小值`epsilon`避免特征值为零时的除零错误[^3]。 2. **使用SVD替代特征分解**:对大型数据更高效: ```python U, s, Vt = np.linalg.svd(X_scaled, full_matrices=False) X_whitened_svd = U * np.sqrt(X_scaled.shape[0] - 1) ``` 3. **降维与白化结合**:通过`n_components`参数保留主要成分,例如`PCA(n_components=3, whiten=True)`[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值