运行Tensorflow卷积神经网络代码时

时间:2019-03-21 06:47:54

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

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

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

我看到了与此类似的帖子,并且看到了答案,但是问题是我的代码完全不同,并且对于tensorflow来说相对较新,我对某些代码没有深刻的理解。 / p>

以下是我的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,还是来自标签?如果有人可以帮助我解决这个错误,那就太好了。

1 个答案:

答案 0 :(得分:0)

按照您的帖子追踪,我认为您的错误来自此处:

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只是替换而已,您应该没问题(至少在该部分而言)