Exemplo n.º 1
0
def scale_int(s, config, num_overlaps):
    shape = config['shape']
    ext = feature_extent(s, config)
    #introduce noise to ext
    ext_noise = config['ext_noise']
    ext = np.random.normal(ext, ext_noise*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
    holo.particle = add_overlaps(ext, num_overlaps, config).append(holo.particle)
    frame += holo.hologram().reshape(newshape)
    frame = np.clip(100 * frame, 0, 255).astype(np.uint8)
    #decimate
    frame = frame[::scale, ::scale]
    return frame, scale
Exemplo n.º 2
0
def format_yolo(sample, config):
    '''Returns a string of YOLO annotations'''
    (h, w) = config['shape']
    fmt = '{}' + 4 * ' {:.6f}' + '\n'
    annotation = ''
    for sphere in sample:
        stype = classify(sphere, config)
        diameter = 2. * feature_extent(sphere, config)
        x_p = sphere.x_p / w
        y_p = sphere.y_p / h
        w_p = diameter / w
        h_p = diameter / h
        annotation += fmt.format(stype, x_p, y_p, w_p, h_p)
    return annotation
Exemplo n.º 3
0
def scale_float(s, config, num_overlaps):
    shape = config['shape']
    ext = feature_extent(s, config)
    #introduce noise to ext
    ext_noise = config['ext_noise']
    ext = np.random.normal(ext, ext_noise*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)
    s.x_p += (scale-1)*100.
    s.y_p += (scale-1)*100.
    totalspheres = add_overlaps(ext, num_overlaps, config)
    totalspheres.append(s)
    holo.lorenzmie.particle = totalspheres
    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
Exemplo n.º 4
0
def localizer_accuracy(configuration='yolov5_test',
                       nframes=None,
                       version=None,
                       plot=False,
                       savedir='./results/',
                       overwrite=False):
    basedir = os.path.dirname(os.path.abspath(__file__)).split('eval')[0]
    path = basedir + 'cfg_yolov5/{}.json'.format(configuration)

    with open(path, 'r') as f:
        config = json.load(f)

    file_header = os.path.abspath(config['directory'])
    eval_dir = file_header + '/eval'

    mtd_config = config.copy()
    mtd_config['directory'] = eval_dir
    if not nframes:
        nframes = config['nframes_eval']

    mtd_config['nframes'] = nframes
    mtd_config['particle']['nspheres'] = [1, 1]
    mtd_config['overwrite'] = overwrite
    print('making data')
    makedata(config=mtd_config)
    print('data made')

    localizer = Localizer(configuration=configuration, version=version)

    imgpath_fmt = config['directory'] + '/eval/images/image{}.png'
    parampath_fmt = config['directory'] + '/eval/params/image{}.json'

    df = pd.DataFrame(columns=[
        'img_num', 'x_true', 'y_true', 'ext_true', 'num_detections', 'x_pred',
        'y_pred', 'ext_pred', 'conf'
    ])
    style = dict(fill=False, linewidth=3, edgecolor='r')
    for n in range(nframes):
        print('frame {}'.format(n), end='\r')
        img = cv2.imread(imgpath_fmt.format(str(n).zfill(5)))
        with open(parampath_fmt.format(str(n).zfill(5)), 'r') as f:
            params = json.load(f)[0]
        sphere = Sphere()
        sphere.loads(params)
        params = ast.literal_eval(params)
        ext = 2. * feature_extent(sphere, mtd_config)

        results = localizer.detect([img])[0]

        if plot:
            fig, ax = plt.subplots()
            ax.imshow(img, cmap='gray')
            for feature in results:
                corner, w, h = feature['bbox']
                ax.add_patch(Rectangle(xy=corner, width=w, height=h, **style))
            plt.show()

        resultsdict = {
            'img_num': n,
            'x_true': params['x_p'],
            'y_true': params['y_p'],
            'ext_true': ext,
            'num_detections': len(results),
            'x_pred': None,
            'y_pred': None,
            'ext_pred': None,
            'conf': None
        }

        if len(results) == 1 and not results[0]['edge']:
            r = results[0]
            p_ext = max(r['bbox'][1:])
            resultsdict['x_pred'] = r['x_p']
            resultsdict['y_pred'] = r['y_p']
            resultsdict['conf'] = r['conf']
            resultsdict['ext_pred'] = p_ext

        df = df.append(resultsdict, ignore_index=True)

    print(df)

    saveheader = savedir + configuration
    if version:
        saveheader += '_v{}'.format(version)
    savepath = saveheader + '_eval.csv'
    df.to_csv(savepath)

    truepos = df[df.num_detections == 1]
    falseneg = df[df.num_detections == 0]
    falsepos = df[df.num_detections > 1]
    numfalsepos = int(falsepos.num_detections.sum() - len(falsepos))
    numtruepos = len(truepos) + len(falsepos)
    numfalseneg = len(falseneg)

    print(
        '{} true positive detections, {} false positive (additional) detections, {} false negative detections'
        .format(numtruepos, numfalsepos, numfalseneg))

    inplane_err_sq = (truepos.x_true - truepos.x_pred)**2 + (truepos.y_true -
                                                             truepos.y_pred)**2
    inplane_RMSE = np.sqrt(inplane_err_sq.sum() / len(inplane_err_sq))
    inplane_err = np.sqrt(inplane_err_sq)

    fig, ax = plt.subplots()
    loc = ax.scatter(truepos.x_true,
                     truepos.y_true,
                     c=np.log(inplane_err),
                     cmap='Spectral')
    ax.set_xlabel(r'$x_p$ [px]')
    ax.set_xlabel(r'$y_p$ [px]')
    ax.grid(alpha=0.3)
    fig.colorbar(loc, label='log(In-plane error [px])')
    ax.annotate('In-plane RMSE: {}px'.format('%.1f' % inplane_RMSE),
                xy=(0.05, 0.95),
                xycoords='axes fraction',
                bbox=dict(facecolor='white', edgecolor='black', alpha=0.5))
    fig.tight_layout()
    fig.savefig(saveheader + '_inplane_err.png')
    plt.show()

    ext_err_sq = (truepos.ext_true - truepos.ext_pred)**2
    ext_RMSE = np.sqrt(ext_err_sq.sum() / len(ext_err_sq))
    ext_percent = np.sqrt(ext_err_sq) / truepos.ext_true
    print(ext_percent)
    ext_perror = ext_percent.mean() * 100

    fig, ax = plt.subplots()
    ax.plot(truepos.ext_true, truepos.ext_true, c='r')
    ax.scatter(truepos.ext_true, truepos.ext_pred, alpha=0.3, c='b')
    ax.annotate('{}% Extent Error'.format('%.1f' % ext_perror),
                xy=(0.05, 0.95),
                xycoords='axes fraction',
                bbox=dict(facecolor='white', edgecolor='black', alpha=0.5))
    ax.set_xlabel('True feature size [px]')
    ax.set_ylabel('Predicted bounding box size [px]')
    ax.grid(alpha=0.3)
    fig.tight_layout()
    fig.savefig(saveheader + '_ext_err.png')
    plt.show()
Exemplo n.º 5
0
def catch_accuracy(loc='yolov5_test', est='test', nframes=None, version=None, plot=False, weights='best', savedir='./results/'):
    basedir = os.path.dirname(os.path.abspath(__file__)).split('eval')[0]
    path = basedir + 'cfg_yolov5/{}.json'.format(loc)

    with open(path, 'r') as f:
        config = json.load(f)
    
    file_header = os.path.abspath(config['directory'])
    eval_dir = file_header + '/eval'
    
    mtd_config = config.copy()
    mtd_config['directory'] = eval_dir
    if not nframes:
        nframes = config['nframes_eval']
        
    mtd_config['nframes'] = nframes
    mtd_config['particle']['nspheres'] = [1,1]
    #mtd_config['overwrite'] = True
    
    makedata(config = mtd_config)

    localizer = Localizer(configuration=loc, version=version)
    estimator = Estimator(configuration=est, weights=weights)
    catch = CATCH(localizer=localizer, estimator=estimator)

    imgpath_fmt = config['directory']+'/eval/images/image{}.png'
    parampath_fmt = config['directory']+'/eval/params/image{}.json'

    df = pd.DataFrame(columns = ['img_num', 'x_true', 'y_true', 'ext_true', 'num_detections', 'x_pred', 'y_pred', 'ext_pred', 'conf', 'z_pred', 'a_pred', 'n_pred', 'z_true', 'a_true', 'n_true'])
    style = dict(fill=False, linewidth=3, edgecolor='r')
    for n in range(nframes):
        img = cv2.imread(imgpath_fmt.format(str(n).zfill(4)))
        with open(parampath_fmt.format(str(n).zfill(4)), 'r') as f:
            params = json.load(f)[0]
        sphere = Sphere()
        sphere.loads(params)
        params = ast.literal_eval(params)
        ext = 2. * feature_extent(sphere, mtd_config)

        results = catch.analyze([img])

        if plot:
            fig, ax = plt.subplots()
            ax.imshow(img, cmap='gray')
            for feature in results:
                corner, w, h = feature['bbox']
                ax.add_patch(Rectangle(xy=corner, width=w, height=h, **style))
            plt.show()
            
        resultsdict = {'img_num':n, 'x_true':params['x_p'], 'y_true':params['y_p'],  'z_true':params['z_p'], 'a_true': params['a_p'], 'n_true':params['n_p'], 'ext_true':ext, 'num_detections':len(results), 'x_pred':None, 'y_pred':None, 'z_pred': None, 'a_pred': None, 'n_pred': None, 'ext_pred':None, 'conf':None, 'edge':None}

        if len(results) == 1:
            r = results.iloc[0]
            p_ext = max(r['bbox'][1:])
            resultsdict['x_pred'] = r['x_p']
            resultsdict['y_pred'] = r['y_p']
            resultsdict['a_pred'] = r['a_p']
            resultsdict['z_pred'] = r['z_p']
            resultsdict['n_pred'] = r['n_p']
            resultsdict['conf'] = r['conf']
            resultsdict['ext_pred'] = p_ext
            resultsdict['edge'] = r['edge']
            
        df = df.append(resultsdict, ignore_index=True)

    print(df)

    if weights=='best':
        wstr = ''
    else:
        wstr=weights
    saveheader = savedir+'est_{}{}_loc_{}'.format(est, wstr, loc)
    if version:
        saveheader += 'v{}'.format(version)
    savepath = saveheader + '_eval.csv'
    df.to_csv(savepath)

    truepos = df[df.num_detections==1]
    falseneg = df[df.num_detections==0]
    falsepos = df[df.num_detections>1]
    numfalsepos = int(falsepos.num_detections.sum() - len(falsepos))
    numtruepos = len(truepos) + len(falsepos)
    numfalseneg = len(falseneg)

    print('{} true positive detections, {} false positive (additional) detections, {} false negative detections'.format(numtruepos, numfalsepos, numfalseneg))

    z_rmse = np.sqrt(((truepos.z_pred - truepos.z_true) **2).mean(axis=0))
    a_rmse = np.sqrt(((truepos.a_pred - truepos.a_true) **2).mean(axis=0))
    n_rmse = np.sqrt(((truepos.n_pred - truepos.n_true) **2).mean(axis=0))


    matplotlib.use('TkAgg')
    fig, axes = plt.subplots(1,3, figsize=(10,5))
    names = ['z_p', 'a_p', 'n_p']
    for ax, name in list(zip(axes, names)):
        ax.grid(alpha=0.3)
        ax.set_xlabel(r'True ${}$'.format(name))
        ax.set_ylabel(r'Pred ${}$'.format(name))
    ax1, ax2, ax3 = axes
    ax1.scatter(df.z_true, df.z_pred, c='b', alpha=0.4)
    ax1.plot(df.z_true, df.z_true, c='r')
    ax1.annotate('RMSE = {} px'.format('%.3f'%z_rmse), xy=(0.05, 0.95), xycoords='axes fraction')
    
    ax2.scatter(df.a_true, df.a_pred, c='b', alpha=0.4)
    ax2.plot(df.a_true, df.a_true, c='r')
    ax2.annotate(r'RMSE = {}$\mu m$ '.format('%.3f'%a_rmse), xy=(0.05, 0.95), xycoords='axes fraction')
    
    ax3.scatter(df.n_true, df.n_pred, c='b', alpha=0.4)
    ax3.plot(df.n_true, df.n_true, c='r')
    ax3.annotate('RMSE = {}'.format('%.3f'%n_rmse), xy=(0.05, 0.95), xycoords='axes fraction')
    fig.tight_layout()

    figsavepath = saveheader+'_eval.png'
    fig.savefig(figsavepath)
    
    plt.show()