Coder Social home page Coder Social logo

Comments (3)

qTipTip avatar qTipTip commented on August 16, 2024 3

I ended up writing it like this @ethanjyx, in case you are still interested. Been a year or so since I last looked at this, but I think this is what you want. Revisiting research code is always difficult 😄

def process_polygon(polygon, polygon_resolution=28, cropped_image_size=224, max_polygon_length=60):
    """
    We superimpose a grid of size polygon_resolution*polygon_resolution over the cropped image.
    We then one-hot-encode the polygon by creating a polygon_resolution*polygon_resolution array of ints,
    where a 0 tells us that there is no vertex present, and a 1 tells us there is a vertex present.
    We need the output to be of a fixed size, so we make sure that every output is of size
    (max_polygon_length, polygon_resolution**2 + additional_info_if_needed).
    """
    assert cropped_image_size % polygon_resolution == 0

    grid_size = cropped_image_size // polygon_resolution

    # we add two to the length, so we can slice for previous, and previous previous vertices, the first vertex is then
    # stored in one_hot_polygon[2].
    one_hot_polygon = torch.zeros((max_polygon_length + 2, polygon_resolution ** 2 + 1))
    one_hot_ordering = np.zeros((max_polygon_length + 2), dtype=np.int)

    # if `polygon` contains more vertices than max_polygon_length, then we subsample the polygon.
    if len(polygon) > max_polygon_length:
        scale = len(polygon) / max_polygon_length
        subsample_selection = (np.arange(0, max_polygon_length) * scale).astype(np.int)
        polygon = polygon[subsample_selection]

    # Fill in the original polygon vertices.
    for i in range(len(polygon)):
        vertex = polygon[i]
        x, y = vertex[0] // grid_size, vertex[1] // grid_size
        vertex_index = np.ravel_multi_index((x, y), dims=(polygon_resolution, polygon_resolution))
        one_hot_polygon[i + 2, vertex_index] = 1
        one_hot_ordering[i + 2] = vertex_index

    # set end-of-polygon bit
    one_hot_polygon[i + 2, polygon_resolution ** 2] = 1
    one_hot_ordering[i + 2] = polygon_resolution ** 2

    # If the polygon is shorter than max_polygon_length, then we pad with the ground truth indices with -1
    for i in range(len(polygon), max_polygon_length):
        one_hot_ordering[i + 2] = -1

    return one_hot_polygon, torch.as_tensor(one_hot_ordering[2:])

from pytorch-polygon-rnn.

AlexMa011 avatar AlexMa011 commented on August 16, 2024 1
  1. Sorry to reply so late, kind of busy in the last 2 years. The 3 more slots are for two starting vertices and one stop vertices.
  2. The filling vector process has two types:
    a. the actual number of vertices is less than the fixed vector length: the vertices are repeated to avoid lots of zeros.
    b. the actual number of vertices is larger than the fixed vector length: the vertices are proportionally selected to satisfy the fixed length.

from pytorch-polygon-rnn.

ethanjyx avatar ethanjyx commented on August 16, 2024

hi @qTipTip , i'm having the same question as you, hopefully @AlexMa011 can answer here.

Specifically, I have the following questions,

  1. for label_array = np.zeros([self.length, 28 * 28 + 3]), why do we need 28 * 28 + 3? what is the 3 slots for?

what is this kkk doing in this block? https://github.com/AlexMa011/pytorch-polygon-rnn/blob/master/data.py#L45-L57

thanks in advance!

from pytorch-polygon-rnn.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.