I am trying to augment the MNIST dataset. This is what I tried. Can't get any success.
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
X = mnist.train.images
y = mnist.train.labels
def flip_images(X_imgs):
X_flip = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (28, 28, 1))
input_d = tf.reshape(X_imgs, [-1, 28, 28, 1])
tf_img1 = tf.image.flip_left_right(X)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img in input_d:
flipped_imgs = sess.run([tf_img1], feed_dict = {X: img})
X_flip.extend(flipped_imgs)
X_flip = np.array(X_flip, dtype = np.float32)
return X_flip
flip = flip_images(X)
What am I doing wrong? I can't seem to figure out.
Error:
Line: for img in input_d:
raise TypeError("'Tensor' object is not iterable.")
TypeError: 'Tensor' object is not iterable
First, note that your tf.reshape changes the type from an ndarray to a tensor. It will take an .eval() call to bring it back down. In that for loop, you are trying to iterate over a tensor (not a list or a true iterable), consider indexing numerically as in:
X = mnist.train.images
y = mnist.train.labels
def flip_images(X_imgs):
X_flip = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (28, 28, 1))
input_d = tf.reshape(X_imgs, [-1, 28, 28, 1])
tf_img1 = tf.image.flip_left_right(X)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img_ind in range(input_d.shape[0]):
img = input_d[img_ind].eval()
flipped_imgs = sess.run([tf_img1], feed_dict={X: img})
X_flip.extend(flipped_imgs)
X_flip = np.array(X_flip, dtype = np.float32)
return X_flip
flip = flip_images(X)
Let me know if this resolves your issue! Might want to set the range to a small constant for testing, this could take a while if you don't have a GPU around.
Related
I am trying to do a multivariate linear regression and I am having some issues. Namely, I am getting the following error:
ValueError: Cannot feed value of shape (0, 31399, 50) for Tensor 'Placeholder_22:0', which has shape '(1, 50)'
I tried X = tf.compat.v1.placeholder(tf.float32, shape=[None, 50]), but it also create error.
import tensorflow as tf
import numpy as np
import pandas as pd
import csv
from math import sin
a = []
A = 0.01
i = A
cnt = i
while i<=3.14:
q = []
q.append(cnt)
for j in range(2,101,2):
#ins = round(i**j,j)
q.append(i**j)
q.append(sin(i))
a.append(q)
print(q)
#print('##',a)
i += A
cnt += A
with open('sinL.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for i in a:
writer.writerow(i)
init = tf.compat.v1.global_variables_initializer()
data = pd.read_csv('sinL.csv', sep=',')
xy = np.array(data, dtype=np.float32)
x_data = xy[:, 1:-1]
y_data = xy[:, [-1]]
X = tf.compat.v1.placeholder(tf.float32, shape=[None, 50])
Y = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.random.normal([50,1], mean=0.01, stddev=0.01), name="weight")
b = tf.Variable(tf.random.normal([1]), name="bias")
hypothesis = tf.matmul(X, W) + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=1e-10)
train = optimizer.minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(11):
cost_, hypo_, _ = sess.run([cost,hypothesis, train] , feed_dict={X: x_data, Y: y_data})
if step%10==0:
print(step,cost_,hypo_)
print result::
0 370825000000.0 [[-1.66055486e-01]
[-1.66046411e-01]
[-1.66033715e-01]
[-1.66017383e-01]
...
[ 2.69337825e+06]
[ 3.70506525e+06]
[ 5.08823150e+06]]
10 nan [[nan]
[nan]
[nan]
[nan]
...
[nan]
[nan]
[nan]]
I would generally recommend against using TensorFlow 1.x code and moving to Tensorflow 2.x code using the built-in Keras API, because it tends to be easier and simpler to use.
However, the error you're seeing is because you define your input X to be of shape (1, 50) or (None, 50) whereas the actual input is 3-D, even though the first dimension is of size 0, so no data is actually in the array. I think there is an issue with your data loading into x_data and y_data. So verify that there is valid data in x_data and y_data and that _x and _y are 2-D arrays and it then it should work.
i m getting the mentioned error. i want to load single image as input
and train it across given masked image for image binary
classification.
import tensorflow as tf
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
images = []
file_names = [os.path.join('../', f)
for f in os.listdir('../')
if f.endswith(".jpg")]
for f in file_names:
images.append(cv2.cvtColor(cv2.imread(f,1), cv2.COLOR_BGR2GRAY))
img_mask = images[0];
retval,mask_img = cv2.threshold(img_mask, 50, 255, cv2.THRESH_BINARY)
mask_img = mask_img/255
maskk = np.concatenate(mask_img)
x = tf.placeholder(dtype = tf.float32, shape = [None, 637, 1162])
y = tf.placeholder(dtype = tf.int32, shape = [None])
Flatten the input data
images_flat = tf.contrib.layers.flatten(x)
Fully connected layer
logits = tf.contrib.layers.fully_connected(images_flat, 2, tf.nn.relu)
Define a loss function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits))
Define an optimizer
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
Convert logits to label indexes
correct_pred = tf.argmax(logits, 1)
Define an accuracy metric
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)
tf.set_random_seed(1234)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(201):
print('EPOCH', i)
_, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:
images[1], y: maskk})
if i % 10 == 0:
print("Loss: ", loss)
print('DONE WITH EPOCH')
Sounds like you are missing the batch size dimension, try np.expand_dims(image, dim=0)
I wonder there is any way to connect multiple NN as a series in tensorflow.
For example, input features to DNN structure, and get the result values for input data of RNN structure.
Example code:
import tensorflow as tf
import numpy as np
a = 50 #batch_size
b = 60 #sequence in RNN
c = 40 #features
d = 6 #label classes
rnn_size = b
x_data = np.random.rand(a,b,c)
y_data = np.random.randint(0,high=d,size=[a,1])
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None,b,c])
Y = tf.placeholder(tf.float32, shape=[None,d])
X = tf.transpose(X, (1,0,2))
X = tf.reshape(X, (-1,c))
X = tf.split(X, b)
hidden_units = [40,20,10]
#DNN Structure
dnn = []
for i in range(len(hidden_units)):
if i == 0:
T = X
else:
T = dnn[-1]
dnn.append(tf.layers.dense(T, hidden_units[i], activation=tf.nn.relu, kernel_initializer=tf.contrib.layers.xavier_initializer()))
# RNN Structure
rnn = {'w': tf.Variable(tf.random_normal([rnn_size, d], stddev = 0.01), dtype=tf.float32),
'b': tf.Variable(tf.random_normal([d], stddev = 0.01), dtype=tf.float32)}
cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size)
outputs, states = tf.contrib.rnn.static_rnn(cell, dnn[-1], dtype=tf.float32)
output = tf.matmul(outputs[-1], rnn['w'])+rnn['b']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y,logits=output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
correct = tf.equal(tf.argmax(output,1),tf.argmax(cost,1))
acc = tf.reduce_mean(tf.cast(correct, tf.float32))
# Run Session
sess = tf.Session()
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
_, c = sess.run([optimizer, cost],feed_dict={X: x_data, Y: tf.Session().run(tf.one_hot(y_data), d)})
print('Accuracy: ', sess.run(acc, feed_dict={X: x_data, Y: tf.Session().run(tf.one_hot(y_data), d)}))
When I run this code, there is an error raised:
File "C:\Anaconda3\Lib\site-packages\tensorflow\python\layers\core.py", line 250, in dense
dtype=inputs.dtype.base_dtype,
AttributeError: 'list' object has no attribute 'dtype'
it seems to be related with type of 'dnn[-1]'
Is there a connective function or data type controller for the connection of the neural networks?
I've solved the problem, finally.
The reason of error was little bit ambiguous but X was recognized by 'list' after running 'tf.split', finitely...
After I generate a list of DNN Structures which as a length of the sequence, as following:
seq = []
for i in range(b):
###dnn structure for i-th array of split###
seq.append(dnn structure)
and tuned some codes, then the whole code worked well.
Thanks for an attention :)
Thanks for your help.
This question has bothered me for 2 days. I searched many sites and did not solve.
Background:
I am learning mnist, and it goes right at first, but when I save the model and restore, there is an Error, told me placeholder_1 must be fed. I am confused.
Code:
Below code goes right.
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("mnist data/", one_hot=True)
import tensorflow as tf
# 操作符号变量来描述这些可交互的操作单元
x = tf.placeholder(tf.float32, [None, 784])
# 权重值和偏置量
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
# 实现模型
y = tf.nn.softmax(tf.matmul(x,W) + b)
# 添加一个新的占位符用于输入正确值
y_ = tf.placeholder("float", [None,10])
# 计算交叉熵:
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
# 要求TensorFlow用梯度下降算法(gradient descent algorithm)以0.01的学习速率最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 添加一个操作来初始化我们创建的变量
init = tf.initialize_all_variables()
saver = tf.train.Saver()
sess = tf.Session()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
save_path = saver.save(sess, "./model_mnist.ckpt",write_meta_graph=False)
print("Model saved in life:", save_path)
import cv2
import numpy as np
img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
arr = []
for i in range(28):
for j in range(28):
gray = 1 - img[i,j]/255
arr.append(gray)
arr_mnist = np.array([arr])
#print(arr_mnist)
result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
#print(np.argmax(result[0]))
#print(np.sum(result[0]))
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))
#print(tf.argmax(result,1))
But, when I want to use the model to restore. it goes wrong.
import cv2
import numpy as np
import tensorflow as tf
img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
arr = []
for i in range(28):
for j in range(28):
gray = 1 - img[i,j]/255
arr.append(gray)
arr_mnist = np.array([arr])
#print(arr_mnist)
tf.reset_default_graph()
x = tf.placeholder("float", shape=[None, 784])
y = tf.placeholder("float", shape=[None, 10])
#keep_prob = tf.placeholder("float")
sess = tf.Session()
saver = tf.train.import_meta_graph('./model_mnist.ckpt.meta')
saver.restore(sess, './model_mnist.ckpt')
result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))
the Error is :
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
so, I suppose the problem should be in the model save or restore process, but I cannot figure out.
how can I correct the code? thank you!
When restoring the graph, you are declaring two placeholders, and only feed one when running the session. The y placeholder is the one matching the error name.
But you do not need to declare these placeholders in the run script. The restoration takes care of it. Note that you can specify the feed dictionary with string keys:
feed_dict: { 'x': [1,2,3] }
I have a function that returns me a variable called layer - images in the format:
<tf.Tensor 'Conv2D_1:0' shape=(?, 16, 16, 1) dtype=float32>
I need to save these images in .jpeg.
So far I've thought of doing this:
# Reshape into tf.image.encode_jpeg format
images = tf.image.convert_image_dtype(layer, tf.uint8)
train_batch_size = 300
And in session = tf.Session ()
images_encode = tf.map_fn(lambda x: tf.image.encode_jpeg(x), images, dtype=tf.uint8) # There was no error in this line, is it right?
My doubt now is how to configure it to save them?
I've tried this:
# That means it will only scroll through my 300 images
# And it's these 300 images that I want to save
x_batch, y_true_batch = next_batch_size(train_batch_size)
feed_dict_train = {x: x_batch, y_true: y_true_batch}
result = session.run(images_encode, feed_dict=feed_dict_train)
format_str = ('%s.jpeg')
fr = format_str % datetime.now()
f = open(fr, "wb+")
f.write(result.eval())
f.close()
But I'm getting the following error:
InvalidArgumentError (see above for traceback): TensorArray dtype is uint8 but Op is trying to write dtype string.
[[Node: map_5/while/TensorArrayWrite/TensorArrayWriteV3 = TensorArrayWriteV3[T=DT_STRING, _class=["loc:#map_5/TensorArray_1"], _device="/job:localhost/replica:0/task:0/cpu:0"](map_5/while/TensorArrayWrite/TensorArrayWriteV3/Enter, map_5/while/Identity, map_5/while/EncodeJpeg, map_5/while/Identity_1)]]
My placeholders are:
# Placeholder variable for the input images
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
# Reshape 'x'
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
# Placeholder variable for the true labels associated with the images
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
Your are almost done. The type needs to be a tf.string:
This gives a noisy image:
import tensorflow as tf
import numpy as np
noise = np.random.randn(3, 128, 128, 1).astype(np.float32) * 255
# your data
layer = tf.convert_to_tensor(noise)
images = tf.image.convert_image_dtype(layer, tf.uint8)
images_encode = tf.map_fn(lambda x: tf.image.encode_jpeg(x),
images, dtype=tf.string)
def write_jpg(buf, fn):
with open(fn, 'wb') as f:
f.write(encoded_jpegs[0])
with tf.Session() as sess:
encoded_jpegs = sess.run(images_encode)
for k, jpg in enumerate(encoded_jpegs):
with open("test%03i.jpg" % k, 'wb') as f:
f.write(jpg)