kalaspuffar / tensorflow-data

An small example how to use tensorflow data (tf.data)
MIT License
62 stars 30 forks source link

Problems with InvalidArgumentError #4

Closed simobrazz closed 6 years ago

simobrazz commented 6 years ago

Hello,

first of all I would like to say thank you for this example. Btw I have some problems to make your code run with my own dataset, and I don't understand why of this error:

` Caused by op u'Reshape', defined at:

net = tf.reshape(net, [-1, 306, 306, 3])

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1123632 values, but the requested shape has 280908 [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/device:CPU:0"](Cast, Reshape/shape)]] [[Node: IteratorGetNext = IteratorGetNextoutput_shapes=[[?,306,306,3], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"]] `

These are the two files I used:

eastimator_model_def_train.txt raw_images_to_tfrecords.txt

Any suggestion is appreciated.

kalaspuffar commented 6 years ago

Hi @simobrazz

Well, you reshape complain that there are too many values so a reshape is not possible. So the data into the reshape is 1123632 and that should be reshaped to 3063063 = 280908 and that's not possible.

Does it make sense and does it help?

Otherwise please clarify.

Best regards Daniel

simobrazz commented 6 years ago

Sorry @kalaspuffar, I will try to explain better my solution.

Since your code works well with the dataset of cats and dog I thought that it can work well also with my own dataset which has all 306x306 images.

I am a newbie of TensorFlow, in my mind the code saved 306x306 images in tfrecords which should therefore loaded during the training step. But this is not the case because it seems that the net = features['image'] loads four images (3063063 = 280908 and 280908*4 = 1123632).

Why my code does not work?

kalaspuffar commented 6 years ago

Hi @simobrazz

I've not tried this code myself but I guess this function might be a part of the problem.

def load_image(addr):
    img = cv2.imread(addr)
    img = cv2.resize(img, (306, 306), interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img.astype(np.float32)
    return img

This line

img = img.astype(np.float32)

Will make each image value into an 4 byte float. Then you save the byte string and cast the values to float again on the other side like this

    image = tf.decode_raw(parsed['raw_image'], tf.uint8)
    image = tf.cast(image, tf.float32)

I had this bug myself in the early iterations of my code but after removing the img.astype(np.float32) line my code worked.

Hope this helps.

Best regards Daniel

simobrazz commented 6 years ago

Hi @kalaspuffar,

with your suggestion it seems to work!

Thank you,

Best Regards, Simone