ValueError: cannot reshape array of size 300 into shape (100,100,3)

ValueError: cannot reshape array of size 300 into shape (100,100,3)

我正在努力重塑自己的形象。维度为 (100,100,3)。所有图像的总数组组成 (3267, 100, 3)

def get_batch(batch_size,s="train"):
    """Create batch of n pairs, half same class, half different class"""
    if s == 'train':
        X = Xtrain
        X= X.reshape(-1,100,100,3)
        #X= X.reshape(-1,20,105,105)
        categories = train_classes
    else:
        X = Xval
        X= X.reshape(-1,100,100,3)
        categories = val_classes
    n_classes, n_examples, w, h, chan = X.shape
    print(n_classes)
    print(type(n_classes))
    print(n_classes.shape)
    # randomly sample several classes to use in the batch
    categories = rng.choice(n_classes,size=(batch_size,),replace=False)
    
    # initialize 2 empty arrays for the input image batch
    pairs=[np.zeros((batch_size, h, w,1)) for i in range(2)]
    
    # initialize vector for the targets
    targets=np.zeros((batch_size,))
    
    # make one half of it '1's, so 2nd half of batch has same class
    targets[batch_size//2:] = 1
    for i in range(batch_size):
        category = categories[i]
        idx_1 = rng.randint(0, n_examples)
        pairs[0][i,:,:,:] = X[category, idx_1].reshape(w, h, chan)
        idx_2 = rng.randint(0, n_examples)
        
        # pick images of same class for 1st half, different for 2nd
        if i >= batch_size // 2:
            category_2 = category  
        else: 
            # add a random number to the category modulo n classes to ensure 2nd image has a different category
            category_2 = (category + rng.randint(1,n_classes)) % n_classes
        
        pairs[1][i,:,:,:] = X[category_2,idx_2].reshape(w, h,1)
    
    return pairs, targets

然而,当尝试重塑数组时 pairs[0][i,:,:,:] = X[category, idx_1].reshape(w, h, chan) 我总是得到一个错误,即 300 的数组大小不能重塑为 (100,100,3)。老实说,我不明白为什么会这样…… 有人可以帮我吗?

您想要将 300 的数组放入 100,100,3。这不可能是因为 (100*100*3)=3000030000 not equal to 300 只有当输出形状具有与输入相同数量的值时才能重塑。

我建议你应该做 (10,10,3) 因为 (10*10*3)=300