question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

weight sampling example

See original GitHub issue

Hi, i’ve tried to follow your weight sampling example, but i’m stuck with this error:

ValueError: Dimension 0 in both shapes must be equal, but are 3 and 324 for 'Assign_49' (op: 'Assign') with input shapes: [3,3,512,12], [324,512,3,3].

At first i thought it was because i was trying to adapt the example to SDD512, but a clean copy/paste of the original example ends up with same error. It looks like the vector is backwards, but i’m not yet fluent enough in Python/Keras/Tensorflow to actually figure where it might be happening …

I wasn’t using the original ipynb, just a local file, but should be pretty much the same as it was a plain copy/paste…

import h5py
import numpy as np
import shutil

from tensor_sampling_utils import sample_tensors

# TODO: Set the path for the source weights file you want to load.

weights_source_path = 'VGG_coco_SSD_300x300_iter_400000.h5'

# TODO: Set the path and name for the destination weights file
#       that you want to create.

# *** New class number ***
num_classes=2

weights_destination_path = 'VGG_coco_SSD_300x300_iter_400000_subsampled_'+str(num_classes)+'_classes.h5'

# Make a copy of the weights file.
shutil.copy(weights_source_path, weights_destination_path)

# Load both the source weights file and the copy we made.
# We will load the original weights file in read-only mode so that we can't mess up anything.
weights_source_file = h5py.File(weights_source_path, 'r')
weights_destination_file = h5py.File(weights_destination_path)

classifier_names = ['conv4_3_norm_mbox_conf',
                    'fc7_mbox_conf',
                    'conv6_2_mbox_conf',
                    'conv7_2_mbox_conf',
                    'conv8_2_mbox_conf',
                    'conv9_2_mbox_conf']

# *** Output the original model shape ***

conv4_3_norm_mbox_conf_kernel = weights_source_file[classifier_names[0]][classifier_names[0]]['kernel:0']
conv4_3_norm_mbox_conf_bias = weights_source_file[classifier_names[0]][classifier_names[0]]['bias:0']

print("Shape of the '{}' weights:".format(classifier_names[0]))
print()
print("kernel:\t", conv4_3_norm_mbox_conf_kernel.shape)
print("bias:\t", conv4_3_norm_mbox_conf_bias.shape)

# *** Preset paramas for the new model ***

# Number of classes that the model was trained for
n_classes_source = 81
# Number of classes we want to re-train for
# n_classes_dest = 2
# We add 1 because 'backgound' is a class too, 0 to be more precise
# classes_of_interest = list(range(n_classes_dest+1))

# The subset of classes we want to extract from our source model
# 0 SHOULD carried over to the new model.

classes_of_interest=[0,3,8]

subsampling_indices = []
for i in range(int(324/n_classes_source)):
    indices = np.array(classes_of_interest) + i * n_classes_source
    subsampling_indices.append(indices)
subsampling_indices = list(np.concatenate(subsampling_indices))

print(subsampling_indices)

for name in classifier_names:
    # Get the trained weights for this layer from the source HDF5 weights file.
    kernel = weights_source_file[name][name]['kernel:0'].value
    bias = weights_source_file[name][name]['bias:0'].value

    # Get the shape of the kernel. We're interested in sub-sampling
    # the last dimension, 'o'.
    height, width, in_channels, out_channels = kernel.shape
    
    # Compute the indices of the elements we want to sub-sample.
    # Keep in mind that each classification predictor layer predicts multiple
    # bounding boxes for every spatial location, so we want to sub-sample
    # the relevant classes for each of these boxes.
    subsampling_indices = []
    for i in range(int(out_channels/n_classes_source)):
        indices = np.array(classes_of_interest) + i * n_classes_source
        subsampling_indices.append(indices)
    subsampling_indices = list(np.concatenate(subsampling_indices))
    
    # Sub-sample the kernel and bias.
    # The `sample_tensors()` function used below provides extensive
    # documentation, so don't hesitate to read it if you want to know
    # what exactly is going on here.
    new_kernel, new_bias = sample_tensors(weights_list=[kernel, bias],
                                          sampling_instructions=[height, width, in_channels, subsampling_indices],
                                          axes=[[3]], # The one bias dimension corresponds to the last kernel dimension.
                                          init=['gaussian', 'zeros'],
                                          mean=0.0,
                                          stddev=0.005)
    
    # Delete the old weights from the destination file.
    del weights_destination_file[name][name]['kernel:0']
    del weights_destination_file[name][name]['bias:0']
    # Create new datasets for the sub-sampled weights.
    weights_destination_file[name][name].create_dataset(name='kernel:0', data=new_kernel)
    weights_destination_file[name][name].create_dataset(name='bias:0', data=new_bias)

# *** Print the new model shape ***

conv4_3_norm_mbox_conf_kernel = weights_destination_file[classifier_names[0]][classifier_names[0]]['kernel:0']
conv4_3_norm_mbox_conf_bias = weights_destination_file[classifier_names[0]][classifier_names[0]]['bias:0']

print("Shape of the '{}' weights:".format(classifier_names[0]))
print()
print("kernel:\t", conv4_3_norm_mbox_conf_kernel.shape)
print("bias:\t", conv4_3_norm_mbox_conf_bias.shape)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
pierluigiferraricommented, May 6, 2018

@shivam6294 you probably forgot to set the list of classifier layers correctly for SSD512. Note that SSD512 has one more predictor layer than SSD300 (‘conv10_2_mbox_conf’). The list of classifier layers for SSD512 must be set as follows:

classifier_names = ['conv4_3_norm_mbox_conf',
                    'fc7_mbox_conf',
                    'conv6_2_mbox_conf',
                    'conv7_2_mbox_conf',
                    'conv8_2_mbox_conf',
                    'conv9_2_mbox_conf',
                    'conv10_2_mbox_conf']

In hindsight, @yf704475209 probably had the same problem.

0reactions
Alex1101acommented, Jan 16, 2020

@shivam6294 did you solve the Problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Introduction to Sampling Weights - Alteryx Community
Sampling weights, also known as survey weights, are positive values associated with the observations (rows) in your dataset (sample), ...
Read more >
Sample Weights | OECD iLibrary
Weighting consists of acknowledging that some units in the sample are more important than others and have to contribute more than others for...
Read more >
1. How different weighting methods work - Pew Research Center
A key concept in probability-based sampling is that if survey respondents have different probabilities of selection, weighting each case by the ...
Read more >
Sample Weights & Design Effects
What does weighting do? Weights are in place to make sure the sample is representative of the population of interest and that other...
Read more >
The Correct Treatment of Sampling Weights in Statistical Tests
Sampling weights are used to correct for the over-representation or under-representation of key groups in a survey. For example, if 51% of a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found