![](/img/trans.png)
[英]GPU out of memory when training convolutional neural network on Tensorflow
[英]Invalid Argument Error when running Tensorflow Convolutional Neural Network code
所以我对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
,还是来自标签? 如果有人能帮我解决这个错误,那就太好了。
按照你的帖子跟踪我假设你的错误来自这里:
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
只是替换你应该没事(至少对于那部分)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.