0
$\begingroup$

I am running a convolutional neural network with CSV files as training and test input. I am getting a strange error that I cannot solve.

    Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1350, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1320, in _run_fn
    self._extend_graph()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1381, in _extend_graph
    self._session, graph_def.SerializeToString(), status)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: No OpKernel was registered to support Op 'Switch' with these attrs.  Registered devices: [CPU], Registered kernels:
  device='CPU'; T in [DT_BOOL]
  device='CPU'; T in [DT_FLOAT]
  device='CPU'; T in [DT_INT32]
  device='GPU'; T in [DT_STRING]
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_FLOAT]

     [[Node: remove_squeezable_dimensions/cond/Switch_1 = Switch[T=DT_INT64, _class=["loc:@ArgMax"]](ArgMax, remove_squeezable_dimensions/cond/pred_id)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Desktop/CNN.py", line 151, in <module>
    sess.run(init)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1128, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1344, in _do_run
    options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1363, in _do_call
    raise type(e)(node_def, op, message)

I am running Tensorflow 1.5 on a RaspberryPi 3. Here is the neural network below.

    import tensorflow as tf
import numpy as np
import csv
import pandas as pd
import os

image_height = 60
image_width = 1

image1_height = 15
image2_height = 1

model_name = "TensorflowCNN"


#Training Data Configuration

train_data = np.asarray(pd.read_csv("/media/pi/DISK_IMG/TrainingInput.csv", usecols=[1]))
lis = train_data.tolist()
lis = lis[0:60]
lis = [x[0].strip('[]\n,') for x in lis]
nlis = []
for i in lis:
    nlis.append(i.split())
for i in range(len(nlis)):
    nlis[i] = [float(x) for x in nlis[i] if x != "...,"]
nlis = [np.mean(x) for x in nlis]
train_data = np.asarray(nlis)

#Training Labels Configuration

train_labels = np.asarray(pd.read_csv("/media/pi/DISK_IMG/TrainingInput.csv", usecols=[2]))
mylist = train_labels.tolist()
mylist = mylist[0:60]
mylist = [x[0] for x in mylist]
index = 0
while index < len(mylist):
    if mylist[index] == "GravelTraining":
        mylist[index] = 1
    elif mylist[index] == "WaterTraining":
        mylist[index] = 2
    else:
        mylist[index] = 3

    index=index+1

train_labels = np.asarray(mylist)

#Validation Data Configuration

eval_data = np.asarray(pd.read_csv("/media/pi/DISK_IMG/TestingInput.csv", usecols=[1]))
List = eval_data.tolist()
List = List[0:15]
eval_data = np.asarray(List)

#Validation Labels Configuration

eval_labels = np.asarray(pd.read_csv("/media/pi/DISK_IMG/TestingInput.csv", usecols=[2]))
myList = eval_labels.tolist()
myList = myList[0:15]
index = 0
while index < len(myList):
    if myList[index] == "GravelTesting":
        myList[index] = 1
    elif myList[index] == "WaterTesting":
        myList[index] = 2
    else:
        myList[index] = 3

    index=index+1
eval_labels = np.asarray(myList)

category_names = list(map(str, range(3)))

#Processing and reshaping data

train_data = np.reshape(train_data, (-1, image_height, image_width, 1))
train_labels = np.reshape(train_labels, (-1, image_height, image_width, 1))

eval_data = np.reshape(eval_data, (-1, image1_height, image2_height, 1))
eval_labels = np.reshape(eval_labels, (-1, image1_height, image2_height, 1))


#CLASS FOR THE CONVOLUTIONAL NEURAL NETWORK


class ConvNet:

    def __init__(self, image_height, Image_width, num_classes, chan):

        self.input_layer = tf.placeholder(dtype = tf.float32, shape = [1,image_height, Image_width, chan], name = "inputs")

        conv_layer_1 = tf.layers.conv2d(self.input_layer, filters = 32, kernel_size = [5,5], padding = "same", activation = tf.nn.relu)
        pooling_layer_1 = tf.layers.max_pooling2d(conv_layer_1, pool_size = [2,1], strides = 1)

        flattened_pooling = tf.layers.flatten(pooling_layer_1)
        dense_layer = tf.layers.dense(flattened_pooling, 60, activation = tf.nn.relu)

        dropout = tf.layers.dropout(dense_layer, rate = 0.4, training = True)

        output_dense_layer = tf.layers.dense(dropout, num_classes)

        self.choice = tf.argmax(output_dense_layer, axis=1)
        self.probabilities = tf.nn.softmax(output_dense_layer)

        self.labels = tf.placeholder(dtype=tf.float32, name="labels")
        self.accuracy, self.accuracy_op = tf.metrics.accuracy(self.labels, self.choice)

        one_hot_labels = tf.one_hot(indices=tf.cast(self.labels, dtype=tf.int32), depth=num_classes)
        self.loss = tf.losses.softmax_cross_entropy(onehot_labels = one_hot_labels, logits=output_dense_layer)

        optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-2)
        self.train_operation = optimizer.minimize(loss=self.loss, global_step=tf.train.get_global_step())

#Training process:variables

training_steps = 20000
batch_size = 60

path = "./" + model_name + "-cnn/"

load_checkpoint = False

tf.reset_default_graph()
dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
dataset = dataset.shuffle(buffer_size=train_labels.shape[0])
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()

dataset_iterator = dataset.make_initializable_iterator()
next_element = dataset_iterator.get_next()

#Final initialization of Neural Network and Training Process

cnn = ConvNet(image_height, image_width, 3, 1)
print("milestone1")

saver = tf.train.Saver(max_to_keep=2)
print('milestone2')

if not os.path.exists(path):
    os.makedirs(path)
print('milestone3')

#Training Loop For neural network

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())
    print('milestone4')

    sess.run(tf.local_variables_initializer())
    sess.run(dataset_iterator.initializer)

    for step in range(training_steps):

        current_batch = sess.run(next_element)
        batch_inputs = current_batch[0]
        batch_labels = current_batch[1]
        print("milestone5")
        sess.run((cnn.train_operation, cnn.accuracy_op), feed_dict={cnn.input_layer:batch_inputs, cnn.labels:batch_labels})

        if step % 1 == 0 and step > 0:
            current_acc = sess.run(cnn.accuracy)
            print("Accuracy at step " + str(step) + ":" + str(current_acc))
            saver.save(sess, path + model_name, step)

    print("Saving final checkpoint for training session.")
    saver.save(sess, path + model_name, step)

I would appreciate it if someone could point me in the right direction.

_________________________________UPDATE___________________________________

Based on an answer given earlier to this question, I updated my version of tensorflow. I now get this error:

2018-08-15 17:16:42.551134: F ./tensorflow/core/util/bcast.h:111] Check failed: vec.size() == NDIMS (1 vs. 2)

Aborted

How should I fix this?

$\endgroup$
0
$\begingroup$

From the error message you gave, it seems like it happens because of your hardware than your code.
Now Tensorflow officially supports for Raspberry Pi 3 since the version 1.8, so you may want to consider the higher version of TF for Raspberry Pi porting.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I updated and ran into another error. I've updated the question in case you can help me with this new problem. But in any case, I've already marked your answer as correct because you solved my original problem. Thanks and hope you can help me further! $\endgroup$ – ab123 Aug 16 '18 at 0:21
  • $\begingroup$ Just checking in to see if you have a solution to this new problem. $\endgroup$ – ab123 Aug 20 '18 at 4:39
  • $\begingroup$ Hi. I've unmarked your question to attract more traffic. After I get a solution, I'll mark it again. $\endgroup$ – ab123 Aug 22 '18 at 2:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.