示例#1
0
    def fit(self, x_s, y_s, x_t, verbose=False, y_t=[], init_weights=None):
        """
        train classifier
        """
        start = datetime.datetime.now().replace(microsecond=0)
        self.create()
        #        np.random.seed(0)
        #         print("========================================")
        dense1_layer_model = Model(input=self.nn.input[0],
                                   output=self.nn.get_layer(
                                       self.nn.layers[3].name).output)
        dense1_layer_model1 = Model(input=self.nn.input[1],
                                    output=self.nn.get_layer(
                                        self.nn.layers[4].name).output)
        a, b = dense1_layer_model.predict(x_s), dense1_layer_model1.predict(
            x_t)
        tau_to_npy(a, 'xs')
        tau_to_npy(b, 'xt')
        # print("========================================第一次聚类完成")
        if init_weights:
            self.load(init_weights)
        best_acc = 0
        best_loss = 0
        counter = 0
        dummy_y_t = np.zeros((x_t.shape[0], y_s.shape[1]))

        iter_batches = Batches(x_s, y_s, x_t.shape[0])

        for i in range(self.max_n_epoch):
            x_s_batch, y_s_batch = iter_batches.next_batch()
            # print(self.nn.metrics_names)
            metrics = self.nn.train_on_batch([x_s_batch, x_t],
                                             [y_s_batch, dummy_y_t])
            if metrics[3] > best_acc:
                self.save(self.save_weights)
                best_acc = metrics[3]
                best_loss = metrics[1]
                counter = 0
            elif metrics[3] == best_acc and metrics[1] < best_loss:
                self.save(self.save_weights)
                best_loss = metrics[1]
                best_acc = metrics[3]
                counter += 1
            else:
                counter += 1
            if i % 20 == 0 and verbose:
                accs = self.nn.evaluate([x_t, x_t], [y_t, y_t], verbose=0)
                print(
                    'Batch update %.4d loss= %.4f tr-acc= %.4f tst-acc= %.4f' %
                    (i, metrics[1], best_acc, accs[4]))
            if counter > 1000:
                break
        self.load(self.save_weights)
        stop = datetime.datetime.now().replace(microsecond=0)
        print('done in ' + str(stop - start))
    def fit(self, x_s, y_s, x_t, verbose=False, x_val=[], y_val=[]):
        """
        train classifier
        """
        start = datetime.datetime.now().replace(microsecond=0)
        # init
        self.create()
        best_acc = 0
        best_loss = 0
        counter = 0
        dummy_y_t = np.zeros((x_t.shape[0], y_s.shape[1]))
        # batch size is 2000 (arbitrary) when working with augmented data
        # batch size is
        # Note that such high numbers are not possible in fine-tuning with
        # the learning rates of lower layers >0. If we set the lower learning
        # rates to zero, this is equivalent to pre-computing image
        # representations, as we are doing.
        dense1_layer_model = Model(input=self.nn.input[0],
                                   output=self.nn.get_layer(
                                       self.nn.layers[3].name).output)
        dense1_layer_model1 = Model(input=self.nn.input[1],
                                    output=self.nn.get_layer(
                                        self.nn.layers[4].name).output)
        a, b = dense1_layer_model.predict(x_s), dense1_layer_model1.predict(
            x_t)
        tau_to_npy(a, 'xs')
        tau_to_npy(b, 'xt')
        iter_batches = None
        if x_t.shape[0] > 3000:
            # data augmentation is used : equal batches are computed
            batch_s = Batches(x_s, y_s, 2000)
            batch_t = Batches(x_t, dummy_y_t, 2000)
        elif x_t.shape[0] >= x_s.shape[0]:
            # target batch is larger than source batch
            # source batch will be up-sampled via class-balanced copies
            iter_batches = Batches(x_s, y_s, x_t.shape[0])
        else:
            # target batch is smaller than source batch
            # target batch will be randomly up-sampled
            iter_batches = Batches(x_t, dummy_y_t, x_s.shape[0])

        for i in range(self.max_n_epoch):
            if x_t.shape[0] > 3000:
                # equal batches are generated
                x_s_batch, y_s_batch = batch_s.next_batch()
                x_t_batch, y_t_batch = batch_t.next_batch()
            elif x_t.shape[0] >= x_s.shape[0]:
                # source batch is up-sampled via class-balanced copies
                x_s_batch, y_s_batch = iter_batches.next_batch()
                x_t_batch, y_t_batch = x_t, dummy_y_t
            else:
                # target batch is randomly up-sampled
                x_s_batch, y_s_batch = x_s, y_s
                x_t_batch, y_t_batch = iter_batches.next_batch()
            # one full-batch update
            metrics = self.nn.train_on_batch([x_s_batch, x_t_batch],
                                             [y_s_batch, y_t_batch])
            if metrics[3] > best_acc:
                # an improvement happened
                self.save(self.save_weights)
                best_acc = metrics[3]
                best_loss = metrics[1]
                counter = 0
            elif metrics[3] == best_acc and metrics[1] < best_loss:
                # save model with best accuracy and best loss
                self.save(self.save_weights)
                best_loss = metrics[1]
                best_acc = metrics[3]
                counter += 1
            else:
                counter += 1
            # Try the verbose command and you will get a fealing for the target
            # error during training. Maybe manually decreasing CMD weighting
            # can help the optimization, as used by various other works.
            if i % 20 == 0 and verbose:
                accs = self.nn.evaluate([x_val, x_val], [y_val, y_val],
                                        verbose=0)
                print(
                    'Batch update %.4d loss= %.4f tr-acc= %.4f tst-acc= %.4f' %
                    (i, metrics[1], best_acc, accs[4]))
            if counter > 1000:
                # early stopping after 1000 epochs
                # without accuracy increase
                break
        # load best model
        self.load(self.save_weights)
        stop = datetime.datetime.now().replace(microsecond=0)
        print('done in ' + str(stop - start))