示例#1
0
    def evaluate(self, dataset_path):
        pil_images, image_classes, num_samples, num_classes = load_image_classification_dataset(dataset_path, 'RGB')
        images = self._trans_image(pil_images)
        # images = np.pad(images, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
        X_test, y_test = images, to_categorical(image_classes, num_classes=self._num_classes)

        # Compute test accuracy
        (test_loss, test_acc) = self._model.evaluate(X_test, y_test)
        return test_loss, test_acc
示例#2
0
    def train(self, dataset_path, **train_args):
        batch_size = train_args.get('batch_size')
        max_epochs = train_args.get('max_epochs')
        learning_rate = float(train_args['learning_rate'])

        pil_images, image_classes, num_samples, num_classes = load_image_classification_dataset(
            dataset_path, 'RGB')
        self._num_classes = num_classes
        images = self._trans_image(pil_images)
        # pil_images = [x.resize([self._image_size, self._image_size]) for x in pil_images]

        # # Convert to numpy arrays
        # images = [np.asarray(x) for x in pil_images]
        # images = np.asarray(images) / 255

        # images = self._prepare_X(images)
        train = {}
        train['images'] = images
        train['classes'] = image_classes
        validation = {}
        train['images'], validation['images'], train['classes'], validation[
            'classes'] = train_test_split(train['images'],
                                          train['classes'],
                                          test_size=0.2,
                                          random_state=0)
        # train['images'] = np.pad(train['images'],
        #                          ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
        # validation['images'] = np.pad(validation['images'],
        #                               ((0, 0), (2, 2), (2, 2), (0, 0)),
        #                               'constant')

        X_train, y_train = train['images'], to_categorical(
            train['classes'], num_classes=num_classes)
        X_validation, y_validation = validation['images'], to_categorical(
            validation['classes'], num_classes=self._num_classes)

        train_generator = ImageDataGenerator().flow(X_train,
                                                    y_train,
                                                    batch_size=batch_size)
        validation_generator = ImageDataGenerator().flow(X_validation,
                                                         y_validation,
                                                         batch_size=batch_size)
        steps_per_epoch = X_train.shape[0] // batch_size
        validation_steps = X_validation.shape[0] // batch_size

        tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
        if not self._model:
            self._model = self._build_classifier(float(learning_rate))
        logger.log(logging.INFO, 'Begin loading train dataset')
        self._model.fit_generator(train_generator,
                                  steps_per_epoch=steps_per_epoch,
                                  epochs=max_epochs,
                                  validation_data=validation_generator,
                                  validation_steps=validation_steps,
                                  shuffle=True,
                                  callbacks=[tensorboard])

        # Compute train accuracy
        (train_loss, train_acc) = self._model.evaluate(X_validation,
                                                       y_validation)
        logger.info('Train loss: {}'.format(train_loss))
        logger.info('Train accuracy: {}'.format(train_acc))