facebookresearch / moco

PyTorch implementation of MoCo: https://arxiv.org/abs/1911.05722
MIT License
4.8k stars 792 forks source link

Issue about dequeue_and_enqueue #128

Closed qishibo closed 2 years ago

qishibo commented 2 years ago

Hi, I am a little confused about the code of _dequeue_and_enqueue

https://github.com/facebookresearch/moco/blob/main/moco/builder.py#L53-L66

        # replace the keys at ptr (dequeue and enqueue)
        self.queue[:, ptr:ptr + batch_size] = keys.T
        ptr = (ptr + batch_size) % self.K  # move pointer

this implement will update queue gradually, just as below: image

but as described in your paper, the tensor which will be replaced should be pushed into the queue[left side] and the oldest should be removed[right side], just as fisrt-in-first-out, but the code above just replace red rectangle from left to right, so I'm confused about your code.

Paper:

The samples in the dictionary are progressively replaced. The current mini-batch is enqueued to the dictionary, and the oldest mini-batch in the queue is removed.

qishibo commented 2 years ago

I think code should like this:


self.queue = torch.cat((keys.T, self.queue), 1)
self.queue = self.queue[:,0:self.K]
powermano commented 2 years ago

The self.queue is randomly initialized. We need to fill the queue first, and then update it from left to right, which is equivalent to FIFO

Giles-Billenness commented 2 years ago

The queue code seems to limit the batch size to only the whole divisors of K, why have this limitation? Is it a problem with cut off pieces at the end of the queue (being removed), ie incomplete representations?