def loaddata(settype='train/', nframes=10): directory = os.path.expanduser(config['directory']) + settype for dir in ('images', 'params'): if not os.path.exists(os.path.join(directory, dir)): print('No such directory, check your config file') break imgname = os.path.join(directory, 'images', 'image{:04d}.' + imgtype) jsonname = os.path.join(directory, 'params', 'image{:04d}.json') img_list = [] zlist = [] alist = [] nlist = [] for n in range(nframes): # for each frame ... with open(jsonname.format(n), 'r') as fp: param_string = json.load(fp)[0] params = ast.literal_eval(param_string) zlist.append(params['z_p']) alist.append(params['a_p']) nlist.append(params['n_p']) localim = cv2.imread(imgname.format(n))[:, :, 0] img_list.append(localim) img_list = np.array(img_list).astype('float32') img_list *= 1. / 255 zlist = np.array(zlist).astype('float32') zlist = rescale(zmin, zmax, zlist) alist = np.array(alist).astype('float32') alist = rescale(amin, amax, alist) nlist = np.array(nlist).astype('float32') nlist = rescale(nmin, nmax, nlist) params_list = [zlist, alist, nlist] return img_list, params_list
def makedata(settype='train/', nframes=10): # create directories and filenames directory = os.path.expanduser(config['directory']) + settype for dir in ('images', 'params'): if not os.path.exists(os.path.join(directory, dir)): os.makedirs(os.path.join(directory, dir)) shutil.copy2(configfile, directory) filetxtname = os.path.join(directory, 'filenames.txt') imgname = os.path.join(directory, 'images', 'image{:04d}.' + imgtype) jsonname = os.path.join(directory, 'params', 'image{:04d}.json') filetxt = open(filetxtname, 'w') img_list = [] zlist = [] alist = [] nlist = [] for n in range(nframes): # for each frame ... print(imgname.format(n)) sample = make_sample(config) # ... get params for particles s = sample[0] zlist.append(s.z_p) alist.append(s.a_p) nlist.append(s.n_p) # ... calculate hologram frame = np.random.normal(0, config['noise'], shape) if len(sample) > 0: holo.particle = sample frame += holo.hologram().reshape(shape) else: frame += 1. frame = np.clip(100 * frame, 0, 255).astype(np.uint8) img_list.append(frame) # ... and save the results #do we need? cv2.imwrite(imgname.format(n), frame) with open(jsonname.format(n), 'w') as fp: fp.write(format_json(sample, config)) filetxt.write(imgname.format(n) + '\n') img_list = np.array(img_list).astype('float32') img_list *= 1. / 255 zlist = np.array(zlist).astype('float32') zlist = rescale(zmin, zmax, zlist) alist = np.array(alist).astype('float32') alist = rescale(amin, amax, alist) nlist = np.array(nlist).astype('float32') nlist = rescale(nmin, nmax, nlist) params_list = [zlist, alist, nlist] return img_list, params_list
def makedata(settype='train/', nframes=10): # create directories and filenames directory = os.path.expanduser(config['directory'])+settype for dir in ('images', 'params'): if not os.path.exists(os.path.join(directory, dir)): os.makedirs(os.path.join(directory, dir)) shutil.copy2(configfile, directory) filetxtname = os.path.join(directory, 'filenames.txt') imgname = os.path.join(directory, 'images', 'image{:04d}.' + imgtype) jsonname = os.path.join(directory, 'params', 'image{:04d}.json') filetxt = open(filetxtname, 'w') img_list = [] scale_list = [] zlist = [] alist = [] nlist = [] for n in range(nframes): # for each frame ... print(imgname.format(n)) sample = make_sample(config) # ... get params for particles s = sample[0] zlist.append(s.z_p) alist.append(s.a_p) nlist.append(s.n_p) ext = feature_extent(s, config) #introduce 1% noise to ext ext = np.random.normal(ext, 0.01*ext) extsize = ext*2 shapesize = shape[0] if extsize <= shapesize: scale = 1 else: scale = int(np.floor(extsize/shapesize) + 1) newshape = [i * scale for i in shape] holo = LMHologram(coordinates=coordinates(newshape)) holo.instrument.properties = config['instrument'] # ... calculate hologram frame = np.random.normal(0, config['noise'], newshape) if len(sample) > 0: holo.particle = sample[0] holo.particle.x_p += (scale-1)*100 holo.particle.y_p += (scale-1)*100 frame += holo.hologram().reshape(newshape) else: frame += 1. frame = np.clip(100 * frame, 0, 255).astype(np.uint8) #decimate frame = frame[::scale, ::scale] img_list.append(frame) scale_list.append(scale) # ... and save the results #do we need? cv2.imwrite(imgname.format(n), frame) with open(jsonname.format(n), 'w') as fp: fp.write(format_json(sample, config, scale)) filetxt.write(imgname.format(n) + '\n') img_list = np.array(img_list).astype('float32') img_list *= 1./255 zlist = np.array(zlist).astype('float32') zlist = rescale(zmin, zmax, zlist) alist = np.array(alist).astype('float32') alist = rescale(amin, amax, alist) nlist = np.array(nlist).astype('float32') nlist = rescale(nmin, nmax, nlist) scale_list = np.array(scale_list).astype(int) params_list = [zlist, alist, nlist] return img_list, params_list, scale_list