我是深度学习和 TensorFlow/Keras 的新手,所以我无法理解为什么我在尝试拟合模型以将图像分类为“狗”或“猫”时抛出错误。第一个代码块涉及创建和保存模型(使用 pickle),第二个代码块是训练实际卷积网络的部分。
下载图像数据库,保存到文件目录,编写模型训练分类器。代码如下:
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
DATADIR = "Pictures\\kagglecatsanddogs_3367a\\PetImages"
#Workspace directory changed for posting
CATEGORIES = ["Dog", "Cat"]
#Iterate between all photos of dogs and cats
for category in CATEGORIES:
path = os.path.join(DATADIR, category) #path to cats or dogs dir
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) #Converts to grayscale, does not need color in this specific instance)
plt.imshow(img_array, cmap = "gray")
break
break
#Print image dimensions
print(img_array.shape)
#All the images are different-shaped photos, so they must be normalized
#Everything must be made the same shape
#Decide on the image size you want to go with
IMG_SIZE = 180
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data = []
def create_training_data(): #With goal of iterating through everything and building the dataset
for category in CATEGORIES:
path = os.path.join(DATADIR, category) #path to cats or dogs dir
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) #Converts to grayscale, does not need color in this specific instance)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
pass
青春有我
翻翻过去那场雪
繁花不似锦
芜湖不芜
相关分类