Computer Vision News - September 2019

Data augmentation with Keras 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 from keras.models import Model from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense import numpy as np import matplotlib.pyplot as plt from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img import matplotlib.pyplot as plt import tensorflow as tf from keras.applications.vgg16 import VGG16 vgg = VGG16(include_top = False , weights = 'imagenet' , input_shape = ( 64 , 64 , 3 )) for layer in vgg . layers: layer . trainable = False output = vgg . layers[ - 1 ] . output output = Flatten()(output) output = Dense( 256 , activation = 'relu' )(output) output = Dropout( 0.5 )(output) output = Dense( 1 ,activation = 'sigmoid' )(output) vgg_model = Model(vgg . input, output) vgg_model . output model = vgg_model Batch_size = 16 datagen = ImageDataGenerator(rescale = 1. / 255 , rotation_range = 30 , zoom_range = 0.15 , width_shift_range = 0.2 , height_shift_range = 0.2 , shear_range = 0.15 , horizontal_flip = True , fill_mode = "nearest" ) # load and iterate training dataset train_it = datagen . flow_from_directory( 'Data/Train/' , batch_size = Batch_size, target_size = ( 64 , 64 ), class_mode = 'binary' ) # load and iterate validation dataset val_it = datagen . flow_from_directory( 'Data/Validation/' , batch_size = Batch_size, target_size = ( 64 , 64 ), class_mode = 'binary' ) model . compile(loss = 'binary_crossentropy' , optimizer = 'rmsprop' , metrics = [ 'accuracy' ]) history = model . fit_generator( train_it, steps_per_epoch = 100 , epochs = 50 , validation_data = val_it, validation_steps = 100 ) plt . plot(history . history[ 'acc' ]) plt . plot(history . history[ 'val_acc' ]) plt . title( 'model accuracy' ) plt . ylabel( 'accuracy' ) plt . xlabel( 'epoch' ) plt . legend([ 'train' , 'test' ], loc = 'upper left' ) plt . show() plt . plot(history . history[ 'loss' ]) plt . plot(history . history[ 'val_loss' ]) plt . title( 'model loss' ) plt . ylabel( 'loss' ) plt . xlabel( 'epoch' ) plt . legend([ 'train' , 'test' ], loc = 'upper left' ) plt . show()

RkJQdWJsaXNoZXIy NTc3NzU=