我想读取图像以对模型进行预测,首先探究要对模型进行预测的图像的tf.record,这是对tf.record进行解码的代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
data_path = '/Users/David/SA_TfRecord' # address to save the hdf5 file
with tf.Session() as sess:
feature = {'pred/image': tf.FixedLenFeature([], tf.string),
'pred/label': tf.FixedLenFeature([], tf.int64)}
# Create a list of filenames and pass it to a queue
filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
# Define a reader and read the next record
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
# Decode the record read by the reader
features = tf.parse_single_example(serialized_example, features=feature)
# Convert the image data from string back to the numbers
image = tf.decode_raw(features['pred/image'], tf.float32)
# Cast label data into int32
label = tf.cast(features['pred/label'], tf.int32)
# Reshape image data into the original shape
image = tf.reshape(image, [224, 224, 3])
# Creates batches by randomly shuffling tensors
images, labels = tf.train.shuffle_batch([image, label], batch_size=1,
capacity=30, num_threads=1, min_after_dequeue=1)
print(images)
# Initialize all global and local variables
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
sess.run(init_op)
# Create a coordinator and run all QueueRunner objects
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for batch_index in range(1):
img, lbl = sess.run([images, labels])
print(img)
# Stop the threads
coord.request_stop()
# Wait for threads to stop
coord.join(threads)
sess.close()
我是通过解码得到的
[[[[ 53. 49. 66.]
[ 53. 49. 66.]
[ 53. 49. 66.]
...
[ 68. 154. 171.]
[ 68. 151. 167.]
[ 69. 150. 167.]].......
使用此代码,我制作了tf.record
from random import shuffle
import glob
import cv2
import tensorflow as tf
import numpy as np
import sys
shuffle_data = True # shuffle the addresses before saving
data_path = '/Users/David/Desktop/David/Tesis/Practica/Programas/Versiones/Sexta_Version_Combinacion/Direct/SA.jpg'
# read addresses and labels from the 'train' folder
addrs = glob.glob(data_path)
labels = [0 if 'CA' in addr else 1 for addr in addrs] # 0 = Con Arms, 1 = Sin Arma
# to shuffle data
if shuffle_data:
c = list(zip(addrs, labels))
shuffle(c)
addrs, labels = zip(*c)
# Divide the hata into 60% train, 20% validation, and 20% test
#train_addrs = addrs[0:int(0.8 * len(addrs))]
#train_labels = labels[0:int(0.8 * len(labels))]
#val_addrs = addrs[int(0.8 * len(addrs)):int(1 * len(addrs))]
#val_labels = labels[int(0.8 * len(addrs)):int(1 * len(addrs))]
pred_addrs = addrs[0:int(1 * len(addrs))]
pred_labels = labels[0:int(1 * len(labels))]
def load_image(addr):
# read an image and resize to (224, 224)
# cv2 load images as BGR, convert it to RGB
img = cv2.imread(addr)
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(np.float32)
return img
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]))
pred_filename ='/Users/David/Desktop/David/Tesis/Practica/Programas/Versiones/Sexta_Version_Combinacion/Direct/SA_TfRecord' # address to save the TFRecords file
# open the TFRecords file
writer = tf.python_io.TFRecordWriter(pred_filename)
for i in range(len(pred_addrs)):
# print how many images are saved every 1000 images
if not i % 1:
print 'pred data: {}/{}'.format(i, len(pred_addrs))
sys.stdout.flush()
# Load the image
img = load_image(pred_addrs[i])
label = pred_labels[i]
# Create a feature
feature = {'pred/label': _int64_feature(label),
'pred/image': _bytes_feature(tf.compat.as_bytes(img.tostring()))}
# Create an example protocol buffer
example = tf.train.Example(features=tf.train.Features(feature=feature))
# Serialize to string and write on the file
writer.write(example.SerializeToString())
writer.close()
sys.stdout.flush()
然后,我想直接放置图像,以免进行tfrecord记录,但是当我读取图像时,得到了不同的结果。这是我直接阅读的代码
import tensorflow as tf
with tf.Session() as sess:
# Make a queue of file names including all the JPEG images files in the relative
# image directory.
filename_queue = tf.train.string_input_producer(
tf.train.match_filenames_once("/Users/David/Desktop/David/Tesis/Practica/Programas/Versiones/Sexta_Version_Combinacion/Direct/SA.jpg"))
# Read an entire image file which is required since they're JPEGs, if the images
# are too large they could be split in advance to smaller files or use the Fixed
# reader to split up the file.
image_reader = tf.WholeFileReader()
# Read a whole file from the queue, the first returned value in the tuple is the
# filename which we are ignoring.
_, image_file = image_reader.read(filename_queue)
# Decode the image as a JPEG file, this will turn it into a Tensor which we can
# then use in training.
image = tf.image.decode_jpeg(image_file)
image = tf.image.resize_images(image, [224, 224])
image.set_shape((224, 224, 3))
print(image)
batch_size = 1
num_preprocess_threads = 1
min_queue_examples = 1
images = tf.train.shuffle_batch([image],batch_size=batch_size,num_threads=num_preprocess_threads,capacity=min_queue_examples+3*batch_size,min_after_dequeue=min_queue_examples)
print(images)
# Start a new session to show example output.
# Required to get the filename matching to run.
tf.local_variables_initializer().run()
# Coordinate the loading of image files.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# Get an image tensor and print its value.
image_tensor = sess.run(images)
print(image_tensor)
# Finish off the filename queue coordinator.
coord.request_stop()
coord.join(threads)
我得到这个结果
[[[[ 51. 49. 63.]
[ 52. 50. 64.]
[ 51. 49. 63.]
...
[ 66. 153. 170.]
[ 67. 150. 166.]
[ 68. 151. 167.]]........
我不知道错误是什么,请帮助我,为什么我得到不同的结果?
答案 0 :(得分:0)
我认为这是因为jpeg是损耗压缩,因此每次保存图像时都会有所不同。
如果希望每次阅读时的解码结果都相同,则可以使用其他格式,例如png,bmp等。