cannot reshape array of size 541 into shape (541,580,775,1)问题解决!

一、报错截图:

二、分析原因:

ValueError可知,我们的数据格式可能有问题,这里需要shape一下我们的数据,看一下数据的大小是否一致。

# 将数据集打乱随机分组
x_train, x_test, y_train, y_test = train_test_split(imgs,
                                                    labels,
                                                    test_size=0.4,
                                                    random_state=random.randint(0, 100))
for i in x_train:
    print(i.shape)

结果发现,数据中有些图像的大小不一致。

 

三、挑出数据中不一样的数据,并保存:

# Inspired by: DuckSoft <realducksoft@gmail.com>
# GitHub: https://github.com/DuckSoft

import os
import cv2

DATASET_ROOT = r"F:\datas\BreastCancerData\gray_breast_raw(902)"
DATASET_TARGET_ROOT = r'C:\Users\tianr\Desktop\Experiment _Data\Sampling_Images'
DATASET_TYPES = ['benign', 'malignant']
# cv2读取图像的属性:高、宽
IMAGE_SIZE = (580, 775)
# 将小尺寸的图像(630,500)上采样成统一的尺寸(775,580)
TARGET_SIZE = (775, 580)


for dataset_type in DATASET_TYPES:
    for filename in os.listdir(os.path.join(DATASET_ROOT, dataset_type)):
        path = os.path.join(DATASET_ROOT, dataset_type, filename)
        # print(path)
        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        print(img.shape)
        if img.shape != IMAGE_SIZE:
            cv2.imwrite(os.path.join(DATASET_TARGET_ROOT, dataset_type, filename), cv2.resize(img, TARGET_SIZE))
            print(f"Up-sampling from {img.shape} to {TARGET_SIZE}: {path}")

将挑选出的已统一格式的图像复制到原数据集,更新成功。

四、问题解决,模型成功跑通。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

despacito,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值