/keras-secure-image

Add-on library for Keras to train on encrypted images for humans

Primary LanguagePythonMIT LicenseMIT

Keras Secure Image

Keras Secure Image logo

Description

This is an add-on library for Keras. The main functionality is to encrypt the images in the dataset so they are secure. You can write your own generator and call the decrypt functionality at runtime. The decrypted images are not stored, they are stored in tuple by the generator ensuring the safety of your images.

Installation

pip install keras_secure_image

Usage

Encrypting the images

from keras_secure_image import encrypt_directory
encrypt_directory(src_dir="/path/to/src",
                                   dest_dir="/path/to/dest", image_x=100, image_y=100,
                                   password="<PASSWORD>)
												  
Original Image

Encrypted Image

Training on encrypted images

from keras_secure_image import decrypt_img
def generator_from_encrypted_data(path_to_features, labels, batch_size):
 batch_features = np.zeros((batch_size, 64, 64, 3))
 batch_labels = np.zeros((batch_size,1))
 while True:
   for i in range(batch_size):
     # choose random index in path_to_features
     index= random.choice(len(path_to_features),1)
     img = decrypt_img(path_to_img=path_to_features[index], password="<PASSWORD>", image_x=100, image_y=100)
     batch_features[i] = img
     batch_labels[i] = labels[index]
   yield batch_features, batch_labels
   

Note : Check the line

img = decrypt_data(path_to_img=path_to_features[index], password="<PASSWORD>", image_x=100, image_y=100)

This decrypt_data function takes the path to the image and decrypts it. Make sure that the <PASSWORD> is the same for encryption.

Calling the fit_generator in Keras

model.fit_generator(generator_from_encrypted_data(path_to_features,labels, 32),
                    samples_per_epoch=20, nb_epoch=10,
                    validation_data=generator_from_encrypted_data(features,labels, 16),
                    validation_steps=5, callbacks=callbacks_list, shuffle=True,use_multiprocessing=True)