环境是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