import torch
import itertools


def tensors_to_tensor(tensors, shapes):
    """
    Restore N-tensors into one
        args:
            tensors: tensors [nb, ch, row, col]
            shapes: [nb_rows, nb_cols] of original position
        return img
    """
    nb, ch, row, col = tensors.shape
    assert nb == (shapes[0] * shapes[1]), 'Number of tensor should be equals.'
    # set img placeholder
    tmp = torch.zeros(size=(1, ch, row*shapes[0], col*shapes[1]),dtype=torch.float32)
    # merge
    for i, j in itertools.product(range(shapes[0]), range(shapes[1])):
        tmp[0, 
            :,
            i * row:(i+1) * row,
            j * col:(j+1) * col] = tensors[i * shapes[1] + j]
    return tmp