gpt4 book ai didi

python - 使用 CNN 模型中的 channel 均值和标准差对训练数据进行归一化

转载 作者:行者123 更新时间:2023-12-04 12:58:18 25 4
gpt4 key购买 nike

我正在使用CNN进行多类图像分类,但准确性不是很好。我假设我需要使用 channel 均值和标准偏差对训练数据进行归一化,以便它可能有助于提高准确性。我想出了一种方法来做到这一点,但它不是很有效,因为我只是为均值和标准化的标准差设置了随机值。我不确定如何找到 channel 均值及其标准差。我想知道有没有办法做到这一点。谁能指出我如何实现这一目标?任何可能的想法?
我目前的尝试 :

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Input
from keras.datasets import cifar10
from keras.utils import to_categorical

(X_train, y_train), (X_test, y_test)= cifar10.load_data()
output_class = np.unique(y_train)
n_class = len(output_class)

input_shape = (32, 32, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
y_train_one_hot = to_categorical(y_train)
y_test_one_hot = to_categorical(y_test)

x = tf.keras.Input(shape=(32, 32, 3))
conv = Conv2D(128, (3, 3), activation='relu',input_shape=(32, 32, 3))(x)
conv = MaxPooling2D(pool_size=(2,2))(conv)
conv = Conv2D(64, (2,2))(conv)
conv = MaxPooling2D(pool_size=(2,2))(conv)
conv = Flatten()(conv)
conv = Dense(64, activation='relu')(conv)
conv = Dense(10, activation='softmax')(conv)
model = Model(inputs = x, outputs = conv)
我的标准化尝试 :
这是我的归一化方式,我只是将随机值分配给均值和标准差:
mean = [125.307, 122.95, 113.865]  ## random value
std = [62.9932, 62.0887, 66.7048] ## random value

for i in range(3):
X_train[:,:,:,i] = (X_train[:,:,:,i] - mean[i]) / std[i]
X_test[:,:,:,i] = (X_test[:,:,:,i] - mean[i]) / std[i]
我想知道有没有办法以编程方式找到 channel 均值及其标准偏差,以便我们可以进行归一化。这样做有什么更好的主意吗?还有什么可以提高我的样本模型的准确性?如何找到 channel 均值及其标准差?任何可能的策略或编码尝试?

最佳答案

我相信您可以通过这种方式进行数据规范化,这很有希望:

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
nb_classes = 10
Y_train = to_categorical(y_train, nb_classes)
Y_test = to_categorical(y_test, nb_classes)

## find channel mean, std and do data normalization
train_mean = np.mean(X_train, axis=0)
train_std = np.std(X_train, axis=0)
X_train = (X_train - train_mean) / train_std
X_test = (X_test - train_mean) / train_std

## then do training ....
希望这是您为规范化所做的。如果您有任何问题,请告诉我 :)

关于python - 使用 CNN 模型中的 channel 均值和标准差对训练数据进行归一化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63027146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com