python - How to implement RGB images as tensors in tensorflow? -
i'm new tensorflow , i'm trying create model of stacked sparse denoising auto-encoders. have found way on how load training ( , testing) set through examples here , github cannot use them tensor perform required multiplications etc. (this code loading images)
import tensorflow tf import glob import numpy np pil import image im im_list = [] #load sets training_set = [] training_set = glob.glob("folder/training_set/*.jpg") testing_set = [] testing_set = glob.glob("folder/corrupted/*.jpg") # testing code training set filename_queue = tf.train.string_input_producer(training_set) reader = tf.wholefilereader() key, value = reader.read(filename_queue) #data = tf.image.decode_jpeg(value) data = tf.decode_raw(value, tf.uint8) sess = tf.interactivesession() sess.run(tf.global_variables_initializer()) #sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coord = tf.train.coordinator() threads = tf.train.start_queue_runners(coord=coord) in range (196): print m_key = sess.run([key,data]) im_list.append(m_key[1]) coord.request_stop() coord.join(threads)
by using code manage save images list of uint8 arrays containing data size ~800 ~1000 . images of size 32x32x3 missing.
they other way tried is:
filename_queue = tf.train.string_input_producer(training_set) image_reader = tf.wholefilereader() _, image_file = image_reader.read(filename_queue) imagee = tf.image.decode_jpeg(image_file) #tf.cast(imagee, tf.float32) sess = tf.interactivesession() sess.run(tf.global_variables_initializer()) coord = tf.train.coordinator() threads = tf.train.start_queue_runners(coord=coord) image = sess.run(imagee) imaginar = image.astype(np.float32) #train_step.run(feed_dict={x: imaginar, y_: imaginar_test}) coord.request_stop() coord.join(threads)
and im trying calculate
y = tf.matmul(x,w) + b h_x_s = tf.sigmoid(y) h_x = tf.matmul(h_x_s,w_) + b_ y_xi = tf.sigmoid(h_x)
this way images numpy arrays of 32x32x3 cant find way save them tensor tf.matmul works. errors non fitting shapes of arrays.
# variables x= tf.placeholder(tf.float32,[32, 32, 3]) y_ = tf.placeholder(tf.float32,[32, 32, 3]) w = tf.variable(tf.zeros([32,32,3])) b = tf.variable(tf.zeros([32,32,3])) w_ = tf.variable(tf.zeros([32,32,3])) b_= tf.variable(tf.zeros([32,32,3]))
(unsuccessful try)
how should load (and decode) images , sizes should variables , placeholders be? appreciated!
thanks :)
just in case has same problem:
first of use decode_jpeg(data, channels = 3)
(channels = 3 means rgb) or other decoder depending on image type.
so can turn 3d image 2d vector. example if image (32,32,3) vector should (1,32*32*3) -> (1, 3072). can using
2d_vec = original_3d_image.reshape(1,-1)
you can turn 3d using
2d_vec.reshape(32,32,3)
do not forget normalize data before use them input. have is
2d_vec = 2d_vec / max_value_of_2d_vec
i have changed lot in code posted before if have questions ask me!
Comments
Post a Comment