转载自:http://blog.csdn.net/qq_16949707/article/details/56306720import os
import glob
image_dir = "D:\\data\\ADEChallengeData2016"
file_glob = os.path.join(image_dir, "images", "training", '*.' + 'jpg')
file_list = []
file_list.extend(glob.glob(file_glob))
1
2
3
4
5
6
file_list_test = file_list[0:10]
1
import random
random.shuffle(file_list_test)
1
2
import numpy as np
import scipy.misc as misc
1
2
def read_and_transform(filename):
image = misc.imread(filename)
resize_image = misc.imresize(image,[224, 224], interp='nearest')
return np.array(resize_image)
1
2
3
4
images = np.array([read_and_transform(filename) for filename in file_list_test])
1
def find_annotations_image_path(image_path):
return image_path.replace("images","annotations").replace("jpg","png")
1
2
annotations_file_list_test = [find_annotations_image_path(image_path) for image_path in file_list_test]
1
annotations = np.array([np.expand_dims(read_and_transform(filename), axis=3) for filename in annotations_file_list_test])
1
batch_offset = 0
1
def next_batch(batch_size,batch_offset,images,annotations,epochs_completed):
start = batch_offset
batch_offset += batch_size
if batch_offset > images.shape[0]:
# Finished epoch
epochs_completed += 1
print("****************** Epochs completed: " + str(epochs_completed) + "******************")
# Shuffle the data
perm = np.arange(images.shape[0])
np.random.shuffle(perm)
images = images[perm]
annotations = annotations[perm]
# Start next epoch
start = 0
batch_offset = batch_size
end = batch_offset
print start,end
# return images[start:end], self.annotations[start:end]
1
2
3