简体   繁体   English

使用TensorFlow和Keras进行图像分类

[英]Image Classification with TensorFlow and Keras

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K


# dimensions of our images.
img_width, img_height = 150, 150


train_data_dir = 'flowers/train'
validation_data_dir = 'flowers/validation'
nb_train_samples = 2500
nb_validation_samples = 1000
epochs = 20
batch_size = 50


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


model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])


# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')


validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')


model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)


model.save_weights('first_flowers_try.h5')

We trained this model for classify 5 image classes. 我们训练了这个模型来分类5个图像类。 We used 500 images for each class for train the model and 200 images for each class to validate the model. 我们为每个类使用500个图像来训练模型,并为每个类使用200个图像来验证模型。 We used keras in tensorflow backend.It uses data that can be downloaded at: https://www.kaggle.com/alxmamaev/flowers-recognition 我们在tensorflow后端使用了keras。它使用的数据可以在以下网址下载: https ://www.kaggle.com/alxmamaev/flowers-recognition

In our setup, we: 在我们的设置中,我们:

  • created a data/ folder 创建了一个数据/文件夹
  • created train/ and validation/ subfolders inside data/ 在数据中创建了train /和validation /子文件夹/
  • created daisy/, dandelion/, rose/ ,sunflower/ and tulip/ subfolders inside train/ and validation/ 在火车里面创建雏菊/,蒲公英/,玫瑰/,向日葵/和郁金香/子文件夹/验证/
  • put the 500 images in each data/train/daisy, dandelion, rose, sunflower and tulip 将500张图像放在每个数据/火车/雏菊,蒲公英,玫瑰,向日葵和郁金香中
  • put the 200 images in each data/validation/daisy, dandelion, rose, sunflower and tulip So that we have 500 training examples for each class, and 200 validation examples for each class. 将200个图像放在每个数据/验证/菊花,蒲公英,玫瑰,向日葵和郁金香中。因此,我们每个类有500个训练样例,每个类有200个验证示例。

How can we predict/ test and identify another image using this trained model? 我们如何使用这种训练模型预测/测试和识别另一个图像?

You have to model.load_weights() from the file you saved them to. 您必须从保存它们的文件中model.load_weights() Then you get a sample image you need a prediction for and call model.predict( [sample_image] ) and use the result returned as a prediction. 然后,您将获得需要预测的示例图像并调用model.predict( [sample_image] )并使用返回的结果作为预测。

Construct your model as you did upon training 像训练一样构建模型

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

Load model' weights from disk 从磁盘加载模型的权重

model.load_weights('first_flowers_try.h5')

Load new image. 加载新图片。 Because we are only using one image we have to expand dims - add another dimension. 因为我们只使用一个图像,所以我们必须扩展dims - 添加另一个维度。

from keras.preprocessing import image

img_path = 'path_to_your_new_image'
#img = image.load_img(img_path, target_size=(224, 224)) # if a you want a spesific image size
img = image.load_img(img_path)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x*1./255 #rescale as training

Make Prediction 做出预测

prediction = model.predict(x) #Vector with the prob of each class

As per keras' documentation , you will have to use predict(self, x, batch_size=None, verbose=0, steps=None) . 根据keras的文档 ,您将不得不使用predict(self, x, batch_size=None, verbose=0, steps=None) Since you use the Softmax as an activation function in your final layer, this will return the probability of each class. 由于您在最后一层使用Softmax作为激活函数,因此将返回每个类的概率。 If you just want the most probable class, you will have to take the one with the highest probability: 如果您只想要最可能的课程,则必须选择概率最高的课程:

class_list = [class1, class2, class3, class4, class5] #A list of your classes
model.load_weights('first_flowers_try.h5') #Loads the saved weights
predicted_vector = model.predict(path_to_your_new_image) #Vector with the prob of each class
print(class_list[np.argmax(predicted_vector)) #Prints ellement with highest prob

Now, about getting class_list, you can try this: 现在,关于获取class_list,你可以试试这个:

import os
class_list = os.listdir('train')
class_list = sorted(class_list)

Let me know if this worked. 如果有效,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM