コード例 #1
0
ファイル: utils.py プロジェクト: kristiantorres/resfoc
def torchprogress(cur,bsz,tot,loss,acc,size=40,file=sys.stdout,save=True):
  """
  Prints a progress bar during training of a torch
  neural network

  Parameters:
    cur  - index of the current batch
    bsz  - size of a batch
    tot  - the total number batches
    loss - the current running loss value
    acc  - the current accuracy
    save - flag indicating whether to return the computed values

  Prints a progressbar to the screen
  """
  x = int(size*cur/tot)
  if(cur == 0): div = 1
  else: div = cur
  curform = create_inttag(cur,tot)
  loss /= div
  acc /= ((cur+1)*bsz)
  file.write("%s/%d [%s%s] loss=%.4g acc=%.4f\r" % (curform,tot,"#"*x, "."*(size-x),loss,acc))
  if(cur == tot):
    file.write("\n")
  file.flush()
  if(save):
    return loss,acc
コード例 #2
0
def makemovie_mpl(arr,odir,ftype='png',qc=False,skip=1,pttag=False,**kwargs):
  """
  Saves each frame on the fast axis to a png for viewing

  Parameters:
    arr       - input array where the frames are on the fast axis
    odir      - the output directory where to save the frames
    ftype     - the type (extension) of figure to save [png]
    qc        - look at each frame as it is saved [False]
    skip      - whether to skip frames [1]
    pttag     - write the frame number in pretty (unix-friendly) format
    wbox      - the first dimension of the figure size (figure width) [10]
    hbox      - the second dimension of the figure size (figure height) [10]
    xmin      - the minimum lateral point in your data [0.0]
    xmax      - the maximum lateral point in your data (typically (nx-1)*dx) [nx]
    zmin      - the minimum depth point in your data [0.0]
    zmax      - the maximum depth point in your data (typically (nz-1)*dz) [nz]
    vmin      - the minumum value desired to be plotted (min(data))
    vmax      - the maximum value desired to be plotted (max(data))
    pclip     - a scale to be applied to shrink or expand the dynamic range
    xlabel    - the label of the x-axis [None]
    ylabel    - the label of the y-axis [None]
    labelsize - the fontsize of the labels [18]
    ticksize  - the fontsize of the ticks [18]
    ttlstring - title to be printed. Can be printed of the form ttlstring%(ottl + dttl*(framenumber))
    ottl      - the first title value to be printed
    dttl      - the sampling of the title value to be printed
    aratio    - aspect ratio of the figure [1.0]
  """
  # Check if directory exists
  if(os.path.isdir(odir) == False):
    os.mkdir(odir)
  nfr = arr.shape[2]
  k = 0
  frames = np.arange(0,nfr,skip)
  vmin = np.min(arr); vmax = np.max(arr)
  for ifr in progressbar(frames, "frames"):
    fig = plt.figure(figsize=(kwargs.get("wbox",10),kwargs.get("hbox",10)))
    ax = fig.add_subplot(111)
    ax.imshow(arr[:,:,ifr],cmap=kwargs.get("cmap","gray"),
        extent=[kwargs.get("xmin",0),kwargs.get("xmax",arr.shape[1]),
          kwargs.get("zmax",arr.shape[0]),kwargs.get("zmin",0)],
        vmin=kwargs.get("vmin",vmin)*kwargs.get('pclip',1.0),
        vmax=kwargs.get("vmax",vmax)*kwargs.get('pclip',1.0))
    ax.set_xlabel(kwargs.get('xlabel',''),fontsize=kwargs.get('labelsize',18))
    ax.set_ylabel(kwargs.get('ylabel',''),fontsize=kwargs.get('labelsize',18))
    ax.tick_params(labelsize=kwargs.get('ticksize',18))
    if('%' in kwargs.get('ttlstring'),''):
      ax.set_title(kwargs.get('ttlstring','')%(kwargs.get('ottl',0.0) + kwargs.get('dttl',1.0)*k),
          fontsize=kwargs.get('labelsize',18))
    ax.set_aspect(kwargs.get('aratio',1.0))
    if(pttag):
      tag = create_inttag(k,nfr)
    else:
      tag = str(k)
    plt.savefig(odir+'/'+tag+'.'+ftype,bbox_inches='tight',dpi=kwargs.get("dpi",150))
    k += 1
    if(qc):
      plt.show()
コード例 #3
0
 def on_epoch_end(self, epoch, logs={}):
     if (epoch % self.skip == 0):
         # Make a prediction on the f3 data and save it
         print("Predicting on F3 dataset...")
         pred = self.model.predict(self.f3dat, verbose=1)
         # Save predictions to file
         with h5py.File(
                 self.predpath + '/ep%s.h5' % (create_inttag(epoch, 100)),
                 "w") as hf:
             hf.create_dataset("pred",
                               self.f3dat.shape,
                               data=pred,
                               dtype=np.float32)
         # Reconstruct a single inline
         iimg = self.f3dat[self.xlidx * self.dsize:(self.xlidx + 1) *
                           self.dsize, :, :]
         iimg = iimg.reshape([7, 15, 128, 128])
         rimg = self.pe.reconstruct(iimg)
         # Reconstruct the predictions
         ipred = pred[self.xlidx * self.dsize:(self.xlidx + 1) *
                      self.dsize, :, :]
         ipred = ipred.reshape([7, 15, 128, 128])
         rpred = self.pe.reconstruct(ipred)
         # Apply threshold and plot
         tpred = thresh(rpred, self.thresh)
         nt = rimg.shape[0]
         nx = rimg.shape[1]
         plotseglabel(rimg[self.fs:, :],
                      tpred[self.fs:, :],
                      color='blue',
                      xlabel='Inline',
                      ylabel='Time (s)',
                      xmin=0.0,
                      xmax=(nx - 1) * 25 / 1000.0,
                      zmin=(self.fs - 1) * 0.004,
                      zmax=(nt - 1) * 0.004,
                      vmin=-2.5,
                      vmax=2.5,
                      interp='sinc',
                      aratio=6.5)
         plt.savefig(self.figpath + '/ep%s.png' %
                     (create_inttag(epoch, 100)),
                     bbox_inches='tight',
                     dpi=150)
コード例 #4
0
def run_cgshape(op, dat, mod, shpop, eps, niter, toler, objs, mods, grds, ress,
                optqc, verb):
    """ Sergey Fomel's conjugate gradient with shaping regularization """
    ## Allocate memory
    # Model and residual
    pod = np.zeros(mod.shape, dtype='float32')
    res = np.zeros(dat.shape, dtype='float32')
    # Gradients
    grp = np.zeros(mod.shape, dtype='float32')
    grm = np.zeros(mod.shape, dtype='float32')
    grr = np.zeros(dat.shape, dtype='float32')
    # Search directions
    srp = np.zeros(mod.shape, dtype='float32')
    srm = np.zeros(mod.shape, dtype='float32')
    srr = np.zeros(dat.shape, dtype='float32')

    # Epsilon scaling
    eps2 = eps * eps

    # Compute the initial residual
    res[:] = -dat[:]
    op.forward(True, mod, res)

    dg = g0 = gnp = 0.0
    r0 = gdot(res, res)
    if (r0 == 0.0):
        print("Residual is zero: r0=%f" % (r0))
        return

    # Iteration loop
    for iiter in range(niter):
        grp[:] = eps * pod[:]
        grm = -eps * mod[:]

        # Compute the traditional gradient
        op.adjoint(True, grm, res)

        # Symmetrized shaping operator
        shpop.adjoint(True, grp, grm)
        shpop.forward(False, grp, grm)

        # Data space gradient
        op.forward(False, grm, grr)

        gn = gdot(grp, grp)

        if (iiter == 0):
            # Use only current gradient for first iteration
            g0 = gn
            srp[:] = grp[:]
            srm[:] = grm[:]
            srr[:] = grr[:]
        else:
            alpha = gn / gnp
            dg = gn / g0

            if (alpha < toler or dg < toler):
                if (verb):
                    print("converged in %d iterations, alpha=%f gd=%f" %
                          (iiter, alpha, dg))
                break

            scale_add(grp, 1.0, srp, alpha)
            swap(srp, grp)
            scale_add(grm, 1.0, srm, alpha)
            swap(srm, grm)
            scale_add(grr, 1.0, srr, alpha)
            swap(srr, grr)

        beta = gdot(srr, srr) + eps * (gdot(srp, srp) - gdot(srm, srm))

        # Compute step length
        alpha = -gn / beta

        # Update model and residual
        scale_add(pod, 1.0, srp, alpha)
        scale_add(mod, 1.0, srm, alpha)
        scale_add(res, 1.0, srr, alpha)

        # Verbosity
        rout = gdot(res, res) / r0
        # Save results to provided lists
        save_results(mod, mods, grm, grds, res, ress, rout, objs)
        # Save to SEPlib file or image if desired
        if (optqc is not None):
            optqc.output(rout, mod, grm, res)
        if (verb):
            print("iter=%s res=%.6f grd=%.6f" %
                  (create_inttag(iiter + 1, niter), rout, dg))

        gnp = gn
コード例 #5
0
def run_toler(op, dat, mod, toler, grdop, mods, objs, grds, ress, optqc, verb):
    """ Runs conjugate direction solver until a tolerance is reached """
    # Temporary data space arrays
    if (isinstance(dat, list)):
        res = []
        drs = []
        dsz = []
        for idat in dat:
            res.append(np.zeros(idat.shape, dtype='float32'))
            drs.append(np.zeros(idat.shape, dtype='float32'))
            dsz.append(idat.shape)
    else:
        res = np.zeros(dat.shape, dtype='float32')
        drs = np.zeros(dat.shape, dtype='float32')
        dsz = dat.shape

    # Temporary model space arrays
    if (isinstance(mod, list)):
        grd = []
        tap = []
        msz = []
        for imod in mod:
            grd.append(np.zeros(imod.shape, dtype='float32'))
            tap.append(np.zeros(imod.shape, dtype='float32'))
            msz.append(imod.shape)
    else:
        grd = np.zeros(mod.shape, dtype='float32')
        tap = np.zeros(mod.shape, dtype='float32')
        msz = mod.shape

    # Create a stepper object
    stpr = cdstep(msz, dsz)

    # Loop until tolerance is reached
    f1 = 1.0 + toler
    iiter = 0
    while (f1 > rtol):
        # First compute the objective function
        op.forward(False, mod, res)
        scale_add(res, 1.0, dat, -1.0)
        f0 = (0.5) * gdot(res, res)
        # Compute the gradient
        op.adjoint(False, grd, res)
        # Process the gradient if desired
        if (grdop is not None):
            grdop.forward(False, grd, tap)
        else:
            tap = grd
        # Compute the data space gradient
        op.forward(False, tap, drs)
        # Compute the step length and update
        if (not stpr.step(mod, tap, res, drs)): break
        # Reevaluate objective function and output
        f1 = (0.5) * gdot(res, res)
        if (f1 >= f0):
            if (verb):
                print("Objective function did not reduce, terminating solver")
            break
        # Save results to provided lists
        save_results(mod, mods, tap, grds, res, ress, f1, objs)
        # Save to SEPlib file or image if desired
        if (optqc is not None):
            optqc.output(f1, mod, tap, res)
        if (verb):
            print("iter=%s objf=%.6f gnrm=%.6f" %
                  (create_inttag(iiter + 1, 10000), f1, np.linalg.norm(tap)))
        iiter += 1
コード例 #6
0
def makemoviesbs_mpl(arr1,arr2,odir,ftype='png',qc=False,skip=1,pttag=False,**kwargs):
  """
  Saves each frame on the fast axis to a png for viewing

  Parameters:
    arr1  - first input array (left panel) where the frames are on the fast axis
    arr2  - second input array (right panel) where the frames are on the fast axis
    odir  - the output directory where to save the frames
    ftype - the type (extension) of figure to save [png]
    qc    - look at each frame as it is saved [False]
    skip  - whether to skip frames [1]
    pttag - write the frame number in pretty (unix-friendly) format
  """
  # Check if directory exists
  if(os.path.isdir(odir) == False):
    os.mkdir(odir)
  assert(arr1.shape[2] == arr2.shape[2]),"Both movies must have same number of frames"
  nfr = arr1.shape[2]
  k = 0
  frames = np.arange(0,nfr,skip)
  vmin1 = np.min(arr1); vmax1 = np.max(arr1)
  vmin2 = np.min(arr2); vmax2 = np.max(arr2)
  for ifr in progressbar(frames, "frames"):
    # Setup plot
    f,ax = plt.subplots(1,2,figsize=(kwargs.get("figsize1",15),kwargs.get("figsize2",8)),
        gridspec_kw={'width_ratios': [kwargs.get("wratio1",1), kwargs.get("wratio2",1)]})
    # First plot
    im1 = ax[0].imshow(arr1[:,:,ifr],cmap=kwargs.get("cmap1","gray"),
        extent=[kwargs.get("xmin1",0),kwargs.get("xmax1",arr1.shape[1]),
          kwargs.get("zmax1",arr1.shape[0]),kwargs.get("zmin1",0)],
        vmin=kwargs.get("vmin1",vmin1)*kwargs.get('pclip',1.0),
        vmax=kwargs.get("vmax1",vmax1)*kwargs.get('pclip',1.0),aspect=kwargs.get('aspect1',1.0))
    ax[0].set_xlabel(kwargs.get('xlabel1',''),fontsize=kwargs.get('labelsize',18))
    ax[0].set_ylabel(kwargs.get('ylabel',''),fontsize=kwargs.get('labelsize',18))
    ax[0].tick_params(labelsize=kwargs.get('ticksize',18))
    if('%' in kwargs.get('ttlstring1'),''):
      ax[0].set_title(kwargs.get('ttlstring1','')%(kwargs.get('ottl1',0.0) + kwargs.get('dttl1',1.0)*k),
          fontsize=kwargs.get('labelsize',18))
    else:
      ax[0].set_title(kwargs.get('ttlstring1',''),fontsize=kwargs.get('labelsize',18))
    # Second plot
    im2 = ax[1].imshow(arr2[:,:,ifr],cmap=kwargs.get("cmap2","gray"),
        extent=[kwargs.get("xmin2",0),kwargs.get("xmax2",arr2.shape[1]),
          kwargs.get("zmax2",arr2.shape[0]),kwargs.get("zmin2",0)],
        vmin=kwargs.get("vmin2",vmin2)*kwargs.get('pclip',1.0),
        vmax=kwargs.get("vmax2",vmax2)*kwargs.get('pclip',1.0),aspect=kwargs.get('aspect2',1.0))
    ax[1].set_xlabel(kwargs.get('xlabel2',''),fontsize=kwargs.get('labelsize',18))
    ax[1].set_ylabel(kwargs.get('ylabel2',''),fontsize=kwargs.get('labelsize',18))
    ax[1].tick_params(labelsize=kwargs.get('ticksize',18))
    if('%' in kwargs.get('ttlstring2','')):
      ax[1].set_title(kwargs.get('ttlstring2','')%(kwargs.get('ottl2',0.0) + kwargs.get('dttl2',1.0)*k),
          fontsize=kwargs.get('labelsize',18))
    else:
      ax[1].set_title(kwargs.get('ttlstring2',''),fontsize=kwargs.get('labelsize',18))
    # Color bar
    if(kwargs.get('cbar',False)):
      cbar_ax = f.add_axes([kwargs.get('barx',0.91),kwargs.get('barz',0.12),
        kwargs.get('wbar',0.02),kwargs.get('hbar',0.75)])
      cbar = f.colorbar(im2,cbar_ax,format='%.2f')
      cbar.ax.tick_params(labelsize=kwargs.get('ticksize',18))
      cbar.set_label(kwargs.get('barlabel',''),fontsize=kwargs.get("barlabelsize",18))
      cbar.draw_all()
    # Spacing between plots
    plt.subplots_adjust(wspace=kwargs.get("pltspace",0.2))
    if(pttag):
      tag = create_inttag(k,nfr)
    else:
      tag = str(k)
    plt.savefig(odir+'/'+tag+'.'+ftype,bbox_inches='tight',dpi=kwargs.get("dpi",150))
    k += 1
    if(qc):
      plt.show()
コード例 #7
0
    def write_examples(self, x, y):
        """
    Writes training examples to the H5 file

    Parameters:
      x - the training examples
      y - the corresponding labels
    """
        tag = create_inttag(self.__ctr, self.__nmax)
        if (x.shape[0] != y.shape[0]):
            raise Exception(
                "Number of examples must be same for input data and labels")
        nex = x.shape[0]

        # Shapes of each example
        if (self.__channels_last):
            xshape = [self.__dsize, *x.shape[1:], 1]
            yshape = [self.__dsize, *y.shape[1:], 1]
        else:
            xshape = [self.__dsize, 1, *x.shape[1:]]
            yshape = [self.__dsize, 1, *y.shape[1:]]

        igr, rem = divmod(nex, self.__dsize)

        if (igr > 0):
            # Write out what fits
            beg = 0
            end = 0
            for k in range(igr):
                beg = end
                end += self.__dsize
                datatag = create_inttag(self.__ctr, self.__nmax)
                if (self.__channels_last):
                    xot = np.expand_dims(x[beg:end], axis=-1)
                    yot = np.expand_dims(y[beg:end], axis=-1)
                else:
                    xot = x[beg:end, np.newaxis]
                    yot = y[beg:end, np.newaxis]
                self.__hf.create_dataset('x' + datatag,
                                         xshape,
                                         data=xot,
                                         dtype=np.float32)
                self.__hf.create_dataset('y' + datatag,
                                         yshape,
                                         data=yot,
                                         dtype=np.float32)
                self.__ctr += 1
            # Save what you can
            if (end < nex):
                if (self.__recurse):
                    self.__xlef = []
                    self.__ylef = []
                    self.__recurse = False
                self.__xlef.append(x[end:])
                self.__ylef.append(y[end:])
            elif (end == nex):
                if (self.__recurse):
                    self.__recurse = False
                    self.__xlef = []
                    self.__ylef = []
        else:
            # Append the examples to the saved list
            self.__xlef.append(x)
            self.__ylef.append(y)

        # If the size of the left over array is larger than dsize
        # and recurse
        nlef = np.sum([ex.shape[0] for ex in self.__ylef])
        if (nlef >= self.__dsize):
            xr = np.concatenate(self.__xlef, axis=0)
            yr = np.concatenate(self.__ylef, axis=0)
            self.__recurse = True
            self.write_examples(xr, yr)