繁体   English   中英

ValueError:无法将大小为 300 的数组重塑为形状 (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 ,如果 output 形状具有与输入相同数量的值,则只能重新整形。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM