Keras用于创建CNN – 数组大小加倍训练图像数量

huangapple go评论62阅读模式
英文:

Keras for creating CNN - Size of array double number of training images

问题

我正在创建一个卷积神经网络,用于确定扫描的图像是否是猫还是狗。我有一个包含猫和狗图像的文件夹。猫的图像文件名以"cat."开头,而狗的图像文件名以"dog."开头;例如,猫的图像可能是"cat.403.jpg"。总共大约有20,000张图像。但是,当我运行下面的代码时,我的数组存储了大约40,000个值,几乎是原来数量的两倍。我不明白发生了什么。以下是代码:

DATADIR = "C:/Users/me/Jupyter Codes/dogs-vs-cats/train/train"
CATEGORIES = ['dog', 'cat']

IMG_SIZE = 50 
training_data = []
cat_img_array = []
dog_img_array = []

def create_training_data():
    for category in CATEGORIES:
        train_path = os.path.join(DATADIR)   # path to train folder
        class_num = CATEGORIES.index(category)
        for img in os.listdir(train_path):
            animal = img.split('.')[0]
            if animal == 'cat':
                cat_img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)  # convert to grayscale bc RGB > gray; plus not essential
                cv2.resize(cat_img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([cat_img_array, class_num])
                
            elif animal == 'dog':
                dog_img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)
                cv2.resize(dog_img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([dog_img_array, class_num])

create_training_data()
print(len(training_data))

这返回了41900张图像。

然后,当我运行这段代码时:

# 将数据打包到变量中以输入到CNN
X = []
Y = []

for features, label in training_data:
    X.append(features)
    Y.append(label)

X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1) # -1 -> 根据数据量确定数组大小;1 -> 灰度图像 (3 是RGB)
Y = np.array(Y).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

我得到了以下错误消息:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1) # -1 -> 根据数据量确定数组大小;1 -> 灰度图像 (3 是RGB)

ValueError: 无法将大小为41900的数组重新形状为 (50,50,1)

请注意,您的代码中存在一些问题,导致出现错误。要解决这些问题,您需要仔细检查代码并确保正确加载和处理图像数据。如果您需要进一步的帮助,可以提出具体的问题。

英文:

I'm creating a CNN that determines whether an image scanned is a cat or dog. I have one folder with images of both cats and dogs. The cats have "cat." before the image # and dogs have "dog."; for example, an image of a cat may be "cat.403.jpg". In total there are around 20,000-ish images. However, when I run my code below, my array stores 40,000-ish values, nearly double the amount. I don't understand what's going on. Here's the code:

DATADIR = "C:/Users/me/Jupyter Codes/dogs-vs-cats/train/train"
CATEGORIES = ['dog', 'cat']

IMG_SIZE = 50 
training_data = []
cat_img_array = []
dog_img_array = []

def create_training_data():
    for category in CATEGORIES:
        train_path = os.path.join(DATADIR)   # path to train folder
        class_num = CATEGORIES.index(category)
        for img in os.listdir(train_path):
            animal = img.split('.')[0]
            if animal == 'cat':
                cat_img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)  # convert to grayscale bc RGB > gray; plus not essential
                cv2.resize(cat_img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([cat_img_array, class_num])
                
            elif animal == 'dog':
                dog_img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)
                cv2.resize(dog_img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([dog_img_array, class_num])

create_training_data()
print(len(training_data))

This returns 41900 images

Then when I run this code:

# pack data into variables before fed into CNN
X = []
Y = []

for features, label in training_data:
    X.append(features)
    Y.append(label)

    
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1) # -1 -> make array number based on data; 1 -> grayscale (3 if RGB)
Y = np.array(Y).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

I get these error messages:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1) # -1 -> make array number based on data; 1 -> grayscale (3 if RGB)

ValueError: cannot reshape array of size 41900 into shape (50,50,1)

答案1

得分: 1

你正在使用以下代码两次遍历数据集:

for category in CATEGORIES: ## CATEGORIES = ['dog', 'cat']

在这种情况下,你会将所有内容都添加了两次,而且还两次错误标记了数据 - 第一次会将所有内容标记为"dog",然后第二次标记为"cat"。

还要检查以下代码,它不会按预期修改数组,因为它没有原地修改数组:

cv2.resize(cat_img_array, (IMG_SIZE, IMG_SIZE))

如果你只是像下面这样做会怎么样。你还可以摆脱if语句:

CATEGORIES = {'cat': 0, 'dog': 1}
for img in os.listdir(train_path):
    animal = img.split('.')[0]
    img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)  # 转换为灰度图像,因为RGB > 灰度;另外这不是必要的
    resized = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    training_data.append([resized, CATEGORIES[animal]])
英文:

You are iterating through the dataset twice with this line:

for category in CATEGORIES: ## CATEGORIES = ['dog', 'cat']

In this sense, you adding everything twice and are also mislabeling the data twice - the first time through it will mark everything as "dog", then the second time "cat".

Also check this line..it's not doing what you want as it does not modify the array inplace.

cv2.resize(cat_img_array, (IMG_SIZE, IMG_SIZE))

What if you just did something like this. You can also get rid of the if statement.

CATEGORIES = {'cat': 0, 'dog': 1}
for img in os.listdir(train_path):
    animal = img.split('.')[0]
    img_array = cv2.imread(os.path.join(train_path, img), cv2.IMREAD_GRAYSCALE)  # convert to grayscale bc RGB > gray; plus not essential
    resized = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    training_data.append([resized, CATEGORIES[animal]])

huangapple
  • 本文由 发表于 2023年6月26日 10:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定