简体   繁体   English

将 rgb 图像的 numpy 数组转换为神经网络的灰度图像数组

[英]Conversion of a numpy array of rgb images to an array of grayscale images for a neural network

I'm trying to train a model to classify two different types of dog breed.我正在尝试训练 model 来对两种不同类型的犬种进行分类。 I was given colour images in an array of shape (267, 100, 100, 3).我得到了一组形状(267、100、100、3)的彩色图像。 I want to convert them to a new array of shape (267, 100, 100) of grayscale images.我想将它们转换为灰度图像的新形状(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)

The error:错误:

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

Full error on console:控制台上的完整错误:

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.

(I'm also not sure why x_train and x_test change in the number of samples. I suspect its because the arrays are in the wrong dimension to begin with.) (我也不确定为什么 x_train 和 x_test 的样本数量会发生变化。我怀疑这是因为 arrays 一开始的维度是错误的。)

Thank you.谢谢你。

When you assign the calculated grays scale value to i[x,y] , it broadcasts the RGB value to all 3 ndims entries.当您将计算出的灰度值分配给i[x,y]时,它会将 RGB 值广播到所有 3 个 ndims 条目。 Alos, x_train still has shape: (267, 100, 100, 3) .另外, x_train仍然具有形状: (267, 100, 100, 3) This code snippet demonstrates the behavior with a much smaller array:此代码片段演示了使用小得多的数组的行为:

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])

To get the desired the Gray Scale array, you need to modify x_train after you calculate the values (delete 2 columns and reshape).要获得所需的灰度数组,您需要在计算值后修改x_train (删除 2 列并重新整形)。 You can do this by adding the following line AFTER you exit the for loop:您可以通过在退出 for 循环后添加以下行来执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM