def test_model(self):
        if not self.config.is_training:
            # initialize variables
            tf.global_variables_initializer().run()
            # load model
            if self.load_model():
                print('load model successfully')
            else:
                print('fail to load model')

        start_time = time.time()
        datasource = get_data(self.config.dataset, is_training=False)      
        gen_data = gen_batch_data(datasource, self.batchsize, is_training=False)
        ites_per_epoch = int(len(datasource.images)/self.batchsize)
        
        accuracy = []
        for ite in range(ites_per_epoch):
            images, labels = next(gen_data)
            accuracy_per_epoch = self.sess.run([self.accuracy], feed_dict={
                                                                            self.input_test:images,
                                                                            self.one_hot_labels:labels
                                                                            })
            accuracy.append(accuracy_per_epoch[0])
    
        acc = np.mean(accuracy)
        end_time = time.time()
        FPS=1000.0/(end_time - start_time)
        print('FPS:{}'.format(FPS))
        print('total time:{}'.format(end_time - start_time))
        print('--test epoch -- accuracy:{:.4f}'.format(acc))
Ejemplo n.º 2
0
    def test_model(self):
        if not self.config.is_training:
            try:
                tf.global_variables_initializer().run()
            except:
                tf.initialize_all_variables().run()
            is_load = self.load_model()
            if is_load:
                print('[***]load model successfully')
            else:
                print('[!!!]fail to load model')
                return
        datasource = get_data(self.config.datasets, is_training=False)
        gen_data = gen_batch_data(datasource,
                                  self.batchsize,
                                  is_training=False)
        ites = int(len(datasource.images) / self.batchsize)
        counter = 0
        for ite in range(ites):
            images, labels = next(gen_data)
            tmp = self.sess.run(self.accuracy_counter,
                                feed_dict={
                                    self.input_images: images,
                                    self.input_labels: labels
                                })
            counter = counter + tmp

        accuracy = float(counter) / float(ites * self.batchsize)
        return accuracy
Ejemplo n.º 3
0
    def train_model(self):
        # initialize variables
        try:
            tf.global_variables_initializer().run()
        except:
            tf.initialize_all_variables().run()

        # load model if model is exist
        bool_load = self.load_model()
        if bool_load:
            print('[***] load model successfully')
        else:
            print('[!!!] fail to load model')

        # get mnist dataset
        datasource = get_data(data_type=self.config.dataset,
                              is_training=self.config.is_training)
        data_gen = gen_batch_data(batchsize=self.batchsize,
                                  datasource=datasource)

        counter = 0
        for epoch in range(self.config.epoch):
            # save model per 10 epoches
            print('epoch:{}'.format(epoch))
            if np.mod(epoch, 10) == 0:
                self.save_model()

            for ite in tqdm(range(50000 / self.batchsize)):
                input_x, _ = next(data_gen)
                noise_z = np.random.uniform(-1,
                                            1,
                                            size=[self.batchsize,
                                                  self.z_dim]).astype(np.float)

                # optimize discriminator
                _, d_loss, summaries = self.sess.run(
                    [self.d_optim, self.d_loss, self.summaries],
                    feed_dict={
                        self.z: noise_z,
                        self.input_x: input_x
                    })
                # optimize generator
                _, g_loss = self.sess.run([self.g_optim, self.g_loss],
                                          feed_dict={self.z: noise_z})
                # optimize generator
                _, g_loss = self.sess.run([self.g_optim, self.g_loss],
                                          feed_dict={self.z: noise_z})

                # sample image during training phase
                if np.mod(ite, 100) == 0:
                    sample = self.sample.eval({self.z: noise_z})
                    save_image([8, 8], '{}/sample_{:3d}_{:4d}.png'.format(
                        self.config.sample_dir, epoch, ite), sample)
                    # save_image([8,8], '{}/input_{:3d}_{:4d}.png'.format(self.config.sample_dir, epoch, ite), input_x)

                # visualize loss in browser using tensorboard
                counter = counter + 1
                self.summary_writer.add_summary(summaries, global_step=counter)
Ejemplo n.º 4
0
    def train_model(self):
        try:
            tf.global_variables_initializer().run()
        except:
            tf.initialize_all_variables().run()

        is_load = self.load_model()
        if is_load:
            print('[***]load model successfully')
        else:
            print('[!!!]fail to load model')
        # load training data
        datasource = get_data(self.config.datasets)
        gen_data = gen_batch_data(datasource, self.batchsize)
        idxs = int(len(datasource.images) / self.batchsize)
        step = 0

        for epoch in range(self.config.epochs):
            counter = 0
            for idx in tqdm(range(idxs)):
                images, labels = next(gen_data)
                _, loss, summaries, train_counter = self.sess.run(
                    [
                        self.optim, self.loss, self.summaries,
                        self.accuracy_counter
                    ],
                    feed_dict={
                        self.input_images: images,
                        self.input_labels: labels
                    })
                counter = counter + train_counter
                step = step + 1
                self.summary_writer.add_summary(summaries, global_step=step)

            train_accuracy = float(counter) / (idxs * self.batchsize)
            print('epoch[{}/{}]:training accuracy:{:.4f}'.format(
                epoch, self.config.epochs, train_accuracy))

            if np.mod(epoch, 5) == 0:
                test_accuracy = self.test_model()
                print('epoch[{}/{}]:testing accuracy:{:.4f}'.format(
                    epoch, self.config.epochs, test_accuracy))

            if np.mod(epoch, 10) == 0:
                self.save_model()
Ejemplo n.º 5
0
    def train_model(self):
        # initialize variables
        try:
            tf.global_variables_initializer().run()
        except:
            tf.initialize_all_variables().run()

        # load model if model is exist
        bool_load = self.load_model()
        if bool_load:
            print('[***] load model successfully')
        else:
            print('[!!!] fail to load model')

        # get mnist dataset
        datasource = get_data(data_type=self.config.dataset,
                              is_training=self.config.is_training)
        data_gen = gen_batch_data(batchsize=self.batchsize,
                                  datasource=datasource)

        counter = 0
        for epoch in range(self.config.epoch):
            # model testing per epoch
            self.test_model()

            # save model per 10 epoches
            print('epoch:{}'.format(epoch))
            if np.mod(epoch, 10) == 0:
                self.save_model()

            for ite in tqdm(range(50000 / self.batchsize)):
                input_x, input_label = next(data_gen)
                # optimization
                _, loss, summaries = self.sess.run(
                    [self.optim, self.loss, self.summaries],
                    feed_dict={
                        self.input_x: input_x,
                        self.input_label: input_label
                    })
                # visualize loss in browser using tensorboard
                counter = counter + 1
                self.summary_writer.add_summary(summaries, global_step=counter)
Ejemplo n.º 6
0
    def train_model(self):
        # initialize variables
        tf.global_variables_initializer().run()

        # load model
        if self.load_model():
            print('load model successfully')
        else:
            print('fail to load model')

        # get datasource
        datasource = get_data(self.config.dataset, is_training=True)
        gen_data = gen_batch_data(datasource, self.batchsize, is_training=True)
        ites_per_epoch = int(len(datasource.images) / self.batchsize)

        step = 0
        for epoch in range(self.config.epochs):
            for ite in tqdm(range(ites_per_epoch)):
                images, labels = next(gen_data)
                _, loss, accuracy, summaries = self.sess.run(
                    [
                        self.rmsprop_optim, self.loss, self.accuracy,
                        self.summaries
                    ],
                    feed_dict={
                        self.input_train: images,
                        self.input_test: images,
                        self.one_hot_labels: labels
                    })

                step = step + 1
                self.summary_writer.add_summary(summaries, global_step=step)

            # test model
            if np.mod(epoch, 1) == 0:
                print('--epoch_{} -- training accuracy:{}'.format(
                    epoch, accuracy))
                self.test_model()

            # save model
            if np.mod(epoch, 5) == 0:
                self.save_model()