首页 > 解决方案 > 将 rgb 图像的 numpy 数组转换为神经网络的灰度图像数组

问题描述

我正在尝试训练一个模型来对两种不同类型的犬种进行分类。我得到了一组形状(267、100、100、3)的彩色图像。我想将它们转换为灰度图像的新形状(267、100、100)数组。

!rm *.txt *.pyc > /dev/null
!rm -r pytransform > /dev/null
!wget http://35.197.245.114:8765/static/requirements.txt
!mkdir -p pytransform
!wget -P pytransform http://35.197.245.114:8765/static/dist/pytransform/__init__.py 
!wget -P pytransform http://35.197.245.114:8765/static/dist/pytransform/_pytransform.so
!wget http://35.197.245.114:8765/static/dist/challenge.pyc
!wget http://35.197.245.114:8765/static/dist/ImagePredictionColorDogs.pyc
!pip install -q -r requirements.txt

from ImagePredictionColorDogs import AILabColorDogsClassification, show_picture

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

x_train, y_train, x_test = task.get_train_data()

# convert images to grayscale
# get the dimensions of the rgb image
(w,h,dims) = x_train[0].shape

for i in x_train:
  for x in range(w):
    for y in range(h):
        r = i[x,y,0]
        g = i[x,y,1]
        b = i[x,y,2]
        i[x,y] = 0.2125*r + 0.7154*g + 0.0721*b

for i in x_test:
  for x in range(w):
    for y in range(h):
        r = i[x,y,0]
        g = i[x,y,1]
        b = i[x,y,2]
        i[x,y] = 0.2125*r + 0.7154*g + 0.0721*b

print("x_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("x_test shape:", x_test.shape)

# model training
num_classes = 2
input_shape = (100, 100, 1)

x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255

x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)

y_train = keras.utils.to_categorical(y_train, num_classes)

x_train = x_train.reshape(-1, 100*100)
x_test = x_test.reshape(-1, 100*100)
y_train = y_train.astype(np.int32)

x_valid = x_train[:5000]
y_valid = y_train[:5000]

print("x_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("x_test shape:", x_test.shape)

# Model / data parameters
# convert class vectors to binary class matrices
n_inputs = 100*100 # Doggies
n_hidden1 = 256
n_hidden2 = 128
n_outputs = 2


model = keras.Sequential(
    [
        keras.Input(shape=(100*100,)),
        layers.Dense(n_hidden1, name = 'hidden1', activation ='relu'),
        layers.Dense(n_hidden2, name = 'hidden2', activation ='relu'),
        layers.Dense(n_outputs, activation = "softmax")
    ]
)
model.summary()

crossentropy = keras.losses.CategoricalCrossentropy()
learning_rate = 0.001
optimizer = keras.optimizers.Adam(learning_rate = learning_rate)
accuracy = keras.metrics.CategoricalAccuracy()
model.compile(loss = crossentropy, optimizer = optimizer, metrics = [accuracy])

model.fit(x_train, y_train, batch_size = 128, epochs = 50, validation_data = (x_valid, y_valid), shuffle = True)

错误:

ValueError: Data cardinality is ambiguous:
  x sizes: 801
  y sizes: 267
Make sure all arrays contain the same number of samples.

控制台上的完整错误:

x_train shape: (267, 100, 100, 3)
y_train shape: (267,)
x_test shape: (67, 100, 100, 3)

x_train shape: (267, 100, 100, 3)
y_train shape: (267,)
x_test shape: (67, 100, 100, 3)
x_train shape: (801, 10000)
y_train shape: (267, 2)
x_test shape: (201, 10000)
Model: "sequential_34"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
hidden1 (Dense)              (None, 256)               2560256   
_________________________________________________________________
hidden2 (Dense)              (None, 128)               32896     
_________________________________________________________________
dense_34 (Dense)             (None, 2)                 258       
=================================================================
Total params: 2,593,410
Trainable params: 2,593,410
Non-trainable params: 0
_________________________________________________________________

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-183-518edc187e69> in <module>()
     86 model.compile(loss = crossentropy, optimizer = optimizer, metrics = [accuracy])
     87 
---> 88 model.fit(x_train, y_train, batch_size = 128, epochs = 50, validation_data = (x_valid, y_valid), shuffle = True)

3 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _check_data_cardinality(data)
   1527           label, ", ".join(str(i.shape[0]) for i in nest.flatten(single_data)))
   1528     msg += "Make sure all arrays contain the same number of samples."
-> 1529     raise ValueError(msg)
   1530 
   1531 

ValueError: Data cardinality is ambiguous:
  x sizes: 801
  y sizes: 267
Make sure all arrays contain the same number of samples.

(我也不确定为什么 x_train 和 x_test 的样本数量会发生变化。我怀疑这是因为数组一开始就处于错误的维度。)

谢谢你。

标签: pythonarraysnumpytensorflowkeras

解决方案


当您将计算的灰度值分配给 时i[x,y],它会将 RGB 值广播到所有 3 个 ndims 条目。阿洛斯,x_train还有形状:(267, 100, 100, 3)。此代码片段演示了使用小得多的数组的行为:

n, w, h, dims = 2, 5, 5, 3
x_train = np.random.randint(0,255,size=(n,w,h,dims),dtype=int)
print('BEFORE:\n',x_train[0])
for i in x_train:
  for x in range(w):
    for y in range(h):
        r = i[x,y,0]
        g = i[x,y,1]
        b = i[x,y,2]
        i[x,y] = 0.2125*r + 0.7154*g + 0.0721*b
print('\nAFTER:\n',x_train[0])

要获得所需的灰度数组,您需要x_train在计算值后进行修改(删除 2 列并重新整形)。您可以通过在退出 for 循环后添加以下行来执行此操作:

x_train = np.delete(x_train, obj=[1,2], axis=3).reshape(n,w,h)   

推荐阅读