簡體   English   中英

將 rgb 圖像的 numpy 數組轉換為神經網絡的灰度圖像數組

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

我正在嘗試訓練 model 來對兩種不同類型的犬種進行分類。 我得到了一組形狀(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 的樣本數量會發生變化。我懷疑這是因為 arrays 一開始的維度是錯誤的。)

謝謝你。

當您將計算出的灰度值分配給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)   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM