caffe中用python读写lmdb

环境是ubuntu + python2.7

中间加上了gaussian模糊

import os
import glob
import sys
import random
import math
import time
from PIL import Image, ImageFilter
import lmdb
import caffe
import numpy as np

 # get the name of image in path, s for label(not use), ratio for the ratio of train samples to test samples 
def PP2TXT(path, s, ratio=0.9):
    realim = []
    fakeim = []
    P = glob.glob(path + os.sep + '*.bmp')
    index = range(len(P))
    random.shuffle(P)
    indexi = int(math.floor(ratio*len(index)))
    realim.extend(P[:indexi])
    fakeim.extend(P[indexi:])
    print indexi
    #realim.extend([f +' '+ str(s) for f in P[:indexi]])
    #fakeim.extend([f +' '+ str(s) for f in P[indexi:]])
    return realim, fakeim

    train = []
    test = []

    path = PATH
    folder = os.listdir(path)
    for f in folder:
        filepath = os.path.join(path,f)
        ltrain, ltest = PP2TXT(filepath, 0)
        train.extend(ltrain)
        test.extend(ltest)

    # shuffle
    random.shuffle(train)
    random.shuffle(test)
    f = open(TRAINIMAGEPATHTXT, 'w+')
    f1 = open(TESTIMAGEPATHTXT, 'w+')
    for pathw in train:
        f.write(pathw+'\n')
    for pathw in test:
        f1.write(pathw+'\n')

    f.close()
    f1.close()

    # image preprocessing
    patch_size = 160       #resize 
    patch_s = 128          #size written to lmdeb
    count = 0
    t0=time.time()

    env = lmdb.open(LMDBPATH, map_size=int(1e12))
    with env.begin(write=True) as txn:
        for filename in train:
            if count%1000 == 0:
                print('finish %d images' %count)
            if STRING in filename:
                # original image
                filename_full=filename
                imoo = Image.open(filename_full)
                imo = imoo.resize((patch_size,patch_size))
                width_start = patch_size - patch_s
                height_start = patch_size - patch_s
                width_start = random.randint(0, width_start)
                height_start = random.randint(0, height_start)
                box = (width_start,height_start,width_start+patch_s,height_start+patch_s)

                im = imoo.crop(box)
                im = np.array(im, dtype=np.uint8)
                im = im[:,:,::-1]                         # RGB TO BGR
                im = im.transpose((2, 0, 1))              # X1*X2*3 TO 3*X1*X2              im = im.reshape([1,3,patch_s,patch_s])    #4D blob num*channel*height*width
                datum = caffe.proto.caffe_pb2.Datum()
                datum.label=int(1)        
                datum.channels=im.shape[1]
                datum.width=im.shape[2] # take care of width and height
                datum.height=im.shape[3]
                datum.data = im.tostring()  # or .tostring() if numpy 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值