示例#1
0
    def compute(self):
        #

        # Feature to Raster
        ras_asc_path = "/tmp/ascending_raster.tiff"  # tmp file, Ras -> Raster
        utils.rasterize(self.asc_input_path, ras_asc_path, self.point_size)
        clipped_asc_array = utils.clip_from_extent_as_array(
            ras_asc_path, self.extent)

        ras_desc_path = "/tmp/descending_raster.tiff"  # tmp file
        utils.rasterize(self.desc_input_path, ras_desc_path, self.point_size)
        clipped_desc_array = utils.clip_from_extent_as_array(
            ras_desc_path, self.extent)

        # Constant images
        self.rows, self.cols = clipped_asc_array.shape
        CosDir1_array = self.cd_e_desc * numpy.ones(
            (self.rows, self.cols), dtype=numpy.byte)
        CosDir2_array = self.cd_h_desc * numpy.ones(
            (self.rows, self.cols), dtype=numpy.byte)
        CosDir3_array = self.cd_e_asc * numpy.ones(
            (self.rows, self.cols), dtype=numpy.byte)
        CosDir4_array = self.cd_h_asc * numpy.ones(
            (self.rows, self.cols), dtype=numpy.byte)

        # "(([ResDisc] div [CosDir1])  - ([ResAsc] div ([CosDir3])) ) div (([CosDir2] div [CosDir1]) - ([CosDir4] div ([CosDir3])))"
        num = ((clipped_desc_array / CosDir1_array) -
               (clipped_asc_array / CosDir3_array))
        den = (
            (CosDir2_array / CosDir1_array) - (CosDir4_array / CosDir3_array)
        )  # for den == [0...] -> wrong image
        ew_speed_array = num / den

        self._save(ew_speed_array)
示例#2
0
    def compute(self):
        #
        
        # Feature to Raster
        ras_asc_path = "/tmp/ascending_raster.tiff"    # tmp file, Ras -> Raster
        utils.rasterize(self.asc_input_path, ras_asc_path, self.point_size)     
        clipped_asc_array = utils.clip_from_extent_as_array(ras_asc_path, self.extent)
        
        ras_desc_path = "/tmp/descending_raster.tiff"  # tmp file
        utils.rasterize(self.desc_input_path, ras_desc_path, self.point_size)   
        clipped_desc_array = utils.clip_from_extent_as_array(ras_desc_path, self.extent)

        # Constant images
        self.rows, self.cols = clipped_asc_array.shape
        CosDir1_array = self.cd_e_desc * numpy.ones((self.rows, self.cols), dtype=numpy.byte)
        CosDir2_array = self.cd_h_desc * numpy.ones((self.rows, self.cols), dtype=numpy.byte)
        CosDir3_array = self.cd_e_asc * numpy.ones((self.rows, self.cols), dtype=numpy.byte)
        CosDir4_array = self.cd_h_asc * numpy.ones((self.rows, self.cols), dtype=numpy.byte)

        # "(([ResDisc] div [CosDir1])  - ([ResAsc] div ([CosDir3])) ) div (([CosDir2] div [CosDir1]) - ([CosDir4] div ([CosDir3])))"
        num = ((clipped_desc_array / CosDir1_array) - (clipped_asc_array / CosDir3_array))
        den = ((CosDir2_array / CosDir1_array) - (CosDir4_array / CosDir3_array))           # for den == [0...] -> wrong image
        ew_speed_array = num / den
              
        self._save(ew_speed_array)
示例#3
0
    def forward(self, x, raster_input=False):
        if not raster_input:
            if isinstance(x, torch.Tensor):
                # x.shape = [batch_size, 20, 30, 3, 2]
                # `forward` must return shape [batch_size, 70]
                x = x.squeeze()
                batch_size = x.shape[0]
                x = x.view(batch_size, -1, 2)
                rasters = rasterize(x, device=self.device)
            elif isinstance(x, list):
                rasters = []
                # Rasterize glyph by glyph, due to variable number of curves
                for glyph in x:
                    # points.shape == [N, 2], where N = num_contours * num_curves * 3
                    points = torch.cat(
                        [curve for contour in glyph for curve in contour])
                    points = points.unsqueeze(0)
                    raster = rasterize(points, device=self.device)
                    rasters.append(raster)
                rasters = torch.cat(rasters, dim=0)
            rasters = rasters.unsqueeze(1)
        else:
            rasters = x

        clf = self.resnet(rasters)
        return clf
示例#4
0
def main(argv):
    print('Starting...')

    trainset = Dataset(FONT_FILES, DIMENSIONS)
    trainloader = data.DataLoader(
        trainset,
        batch_size=FLAGS.batch,
        shuffle=True,
        num_workers=4,
        pin_memory=True)

    disc = pantry.disc[FLAGS.disc](FLAGS.device).to(FLAGS.device)
    kernelDim = [int(x) for x in FLAGS.kernelDim]
    outputDim = [int(x) for x in FLAGS.outputDim]
    gen = pantry.gen[FLAGS.
                     gen](  # All these flags are specifically for matzah.
                         FLAGS.device,
                         fcSize=FLAGS.fcSize,
                         numFC=FLAGS.numFC,
                         styleDim=FLAGS.styleDim,
                         outputDim=outputDim,
                         numBlocks=FLAGS.numBlocks,
                         channels=FLAGS.channels,
                         kernel=kernelDim,
                         numClasses=FLAGS.numClasses).to(FLAGS.device)
    optimizer_disc = pantry.optimsDisc[FLAGS.disc](disc)
    optimizer_gen = pantry.optimsGen[FLAGS.gen](gen)
    num_epochs = FLAGS.epochs

    for epoch in range(num_epochs):
        for i, (real_chars, _) in enumerate(trainloader):
            # -------------------
            # Train Discriminator
            # -------------------

            # Zero the gradients for the generator
            optimizer_disc.zero_grad()

            # Create random dense style vector
            style_vector = torch.rand([FLAGS.batch, FLAGS.styledim],
                                      device=FLAGS.device)

            # Create random one-hot character vector
            char_vector = torch.zeros(
                [FLAGS.batch, len(CHARACTERS)], device=FLAGS.device)
            onehot = np.random.randint(
                low=0, high=len(CHARACTERS), size=FLAGS.batch)
            char_vector[range(FLAGS.batch), onehot] = 1

            # Convert real characters and generate a batch of fake characters
            real_chars = real_chars.float().to(FLAGS.device)
            fake_chars = gen(char_vector, style_vector)

            real_validity = disc(real_chars)  # Real characters
            fake_validity = disc(fake_chars)  # Fake characters

            # Gradient penalty. Calculate glyph by glyph to avoid OOM
            all_gradients = []
            for real_char, fake_char in zip(real_chars, fake_chars):
                real_char = real_char.view(1, -1, 2)
                real_char_raster = rasterize(real_char, device=FLAGS.device)
                real_char_raster = real_char_raster.unsqueeze(1)

                points = torch.cat(
                    [curve for contour in fake_char for curve in contour])
                points = points.unsqueeze(0)
                fake_char_raster = rasterize(points, device=FLAGS.device)
                fake_char_raster = fake_char_raster.unsqueeze(1)

                gradients = compute_gradients(disc, real_char_raster, fake_char_raster)
                all_gradients.append(gradients)

            gradient_penalty = ((torch.cat(all_gradients, dim=0).norm(dim=1) - 1)**2).mean()

            # Discriminator loss
            loss_disc = -torch.mean(real_validity) + torch.mean(
                fake_validity) + LAMBDA_GP * gradient_penalty

            # Update discriminator with clipped gradients
            loss_disc.backward()
            nn.utils.clip_grad_norm_(
                disc.parameters(), FLAGS.clip, norm_type=2)
            optimizer_disc.step()

            # Print statistics and delete loss
            print('[%d, %5d] loss Discriminator: %.3f' % (epoch + 1, i + 1,
                                                          loss_disc.item()))
            del loss_disc

            # ---------------
            # Train Generator
            # ---------------

            # Zero the gradients for the generator
            optimizer_gen.zero_grad()

            # Generate a batch of fake characters
            fake_chars = gen(char_vector, style_vector)

            # Generator loss
            fake_validity = disc(fake_chars)
            loss_gen = -torch.mean(fake_validity)

            # Update generator with clipped gradients
            loss_gen.backward()
            nn.utils.clip_grad_norm_(gen.parameters(), FLAGS.clip, norm_type=2)
            optimizer_gen.step()

            # Print statistics and delete loss
            print('[%d, %5d] loss Generator: %.3f' % (epoch + 1, i + 1,
                                                      loss_gen.item()))
            del loss_gen

            # --------------------
            # Infer From Generator
            # --------------------
            if i % 2000 == 1999:  # Infer every 2000 backprops.
                name_end = save_model(  # save_model returns filename
                    epoch,
                    epoch,
                    gen,
                    disc,
                    optimizer_gen,
                    optimizer_disc,
                    FLAGS.gen,
                    FLAGS.disc,
                    path='../output/checkpoints/')
                gen.eval()
                infer(
                    gen,
                    num_fonts=FLAGS.numfonts,
                    path='../output/fonts/font{}'.format(name_end),
                    style_dim=FLAGS.styledim,
                    resolution=FLAGS.resolution,
                    device=FLAGS.device)
                gen.train()

    print('Finished.')
示例#5
0
lakes_path = "/sciclone/aiddata10/REU/raw/natural_earth/ne_10m_lakes/ne_10m_lakes.shp"

rivers_path = "/sciclone/aiddata10/REU/raw/natural_earth/ne_10m_rivers_lake_centerlines/ne_10m_rivers_lake_centerlines.shp"

pixel_size = 0.01

xmin = -180
xmax = 180
ymin = -90
ymax = 90

affine = Affine(pixel_size, 0, xmin, 0, -pixel_size, ymax)

shape = (int((ymax - ymin) / pixel_size), int((xmax - xmin) / pixel_size))

shorelines, _ = rasterize(path=shorelines_path, affine=affine, shape=shape)
shorelines = np.logical_not(shorelines).astype(int)

lakes, _ = rasterize(path=lakes_path, affine=affine, shape=shape)
rivers, _ = rasterize(path=rivers_path, affine=affine, shape=shape)

water = shorelines + lakes + rivers

water_output_raster_path = "/sciclone/aiddata10/REU/data/rasters/external/global/distance_to/water/water_binary.tif"

export_raster(water, affine, water_output_raster_path)

# -----------------------------------------------------------------------------

# import rasterio
# water_src = rasterio.open(water_output_raster_path)
示例#6
0
# shp_path = "data/line_test/big_line.shp"
# out_name = "big_line"

# shp_path = "data/ca_riv_15s/ca_riv_15s.shp"
# out_name = "ca_riv_15s"

# -----------------------------------------------------------------------------

pixel_size = 0.01

rasterized_features_path = "{0}/data/{1}_binary_raster.tif".format(
    base, out_name)

rv_array, affine = rasterize(path=shp_path,
                             pixel_size=pixel_size,
                             output=rasterized_features_path)

# export_raster(rv_array, affine, rasterized_features_path)

# print rv_array
print rv_array.shape
print affine

# -----------------------------------------------------------------------------

distance_raster_path = "{0}/data/{1}_distance_raster.tif".format(
    base, out_name)


def raster_conditional(rarray):
xmin = -180
xmax = 180
ymin = -90
ymax = 90

affine = Affine(pixel_size, 0, xmin, 0, -pixel_size, ymax)

shape = (int((ymax - ymin) / pixel_size), int((xmax - xmin) / pixel_size))

roads = np.zeros(shape, dtype='byte')

for n in names:
    path = "{0}/{1}".format("/sciclone/aiddata10/REU/raw/groads",
                            "groads-v1-{0}-shp/gROADS-v1-{0}.shp".format(n))
    rv_array, _ = rasterize(path=path, affine=affine, shape=shape)
    roads = roads | rv_array

roads_output_raster_path = "/sciclone/aiddata10/REU/data/rasters/external/global/distance_to/roads/roads_binary.tif"

export_raster(roads, affine, roads_output_raster_path)

# -----------------------------------------------------------------------------

# import rasterio
# roads_src = rasterio.open(roads_output_raster_path)
# roads = roads_src.read()[0]
# affine = roads_src.affine

distance_output_raster_path = "/sciclone/aiddata10/REU/data/rasters/external/global/distance_to/roads/roads_distance.tif"