【问题标题】:How much time a image classification algo can take?图像分类算法需要多长时间?
【发布时间】:2018-07-28 12:49:56
【问题描述】:

我已经编写了以下用于识别图像的代码。

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('E:\\ML_R&D\\training_set\\cats1',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('E:\\ML_R&D\\test_set\\cats1',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 25,
validation_data = test_set,
validation_steps = 2000)
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('E:\\ML_R&D\\cat.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
else:
    prediction = 'cat'

当我运行此代码时,它运行成功且没有任何错误,但在等待数小时 (2) 后未显示任何结果。它只显示在下面。

找到属于 0 个类别的 0 个图像。 找到属于 0 个类别的 0 个图像。 纪元 1/25

【问题讨论】:

  • 生成器找到 0 个图像和 0 个类,这是个问题。
  • 我在指定位置有大约200张图片,位置和图片都可以访问。

标签: python-3.x tensorflow machine-learning keras anaconda


【解决方案1】:

问题在于您提供的目录结构。

training_set = train_datagen.flow_from_directory('E:\\ML_R&D\\training_set\\cats1',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')

这里的路径E:\\ML_R&D\\training_set\\cats1 必须包含子文件夹(代表每个类),在每个子文件夹中,您都有属于该特定类的图像。

例如

/home/tlokeshkumar/Documents/image_data 是我的数据集所在的位置。

image_data
    class_1
        class_1_1.jpg
        class_1_2.jpg
        ...
    class_2
        class_2_1.jpg
        class_2_2.jpg
        class_2_3.jpg
        ...
    class_3
    class_4
    ...

如果遵循这种结构,则必须输入主文件夹的路径(image_data)。

training_set = train_datagen.flow_from_directory('home/tlokeshkumar/Documents/image_data',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')

对于Fast image classification,您可以查看我的存储库,在那里我使用 keras 编写了一个图像分类器,该分类器使用瓶颈来训练它比正常训练过程快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多