Example #1
0
    def save_bottleneck_features(self, train_data_dir, val_data_dir,
                                 batch_size):
        train_generator = util.get_generator(train_data_dir, self.img_width,
                                             self.img_height, batch_size)
        val_generator = util.get_generator(val_data_dir, self.img_width,
                                           self.img_height, batch_size)

        model = self.get_base_model()
        util.save_model_plot(self.base_model_plot_path, model)

        train_bottleneck_features = model.predict_generator(
            train_generator,
            len(train_generator.filenames) // batch_size)
        util.save_bottleneck_features(self.train_bottleneck_features_path,
                                      train_bottleneck_features)

        val_bottleneck_features = model.predict_generator(
            val_generator,
            len(val_generator.filenames) // batch_size)
        util.save_bottleneck_features(self.val_bottleneck_features_path,
                                      val_bottleneck_features)
Example #2
0
 def get_wrong_predictions(self,
                           directory,
                           batch_size=defaults['batch_size']):
     validation_data = util.load_bottleneck_features(
         self.val_bottleneck_features_path)
     val_generator = util.get_generator(directory, self.img_width,
                                        self.img_height, batch_size)
     train_labels = val_generator.classes
     top_model = load_model(self.model_path)
     return np.nonzero(
         top_model.predict_classes(validation_data).reshape((
             -1, )) != train_labels)
Example #3
0
    def train_top_model(self,
                        train_data_dir,
                        val_data_dir,
                        epochs=defaults['epochs'],
                        batch_size=defaults['batch_size']):
        train_generator = util.get_generator(train_data_dir, self.img_width,
                                             self.img_height, batch_size)
        val_generator = util.get_generator(val_data_dir, self.img_width,
                                           self.img_height, batch_size)

        train_data = util.load_bottleneck_features(
            self.train_bottleneck_features_path)
        val_data = util.load_bottleneck_features(
            self.val_bottleneck_features_path)

        num_classes = train_generator.num_classes

        train_labels = train_generator.classes
        train_labels = to_categorical(train_labels, num_classes=num_classes)
        val_labels = val_generator.classes
        val_labels = to_categorical(val_labels, num_classes=num_classes)

        model = self.get_top_model(train_data.shape[1:], num_classes)
        model.compile(optimizer='rmsprop',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
        util.save_model_plot(self.top_model_plot_path, model)

        history = model.fit(train_data,
                            train_labels,
                            epochs=epochs,
                            batch_size=batch_size,
                            validation_data=(val_data, val_labels))
        model.save(self.model_path)
        util.save_history(self.history_path, history)
        util.eval_model_loss_acc(model, val_data, val_labels, batch_size)
Example #4
0
    def get_wrong_predictions(self,
                              directory,
                              batch_size=defaults['batch_size']):
        generator = util.get_generator(directory, self.img_width,
                                       self.img_height, batch_size)
        true_labels = generator.classes
        model = self.get_trained_model()
        predictions = model.predict_generator(generator)
        pred_labels = np.argmax(predictions, axis=-1)

        wrong_pred = []
        for true_label, pred_label, index in zip(true_labels, pred_labels,
                                                 range(0, len(true_labels))):
            if true_label != pred_label:
                wrong_pred.append((index, true_label, pred_label))
        return wrong_pred
Example #5
0
    def evaluate(self, data_dir, batch_size=defaults['batch_size']):
        test_generator = util.get_generator(data_dir, self.img_width, self.img_height, batch_size)

        base_model = self.get_base_model()
        test_bottleneck_features = base_model.predict_generator(
            test_generator, len(test_generator.filenames) // batch_size)

        num_classes = test_generator.num_classes

        test_labels = test_generator.classes
        test_labels = to_categorical(test_labels, num_classes=num_classes)

        top_model = load_model(self.model_path)
        test_loss, test_acc = top_model.evaluate(test_bottleneck_features, test_labels, batch_size=batch_size)

        print('Test accuracy: ', test_acc)
        print('Test loss: ', test_loss)