图像识别

2024-03-29 07:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用卷积神经网络(CNN)对30种不同的水果进行图像检测。 我目前拥有的数据集由“train”和“test”文件夹组成,每个文件夹都有30个子目录,用于30个不同的类。你知道吗

“train”文件夹共有671个jpg文件,“test”文件夹共有300个jpg文件。你知道吗

我为实现图像检测而编写的Python代码如下-

from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Dropout
from keras.layers.convolutional import MaxPooling2D
from keras import backend as K
from sklearn.metrics import accuracy_score, precision_score, recall_score


# Read in images from 'train' folder-
train_datagen = ImageDataGenerator(
    rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True
)

train_generator = train_datagen.flow_from_directory(
    directory=r"./train/", target_size=(420, 420), color_mode="rgb",
    batch_size=30, class_mode="categorical", shuffle=True
    )
# O/P-
# Found 671 images belonging to 30 classes.


# Read in images from 'test' folder-
test_datagen = ImageDataGenerator(
    rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True
)

valid_generator = test_datagen.flow_from_directory(
    directory=r"./test/", target_size=(420, 420), color_mode="rgb", batch_size=30,
    class_mode="categorical", shuffle=True
    )
# O/P-
# Found 300 images belonging to 30 classes.




# Dimensions of our image(s)-
img_width, img_height = 420, 420

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

print("\ninput_shape = {0}\n\n".format(input_shape))
# input_shape = (420, 420, 3)




# Build the CNN-

model = Sequential()

# model.add(Conv2D(32, (5, 5), input_shape = (32, 32, 3), activation = 'relu'))
model.add(Conv2D(32, (3, 3), input_shape = input_shape, activation = 'relu'))
# model.add(Conv2D(32, (3, 3), activation = 'relu'))
# model.add(Dense(40, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.2))
# model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
# model.add(Dense(512, activation = 'relu'))
model.add(Dense(128, activation = 'relu'))
model.add(Dense(30, activation = 'softmax'))


# Compiling the model-
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics = ['accuracy'])


STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size

model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, epochs=5)

当我试图执行此代码时,我得到以下消息-

使用TensorFlow后端。 找到了30个班级的671张照片。 找到了30个班级的300张照片。你知道吗

输入形状=(420,420,3)

Epoch 1/5 2019-02-12 14:48:18.088495: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-02-12 14:48:23.270184: W tensorflow/core/framework/allocator.cc:122] Allocation of 670940160 exceeds 10% of system memory. 2019-02-12 14:48:31.747262: W tensorflow/core/framework/allocator.cc:122] Allocation of 670940160 exceeds 10% of system memory.

在此之后,我的系统挂起,我必须重新启动系统。这种情况已经发生过4次了。我的系统有Intel Core i5@2.2 GHz和8 GB RAM。你知道吗

怎么了?你知道吗


Tags: fromtestimportaddimginputsizemodel