Exemplo n.º 1
0
    def post(self, bot_id):
        """
        Provides a REST Interface to retrieve the classification of an image for a pretrained model belonging to a bot.
        The bot_id is passed as URL parameter /bot_id.
        The image is mandatory and needs to be part of the http Request Body as file, retrievable with the key 'image'.
        Optionally the number of return_labels can be passed to the Request Body Form with the key 'return_labels'
        :param bot_id: Id of the Bot to identify the correct Model
        :return: HTTP Response with the classification result or an Error
        """
        response = request.get_json(force=True)
        base64_image = response['base64Image']

        return handler.handle_post(bot_id, base64_image)
    def test_train(self):
        msg, status = tl_handler.handle_put(TEST_BOT_ID,
                                            test=True,
                                            max_train_time=100)

        self.assertEqual(200, status, 'http status code is %s' % status)

        bot_model_dir = dirs.get_model_data_dir(TEST_BOT_ID)

        # Check if the bot model dir contains a model now
        self.assertTrue(
            os.listdir(bot_model_dir),
            'bot_model_dir %s is empty after transfer learning' %
            bot_model_dir)
        self.assertTrue(
            os.path.isfile(os.path.join(bot_model_dir, 'checkpoint')),
            'not checkpoints file in bot_model_dir %s after transfer learning'
            % bot_model_dir)

        # Mock a file for classification
        temp_file = tempfile.NamedTemporaryFile()
        temp_file.write(
            base64.b64encode(
                open(os.path.join(FILES_DIR, 'tulip.jpg'), "rb").read()))
        temp_file.seek(0)

        json_result, status = handler.handle_post(TEST_BOT_ID,
                                                  temp_file.read(),
                                                  return_labels=5)
        print(json_result)
        temp_file.close()

        self.assertTrue(json_result, 'Classification result is empty')

        json_result = json.loads(json_result)

        self.assertTrue(json_result['labels'],
                        'No labels in json result %s' % json_result)
        self.assertTrue(json_result['probabilities'],
                        'No predictions in json result %s' % json_result)

        print(json_result)

        # Clean the bot_model directory for next test run
        for file in os.listdir(bot_model_dir):
            file_path = os.path.join(bot_model_dir, file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception as e:
                print(e)
Exemplo n.º 3
0
    def test_handle_post(self):
        if not os.listdir(dirs.get_protobuf_dir(TEST_BOT_ID)):
            shutil.copytree(os.path.join(FILES_DIR, 'flower_protobuf'), dirs.get_protobuf_dir(TEST_BOT_ID))

        if os.path.exists(dirs.get_model_data_dir(TEST_BOT_ID)):
            shutil.rmtree(dirs.get_model_data_dir(TEST_BOT_ID))
        shutil.copytree(os.path.join(FILES_DIR, 'protobuf/bot_test'), dirs.get_model_data_dir(TEST_BOT_ID))

        expected_return_labels = 3

        temp_file = tempfile.NamedTemporaryFile()
        temp_file.write(
            base64.b64encode(
                open(
                    os.path.join(FILES_DIR, 'tulip.jpg'), "rb"
                ).read()
            )
        )
        temp_file.seek(0)

        json_result, status = handler.handle_post(TEST_BOT_ID, temp_file.read(), return_labels=expected_return_labels)

        temp_file.close()

        json_result = json.loads(json_result)
        labels = json_result['labels']
        probs = json_result['probabilities']
        self.assertTrue(labels)
        self.assertTrue(probs)
        self.assertEqual(expected_return_labels, len(labels))
        self.assertEqual(expected_return_labels, len(probs))
        # Clean the bot_model directory for next test run
        for file in os.listdir(dirs.get_model_data_dir(TEST_BOT_ID)):
            file_path = os.path.join(dirs.get_model_data_dir(TEST_BOT_ID), file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception as e:
                print(e)
    def test_transfer_learning(self):
        # Root model to initialize from
        root_model_dir = dirs.get_test_root_model_dir()
        if not os.listdir(root_model_dir):
            print('root_model_dir %s empty. Cannot start test' %
                  root_model_dir)
            return None
        if not os.path.isfile(os.path.join(root_model_dir, 'checkpoint')):
            print('No Checkpoint File in %s. Cannot start test.' %
                  root_model_dir)
            return None

        # Folder to load the additional training data from
        bot_protobuf_dir = dirs.get_protobuf_dir(TEST_BOT_ID)
        if not os.path.isdir(bot_protobuf_dir):
            print('bot_protobuf_dir %s does not exist. Cannot start test' %
                  bot_protobuf_dir)
            return None
        if not os.listdir(bot_protobuf_dir):
            print("bot_protobuf_dir %s is empty. Cannot start test." %
                  bot_protobuf_dir)

        # Bot model folder to write the transfer learned model back to
        bot_model_dir = dirs.get_model_data_dir(TEST_BOT_ID)
        if not os.path.isdir(bot_model_dir):
            print('bot_model_dir %s does not exist. Cannot start test' %
                  bot_model_dir)
            return None
        if os.listdir(bot_model_dir):
            print('bot_model_dir %s is not emtpy. Cannot start test.' %
                  bot_model_dir)
            return None

        # Just run one step to make sure checkpoint files are written appropriately
        transfer_learning.transfer_learning(root_model_dir=root_model_dir,
                                            bot_model_dir=bot_model_dir,
                                            protobuf_dir=bot_protobuf_dir,
                                            max_train_time_sec=100,
                                            log_every_n_steps=2)

        # Check if the root model dir is still intact
        self.assertTrue(
            os.listdir(root_model_dir),
            'root_model_dir %s is empty after transfer learning.' %
            root_model_dir)
        self.assertTrue(
            os.path.isfile(os.path.join(root_model_dir, 'checkpoint')),
            'checkpoints file in root_model_dir %s is gone after transfer learning.'
            % root_model_dir)

        # Check if the bot model dir contains a model now
        self.assertTrue(
            os.listdir(bot_model_dir),
            'bot_model_dir %s is empty after transfer learning' %
            bot_model_dir)
        self.assertTrue(
            os.path.isfile(os.path.join(bot_model_dir, 'checkpoint')),
            'not checkpoints file in bot_model_dir %s after transfer learning'
            % bot_model_dir)

        # Mock a file for classification
        temp_file = tempfile.NamedTemporaryFile()
        temp_file.write(
            base64.b64encode(
                open(os.path.join(FILES_DIR, 'tulip.jpg'), "rb").read()))
        temp_file.seek(0)

        json_result, status = handler.handle_post(TEST_BOT_ID,
                                                  temp_file.read(),
                                                  return_labels=5)
        print(json_result)
        temp_file.close()

        self.assertTrue(json_result, 'Classification result is empty')

        json_result = json.loads(json_result)

        self.assertTrue(json_result['labels'],
                        'No labels in json result %s' % json_result)
        self.assertTrue(json_result['probabilities'],
                        'No predictions in json result %s' % json_result)

        print(json_result)

        # Clean the bot_model directory for next test run
        for file in os.listdir(bot_model_dir):
            file_path = os.path.join(bot_model_dir, file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception as e:
                print(e)