在MNIST上使用CIFAR运行CNN时出现值错误

时间:2019-07-01 15:46:35

标签: python tensorflow keras neural-network image-recognition

最近,我一直在研究Tensorflow和机器学习,在在线教程之后,我能够使用MNIST创建模型,并且运行完美。我想更进一步,并将其设置为与CIFAR-100一起使用。运行模型时,出现错误:

ValueError                                Traceback (most recent call last)
<ipython-input-20-e716d3f3b6f0> in <module>
     21         while step < steps:
     22             step += 1
---> 23             sess.run((imageRecognitionNetwork.trainOperation, imageRecognitionNetwork.accuracy_op), feed_dict = {imageRecognitionNetwork.inputLayer: xTrain[step:step+batchSize], imageRecognitionNetwork.labels: yTrain[step:step+batchSize]})
     24             if step % 10 == 0:
     25                 performanceGraph = np.append(performanceGraph, sess.run(imageRecognitionNetwork.accuracy))

我编写的用于导入数据集的代码是:

# Cifar 100 setup

# Set path for cifar 100
cifarPath = './cifar-100-data/'

# Create arrays for data
trainData = np.array([])
trainLabels = np.array([])

# Load in train data
trainData = unpickle(cifarPath + 'train')

# Load in test data
testData = unpickle(cifarPath + 'test')

# Load in meta
meta = unpickle(cifarPath + 'meta')

# Set up train data
xTrain = trainData[b'data']
yTrain = meta[b'fine_label_names']

# Set up test data
xTest = testData[b'data']

xTrain = processCifar100(xTrain)
xTest = processCifar100(xTest)

testImg = xTrain[41223]
plt.imshow(testImg)
plt.show()

(我在上面创建了一个unpickle函数以作进一步说明)

该模型的代码为:

# Image recognition convolutional neural network
# Mnist = tf.float32
# Cifar = tf.string
class imageRecognition:
    def __init__(self, imageHeight, imageWidth, colorChannels, numClasses):

        # Input layer
        self.inputLayer = tf.placeholder(dtype = tf.float32, shape = [None, imageHeight, imageWidth, colorChannels])

        # Convolutional and pooling layers
        clOne = tf.layers.conv2d(self.inputLayer, filters = 32, kernel_size = [2, 2], activation = tf.nn.relu)
        mplOne = tf.layers.max_pooling2d(clOne, pool_size = [2, 2], strides = 2)
        clTwo = tf.layers.conv2d(mplOne, filters = 32, kernel_size = [2, 2], activation = tf.nn.relu)
        mplTwo = tf.layers.max_pooling2d(clTwo, pool_size = [2, 2], strides = 2)
        clThree = tf.layers.conv2d(mplTwo, filters = 32, kernel_size = [2, 2], activation = tf.nn.relu)
        mplThree = tf.layers.max_pooling2d(clThree, pool_size = [2, 2], strides = 2)

        # Flatten pooling layer
        flattenLayer = tf.layers.flatten(mplThree)
        # Dense layer
        denseLayer = tf.layers.dense(flattenLayer, 1024, activation = tf.nn.relu)
        # Dropout layer
        dropoutLayer = tf.layers.dropout(denseLayer, rate = 0.4, training = toTrain)

        # Output layer
        outputs = tf.layers.dense(dropoutLayer, numClasses)

        # Choose outputs
        self.choice = tf.argmax(outputs, axis = 1)
        self.probability = tf.nn.softmax(outputs)
        self.labels = tf.placeholder(dtype = tf.float32, name = "labels")
        self.accuracy, self.accuracy_op = tf.metrics.accuracy(self.labels, self.choice)
        oneHotLabels = tf.one_hot(indices = tf.cast(self.labels, dtype = tf.int32), depth = numClasses)
        self.loss = tf.losses.softmax_cross_entropy(onehot_labels = oneHotLabels, logits = outputs)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-2)

        # Training
        self.trainOperation = optimizer.minimize(self.loss, global_step = tf.train.get_global_step())

培训内容是:

# Run the network
imageRecognitionNetwork = imageRecognition(imageHeight, imageWidth, colorChannels, numClasses)
with tf.Session() as sess:

    # Declare saver
    checkpointSaver = tf.train.Saver()

    # Load checkpoints if we want
    if not toTrain:
        checkpoint = (tf.train.get_checkpoint_state(path))
        checkpointSaver.restore(sess, checkpoint.model_checkpoint_path)
    else:
        sess.run(tf.global_variables_initializer())

    # Initialize required local variables
    sess.run(tf.local_variables_initializer())

    # Train if we want
    if toTrain:
        step = 0
        while step < steps:
            step += 1
            sess.run((imageRecognitionNetwork.trainOperation, imageRecognitionNetwork.accuracy_op), feed_dict = {imageRecognitionNetwork.inputLayer: xTrain[step:step+batchSize], imageRecognitionNetwork.labels: yTrain[step:step+batchSize]})
            if step % 10 == 0: 
                performanceGraph = np.append(performanceGraph, sess.run(imageRecognitionNetwork.accuracy))

        # Save train data
        checkpointSaver.save(sess, path + modelName)

        # Print out train stats
        print("\nMy accuracy level is: {0}%".format(round((performanceGraph[performanceGraph.size-1]) * 100)))
        plt.figure().set_facecolor('white') 
        plt.xlabel("Steps / 10") 
        plt.ylabel("Accuracy") 
        plt.plot(performanceGraph)

    # Print output
    print("The number is:", sess.run(imageRecognitionNetwork.choice, feed_dict = {imageRecognitionNetwork.inputLayer: testImg}))

我做些明显的错误吗?值错误通常很简单,但我是神经网络的新手,经验不足,无法找出问题所在。

任何帮助将不胜感激,我很高兴能进一步进入机器学习领域。

您可以查看完整的代码here

这里是对错误的完整追溯

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-e716d3f3b6f0> in <module>
     21         while step < steps:
     22             step += 1
---> 23             sess.run((imageRecognitionNetwork.trainOperation, imageRecognitionNetwork.accuracy_op), feed_dict = {imageRecognitionNetwork.inputLayer: xTrain[step:step+batchSize], imageRecognitionNetwork.labels: yTrain[step:step+batchSize]})
     24             if step % 10 == 0:
     25                 performanceGraph = np.append(performanceGraph, sess.run(imageRecognitionNetwork.accuracy))

c:\python37\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

c:\python37\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1140             feed_handles[subfeed_t] = subfeed_val
   1141           else:
-> 1142             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
   1143 
   1144           if (not is_tensor_handle_feed and

c:\python37\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    536 
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539 
    540 

ValueError: could not convert string to float: b'aquarium_fish'

0 个答案:

没有答案