SqueezeNet-Deep-Compression icon indicating copy to clipboard operation
SqueezeNet-Deep-Compression copied to clipboard

Odd non-zero number problem

Open AdrianAlcolea opened this issue 8 years ago • 5 comments

we can't access "net.params[layer][index]" directly, it is neccesary to use iter or either a loop

AdrianAlcolea avatar Nov 09 '17 15:11 AdrianAlcolea

image How to solve this problem ? thank you.

MrLinNing avatar Mar 27 '18 08:03 MrLinNing

The problem arises when there is an odd "total non-zero number":

(1020 - 1) / 2 + 1 # ind_stream size 510 np.arange(0, 1020, 2).shape (510,) np.arange(1, 1020, 2).shape (510,) (3073 - 1) / 2 + 1 # ind_stream size 1537 np.arange(0, 3073, 2).shape (1537,) np.arange(1, 3073, 2).shape (1536,)

A possible solution may be:

def binary_to_net(weights, spm_stream, ind_stream, codebook, num_nz):
    bits = np.log2(codebook.size)
    if bits == 4:
        slots = 2
    elif bits == 8:
        slots = 1
    else:
        print "Not impemented,", bits
        sys.exit()
    code = np.zeros(weights.size, np.uint8) 

    # Recover from binary stream
    spm = np.zeros(num_nz, np.uint8)
    ind = np.zeros(num_nz if num_nz % 2 == 0 else num_nz + 1, np.uint8)
    if slots == 2:
        spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
        spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
    else:
        spm = spm_stream
    ind[np.arange(0, num_nz, 2)] = ind_stream % (2**4)
    ind[np.arange(1, num_nz if num_nz % 2 == 0 else num_nz + 1, 2)] = ind_stream / (2**4)


    # Recover the matrix
    ind = np.cumsum(ind+1)-1
    if num_nz % 2 == 1:
        ind = ind[:-1]
    code[ind] = spm
    data = np.reshape(codebook[code], weights.shape)
    np.copyto(weights, data)

AdrianAlcolea avatar Mar 27 '18 11:03 AdrianAlcolea

maybe change line 99 to: if num_nz%2!=0: ind[np.arange(1, num_nz, 2)] = (ind_stream/ (2**4))[:-1] else: ind[np.arange(1, num_nz, 2)] = (ind_stream/ (2**4))

reinaldomaslim avatar May 07 '18 08:05 reinaldomaslim

Yes, yours is the correct solution. And it is also needed on line 95 "spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)". Thankyou

AdrianAlcolea avatar Sep 06 '18 10:09 AdrianAlcolea

Hello again. In the first solution I just needed it to run, but I did not take care about actual values. Now I really need the values, I realized it was not a good solution. Here is the good one:

if slots == 2:
    spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
    if num_nz % 2 == 0:
        spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
    else:
        spm[np.arange(1, num_nz, 2)] = (spm_stream / (2**4))[:-1]
else:
    spm = spm_stream

ind[np.arange(0, num_nz, 2)] = ind_stream % (2**4)
if num_nz % 2 == 0:
    ind[np.arange(1, num_nz, 2)] = ind_stream / (2**4)
else:
    ind[np.arange(1, num_nz, 2)] = (ind_stream / (2**4))[:-1]

Just change that and leave the rest as it was in the original one (the size of ind and spm should alwais be num_nz, what we really need to do is to ignore the last odd value generated when num_nz is odd)

AdrianAlcolea avatar Oct 08 '18 13:10 AdrianAlcolea