tensorflow 读取图片


用tf.record形式处理打的图片数据比较方便,而且后面sess.run的时候不需要feeddict数据

首先,建议直接把读取的数据封装成一个类:

class data_reader():
    def __init__(self,dict):
        self.train_images=dict['train']
        self.test_images = dict['test']
        self.validation_images = dict['validation']

2.读取图像,这里以list形式存放了图像名称

def create_img_lists(img_dir,test_percent=0.0,validation_percent=0.0):
    if not os.path.exists(img_dir):
        print("Image directory '" + img_dir + "' not found.")
        return None
    train_imgs=[]
    format_extends=['jpg', 'jpeg', 'JPG', 'JPEG','png']#image format 
    #sub_dir=[x[0] for x in os.walk(img_dir)] #dirpath, dirnames, filenames return for walk
    file_lists=[]
    for extends in format_extends:
        file_full_path=os.path.join(img_dir,'*.'+extends) #per image  full name
        file_lists.extend(glob(file_full_path)) #append
    if not file_lists:
        print("No files(image) found")
    else:
        train_imgs.extend([im for im in file_lists]) #直接就是file_lists就好了,无所谓的
    random.shuffle(train_imgs)#permute
    num_imgs=len(train_imgs)#if initial data_set has validation set,following need tobe changed
    validation_offset=int(validation_percent*num_imgs) #以下是中规中矩的划分图像集
    validation_imgs=train_imgs[:validation_offset]
    test_offset = int(test_percent * num_imgs)
    test_imgs = train_imgs[validation_offset:validation_offset+test_offset]
    training_imgs=train_imgs[validation_offset+test_offset:]
    result={
        'train':training_imgs,
        'test':test_imgs,
        'validation':validation_imgs,
    }
    return result 


这里采用了cPickle方式读取,注意这里是直接将整个文件都写入.pkl中的,也就说,你的数据就是整个数据集,要划分batch您得自己动手

def read_data(data_dir):
    pickle_file_name='data_reader.pkl'
    pickle_file_path=os.path.join(data_dir,pickle_file_name)
    if not os.path.exists(pickle_file_path):    #如果没有该文件,会生成.pkl文件
        result=create_img_lists(data_dir)
        print ("Training set: %d" % len(result['train']))
        print ("Test set: %d" % len(result['test']))
        print ("Validation set: %d" % len(result['validation']))
        print ("Pickling ...")
        with open(pickle_file_path,'wb') as f:
            pickle.dump(result,f,pickle.HIGHEST_PROTOCOL)
    with open(pickle_file_path,'rb') as f:
        result=pickle.load(f)
    return  result
3.这里可以考虑用tf的读入方式将数据直接读成tf.tensorde形式,注意这样可直接在创建图的时候使用该数据,不必在tf.placeholder,同时也不能通过feeddict来往graph里面feed数据,

def _read_files_inputs_to_queue(data,batch_size,crop_img_size=None,img_resize=None,is_style=False):
    #data:data_set puts into queue for training
    #crop_img_size:if crop input img or not
    #resize_img_size:if resize input img or not
    filename_queue = tf.train.string_input_producer(data)  # return a string list of train img
    reader=tf.WholeFileReader()#To use, enqueue filenames in a Queue,return dict,filename (key) and the contents of that file (value)
    key,value=reader.read(filename_queue)
    decoded_img=tf.image.decode_jpeg(value,channels=3)#Assumption:Color images are read and are to be generated in jpg format in uint8
    if crop_img_size:
        croped_img=tf.image.crop_to_bounding_box(decoded_img,55,35,crop_img_size,crop_img_size) #return a 3d tensor(crop,croop,channel)
        img_float=tf.cast(croped_img,dtype=tf.float32) #dtype change
    else:
        img_float = tf.cast(decoded_img, dtype=tf.float32)  # dtype change
    if img_resize: #这个地方可以修改一下
        decoded_img_4d=tf.expand_dims(img_float,0)#3d[h,w,c] tensor to 4d[1,h,w,c],just for resize,as tf.resize inputs must be a 4d tensor
        resized_img=tf.image.resize_bilinear(decoded_img_4d,[img_resize,img_resize])
        input_img=tf.squeeze(resized_img,squeeze_dims=[0]) #remove dims with 1,here return 3d[h,w,c]
    else:
        input_img=img_float

    print("Setting up image reader...")
    num_preprocess_threads = 4
    num_examples_per_epoch = 800
    min_queue_examples = int(0.1 * num_examples_per_epoch)
    print("Shuffling")
    input_img=tf.train.batch([input_img],batch_size=batch_size,num_threads=num_preprocess_threads,
                             capacity=min_queue_examples) #这是TF自己规划batch的
    input_img=utils.img_procss(input_img,127.5,127.5) #预处理,normlize to [0,1]=img-127.5)/127.5
    return input_img
 用上述方式,如果强行想要使用,那就sess.run(数据),毕竟run回来的都是np.arry形式。

自己手动划分batch,并且以np.arry形式的方式:

def img_ndarr(img_dir,batch_size,index,img_size):
    #index:current iter
    result = create_img_lists(img_dir)
    train_imgs=result['train']#here is trian images name list
    batch_files=train_imgs[index*batch_size:(index+1)*batch_size]#get each batch for every iter
    batch_img=[utils.get_image(img,img_size) for img in batch_files]#读入image,用scipy.misc.imread就可以了
    batch_img_arr=np.array(batch_img).astype(np.float32)
    return batch_img_arr
最后,安利一个 https://saicoco.github.io/tf3/  这个是对官方tf.record的细致翻译加理解,适合上手

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值