Я пытаюсь изменить код cifar10.py, чтобы иметь возможность передавать изображения в сеть.tenorflow cifar10 изменение кода для чтения изображений
Я действительно могу запустить код и начать учебный процесс, но через некоторое время, если я запустил тензограмму, под разделом «изображения» у меня всегда будет одно и то же изображение. Кроме того, кроссентропия стремится к нулю. Я думаю, что неправильно загружаю изображения.
Вот код
def distorted_inputs():
#Reading the dirs file where all the directories of the images are stored
filedirs = [line.rstrip('\n') for line in open('image_dirs.txt')]
#create a list of files
filenames = []
i = 0
for f in filedirs:
png_files_path = glob.glob(os.path.join(f, '*.[pP][nN][gG]'))
print('found ' + str(len(png_files_path)) + ' files in ' + f)
for filename in png_files_path:
#storing file_name label
s = filename + " " + str(i)
filenames.append(s)
i = i+1
# Create a queue that produces the filenames to read and the labels
filename_queue = tf.train.string_input_producer(filenames)
my_img, label = read_my_file_format(filename_queue.dequeue())
label = tf.string_to_number(label, tf.int32)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
image = my_img.eval()
coord.request_stop()
coord.join(threads)
reshaped_image = tf.cast(image, tf.float32)
resized_image = tf.image.resize_image_with_crop_or_pad(reshaped_image,IMAGE_SIZE, IMAGE_SIZE)
distorted_image = tf.image.random_crop(reshaped_image, [24, 24])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
# Because these operations are not commutative, consider randomizing
# randomize the order their operation.
distorted_image = tf.image.random_brightness(distorted_image,max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image,lower=0.2, upper=1.8)
# Subtract off the mean and divide by the variance of the pixels.
float_image = tf.image.per_image_whitening(distorted_image)
# Ensure that the random shuffling has good mixing properties.
min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN *min_fraction_of_examples_in_queue)
print ('Filling queue with ITSD images before starting to train. ''This will take a few minutes.')
# Generate a batch of images and labels by building up a queue of examples.
return _generate_image_and_label_batch(float_image, label, min_queue_examples)
Изображение чтения часть приходит из https://github.com/HamedMP/ImageFlow Обычай читатель приходит от Tensorflow read images with labels и относительная функция реализована в виде следующих
def read_my_file_format(filename_and_label_tensor):
"""Consumes a single filename and label as a ' '-delimited string.
Args:
filename_and_label_tensor: A scalar string tensor.
Returns:
Two tensors: the decoded image, and the string label.
"""
filename, label = tf.decode_csv(filename_and_label_tensor, [[""], [""]], " ")
file_contents = tf.read_file(filename)
example = tf.image.decode_png(file_contents)
return example, label
Благодарности
Когда вы говорите через некоторое время, как долго? Вам нужно знать, что cifar10_train только обновляет изображения каждые 100 шагов. – jkschin
@jkschin Я знаю это, спасибо вам, – Dario