Reputation: 457
I'm trying to adapt the Tensorflow MNIST beginner tutorial to my own set of images. Here's my code so far:
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A very simple MNIST classifier.
See extensive documentation at
http://tensorflow.org/tutorials/mnist/beginners/index.md
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Import data
# from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import pandas as pd
import numpy as np
IMAGE_WIDTH = 240
IMAGE_HEIGHT = 240
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT
SYMBOLS = ["accident", "bomb", "car", "casualty", "electricity", "fire", "firebrigade", "flood", "gaz", "injury", "paramedics", "person", "police", "roadblock"]
NUM_CLASSES = len(SYMBOLS)
def chunks(l, n):
n = max(1, n)
return (l[i:i+n] for i in xrange(0, len(l), n))
def main(_):
# Create the model
x = tf.placeholder(tf.float32, [None, IMAGE_SIZE])
W = tf.Variable(tf.zeros([IMAGE_SIZE, NUM_CLASSES]))
b = tf.Variable(tf.zeros([NUM_CLASSES]))
y = tf.matmul(x, W) + b
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])
# The raw formulation of cross-entropy,
#
# tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.softmax(y)),
# reduction_indices=[1]))
#
# can be numerically unstable.
#
# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
# outputs of 'y', and then average across the batch.
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
train = pd.read_csv('test.csv')
train.head()
xs = []
for img_name in train.filename:
img = tf.image.decode_png(img_name)
img = tf.image.rgb_to_grayscale(img)
img = tf.image.resize_images(img, [IMAGE_WIDTH, IMAGE_HEIGHT])
img = tf.expand_dims(img, 0)
xs.append(img)
ys = []
for label_name in train.label:
one_hot = [0] * NUM_CLASSES
one_hot[int(label_name)] = 1
ys.append(one_hot)
# Train
tf.initialize_all_variables().run()
batches_img = [xs[x:x+100] for x in xrange(0, len(xs), 100)]
batches_labels = [ys[x:x+100] for x in xrange(0, len(ys), 100)]
for i, e in enumerate(batches_img):
sess.run(train_step, feed_dict={x: batches_img[i], y_: batches_labels[i]})
print("Training done!\n")
# Test trained model
"""test = pd.read_csv('test.csv')
test.head()
xs_test = []
for img_name in test.filename:
img = imread(img_name, flatten=True)
xs_test.append(img)
ys_test = []
for label_name in test.label:
one_hot = [0] * NUM_CLASSES
one_hot[int(label_name)] = 1
ys_test.append(one_hot)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: xs_test, y_: ys_test}))"""
if __name__ == '__main__':
tf.app.run()
And the error I get is
TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.
Upvotes: 1
Views: 164
Reputation: 2585
The feed_dict
parameter expects tensors as its values, whereas you are passing an individual item from your batches_img
and batches_labels
lists. if you see the following line:
for i, e in enumerate(batches_img):
sess.run(train_step, feed_dict={x: batches_img[i], y_: batches_labels[i]})
batches_img[i]
will give you an individual element from the batches_img
list, similarly batches_labels[i]
will get you one item from the batches_label
list, what you need to do is to pass a list (or np array, or a tf.Tensor object).
Upvotes: 0