简体   繁体   中英

Image classification using Keras,CNN training very SLOWLY

I tried a binary classification using CNN. Done with the exact same code as explained on https://www.udemy.com/deeplearning/ . But when I run the code on my PC(CPU- 8 GB RAM) , the training executes very slowly with a single item in each epoch ,even though I have given the batch size as 32. However, it runs just as fine on the instructor's computer(even though he too is using CPU). The train set consists of a total of 8000 images and the test set with 2000 images. I know for such large data, the processing will definitely will be slow, but I am noticing it is much slower than usual.

from keras.layers import Dense
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.models import Sequential
classifier=Sequential()

classifier.add(Convolution2D(32, (3, 3 ), input_shape=(64,64,3),activation='relu'))

classifier.add(MaxPooling2D(pool_size=(2 , 2)))

classifier.add(Flatten())

classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dense(units=1, activation='sigmoid'))

classifier.compile(optimizer='adam' , loss='binary_crossentropy' ,metrics=['accuracy'])


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(
        'dataset/training_set',
        target_size=(64, 64), #since 64,64,pixels
        batch_size=32,
        class_mode='binary')



test_set= test_datagen.flow_from_directory(
        'dataset/test_set',
        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)

The flow from directory based image preprocessing is done as explained in the Keras documentation, With Tensorflow as a backend.

Thanks!

Dan is absolutely correct that running on a GPU is going to be a life-saver. I noticed, however, that your "steps_per_epoch" value is 8000, which is equal to the number of images in your training set. This should generally be equal to the number of images divided by the batch size. In your case steps_per_epoch should equal 8000/32 = 250.

Take a look here for a quick example: https://github.com/brandonschabell/AircraftClassification/blob/new-model-brandon/BrandonsModel.py

put all your data in your google drive, the mount it in http://colab.research.google.com . ... and run your code free on GPU! CPU is not the best choice when you work with images...

Although GPU will doubtlessly provide a substantial speed boost for inference and training of DNNs, the possible performance enhancements with the right CPU optimizations are usually largely overlooked. This question is about the plausible reasons behind slow training on CPU (possibly with Keras) .

Install (or compile) the right TensorFlow binary:

  • When installing TF with pip install tensorflow , you are getting a general version that is compatible with as many machine configurations as possible, because pip ( easy_install , virtualenv , etc.) are focused around Python, neglecting non-Python library dependencies, such as HDF5 , MKL , LLVM , etc. Installing the optimized TF build with Intel's MKL-DNN may bring up to " 70x higher performance for training and up to 85x higher performance for inference " ( reference ). Read here on how to build your pip-wheel with MKL support .

  • If you don't want to bother with compiling TF from source yourself, install TF binary with conda , which handles library dependencies outside of the Python packages as well as the Python packages themselves (eg see here for a general discussion of pip vs conda ). See also this article for direct comparison of performance on CPU of Eigen and MKL-optimized version of TF. Example of installing MKL-optimized build: conda install tensorflow-mkl .

  • Furthermore, build TensorFlow from source for maximum performance. MKL(-DNN) is (usually) fast and allows to deploy a prebuilt binary without knowing about the accessible native SIMD vector extensions, but depending on your CPU-ecosystem using the AVX/AVX2/AVX512 extensions might be (even) faster.

Optimize you input pipeline :

  • Finally, a word of caution on Keras. Using Keras as the input pipeline for your DNN training might also become a substantial bottleneck. Keras relies on Python´s multithreading (and optionally multiprocessing ) libraries, which may both lack performance (when doing heavy I/O or augmentations on-the-fly) and scalability (when running on multiple CPUs) compared to GIL-free implementations . Consider performing augmentations offline, or using alternative input pipelines (eg TF native tf.data , or 3rd party ones, like Tensorpack )

At last, if you're just starting off with TensorFlow, don't bother squeezing the very last FLOPs out of your system. It is not that important in the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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