def mean_pixel(input, dirout): """ Generate a pixelwise mean for a file containing paths to images. Parameters: ----------- input : string File containing the path to all images and their true labels dirout : string Path to the output folder Notes: ------- The function generates three files: fname.binaryproto : contains the pixelwise mean of the images fname.npy : numpy array containing the mean fname.png : image resulting of the mean """ caffe = True try: from caffe.io import array_to_blobproto except: logger.warning( 'The system does not contain caffe.io to save as binaryproto') caffe = False from skimage import io input = realpath(input) fname = fh.filename(input, extension=False) dirout = fh.is_folder(dirout) fnameout = join(dirout, fname + '_mean') pf = fh.PathfileHandler(input) n = pf.nb_lines logger.info('Calculating mean for %d files.' % n) for nbline, arr in enumerate(pf): path = arr[0] img = io.imread(path) if nbline == 0: size = img.shape[1] mean = np.zeros((1, 3, size, size)) mean[0][0] += img[:, :, 0] mean[0][1] += img[:, :, 1] mean[0][2] += img[:, :, 2] mean[0] /= n if caffe: blob = array_to_blobproto(mean) logger.info('Saving data into: %s' % fnameout + '.binaryproto') with open(fnameout + '.binaryproto', 'wb') as f: f.write(blob.SerializeToString()) logger.info('Saving numpy matrix into: %s' % fnameout + '.npy') np.save(fnameout + '.npy', mean[0]) mean_img = np.transpose(mean[0].astype(np.uint8), (1, 2, 0)) logger.info('Saving mean image into: %s' % fnameout + '.png') io.imsave(fnameout + '.png', mean_img)
def getPixelMean(input, dirout, size=256, fname=None): """ Generate a pixelwise mean for a file containing paths to images. Parameters: ----------- input : string File containing the path to all images and their true labels dirout : string Path to the output folder Notes: ------- The function generates three files: fname.binaryproto : contains the pixelwise mean of the images fname.npy : numpy array containing the mean fname.png : image resulting of the mean """ from caffe.io import array_to_blobproto from skimage import io input = realpath(input) fnamein, ext = splitext(basename(input)) dirout = realpath(dirout) if fname: fnameout = join(dirout, fname + '_mean') else: fnameout = join(dirout, fnamein + '_mean') mean = np.zeros((1, 3, size, size)) logger.info('calculating mean for %d files.' % n) n = 251392 pb = progressbar.ProgressBar(n) with open(input) as imgs_path: for i in imgs_path: path = i img = io.imread(path) mean[0][0] += img[:, :, 0] mean[0][1] += img[:, :, 1] mean[0][2] += img[:, :, 2] pb.update() mean[0] /= n blob = array_to_blobproto(mean) with open(fnameout + '.binaryproto', 'wb') as f: f.write(blob.SerializeToString()) np.save(fnameout + ".npy", mean[0]) meanImg = np.transpose(mean[0].astype(np.uint8), (1, 2, 0)) io.imsave(fnameout + ".png", meanImg)
def compute_mean(data_dir): print 'compute mean file...' sumfile = np.zeros((210, 160, 3), dtype=np.float) count = 0 for d in glob.glob(data_dir + '/*'): for im in glob.glob(d + '/*.png'): im = cv2.imread(im, cv2.IMREAD_COLOR) sumfile += im count += 1 meanfile = sumfile / count np.save(data_dir + '/mean.npy', meanfile) cv2.imwrite(data_dir + '/mean.png', meanfile) meanblob = np.zeros((1, 3, 210, 160), dtype=np.float) meanblob[0,:] = meanfile.transpose((2,0,1)) meanblob = array_to_blobproto(meanblob) with open(data_dir + '/mean.binaryproto', 'wb') as f: f.write(meanblob.SerializeToString())
mean = np.zeros((1, 3, 152, 152)) N = 0 classSizes = defaultdict(int) beginTime = time.time() for subdir, dirs, files in os.walk(args.imageDir): for fName in files: (imageClass, imageName) = (os.path.basename(subdir), fName) if any(imageName.lower().endswith("." + ext) for ext in exts): img = io.imread(os.path.join(subdir, fName)) if img.shape == (152, 152, 3): mean[0][0] += img[:, :, 0] mean[0][1] += img[:, :, 1] mean[0][2] += img[:, :, 2] N += 1 if N % 1000 == 0: elapsed = time.time() - beginTime print("Processed {} images in {:.2f} seconds. " "{:.2f} images/second.".format( N, elapsed, N / elapsed)) mean[0] /= N blob = array_to_blobproto(mean) with open("{}.binaryproto".format(args.meanPrefix), 'wb') as f: f.write(blob.SerializeToString()) np.save("{}.npy".format(args.meanPrefix), mean[0]) meanImg = np.transpose(mean[0].astype(np.uint8), (1, 2, 0)) io.imsave("{}.png".format(args.meanPrefix), meanImg)
from caffe.proto import caffe_pb2 from caffe.io import array_to_blobproto OFs = ["1"]; height = 256; width = 340; for count in range(len(OFs)): OF = OFs[count]; stacked_count = int(OF) * 2; mean = np.zeros((1, stacked_count, height, width)); mean[np.where(mean == 0)] = 128; blob = array_to_blobproto(mean); mean_file = OF + "opflows_mean.binaryproto"; with open(mean_file, 'wb') as fid: fid.write(blob.SerializeToString()); arr = np.array( caffe.io.blobproto_to_array(blob) ); mean_file = OF + "opflows_mean.npy"; np.save( mean_file , arr ) for count in range(len(OFs)): OF = OFs[count]; stacked_count = int(OF) * 2 * 2;
def npy_to_bp(npy_data): if npy_data.ndim is 3: npy_data = npy_data.reshape(1,npy_data.shape[0],npy_data.shape[1],npy_data.shape[2]) blob = array_to_blobproto(npy_data) blob_data = blob.SerializeToString() return blob_data
""" Find imageset mean. Calculates the mean of a data in a given file for each channel, and saves them in npy format, in the order BGR """ from PIL import Image import numpy as np caffe_root = '/home/alex/Caffe/caffe/' import sys sys.path.insert(0, caffe_root + 'python') from caffe import io path = '/home/alex/Documents/Caffe_First_Attempt/' imageSet = path + 'resized' imageLabels = path + 'temp/test_labels.txt' destination = path + 'temp/img_mean.binaryproto' img_size = (227, 227) img_mean = np.zeros((3,img_size[0],img_size[1])) imageFile = open(imageLabels,'r') nImages = 0 for line in imageFile: img = Image.open(imageSet + '/' + line.split()[0]) img_mean += np.array(img).transpose(2,0,1) nImages += 1 img_mean = img_mean/nImages img_mean = img_mean[[2,1,0],:,:] meanBlob = io.array_to_blobproto(np.expand_dims(img_mean,0)) toSave = open(destination,'wb') toSave.write(meanBlob.SerializeToString()) toSave.close()