به نام خدا

افزایش داده ها (data augmentationata)

کدها برگرفته از فصل دو کتاب

Deep Learning with Python

و گیت هاب نویسنده کتاب و توسعه دهنده کراس

François Chollet

است.
In [1]:
import os
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
Using TensorFlow backend.
In [2]:
# The directory where we will
# store our smaller dataset
base_dir = 'D:/dataset/catDog/catVsdog'

# Directories for our training,
# validation and test splits
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

# Directory with our validation cat pictures
test_cats_dir = os.path.join(test_dir, 'cats')

# Directory with our validation dog pictures
test_dogs_dir = os.path.join(test_dir, 'dogs')

برای بررسی صحت انجام کار تعداد تصاویرآموزشی/آزمون/توسعه را بررسی میکنیم.

In [3]:
print('total training cat images:', len(os.listdir(train_cats_dir)))
print('total training dog images:', len(os.listdir(train_dogs_dir)))
print('total validation cat images:', len(os.listdir(validation_cats_dir)))
print('total validation dog images:', len(os.listdir(validation_dogs_dir)))
print('total test cat images:', len(os.listdir(test_cats_dir)))
print('total test dog images:', len(os.listdir(test_dogs_dir)))
total training cat images: 1000
total training dog images: 1000
total validation cat images: 500
total validation dog images: 500
total test cat images: 500
total test dog images: 500
In [3]:
datagen = ImageDataGenerator(
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

These are just a few of the options available (for more, see the Keras documentation). Let's quickly go over what we just wrote:

  • rotation_range is a value in degrees (0-180), a range within which to randomly rotate pictures.
  • width_shift and height_shift are ranges (as a fraction of total width or height) within which to randomly translate pictures vertically or horizontally.
  • shear_range is for randomly applying shearing transformations.
  • zoom_range is for randomly zooming inside pictures.
  • horizontal_flip is for randomly flipping half of the images horizontally -- relevant when there are no assumptions of horizontal asymmetry (e.g. real-world pictures).
  • fill_mode is the strategy used for filling in newly created pixels, which can appear after a rotation or a width/height shift.

Let's take a look at our augmented images:

برای درک بهتر نگاهی به تصاویر augment شده می اندازیم:

</div>

In [10]:
# This is module with image preprocessing utilities
from keras.preprocessing import image

fnames = [os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)]

# We pick one image to "augment"
img_path = fnames[9]

# Read the image and resize it
img = image.load_img(img_path, target_size=(150, 150))

# Convert it to a Numpy array with shape (150, 150, 3)
x = image.img_to_array(img)

# Reshape it to (1, 150, 150, 3)
x = x.reshape((1,) + x.shape)

# The .flow() command below generates batches of randomly transformed images.
# It will loop indefinitely, so we need to `break` the loop at some point!
i = 0
for batch in datagen.flow(x, batch_size=1):
    plt.figure(i)
    imgplot = plt.imshow(image.array_to_img(batch[0]))
    i += 1
    if i % 4 == 0:
        break

plt.show()

If we train a new network using this data augmentation configuration, our network will never see twice the same input. However, the inputs that it sees are still heavily intercorrelated, since they come from a small number of original images -- we cannot produce new information, we can only remix existing information. As such, this might not be quite enough to completely get rid of overfitting.

In [11]:
from keras import layers
from keras import models
from keras import optimizers

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=1e-4),
              metrics=['acc'])
In [10]:
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,)

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        # This is the target directory
        train_dir,
        # All images will be resized to 150x150
        target_size=(150, 150),
        batch_size=32,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

history = model.fit_generator(
      train_generator,
      steps_per_epoch=100,
      epochs=100,
      validation_data=validation_generator,
      validation_steps=50)
Found 2000 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.
Epoch 1/100
100/100 [==============================] - 26s 261ms/step - loss: 0.6956 - acc: 0.5063 - val_loss: 0.6861 - val_acc: 0.6085
Epoch 2/100
100/100 [==============================] - 24s 240ms/step - loss: 0.6837 - acc: 0.5525 - val_loss: 0.6568 - val_acc: 0.6034
Epoch 3/100
100/100 [==============================] - 24s 242ms/step - loss: 0.6704 - acc: 0.5913 - val_loss: 0.6503 - val_acc: 0.6187
Epoch 4/100
100/100 [==============================] - 24s 241ms/step - loss: 0.6553 - acc: 0.6109 - val_loss: 0.6512 - val_acc: 0.6148
Epoch 5/100
100/100 [==============================] - 24s 237ms/step - loss: 0.6413 - acc: 0.6303 - val_loss: 0.6052 - val_acc: 0.6713
Epoch 6/100
100/100 [==============================] - 26s 256ms/step - loss: 0.6174 - acc: 0.6603 - val_loss: 0.6332 - val_acc: 0.6396
Epoch 7/100
100/100 [==============================] - 24s 238ms/step - loss: 0.6099 - acc: 0.6694 - val_loss: 0.5860 - val_acc: 0.6840
Epoch 8/100
100/100 [==============================] - 23s 235ms/step - loss: 0.6067 - acc: 0.6644 - val_loss: 0.5608 - val_acc: 0.7043
Epoch 9/100
100/100 [==============================] - 24s 236ms/step - loss: 0.5834 - acc: 0.6956 - val_loss: 0.5619 - val_acc: 0.7126
Epoch 10/100
100/100 [==============================] - 24s 243ms/step - loss: 0.5773 - acc: 0.6956 - val_loss: 0.5450 - val_acc: 0.7202
Epoch 11/100
100/100 [==============================] - 25s 246ms/step - loss: 0.5774 - acc: 0.7003 - val_loss: 0.5608 - val_acc: 0.7088
Epoch 12/100
100/100 [==============================] - 24s 241ms/step - loss: 0.5662 - acc: 0.7006 - val_loss: 0.5453 - val_acc: 0.7297
Epoch 13/100
100/100 [==============================] - 24s 242ms/step - loss: 0.5591 - acc: 0.7012 - val_loss: 0.5296 - val_acc: 0.7354
Epoch 14/100
100/100 [==============================] - 24s 244ms/step - loss: 0.5508 - acc: 0.7100 - val_loss: 0.5434 - val_acc: 0.7335
Epoch 15/100
100/100 [==============================] - 24s 238ms/step - loss: 0.5608 - acc: 0.7097 - val_loss: 0.5770 - val_acc: 0.6935
Epoch 16/100
100/100 [==============================] - 24s 236ms/step - loss: 0.5307 - acc: 0.7303 - val_loss: 0.5593 - val_acc: 0.7119
Epoch 17/100
100/100 [==============================] - 24s 239ms/step - loss: 0.5362 - acc: 0.7247 - val_loss: 0.5292 - val_acc: 0.7240
Epoch 18/100
100/100 [==============================] - 26s 263ms/step - loss: 0.5305 - acc: 0.7294 - val_loss: 0.5225 - val_acc: 0.7373
Epoch 19/100
100/100 [==============================] - 24s 243ms/step - loss: 0.5352 - acc: 0.7306 - val_loss: 0.4940 - val_acc: 0.7570
Epoch 20/100
100/100 [==============================] - 24s 240ms/step - loss: 0.5259 - acc: 0.7359 - val_loss: 0.4924 - val_acc: 0.7500
Epoch 21/100
100/100 [==============================] - 24s 244ms/step - loss: 0.5159 - acc: 0.7350 - val_loss: 0.5044 - val_acc: 0.7443
Epoch 22/100
100/100 [==============================] - 24s 241ms/step - loss: 0.5022 - acc: 0.7566 - val_loss: 0.5162 - val_acc: 0.7341
Epoch 23/100
100/100 [==============================] - 25s 255ms/step - loss: 0.5177 - acc: 0.7372 - val_loss: 0.5109 - val_acc: 0.7437
Epoch 24/100
100/100 [==============================] - 24s 241ms/step - loss: 0.5031 - acc: 0.7437 - val_loss: 0.5093 - val_acc: 0.7456
Epoch 25/100
100/100 [==============================] - 24s 235ms/step - loss: 0.4903 - acc: 0.7631 - val_loss: 0.5001 - val_acc: 0.7411
Epoch 26/100
100/100 [==============================] - 24s 238ms/step - loss: 0.4923 - acc: 0.7581 - val_loss: 0.4832 - val_acc: 0.7563
Epoch 27/100
100/100 [==============================] - 24s 243ms/step - loss: 0.4969 - acc: 0.7578 - val_loss: 0.4734 - val_acc: 0.7544
Epoch 28/100
100/100 [==============================] - 24s 244ms/step - loss: 0.4777 - acc: 0.7741 - val_loss: 0.4935 - val_acc: 0.7487
Epoch 29/100
100/100 [==============================] - 24s 242ms/step - loss: 0.4970 - acc: 0.7581 - val_loss: 0.5283 - val_acc: 0.7164
Epoch 30/100
100/100 [==============================] - 24s 241ms/step - loss: 0.4797 - acc: 0.7775 - val_loss: 0.5174 - val_acc: 0.7443
Epoch 31/100
100/100 [==============================] - 24s 241ms/step - loss: 0.4823 - acc: 0.7662 - val_loss: 0.4725 - val_acc: 0.7703
Epoch 32/100
100/100 [==============================] - 23s 235ms/step - loss: 0.4772 - acc: 0.7759 - val_loss: 0.4759 - val_acc: 0.7646
Epoch 33/100
100/100 [==============================] - 24s 241ms/step - loss: 0.4775 - acc: 0.7641 - val_loss: 0.4625 - val_acc: 0.7716
Epoch 34/100
100/100 [==============================] - 24s 239ms/step - loss: 0.4595 - acc: 0.7791 - val_loss: 0.4946 - val_acc: 0.7652
Epoch 35/100
100/100 [==============================] - 26s 257ms/step - loss: 0.4695 - acc: 0.7784 - val_loss: 0.4604 - val_acc: 0.7754
Epoch 36/100
100/100 [==============================] - 24s 237ms/step - loss: 0.4581 - acc: 0.7759 - val_loss: 0.4648 - val_acc: 0.7747
Epoch 37/100
100/100 [==============================] - 24s 240ms/step - loss: 0.4658 - acc: 0.7759 - val_loss: 0.5514 - val_acc: 0.7183
Epoch 38/100
100/100 [==============================] - 24s 238ms/step - loss: 0.4595 - acc: 0.7809 - val_loss: 0.4571 - val_acc: 0.7716
Epoch 39/100
100/100 [==============================] - 24s 239ms/step - loss: 0.4583 - acc: 0.7841 - val_loss: 0.4494 - val_acc: 0.7938
Epoch 40/100
100/100 [==============================] - 25s 252ms/step - loss: 0.4522 - acc: 0.7800 - val_loss: 0.4583 - val_acc: 0.7843
Epoch 41/100
100/100 [==============================] - 25s 245ms/step - loss: 0.4450 - acc: 0.7903 - val_loss: 0.4482 - val_acc: 0.7893
Epoch 42/100
100/100 [==============================] - 24s 236ms/step - loss: 0.4503 - acc: 0.7887 - val_loss: 0.4892 - val_acc: 0.7659
Epoch 43/100
100/100 [==============================] - 24s 241ms/step - loss: 0.4383 - acc: 0.7934 - val_loss: 0.4261 - val_acc: 0.8008
Epoch 44/100
100/100 [==============================] - 24s 237ms/step - loss: 0.4498 - acc: 0.7891 - val_loss: 0.4275 - val_acc: 0.7963
Epoch 45/100
100/100 [==============================] - 24s 242ms/step - loss: 0.4383 - acc: 0.7953 - val_loss: 0.5126 - val_acc: 0.7621
Epoch 46/100
100/100 [==============================] - 24s 239ms/step - loss: 0.4200 - acc: 0.8056 - val_loss: 0.4312 - val_acc: 0.7951
Epoch 47/100
100/100 [==============================] - 23s 231ms/step - loss: 0.4393 - acc: 0.7959 - val_loss: 0.4752 - val_acc: 0.7919
Epoch 48/100
100/100 [==============================] - 24s 240ms/step - loss: 0.4235 - acc: 0.7978 - val_loss: 0.4366 - val_acc: 0.7855
Epoch 49/100
100/100 [==============================] - 24s 238ms/step - loss: 0.4229 - acc: 0.7947 - val_loss: 0.4600 - val_acc: 0.7747
Epoch 50/100
100/100 [==============================] - 24s 238ms/step - loss: 0.4205 - acc: 0.8100 - val_loss: 0.4544 - val_acc: 0.7938
Epoch 51/100
100/100 [==============================] - 24s 238ms/step - loss: 0.4158 - acc: 0.8119 - val_loss: 0.5194 - val_acc: 0.7532
Epoch 52/100
100/100 [==============================] - 26s 255ms/step - loss: 0.4112 - acc: 0.8187 - val_loss: 0.4695 - val_acc: 0.8008
Epoch 53/100
100/100 [==============================] - 24s 235ms/step - loss: 0.4215 - acc: 0.8119 - val_loss: 0.4226 - val_acc: 0.8077
Epoch 54/100
100/100 [==============================] - 24s 237ms/step - loss: 0.4139 - acc: 0.8087 - val_loss: 0.4635 - val_acc: 0.8020
Epoch 55/100
100/100 [==============================] - 24s 239ms/step - loss: 0.4211 - acc: 0.8034 - val_loss: 0.5894 - val_acc: 0.7246
Epoch 56/100
100/100 [==============================] - 24s 237ms/step - loss: 0.4180 - acc: 0.8069 - val_loss: 0.5548 - val_acc: 0.7602
Epoch 57/100
100/100 [==============================] - 24s 244ms/step - loss: 0.4127 - acc: 0.8035 - val_loss: 0.4298 - val_acc: 0.7963
Epoch 58/100
100/100 [==============================] - 24s 243ms/step - loss: 0.4062 - acc: 0.8100 - val_loss: 0.4800 - val_acc: 0.7938
Epoch 59/100
100/100 [==============================] - 24s 235ms/step - loss: 0.4060 - acc: 0.8103 - val_loss: 0.4208 - val_acc: 0.8103
Epoch 60/100
100/100 [==============================] - 24s 240ms/step - loss: 0.3973 - acc: 0.8184 - val_loss: 0.4811 - val_acc: 0.7976
Epoch 61/100
100/100 [==============================] - 24s 241ms/step - loss: 0.4086 - acc: 0.8128 - val_loss: 0.5112 - val_acc: 0.7627
Epoch 62/100
100/100 [==============================] - 25s 246ms/step - loss: 0.3882 - acc: 0.8216 - val_loss: 0.4306 - val_acc: 0.8230
Epoch 63/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3952 - acc: 0.8222 - val_loss: 0.4172 - val_acc: 0.8192
Epoch 64/100
100/100 [==============================] - 24s 239ms/step - loss: 0.3951 - acc: 0.8262 - val_loss: 0.4121 - val_acc: 0.8230
Epoch 65/100
100/100 [==============================] - 24s 245ms/step - loss: 0.3866 - acc: 0.8322 - val_loss: 0.4309 - val_acc: 0.8141
Epoch 66/100
100/100 [==============================] - 24s 237ms/step - loss: 0.3889 - acc: 0.8266 - val_loss: 0.5182 - val_acc: 0.7487
Epoch 67/100
100/100 [==============================] - 24s 243ms/step - loss: 0.3864 - acc: 0.8241 - val_loss: 0.4499 - val_acc: 0.7868
Epoch 68/100
100/100 [==============================] - 24s 237ms/step - loss: 0.3808 - acc: 0.8284 - val_loss: 0.4648 - val_acc: 0.7855
Epoch 69/100
100/100 [==============================] - 26s 258ms/step - loss: 0.3962 - acc: 0.8156 - val_loss: 0.4179 - val_acc: 0.8249
Epoch 70/100
100/100 [==============================] - 24s 240ms/step - loss: 0.3899 - acc: 0.8231 - val_loss: 0.4338 - val_acc: 0.8008
Epoch 71/100
100/100 [==============================] - 24s 242ms/step - loss: 0.3877 - acc: 0.8269 - val_loss: 0.4642 - val_acc: 0.7811
Epoch 72/100
100/100 [==============================] - 24s 235ms/step - loss: 0.3713 - acc: 0.8312 - val_loss: 0.4076 - val_acc: 0.8319
Epoch 73/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3652 - acc: 0.8472 - val_loss: 0.4146 - val_acc: 0.8084
Epoch 74/100
100/100 [==============================] - 25s 253ms/step - loss: 0.3742 - acc: 0.8303 - val_loss: 0.4684 - val_acc: 0.8077
Epoch 75/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3742 - acc: 0.8313 - val_loss: 0.3998 - val_acc: 0.8261
Epoch 76/100
100/100 [==============================] - 24s 239ms/step - loss: 0.3692 - acc: 0.8362 - val_loss: 0.4253 - val_acc: 0.8147
Epoch 77/100
100/100 [==============================] - 24s 243ms/step - loss: 0.3692 - acc: 0.8306 - val_loss: 0.4016 - val_acc: 0.8185
Epoch 78/100
100/100 [==============================] - 24s 241ms/step - loss: 0.3638 - acc: 0.8328 - val_loss: 0.4466 - val_acc: 0.7849
Epoch 79/100
100/100 [==============================] - 25s 250ms/step - loss: 0.3832 - acc: 0.8319 - val_loss: 0.3934 - val_acc: 0.8242
Epoch 80/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3608 - acc: 0.8406 - val_loss: 0.4071 - val_acc: 0.8223
Epoch 81/100
100/100 [==============================] - 27s 265ms/step - loss: 0.3611 - acc: 0.8381 - val_loss: 0.4209 - val_acc: 0.8084
Epoch 82/100
100/100 [==============================] - 23s 232ms/step - loss: 0.3636 - acc: 0.8372 - val_loss: 0.4146 - val_acc: 0.8179
Epoch 83/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3471 - acc: 0.8466 - val_loss: 0.5172 - val_acc: 0.7970
Epoch 84/100
100/100 [==============================] - 24s 241ms/step - loss: 0.3600 - acc: 0.8369 - val_loss: 0.4031 - val_acc: 0.8299
Epoch 85/100
100/100 [==============================] - 23s 233ms/step - loss: 0.3477 - acc: 0.8447 - val_loss: 0.3953 - val_acc: 0.8255
Epoch 86/100
100/100 [==============================] - 25s 254ms/step - loss: 0.3440 - acc: 0.8466 - val_loss: 0.4556 - val_acc: 0.8192
Epoch 87/100
100/100 [==============================] - 24s 239ms/step - loss: 0.3598 - acc: 0.8384 - val_loss: 0.3960 - val_acc: 0.8293
Epoch 88/100
100/100 [==============================] - 24s 237ms/step - loss: 0.3292 - acc: 0.8534 - val_loss: 0.4451 - val_acc: 0.7944
Epoch 89/100
100/100 [==============================] - 24s 241ms/step - loss: 0.3610 - acc: 0.8450 - val_loss: 0.4191 - val_acc: 0.7976
Epoch 90/100
100/100 [==============================] - 24s 239ms/step - loss: 0.3549 - acc: 0.8403 - val_loss: 0.4418 - val_acc: 0.7995
Epoch 91/100
100/100 [==============================] - 24s 242ms/step - loss: 0.3545 - acc: 0.8422 - val_loss: 0.4233 - val_acc: 0.8052
Epoch 92/100
100/100 [==============================] - 24s 243ms/step - loss: 0.3365 - acc: 0.8528 - val_loss: 0.4575 - val_acc: 0.7989
Epoch 93/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3537 - acc: 0.8412 - val_loss: 0.4040 - val_acc: 0.8179
Epoch 94/100
100/100 [==============================] - 24s 242ms/step - loss: 0.3399 - acc: 0.8503 - val_loss: 0.4291 - val_acc: 0.8287
Epoch 95/100
100/100 [==============================] - 23s 235ms/step - loss: 0.3328 - acc: 0.8544 - val_loss: 0.4589 - val_acc: 0.8274
Epoch 96/100
100/100 [==============================] - 24s 239ms/step - loss: 0.3332 - acc: 0.8559 - val_loss: 0.4174 - val_acc: 0.8280
Epoch 97/100
100/100 [==============================] - 24s 236ms/step - loss: 0.3309 - acc: 0.8656 - val_loss: 0.4109 - val_acc: 0.8192
Epoch 98/100
100/100 [==============================] - 26s 262ms/step - loss: 0.3366 - acc: 0.8506 - val_loss: 0.3842 - val_acc: 0.8338
Epoch 99/100
100/100 [==============================] - 24s 241ms/step - loss: 0.3197 - acc: 0.8628 - val_loss: 0.3947 - val_acc: 0.8420
Epoch 100/100
100/100 [==============================] - 24s 243ms/step - loss: 0.3215 - acc: 0.8597 - val_loss: 0.3635 - val_acc: 0.8452
In [11]:
model.save('cats_and_dogs_small_2.h5')

رسم نمودار

</div>

In [12]:
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()
دوره مقدماتی یادگیری عمیق
علیرضا اخوان پور
پنج شنبه، ۲۵ بهمن ۱۳۹۷
Class.Vision - AkhavanPour.ir - GitHub
In [ ]: