Недавно я возился с 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()
(я создал функцию расслоения выше для дальнейшего разъяснения)
Код модели:
# 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}))
Что-то явно не так, что я сделал? Ошибки значения обычно просты, но я новичок в нейронных сетях и недостаточно опытен, чтобы понять, что не так.
Любая помощь будет принята с благодарностью, я очень взволнован, чтобы углубиться в мир машинного обучения.
Полный код можно посмотреть здесь .
Вот полный ответ об ошибке
---------------------------------------------------------------------------
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'