python - ValueError:无法将大小为 300 的数组重塑为形状 (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)=30000
,30000 not equal to 300
您才能重新整形。
我建议你应该这样做(10,10,3)
,因为(10*10*3)=300
推荐阅读
- c# - 从 CSV 文件中删除指定的文本
- html - 在表格中选择具有特定班级的第一个孩子并将其隐藏
- python - 使用 Django 从另一个 CBV 获取上下文
- java - 请提供您在框架中布局和调整“JPanel”大小的方法
- amazon-web-services - 在 DynamoDB 中保存新行,然后列出所有具有最终读取一致性的项目?
- google-chrome - HTTP 204 导致在 Chrome 中下载
- java - java.util.NoSuchElementException 在使用 java 中的 Apache poi 3.8 在 Excel 文件上写入图像时
- javascript - 403 禁止网站资源(js、png、css)但权限正常?
- python - 如果第 3 列与第 2 列匹配,python 比较 2 列并使用第 1 列中的值写入第 4 列
- javascript - Angular:TypeScript 表中的搜索栏