Exemplo n.º 1
0
    def train(self,
              img_folder,
              ann_folder,
              nb_epoch,
              project_folder,
              batch_size=8,
              jitter=True,
              learning_rate=1e-4,
              train_times=1,
              valid_times=1,
              valid_img_folder="",
              valid_ann_folder="",
              first_trainable_layer=None,
              metrics="mAP",
              validation_freq=1):

        # 1. get annotations
        train_annotations, valid_annotations = get_train_annotations(
            self._labels,
            img_folder,
            ann_folder,
            valid_img_folder,
            valid_ann_folder,
            is_only_detect=False)
        # 1. get batch generator
        valid_batch_size = len(valid_annotations) * valid_times
        if valid_batch_size < batch_size:
            raise ValueError(
                "Not enough validation images: batch size {} is larger than {} validation images. Add more validation images or decrease batch size!"
                .format(batch_size, valid_batch_size))

        train_batch_generator = self._get_batch_generator(train_annotations,
                                                          batch_size,
                                                          train_times,
                                                          jitter=jitter)
        valid_batch_generator = self._get_batch_generator(valid_annotations,
                                                          batch_size,
                                                          valid_times,
                                                          jitter=False)

        # 2. To train model get keras model instance & loss fucntion
        model = self._yolo_network.get_model(first_trainable_layer)
        loss = self._get_loss_func(batch_size)

        # 3. Run training loop
        return train(model,
                     loss,
                     train_batch_generator,
                     valid_batch_generator,
                     learning_rate=learning_rate,
                     nb_epoch=nb_epoch,
                     project_folder=project_folder,
                     first_trainable_layer=first_trainable_layer,
                     network=self,
                     metrics="mAP",
                     validation_freq=validation_freq)
Exemplo n.º 2
0
    def train(self,
              img_folder,
              ann_folder,
              nb_epoch,
              project_folder,
              batch_size=8,
              jitter=True,
              learning_rate=1e-4,
              train_times=1,
              valid_times=1,
              valid_img_folder="",
              valid_ann_folder="",
              first_trainable_layer=None,
              metrics="mAP"):

        # 1. get annotations
        train_annotations, valid_annotations = get_train_annotations(
            self._labels,
            img_folder,
            ann_folder,
            valid_img_folder,
            valid_ann_folder,
            is_only_detect=False)
        # 1. get batch generator
        train_batch_generator = self._get_batch_generator(train_annotations,
                                                          batch_size,
                                                          train_times,
                                                          jitter=jitter)
        valid_batch_generator = self._get_batch_generator(valid_annotations,
                                                          batch_size,
                                                          valid_times,
                                                          jitter=False)

        # 2. To train model get keras model instance & loss fucntion
        model = self._yolo_network.get_model(first_trainable_layer)
        loss = self._get_loss_func(batch_size)

        # 3. Run training loop
        return train(model,
                     loss,
                     train_batch_generator,
                     valid_batch_generator,
                     learning_rate=learning_rate,
                     nb_epoch=nb_epoch,
                     project_folder=project_folder,
                     first_trainable_layer=first_trainable_layer,
                     network=self,
                     metrics="mAP")