ValueError:无法将大小为300的数组重塑为形状(100100,3)

2024-04-26 13:10:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力重塑自己的形象。其尺寸为(100100,3)。所有图像的总阵列组成(3267100,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的数组无法重新塑造为(100100,3)。老实说,我不明白为什么它应该是。。。 有人能帮我吗


Tags: oftheforsizebatchclassclassescategories
1条回答
网友
1楼 · 发布于 2024-04-26 13:10:26

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

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

相关问题 更多 >