簡體   English   中英

Keras Python腳本有時運行正常,有時會因Matrix大小不兼容而失敗:在[0]:[10000,1],In [1]:[3,1]

[英]Keras Python script sometimes runs fine, sometimes fails with Matrix size-incompatible: In[0]: [10000,1], In[1]: [3,1]

我希望Keras能夠識別自拍和非自拍,首先我在使用完整數據之前只開發了4張圖片。

問題 :腳本有時會正常運行並退出,有時它會因錯誤Matrix size-incompatible: In[0]: [10000,1], In[1]: [3,1]而失敗Matrix size-incompatible: In[0]: [10000,1], In[1]: [3,1]下面:

$ python run.py
TensorFlow version: 1.10.1
file_names: ['selfies-dev-data/0/lego.jpg', 'selfies-dev-data/0/dakota.jpg', 'selfies-dev-data/1/ammar.jpg', 'selfies-dev-data/1/olivier.jpg']
labels: [0.0, 0.0, 1.0, 1.0]
dataset: <PrefetchDataset shapes: ((?, 100, 100, 1), (?, 1)), types: (tf.float32, tf.float32)>
images: Tensor("IteratorGetNext:0", shape=(?, 100, 100, 1), dtype=float32)
labels: Tensor("IteratorGetNext:1", shape=(?, 1), dtype=float32)
Epoch 1/1
2018-09-25 13:59:17.285143: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
1/2 [==============>...............] - ETA: 0s - loss: 0.7741 - acc: 0.0000e+00Traceback (most recent call last):
  File "run.py", line 64, in <module>
    model.fit(images, labels, epochs=1, steps_per_epoch=2)
  File "/home/nico/.local/lib/python2.7/site-packages/tensorflow/python/keras/engine/training.py", line 1363, in fit
    validation_steps=validation_steps)
  File "/home/nico/.local/lib/python2.7/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 205, in fit_loop
    outs = f(ins)
  File "/home/nico/.local/lib/python2.7/site-packages/tensorflow/python/keras/backend.py", line 2914, in __call__
    fetched = self._callable_fn(*array_vals)
  File "/home/nico/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1382, in __call__
    run_metadata_ptr)
  File "/home/nico/.local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [10000,1], In[1]: [3,1]
         [[Node: rgb_to_grayscale/Tensordot/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](rgb_to_grayscale/Tensordot/Reshape, rgb_to_grayscale/Tensordot/Reshape_1)]]
         [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,100,100,1], [?,1]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]

如何調查這個問題? 代碼將圖像大小調整為100 * 100,輸入圖層也是100 * 100,我不知道3來自哪里。

供參考,這是我的源代碼:

import os
import tensorflow as tf
print "TensorFlow version: " + tf.__version__

out_shape = tf.convert_to_tensor([100, 100])
batch_size = 2

data_folders = ["selfies-dev-data/0", "selfies-dev-data/1"]
classes = [0., 1.]
epoch_size = len(data_folders)

file_names = [] # Path of all data files
labels = [] # Label of each data file (same size as the array above)
for d, l in zip(data_folders, classes):
    name = [os.path.join(d,f) for f in os.listdir(d)] # get the list of all the images file names
    file_names.extend(name)
    labels.extend([l] * len(name))

print "file_names: " + str(file_names)
print "labels: " +str(labels)

file_names = tf.convert_to_tensor(file_names, dtype=tf.string)
labels = tf.convert_to_tensor(labels)

dataset = tf.data.Dataset.from_tensor_slices((file_names, labels))
dataset = dataset.repeat().shuffle(epoch_size)

def map_fn(path, label):
    # path/label represent values for a single example
    image = tf.image.decode_jpeg(tf.read_file(path))

    # some mapping to constant size - be careful with distorting aspect ratios
    image = tf.image.resize_images(image, out_shape)
    image = tf.image.rgb_to_grayscale(image)

    # color normalization - just an example
    image = tf.to_float(image) * (2. / 255) - 1
    label = tf.expand_dims(label, axis=-1)
    return image, label

# num_parallel_calls > 1 induces intra-batch shuffling
dataset = dataset.map(map_fn, num_parallel_calls=8)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(1)
print "dataset: " + str(dataset)

images, labels = dataset.make_one_shot_iterator().get_next()

# Following is from https://www.tensorflow.org/tutorials/keras/basic_classification
from tensorflow import keras

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(100, 100, 1)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(1, activation=tf.nn.sigmoid)
])

model.compile(optimizer=tf.train.AdamOptimizer(),
              loss='binary_crossentropy',
              metrics=['accuracy'])

print "images: " + str(images)
print "labels: " + str(labels)
model.fit(images, labels, epochs=1, steps_per_epoch=2)

問題似乎在以下幾行:

image = tf.image.rgb_to_grayscale(image)

tf.image.rgb_to_grayscale期望給定的圖像張量具有大小為3的最后一個維度,表示RGB通道。 但是,行:

image = tf.image.decode_jpeg(tf.read_file(path))

可以生成具有不同通道數的張量。 這是因為默認情況下, tf.image.decode_jpeg會使張量與JPEG數據中的張量相同。 因此,如果您的圖像已經是灰度圖像,則張量將只有一個通道,程序將失敗。 您可以通過請求在所有情況下將JPEG數據解碼為RGB圖像來解決問題,將channels參數設置為3

image = tf.image.decode_jpeg(tf.read_file(path), channels=3)

這將確保您的所有圖像均勻處理。

暫無
暫無

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

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