Beispiel #1
0
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_inner(config, settype='train', nframes=None):
    # create directories and filenames
    directory = os.path.abspath(os.path.expanduser(config['directory'])+settype)
    if nframes is None:
        nframes = config[settype]['nframes']
    start = 0
    tempnum = nframes
    for dir in ('images', 'params'):
        path = os.path.join(directory, dir)
        if not os.path.exists(path):
            os.makedirs(path)
        already_files = len(os.listdir(path))
        if already_files < tempnum:  #if there are fewer than the number of files desired
            tempnum = already_files
    if not config['overwrite']:
        start = tempnum
        if start >= nframes:
            return
    with open(directory + '/config.json', 'w') as f:
        json.dump(config, f)
    filetxtname = os.path.join(directory, 'filenames.txt')
    imgname = os.path.join(directory, 'images', 'image{:04d}.' + config['imgtype'])
    jsonname = os.path.join(directory, 'params', 'image{:04d}.json')
    filetxt = open(filetxtname, 'w')
    #always only one particle per stamp
    config['particle']['nspheres'] = [1,2]
    shape = config['shape']
    for n in range(start, nframes):  # for each frame ...
        print(imgname.format(n))
        sample = make_sample(config) # ... get params for particles
        s = sample[0]
#         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]
        if config['scale_integer']:
            frame, scale = scale_int(s, config)
        else:
            frame, scale = scale_float(s, config)
    
        # ... and save the results
        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')
    return
Beispiel #3
0
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