
Description
Code:
- import tensorflow as tf
- import tensorlayer as tl
- import numpy as np
- from PIL import Image
- import os
- def _int64_feature(value):
-
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
- def _bytes_feature(value):
-
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
- path_save = r'W:/PlantClassification/'
- path_train = r"W:/PlantClassification/train/"
- classes = [str(i) for i in list(range(7))]
- writer = tf.python_io.TFRecordWriter(path_save + 'train.tfrecords')
- for index, name in enumerate(classes):
-
class_path = path_train + name + '/'
-
for img_name in os.listdir(class_path):
-
img_path = class_path + img_name
-
img = Image.open(img_path)
-
img = img.resize((227, 227))
-
img_raw = img.tobytes()
-
example = tf.train.Example(features=tf.train.Features(feature={
-
'label': _int64_feature(index),
-
'img_raw': _bytes_feature(img_raw)}))
-
writer.write(example.SerializeToString())
- writer.close()
- def read_and_decode(filename):
-
filename_queue = tf.train.string_input_producer([filename])
-
reader = tf.TFRecordReader()
-
_, serialized_example = reader.read(filename_queue)
-
features = tf.parse_single_example(serialized_example,
-
features={
-
'label': tf.FixedLenFeature([], tf.int64),
-
'img_raw': tf.FixedLenFeature([], tf.string),
-
})
-
# You can do more image distortion here for training data
-
img = tf.decode_raw(features['img_raw'], tf.float32)
-
img = tf.reshape(img, [227, 227, 3])
-
label = tf.cast(features['label'], tf.int32)
-
return img, label
- img, label = read_and_decode(r"W:\PlantClassification\train.tfrecords")
- img_batch, label_batch = tf.train.shuffle_batch([img, label],
-
batch_size=4,
-
capacity=150000,
-
min_after_dequeue=2000,
-
num_threads=4)
- init = tf.global_variables_initializer()
- with tf.Session() as sess:
-
sess.run(init)
-
coord = tf.train.Coordinator()
-
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
-
for i in range(3):
-
print("Step %d" % i)
-
val, l = sess.run([img_batch, label_batch])
-
print(val.shape, l)
-
tl.visualize.images2d(val, second=1, saveable=False, name='batch' + str(i), dtype=np.uint8, fig_idx=2020121)
-
coord.request_stop()
-
coord.join(threads)
-
sess.close()
Error:
S:\Python35\python.exe W:/PlantClassification/visualize_data.py
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfully opened CUDA library cublas64_80.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfully opened CUDA library cudnn64_5.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfully opened CUDA library cufft64_80.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfully opened CUDA library nvcuda.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfully opened CUDA library curand64_80.dll locally
img_batch : (4, 227, 227, 3)
label_batch : (4,)
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:885] Found device 0 with properties:
name: GeForce GTX 960
major: 5 minor: 2 memoryClockRate (GHz) 1.1775
pciBusID 0000:01:00.0
Total memory: 2.00GiB
Free memory: 1.64GiB
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:906] DMA: 0
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:916] 0: Y
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
Step 0
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:975] Invalid argument: Input to DecodeRaw has length 154587 that is not a multiple of 4, the size of float
[[Node: DecodeRaw = DecodeRawlittle_endian=true, out_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:975] Invalid argument: Input to DecodeRaw has length 154587 that is not a multiple of 4, the size of float
[[Node: DecodeRaw = DecodeRawlittle_endian=true, out_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:975] Invalid argument: Input to DecodeRaw has length 154587 that is not a multiple of 4, the size of float
[[Node: DecodeRaw = DecodeRawlittle_endian=true, out_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:975] Invalid argument: Input to DecodeRaw has length 154587 that is not a multiple of 4, the size of float
[[Node: DecodeRaw = DecodeRawlittle_endian=true, out_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
Traceback (most recent call last):
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1021, in _do_call
return fn(*args)
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1003, in _run_fn
status, run_metadata)
File "S:\Python35\lib\contextlib.py", line 66, in exit
next(self.gen)
File "S:\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
[[Node: shuffle_batch = QueueDequeueMany[_class=["loc:@shuffle_batch/random_shuffle_queue"], component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "W:/PlantClassification/visualize_data.py", line 39, in
val, l = sess.run([img_batch, label_batch])
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run
run_metadata_ptr)
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 964, in _run
feed_dict_string, options, run_metadata)
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1014, in _do_run
target_list, options, run_metadata)
File "S:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1034, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
[[Node: shuffle_batch = QueueDequeueMany[_class=["loc:@shuffle_batch/random_shuffle_queue"], component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
Caused by op 'shuffle_batch', defined at:
File "W:/PlantClassification/visualize_data.py", line 27, in
num_threads=4)
File "S:\Python35\lib\site-packages\tensorflow\python\training\input.py", line 917, in shuffle_batch
dequeued = queue.dequeue_many(batch_size, name=name)
File "S:\Python35\lib\site-packages\tensorflow\python\ops\data_flow_ops.py", line 458, in dequeue_many
self._queue_ref, n=n, component_types=self._dtypes, name=name)
File "S:\Python35\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py", line 1099, in _queue_dequeue_many
timeout_ms=timeout_ms, name=name)
File "S:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 759, in apply_op
op_def=op_def)
File "S:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2240, in create_op
original_op=self._default_original_op, op_def=op_def)
File "S:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1128, in init
self._traceback = _extract_stack()
OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
[[Node: shuffle_batch = QueueDequeueMany[_class=["loc:@shuffle_batch/random_shuffle_queue"], component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
Process finished with exit code 1