【问题标题】:Invalid Argument Error when running Tensorflow Convolutional Neural Network code运行 Tensorflow 卷积神经网络代码时出现无效参数错误
【发布时间】:2019-08-11 23:51:24
【问题描述】:

所以我是 tensorflow 和 python 的新手,今年我正在做一个研究项目,我正在开发一个简单的卷积神经网络,可以检测手写图像并根据训练样本猜测哪个手写属于谁。用于此数据集的图像是我自己的。我遇到的问题是我在编译代码时遇到了这个错误:

Shape must be rank 0 but is rank 2 for 'ReadFile' (op: 'ReadFile') with input shapes: [5,4].

我看了一个与此类似的帖子,看到了答案,但问题是我的代码很不一样,而且对tensorflow比较陌生,对一些代码没有深入的理解。

下面是我的 CNN 代码。我使用了一些在线资源和教程来构建它:

# Import statements
import os, sys
import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.utils import *
from tensorflow.python.keras.layers import *
from tensorflow.python.keras.layers.advanced_activations import *
from tensorflow.python.keras.optimizers import *
from tensorflow.python.keras.losses import *
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from math import floor, ceil
from pylab import rcParams

# Disables the warning
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Filename vectors
imageFile = tf.constant([['bob1.jpg', 'bob2.jpg', 'bob3.jpg', 'bob4.jpg'],
                        ['peter1.jpg', 'peter2.jpg', 'peter3.jpg', 'peter4.jpg'],
                        ['steve1.jpg', 'steve2.jpg', 'steve3.jpg', 'steve4.jpg'],
                        ['josh1.jpg', 'josh2.jpg', 'josh3.jpg', 'josh4.jpg'],
                        ['chris1.jpg', 'chris2.jpg', 'chris3.jpg', 'chris4.jpg']])
testFile = tf.constant(['bob5.jpg', 'peter5.jpg', 'steve5.jpg', 'josh5.jpg', 'chris5.jpg'])

# Label vectors
imageLabels = tf.read_file([['bob','bob','bob','bob'],
                        ['peter','peter','peter','peter'],
                        ['steve','steve','steve','steve'],
                        ['josh','josh','josh','josh'],
                        ['chris','chris','chris','chris']])
testLabels = tf.read_file(['bob', 'peter', 'steve', 'josh', 'chris'])

# Resizing function that enables all images to be sized the same
def _resize_function(filename, label):
  image_name = tf.read_file(filename)
  image_decoded = tf.image.decode_jpeg(image_name, channels = 2)
  image = tf.cast(image_decoded, tf.float32)
  image_resized = tf.image.resize_images(image, [1290, 560])
  return image_resized, label

# Datasets
trainSet = tf.data.Dataset.from_tensor_slices((imageFile, imageLabels))
trainSet = trainSet.map(_resize_function)
testSet = tf.data.Dataset.from_tensor_slices((testFile, testLabels))
testSet = testSet.map(_resize_function)

# Iterators
train_iterator = trainSet.make_one_shot_iterator()
images, labels = trainSet.get_next()
test_iterator = testSet.make_one_shot_iterator()
testImg, testLb = testSet.get_next()

# Model
model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape = (5, 4, 1, 1), activation = 'relu'))
model.add(MaxPooling2D((2, 2), padding = 'same'))
model.add(Dropout(0.25))
model.add(Conv2D(64, 3, 3, input_shape = (5, 4, 1, 1), activation = 'relu'))
model.add(MaxPooling2D((2, 2), padding = 'same'))
model.add(Dropout(0.25))
model.add(Conv2D(128, 3, 3, input_shape = (5, 4, 1, 1), activation = 'relu'))
model.add(MaxPooling2D((2, 2), padding = 'same'))
model.add(Dropout(0.25))
# FInal layer - flattening
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))

# Compilation
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# Run. Epochs should be varied as well.
fit = model.fit(images, labels, steps_per_epoch = 4, batch_size = 5, epochs = 2, verbose = 1, validation_data = (testImg, testLb))
score = model.evaluate(testImg, testLb, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

错误来自_resize_function,还是来自标签?如果有人能帮我解决这个错误,那就太好了。

【问题讨论】:

  • 能否用完整的错误消息更新您的问题?
  • 我认为错误来自input_shape = (5, 4, 1, 1)尝试input_shape = (5, 4)

标签: python tensorflow machine-learning conv-neural-network


【解决方案1】:

跟踪您的帖子,我假设您遇到的错误来自这里:

imageLabels = tf.read_file([['bob','bob','bob','bob'],
                    ['peter','peter','peter','peter'],
                    ['steve','steve','steve','steve'],
                    ['josh','josh','josh','josh'],
                    ['chris','chris','chris','chris']])

您正在传递一个形状为 [5,4] 的矩阵,据我了解,这不是函数所期望的。

我认为这是一个错字,你的意思是 tf.constant 用于标签,而不是 tf.read_file 只是替换,你应该没问题(至少在那部分)

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2019-08-18
    • 2020-08-21
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多