Exemplo n.º 1
0
def categories_route():
    """
    List of all categories
    """
    return render_template('categories.html',
                           page={'title': 'Categories'},
                           user=user_info(),
                           content={'categories': get_categories()})
Exemplo n.º 2
0
def index_route():
    """
    Home sweet home, this is page where our journey begins
    """
    return render_template('index.html',
                           page={
                               'title': 'Homepage',
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={'categories': get_categories()})
Exemplo n.º 3
0
def profile_route():
    """
    Originaly I planned to make it big and coolm with API key to update
    and delete stuff, with nice API reference and so on.
    Then I understood that this is overkill,
    so this page is very simple and just shows user's picture.
    """
    user = user_info()

    if not user_is_authorized():
        return redirect(url_for('auth.login_route'))

    return render_template('profile.html',
                           page={'title': user['name'] + ' profile'},
                           user=user,
                           content={'categories': get_categories()})
def item_route(item_id):
    """
    Route that outputs item info
    """
    target_item = get_item(item_id)

    if target_item is None:
        abort(404)

    return render_template('item.html',
                           page={
                               'title': 'Item ' + target_item.name,
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={
                               'categories': get_categories(),
                               'item': target_item
                           })
Exemplo n.º 5
0
def category_route(category_id):
    """
    Outputing category info
    """
    target_category = get_category(category_id)

    # ooops category not found
    if target_category is None:
        abort(404)

    return render_template('category.html',
                           page={
                               'title': 'Category ' + target_category.name,
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={
                               'categories': get_categories(),
                               'category': target_category
                           })
Exemplo n.º 6
0
    def prepare_epoch(self, opts):
        # Shuflle indexes
        if opts.shuffle:
            self.indexes = np.random.choice(range(self.nimages),
                                            self.nimages,
                                            replace=False)
        else:
            self.indexes = range(self.nimages)
        # If the number of images is not a multiple of the number of batches, then we add random images to
        # the last batch:
        empty_slots = self.n_images_per_epoch - self.nimages
        if empty_slots > 0:
            if opts.shuffle:
                self.indexes = np.concatenate(
                    (self.indexes,
                     np.random.choice(range(self.nimages),
                                      empty_slots,
                                      replace=False)))
            else:
                self.indexes = np.concatenate(
                    (self.indexes, range(self.nimages)[0:empty_slots]))
        # If told to provide at least one instance of each class in every batch, do so:
        if opts.all_classes_in_batch:
            # We put the shuffled indexes into unused_indexes, and free self.indexes (then we'll fill it again
            # in another order, making sure every batch has at least one instance of each class).
            unused_indexes = self.indexes.tolist()
            n_unused = len(unused_indexes)
            self.indexes = []
            idx_selected = -1
            idx_unused = -1
            # Flags to detect on what batches we have failed finding all the categories:
            batches_failed = np.zeros(self.n_batches_per_epoch, dtype=bool)
            # Flags to detect on what categories we have failed finding them for the batch:
            flags_failed = np.zeros((self.n_batches_per_epoch, 26), dtype=bool)
            # Loop over all batches:
            for batch_id in range(self.n_batches_per_epoch):
                count_in_batch = 0
                # Flags to detect what categories have already been used in the batch:
                used_categories = np.zeros(26, dtype=bool)
                for cat_idx in range(26):
                    attemps = n_unused
                    while not used_categories[cat_idx] and not flags_failed[
                            batch_id, cat_idx]:
                        idx_unused = idx_unused + 1
                        # If we go over the number of unused images, restart from the beginning:
                        if idx_unused >= n_unused:
                            idx_unused = 0
                        # Get the categories of this image:
                        cat_one_hot = tools.get_categories(
                            self.annotations[unused_indexes[idx_unused]])
                        if cat_one_hot[cat_idx]:
                            # If it has the category we are looking for, we add it.
                            count_in_batch = count_in_batch + 1
                            if count_in_batch > opts.batch_size:
                                # We should never run into this.
                                tools.error('batch overflow.')
                            idx_selected = idx_selected + 1
                            self.indexes.append(unused_indexes[idx_unused])
                            # Remove from unused list:
                            del unused_indexes[idx_unused]
                            n_unused = n_unused - 1
                            # If the length of unused_indexes is lower now than idx_unused, restart this index:
                            if idx_unused >= n_unused:
                                idx_unused = 0
                            # Check if we ran out of unused images:
                            if n_unused < 0:
                                tools.error(
                                    'Number of unused images lower than 0.')
                            # Check if other categories are also included:
                            for idx3 in range(26):
                                if cat_one_hot[idx3]:
                                    used_categories[idx3] = True
                        # Check if we have run over all the unused images, and yet failed finding this
                        # category:
                        attemps = attemps - 1
                        if attemps == 0:
                            print(
                                'Failed finding images for category %i on batch %i'
                                % (cat_idx, batch_id))
                            flags_failed[batch_id, cat_idx] = True
                # Once here, we have already put an instance of each class in the batch, or failed with this.
                # We fill the rest of entries with random images:
                while count_in_batch < opts.batch_size:
                    count_in_batch = count_in_batch + 1
                    if count_in_batch > opts.batch_size:
                        # We should never run into this.
                        tools.error('batch overflow.')
                    idx_selected = idx_selected + 1
                    self.indexes.append(unused_indexes[idx_unused])
                    # Remove from unused list:
                    del unused_indexes[idx_unused]
                    n_unused = n_unused - 1
                    # If the length of unused_indexes is lower now than idx_unused, restart this index:
                    if idx_unused >= n_unused:
                        idx_unused = 0
                    # Check if we ran out of unused images:
                    if n_unused < 0:
                        tools.error('Number of unused images lower than 0.')
                # Check if we have failed finding any category:
                if np.any(flags_failed[batch_id]):
                    batches_failed[batch_id] = True
            # Convert to numpy array:
            self.indexes = np.asarray(self.indexes, dtype=np.int32)
            # Print on screen how many batches were successful and how many not:
            print('%i batches have instances of all classes' %
                  np.sum(batches_failed == False))
            print('%i batches do not have instances of all classes' %
                  np.sum(batches_failed))

            # Check if the indexes are consistent:
            idx_image = -1
            for batch_id in range(self.n_batches_per_epoch):
                used_categories = np.zeros(26, dtype=bool)
                for _ in range(opts.batch_size):
                    idx_image = idx_image + 1
                    cat_one_hot = tools.get_categories(
                        self.annotations[idx_image])
                    for cat_idx in range(26):
                        if cat_one_hot[cat_idx]:
                            used_categories[cat_idx] = True
                for cat_idx in range(26):
                    if not flags_failed[batch_id, cat_idx]:
                        # If we are here, for this batch we found an instance of this category.
                        if not used_categories[cat_idx]:
                            tools.error('Indexes incoherence.')
                    else:
                        # If we are here, for this batch we did not find an instance of this category.
                        if used_categories[cat_idx]:
                            tools.error('Indexes incoherence.')
Exemplo n.º 7
0
def categories_api():
    """
    Lists categories
    """
    plain_list = [e.serialize() for e in get_categories()]
    return jsonify(plain_list)