def train(self):
    self.log_object.log(self.file_object, 'Entered in to train method in Training class.Training started')
    try:
        x_train = []
        for folder in os.listdir(self.train_path):
            sub_path = ((self.train_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_train.append(img_arr)
        x_test = []
        for folder in os.listdir(self.test_path):
            sub_path = ((self.test_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_test.append(img_arr)
        x_val = []
        for folder in os.listdir(self.val_path):
            sub_path = ((self.val_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_val.append(img_arr)
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.train,test,val split successfull')
        train_x = (np.array(x_train) / 255.0)
        test_x = (np.array(x_test) / 255.0)
        val_x = (np.array(x_val) / 255.0)
        train_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        test_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        val_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        training_set = train_datagen.flow_from_directory(self.train_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        test_set = test_datagen.flow_from_directory(self.test_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        val_set = val_datagen.flow_from_directory(self.val_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        train_y = training_set.classes
        test_y = test_set.classes
        val_y = val_set.classes
        IMAGE_SIZE = [224, 224]
        vgg = VGG19(input_shape=(IMAGE_SIZE + [3]), weights='imagenet', include_top=False)
        self.log_object.log(self.file_object, 'Entered in to train method in Training class. Model successfully initialized')
        for layer in vgg.layers:
            layer.trainable = False
        x = Flatten()(vgg.output)
        prediction = Dense(5, activation='softmax')(x)
        model = Model(inputs=vgg.input, outputs=prediction)
        model.summary()
        model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.Model compile successfull')
        file_path = 'vgg19_model/checkpoint-{epoch:02d}-{val_accuracy:.2f}.hdf5'
        self.log_object.log(self.file_object, 'check point directory created')
        check_point = ModelCheckpoint(file_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
        start = datetime.now()
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.Training start time {start}')
        history = model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=20, callbacks=[check_point], batch_size=64, shuffle=True)
        duration = (datetime.now() - start)
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.Total time taken is {duration}')
        model.save('mech_tools_model.h5')
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.model saved successfully')
        plt.plot(history.history['accuracy'], label='train acc')
        plt.plot(history.history['val_accuracy'], label='val acc')
        plt.legend()
        plt.savefig('vgg-acc-rps-1.png')
        plt.plot(history.history['loss'], label='train loss')
        plt.plot(history.history['val_loss'], label='val loss')
        plt.legend()
        plt.savefig('vgg-loss-rps-1.png')
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.model evaluation started')
        model.evaluate(test_x, test_y, batch_size=32)
        y_pred = model.predict(test_x)
        y_pred = np.argmax(y_pred, axis=1)
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.classification report {classification_report(y_pred, test_y)}')
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.confusion matrix is{confusion_matrix(y_pred, test_y)}')
    except Exception as e:
        self.log_object.log(self.file_object, 'Unsuccessful End of Training')
        self.log_object.log(self.file_object, f'exception occured.exception is {e}')
        raise Exception
    self.file_object.close()
def train(self):
    self.log_object.log(self.file_object, 'Entered in to train method in Training class.Training started')
    try:
        x_train = []
        for folder in os.listdir(self.train_path):
            sub_path = ((self.train_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_train.append(img_arr)
        x_test = []
        for folder in os.listdir(self.test_path):
            sub_path = ((self.test_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_test.append(img_arr)
        x_val = []
        for folder in os.listdir(self.val_path):
            sub_path = ((self.val_path + '/') + folder)
            for img in os.listdir(sub_path):
                image_path = ((sub_path + '/') + img)
                img_arr = cv2.imread(image_path)
                if (img_arr is None):
                    os.remove(image_path)
                    continue
                elif (img_arr.shape[0] < 224):
                    os.remove(image_path)
                    continue
                else:
                    img_arr = cv2.resize(img_arr, (224, 224))
                    x_val.append(img_arr)
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.train,test,val split successfull')
        train_x = (np.array(x_train) / 255.0)
        test_x = (np.array(x_test) / 255.0)
        val_x = (np.array(x_val) / 255.0)
        train_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        test_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        val_datagen = ImageDataGenerator(rescale=(1.0 / 255))
        training_set = train_datagen.flow_from_directory(self.train_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        test_set = test_datagen.flow_from_directory(self.test_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        val_set = val_datagen.flow_from_directory(self.val_path, target_size=(224, 224), batch_size=32, class_mode='sparse')
        train_y = training_set.classes
        test_y = test_set.classes
        val_y = val_set.classes
        IMAGE_SIZE = [224, 224]
        vgg = VGG19(input_shape=(IMAGE_SIZE + [3]), weights='imagenet', include_top=False)
        self.log_object.log(self.file_object, 'Entered in to train method in Training class. Model successfully initialized')
        for layer in vgg.layers:
            layer.trainable = False
        x = Flatten()(vgg.output)
        prediction = Dense(5, activation='softmax')(x)
        model = Model(inputs=vgg.input, outputs=prediction)
        model.summary()
        model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.Model compile successfull')
        file_path = 'vgg19_model/checkpoint-{epoch:02d}-{val_accuracy:.2f}.hdf5'
        self.log_object.log(self.file_object, 'check point directory created')
        check_point = ModelCheckpoint(file_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
        start = datetime.now()
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.Training start time {start}')
        history = model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=20, callbacks=[check_point], batch_size=64, shuffle=True)
        duration = (datetime.now() - start)
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.Total time taken is {duration}')
        model.save('mech_tools_model.h5')
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.model saved successfully')
        plt.plot(history.history['accuracy'], label='train acc')
        plt.plot(history.history['val_accuracy'], label='val acc')
        plt.legend()
        plt.savefig('vgg-acc-rps-1.png')
        plt.plot(history.history['loss'], label='train loss')
        plt.plot(history.history['val_loss'], label='val loss')
        plt.legend()
        plt.savefig('vgg-loss-rps-1.png')
        self.log_object.log(self.file_object, 'Entered in to train method in Training class.model evaluation started')
        model.evaluate(test_x, test_y, batch_size=32)
        y_pred = model.predict(test_x)
        y_pred = np.argmax(y_pred, axis=1)
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.classification report {classification_report(y_pred, test_y)}')
        self.log_object.log(self.file_object, f'Entered in to train method in Training class.confusion matrix is{confusion_matrix(y_pred, test_y)}')
    except Exception as e:
        self.log_object.log(self.file_object, 'Unsuccessful End of Training')
        self.log_object.log(self.file_object, f'exception occured.exception is {e}')
        raise Exception
    self.file_object.close()