Esempio n. 1
0
 def mie_loss(self, params, image, dim):
     '''Returns the residual between the image and our Mie model.'''
     p = params.valuesdict()
     h = LMHologram(coordinates=coordinates(dim))
     h.particle.r_p = [p['x'] + dim[0] // 2, p['y'] + dim[1] // 2, p['z']]
     h.particle.a_p = p['a_p']
     h.particle.n_p = p['n_p']
     h.instrument.wavelength = p['lamb']
     h.instrument.magnification = p['mpp']
     h.instrument.n_m = p['n_m']
     hologram = h.hologram().reshape(dim)
     return (hologram - image) / self.noise
def makedata(config={}):
    '''Make Training Data'''
    # set up pipeline for hologram calculation
    shape = config['shape']
    holo = LMHologram(coordinates=coordinates(shape))
    holo.instrument.properties = config['instrument']

    # create directories and filenames
    directory = os.path.expanduser(config['directory'])
    imgtype = config['imgtype']

    nframes = config['nframes']
    start = 0
    tempnum = nframes
    for dir in ('images', 'labels', '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}.' + imgtype)
    jsonname = os.path.join(directory, 'params', 'image{:04d}.json')
    yoloname = os.path.join(directory, 'labels', 'image{:04d}.txt')

    filetxt = open(filetxtname, 'w')
    for n in range(start, nframes):  # for each frame ...
        print(imgname.format(n))
        sample = make_sample(config)  # ... get params for particles
        # ... 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)
        # ... and save the results
        cv2.imwrite(imgname.format(n), frame)
        with open(jsonname.format(n), 'w') as fp:
            fp.write(format_json(sample, config))
        with open(yoloname.format(n), 'w') as fp:
            fp.write(format_yolo(sample, config))
        filetxt.write(imgname.format(n) + '\n')
        #print('finished image {}'.format(n+1))
    return
Esempio n. 3
0
def feature_extent(sphere, config, nfringes=20):
    '''Radius of holographic feature in pixels'''
    s = Sphere()
    s.a_p = sphere.a_p
    s.n_p = sphere.n_p
    s.z_p = sphere.z_p
    h = LMHologram(coordinates=np.arange(300))
    h.instrument.properties = config['instrument']
    h.particle = sphere
    # roughly estimate radii of zero crossings
    b = h.hologram() - 1.
    ndx = np.where(np.diff(np.sign(b)))[0] + 1
    return float(ndx[nfringes])
Esempio n. 4
0
def feature_extent(sphere, config, nfringes=20, maxrange=300):
    '''Radius of holographic feature in pixels'''

    h = LMHologram(coordinates=np.arange(maxrange))
    h.instrument.properties = config['instrument']
    h.particle.a_p = sphere.a_p
    h.particle.n_p = sphere.n_p
    h.particle.z_p = sphere.z_p
    # roughly estimate radii of zero crossings
    b = h.hologram() - 1.
    ndx = np.where(np.diff(np.sign(b)))[0] + 1
    if len(ndx) <= nfringes:
        return maxrange
    else:
        return float(ndx[nfringes])
Esempio n. 5
0
def mtd(configfile='mtd.json'):
    '''Make Training Data'''
    # read configuration
    with open(configfile, 'r') as f:
        config = json.load(f)

    # set up pipeline for hologram calculation
    shape = config['shape']
    holo = LMHologram(coordinates=coordinates(shape))
    holo.instrument.properties = config['instrument']

    # create directories and filenames
    directory = os.path.expanduser(config['directory'])
    imgtype = config['imgtype']
    for dir in ('images_labels', '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_labels', 'image{:04d}.' + imgtype)
    jsonname = os.path.join(directory, 'params', 'image{:04d}.json')
    yoloname = os.path.join(directory, 'images_labels', 'image{:04d}.txt')

    filetxt = open(filetxtname, 'w')
    for n in range(config['nframes']):  # for each frame ...
        print(imgname.format(n))
        sample = make_sample(config)   # ... get params for particles
        # ... 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)
        # ... and save the results
        cv2.imwrite(imgname.format(n), frame)
        with open(jsonname.format(n), 'w') as fp:
            fp.write(format_json(sample, config))
        with open(yoloname.format(n), 'w') as fp:
            fp.write(format_yolo(sample, config))
        filetxt.write(imgname.format(n) + '\n')
Esempio n. 6
0
def scale_float(s, config):
    shape = config['shape']
    ext = feature_extent(s, config)
    #introduce 1% noise to ext
    ext = np.random.normal(ext, 0.01*ext)
    extsize = ext*2
    shapesize = shape[0]
    scale = float(extsize)/float(shapesize)
    newshape = [int(extsize)]*2
    holo = LMHologram(coordinates=coordinates(newshape))
    holo.instrument.properties = config['instrument']
    # ... calculate hologram
    frame = np.random.normal(0, config['noise'], newshape)
    holo.particle = s
    holo.particle.x_p += (scale-1)*100.
    holo.particle.y_p += (scale-1)*100.
    frame += holo.hologram().reshape(newshape)
    frame = np.clip(100 * frame, 0, 255).astype(np.uint8)
    #reshape
    frame = cv2.resize(frame, tuple(shape))
    return frame, scale
Esempio n. 7
0
def scale_int(s, config):
    shape = config['shape']
    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)
    holo.particle = s
    holo.particle.x_p += (scale-1)*100
    holo.particle.y_p += (scale-1)*100
    frame += holo.hologram().reshape(newshape)
    frame = np.clip(100 * frame, 0, 255).astype(np.uint8)
    #decimate
    frame = frame[::scale, ::scale]
    return frame, scale
Esempio n. 8
0
def example():
    '''
    Make a "noisy" hologram. Then fit the noisy hologram. Plot the results.
    '''
    ## Make Noisy Hologram.
    # Create hologram to be fitted.
    from time import time
    x, y, z = 0., 0., 100.
    a_p = 0.5
    n_p = 1.5
    n_m = 1.339
    dim = [201, 201]
    lamb = 0.447
    mpp = 0.048
    h = LMHologram(coordinates=coordinates(dim))
    h.particle.r_p = [x + dim[0] // 2, y + dim[1] // 2, z]
    h.particle.a_p = a_p
    h.particle.n_p = n_p
    h.instrument.wavelength = lamb
    h.instrument.magnification = mpp
    h.instrument.n_m = n_m
    hologram = h.hologram().reshape(dim)

    # Add noise.
    std = 0.05
    noise = np.random.normal(size=hologram.shape) * std
    noisy_hologram = hologram + noise

    # Fit the noisy hologram.
    init_params = {
        'x': x,
        'y': y,
        'z': z + 8,
        'a_p': a_p - .3,
        'n_p': n_p + .03,
        'n_m': n_m,
        'mpp': mpp,
        'lamb': lamb
    }
    mie_fit = Mie_Fitter(init_params)
    t = time()
    result = mie_fit.fit(noisy_hologram)
    print("Time to fit: {:.05f}".format(time() - t))

    # Calculate the resulting image.
    residual = result.residual.reshape(*dim)
    final = hologram

    # Write error report.
    report_fit(result)

    ## Make plots.
    # Plot images.
    sns.set(style='white', font_scale=1.4)
    plt.imshow(np.hstack([noisy_hologram, final, residual + 1]))
    plt.title('Image, Fit, Residual')
    plt.gray()
    plt.show()

    # Plot Covariance.
    f, ax = plt.subplots()
    #cmap = sns.diverging_palette(220, 10, as_cmap=True)

    sns.set(font_scale=1.5)
    plt.title('Log Covariance Matrix')
    sns.heatmap(np.log(result.covar),
                cmap='PuBu',
                square=True,
                cbar_kws={},
                ax=ax)
    ax.set_xticklabels(['x', 'y', 'z', r'a$_p$', r'n$_p$'])
    ax.set_yticklabels([r'n$_p$', r'a$_p$', 'z', 'y', 'x'])
    plt.show()
Esempio n. 9
0
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]

        # ... 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
Esempio n. 10
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