def train(self, counter=1, gen_dirs=()): if self.conf.need_to_load: self.load(self.conf.checkpoint_dir, step=counter) data = self.data logger.info('Total amount of images: %s' % len(data)) # np.random.shuffle(data) tf.initialize_all_variables().run() # counter = 1 start_time = time.time() batch_idxs = min(len(data), self.conf.train_size) / self.conf.batch_size stego_accuracy = 0 accuracies = [] accuracies_steps = [] logger.debug('Starting updating') for epoch in range(self.conf.epoch): losses = [] np.random.shuffle(data) logger.info('Starting epoch %s' % epoch) for idx in range(0, int(batch_idxs)): batch_files = data[idx * self.conf.batch_size:(idx + 1) * self.conf.batch_size] batch = [get_image(batch_file, self.conf.image_size) for batch_file in batch_files] batch_images = np.array(batch).astype(np.float32) batch_targets = self.get_targets(batch_files) self.sess.run(self.optimize, feed_dict={self.images: batch_images, self.target: batch_targets}) loss = self.loss.eval({self.images: batch_images, self.target: batch_targets}) losses.append(loss) # logger.debug("[ITERATION] Epoch [%2d], iteration [%4d/%4d] time: %4.4f, Loss: %8f, accuracy: %8f" % # (epoch, idx, batch_idxs, time.time() - start_time, loss, stego_accuracy)) counter += 1 if counter % 300 == 0: logger.info('------') stego_accuracy = self.accuracy(n_files=-1, test_dir=self.test_dir) logger.info('[TEST] Epoch {:2d} accuracy: {:3.1f}%'.format(epoch + 1, 100 * stego_accuracy)) for gen_dir in gen_dirs: gen_accuracy = self.accuracy(n_files=-1, test_dir=gen_dir) logger.info('[GEN_TEST] Folder {}, accuracy: {:3.1f}%'.format(gen_dir, 100 * gen_accuracy))
def save(self, checkpoint_dir, step): model_dir = "%s_%s" % (self.conf.model_name, self.conf.batch_size) checkpoint_dir = os.path.join(checkpoint_dir, model_dir) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) ckpt_name = '%s_%s.ckpt' % (self.conf.model_name, step) logger.info('[SAVING] step: %s, name: %s' % (step, ckpt_name)) self.saver.save(self.sess, os.path.join(checkpoint_dir, ckpt_name), global_step=step)
def main(_): logger.info('====================================================') logger.info('===================NEW EXPERIMENT===================') logger.info('====================================================') logger.info(flags.FLAGS.__flags) if not os.path.exists(FLAGS.checkpoint_dir): os.makedirs(FLAGS.checkpoint_dir) if not os.path.exists(FLAGS.sample_dir): os.makedirs(FLAGS.sample_dir) with tf.Session() as sess: steganalisys = Steganalyzer(config=FLAGS, sess=sess, stego_algorithm=LSBMatching) # dirs = [ # '/home/dvolkhonskiy/SGAN/code/data/10_seeds/test', # '/home/dvolkhonskiy/SGAN/code/data/10_seeds/other_seeds', # '/home/dvolkhonskiy/SGAN/code/data/10_seeds/more_train', # '/home/dvolkhonskiy/SGAN/code/data/10_seeds/more_train_other_seeds', # ] # dirs = [ # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_0', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_1', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_2', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_3', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_4', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_5', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_6', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_7', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_8', # '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_9', # ] dirs = [ '/home/dvolkhonskiy/SGAN/code/data/overtraining/plus_%s' % i for i in range(0, 66) ] if FLAGS.is_train: steganalisys.train(counter=1, gen_dirs=dirs) else: # steganalisys.load(FLAGS.checkpoint_dir, step=28481) # LSB matching clf steganalisys.load(FLAGS.checkpoint_dir, step=59) for gen_dir in dirs: acc, std = steganalisys.accuracy(n_files=-1, test_dir=gen_dir) logger.info('[GEN_TEST] Folder %s, accuracy: %.4f, std: %.4f' % (gen_dir, acc, std))
def load(self, checkpoint_dir, step): model_dir = "%s_%s" % (self.conf.model_name, self.conf.batch_size) checkpoint_dir = os.path.join(checkpoint_dir, model_dir) try: ckpt_name = '%s_%s.ckpt-%s' % (self.conf.model_name, step, step) logger.info('[LOADING] step: %s, name: %s' % (step, ckpt_name)) self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) except Exception as e: logger.debug(e) ckpt_name = 'StegoDCGAN-%s' % (step) logger.info('[LOADING] step: %s, name: %s' % (step, ckpt_name)) self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name))
def accuracy(self, test_dir='test', abs=False, n_files=2 ** 12): logger.info('[TEST], test data folder: %s, n_files: %s' % (test_dir, 2 * n_files)) X_test = self.get_images_names('%s/*.%s' % (test_dir, self.conf.img_format), abs=abs)[:n_files] accuracies = [] batch_idxs = min(len(X_test), self.conf.train_size) / self.conf.batch_size # logger.debug('Starting iteration') for idx in range(0, int(batch_idxs)): batch_files_stego = X_test[idx * self.conf.batch_size:(idx + 1) * self.conf.batch_size] batch = [get_image(batch_file, self.conf.image_size) for batch_file in batch_files_stego] batch_images = np.array(batch).astype(np.float32) batch_targets = self.get_targets(batch_files_stego) accuracies.append(self.get_accuracy(batch_images, batch_targets)) return np.mean(accuracies)
def train(self, counter=1, gen_dirs=()): if self.conf.need_to_load: self.load(self.conf.checkpoint_dir, step=counter) data = self.data logger.info('Total amount of images: %s' % len(data)) # np.random.shuffle(data) tf.initialize_all_variables().run() # counter = 1 start_time = time.time() batch_idxs = min(len(data), self.conf.train_size) / self.conf.batch_size logger.debug('Starting updating') for epoch in range(self.conf.epoch): losses = [] np.random.shuffle(data) logger.info('Starting epoch %s' % epoch) for idx in range(0, int(batch_idxs)): batch_files = data[idx * self.conf.batch_size:(idx + 1) * self.conf.batch_size] batch = [ get_image(batch_file, self.conf.image_size) for batch_file in batch_files ] batch_images = np.array(batch).astype(np.float32) batch_targets = self.get_targets(batch_files) _, loss = self.sess.run([self.optimize, self.loss], feed_dict={ self.images: batch_images, self.target: batch_targets }) losses.append(loss) # logger.debug("[ITERATION] Epoch [%2d], iteration [%4d/%4d] time: %4.4f, Loss: %8f, accuracy: %8f" % # (epoch, idx, batch_idxs, time.time() - start_time, loss, stego_accuracy)) counter += 1 if epoch % 5 == 0 and epoch > 0: for gen_dir in gen_dirs: acc, std = self.accuracy(n_files=-1, test_dir=gen_dir) logger.info( '[GEN_TEST] Folder %s, accuracy: %.4f, std: %.4f' % (gen_dir, acc, std)) # SAVE after each epoch self.save(self.conf.checkpoint_dir, epoch)
def main(_): logger.info('====================================================') logger.info('===================NEW EXPERIMENT===================') logger.info('====================================================') logger.info(flags.FLAGS.__flags) if not os.path.exists(FLAGS.checkpoint_dir): os.makedirs(FLAGS.checkpoint_dir) if not os.path.exists(FLAGS.sample_dir): os.makedirs(FLAGS.sample_dir) with tf.Session() as sess: dcgan = SGAN(sess, LSBMatching, config=FLAGS, image_size=FLAGS.image_size, batch_size=FLAGS.batch_size) dcgan.load(FLAGS.checkpoint_dir, 4) # 18990 22155 25320 28485 31650 n_batches = 1999 for i in range(n_batches): if i % 100 == 0: print('Iteration', i) z_sample = np.random.uniform(-1, 1, size=(FLAGS.batch_size, dcgan.z_dim)) samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample}) save_images(samples, i, folder='./data/overtraining/') for chpt_i, chpt in enumerate(range(4, 70, 1)): # [18990, 22155, 25320, 28485, 31650] dcgan.load(FLAGS.checkpoint_dir, chpt) n_batches = 199 for i in range(n_batches): if i % 100 == 0: print('Iteration', i) z_sample = np.random.uniform(-1, 1, size=(FLAGS.batch_size, dcgan.z_dim)) samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample}) folder = './data/overtraining/plus_%s' % (chpt_i) if not os.path.exists(folder): os.makedirs(folder) save_images(samples, i, folder=folder)
def main(_): logger.info('====================================================') logger.info('===================NEW EXPERIMENT===================') logger.info('====================================================') logger.info(flags.FLAGS.__flags) if not os.path.exists(FLAGS.checkpoint_dir): os.makedirs(FLAGS.checkpoint_dir) if not os.path.exists(FLAGS.sample_dir): os.makedirs(FLAGS.sample_dir) with tf.Session() as sess: dcgan = SGAN(sess, LSBMatching, config=FLAGS, image_size=FLAGS.image_size, batch_size=FLAGS.batch_size) if FLAGS.is_train: dcgan.train() else: dcgan.load(FLAGS.checkpoint_dir, 50400) # 50400 50800 n_batches = 200 z_sample = np.random.uniform(-1, 1, size=(FLAGS.batch_size, dcgan.z_dim)) for i in range(n_batches): np.random.seed(int(time())) z_sample = np.random.uniform(-1, 1, size=(FLAGS.batch_size, dcgan.z_dim)) samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample}) save_images( samples, i, folder='/home/dvolkhonskiy/datasets/new/sgan_generated')
def main(_): logger.info('====================================================') logger.info('===================NEW EXPERIMENT===================') logger.info('====================================================') logger.info(flags.FLAGS.__flags) if not os.path.exists(FLAGS.checkpoint_dir): os.makedirs(FLAGS.checkpoint_dir) if not os.path.exists(FLAGS.sample_dir): os.makedirs(FLAGS.sample_dir) with tf.Session() as sess: steganalisys = Steganalyzer(config=FLAGS, sess=sess, stego_algorithm=LSBMatching, stego_name='lsb_matching') if FLAGS.is_train: steganalisys.train(counter=1, gen_dirs=['gen_test_seed_666', 'gen_test_more_train']) else: # steganalisys.load(FLAGS.checkpoint_dir, step=28481) # LSB matching clf steganalisys.load(FLAGS.checkpoint_dir, step=122114) print('ACCURACY:::::::::%s' % steganalisys.accuracy(test_dir='gen_test_more_train', n_files=-1))
def main(_): logger.info('====================================================') logger.info('===================NEW EXPERIMENT===================') logger.info('====================================================') logger.info(flags.FLAGS.__flags) if not os.path.exists(FLAGS.checkpoint_dir): os.makedirs(FLAGS.checkpoint_dir) if not os.path.exists(FLAGS.sample_dir): os.makedirs(FLAGS.sample_dir) with tf.Session() as sess: dcgan = SGAN(sess, LSBMatching, config=FLAGS, image_size=FLAGS.image_size, batch_size=FLAGS.batch_size) if FLAGS.is_train: dcgan.train(start_epoch=0) else: dcgan.load(FLAGS.checkpoint_dir, 50400) # 50400 50800