def color(data, template, logs_dir="/data2/ben/HE/data/result/color_logs/"): import tensorflow.compat.v1 as tf tf.reset_default_graph() sess = tf.Session() image_dist = [] is_train = False #print("FLAGS",tf.flags.FLAGS) #print("FLAGS.logs_dir==============",tf.flags.FLAGS.logs_dir) config = get_config(FLAGS, is_train) dist = DCGMM(sess, config, "DCGMM", is_train) db_tmpl = SampleProvider("Template_dataset", template, config.fileformat, config.image_options, is_train) mu_tmpl = 0 std_tmpl = 0 N = 0 while True: X = db_tmpl.DrawSample(config.batch_size) if len(X) == 0: break X_hsd = utils.RGB2HSD(X / 255.0) mu, std, gamma = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu, 1, 2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std, 1, 2) # -> dim: [ClustrNo x 1 x 3] N = N + 1 mu_tmpl = (N - 1) / N * mu_tmpl + 1 / N * mu std_tmpl = (N - 1) / N * std_tmpl + 1 / N * std db = SampleProvider("Test_dataset", data, config.fileformat, config.image_options, is_train) while True: X = db.DrawSample(config.batch_size) if len(X) == 0: break X_hsd = utils.RGB2HSD(X / 255.0) mu, std, pi = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu, 1, 2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std, 1, 2) # -> dim: [ClustrNo x 1 x 3] X_conv = image_dist_transform(X_hsd, mu, std, pi, mu_tmpl, std_tmpl, config.im_size, config.ClusterNo) image_dist.append(X_conv) # misc.imsave('/data2/ben/HE/data/result/color_tmp/color_norma.png', np.squeeze(X_conv)) return image_dist
def main(): sess = tf.Session() if FLAGS.mode == "train": is_train = True else: is_train = False config = get_config(FLAGS, is_train) if not os.path.exists(config.logs_dir): os.makedirs(config.logs_dir) dist = DCGMM(sess, config, "DCGMM", is_train) config.fileformat = "png" db = SampleProvider("Train_dataset", config.data_dir, config.fileformat, config.image_options, is_train) if FLAGS.mode == "train": for i in range(int(config.iteration)): X = db.DrawSample(config.batch_size) X_hsd = utils.RGB2HSD(X[0]/255.0) loss, summary_str, summary_writer = dist.fit(X_hsd) if i % config.ReportInterval == 0: summary_writer.add_summary(summary_str, i) print("iter {:>6d} : {}".format(i+1, loss)) if i % config.SavingInterval == 0: dist.saver.save(sess, config.logs_dir+ "model.ckpt", i) elif FLAGS.mode == "prediction": if not os.path.exists(config.out_dir): os.makedirs(config.out_dir) db_tmpl = SampleProvider("Template_dataset", config.tmpl_dir, config.fileformat, config.image_options, is_train) print(config.fileformat) mu_tmpl = 0 std_tmpl = 0 N = 0 while True: X = db_tmpl.DrawSample(config.batch_size) if len(X) ==0: break X_hsd = utils.RGB2HSD(X[0]/255.0) mu, std, gamma = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu,1,2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std,1,2) # -> dim: [ClustrNo x 1 x 3] N = N+1 mu_tmpl = (N-1)/N * mu_tmpl + 1/N* mu std_tmpl = (N-1)/N * std_tmpl + 1/N* std print("Estimated Mu for template(s):") print(mu_tmpl) print("Estimated Sigma for template(s):") print(std_tmpl) db = SampleProvider("Test_dataset", config.data_dir, config.fileformat, config.image_options, is_train) while True: X = db.DrawSample(config.batch_size) if len(X) ==0: break X_hsd = utils.RGB2HSD(X[0]/255.0) mu, std, pi = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu,1,2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std,1,2) # -> dim: [ClustrNo x 1 x 3] X_conv = image_dist_transform(X_hsd, mu, std, pi, mu_tmpl, std_tmpl, config.im_size, config.ClusterNo) filename = X[1] filename = filename[0].split('/')[-1] print(filename) if not os.path.exists(config.out_dir): os.makedirs(config.out_dir) misc.imsave(config.out_dir+filename.replace('datasett\\',''), np.squeeze(X_conv)) else: print('Invalid "mode" string!') return
def main(): sess = tf.Session() image_dist = [] if FLAGS.mode == "train": is_train = True else: is_train = False config = get_config(FLAGS, is_train) dist = DCGMM(sess, config, "DCGMM", is_train) if FLAGS.mode == "train": db = SampleProvider("Train_dataset", config.data_dir, config.fileformat, config.image_options, is_train) for i in range(int(config.iteration)): X = db.DrawSample(config.batch_size) X_hsd = utils.RGB2HSD(X[0]/255.0) loss, summary_str, summary_writer = dist.fit(X_hsd) if i % config.ReportInterval == 0: summary_writer.add_summary(summary_str, i) print("iter {:>6d} : {}".format(i+1, loss)) if i % config.SavingInterval == 0: dist.saver.save(sess, config.logs_dir+ "model.ckpt", i) elif FLAGS.mode == "prediction": db_tmpl = SampleProvider("Template_dataset", config.tmpl_dir, config.fileformat, config.image_options, is_train) print("db_tmpl") print(db_tmpl) mu_tmpl = 0 std_tmpl = 0 N = 0 while True: X = db_tmpl.DrawSample(config.batch_size) if len(X) == 0: break X_hsd = utils.RGB2HSD(X[0]/255.0) mu, std, gamma = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu,1,2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std,1,2) # -> dim: [ClustrNo x 1 x 3] N += 1 mu_tmpl = (N-1)/N * mu_tmpl + 1/N* mu std_tmpl = (N-1)/N * std_tmpl + 1/N* std print("Estimated Mu for template(s):") print(mu_tmpl) print("Estimated Sigma for template(s):") print(std_tmpl) image_dir_list = glob(join(config.data_dir, r'*/')) for image_dir in image_dir_list: print(image_dir) png_addr = glob(join(image_dir, '*CN.png')) if png_addr != []: continue db = SampleProvider("Test_dataset", image_dir, config.fileformat, config.image_options, is_train) while True: X = db.DrawSample(config.batch_size) if len(X) ==0: break X_hsd = utils.RGB2HSD(X[0]/255.0) mu, std, pi = dist.deploy(X_hsd) mu = np.asarray(mu) mu = np.swapaxes(mu,1,2) # -> dim: [ClustrNo x 1 x 3] std = np.asarray(std) std = np.swapaxes(std,1,2) # -> dim: [ClustrNo x 1 x 3] X_conv = image_dist_transform(X_hsd, mu, std, pi, mu_tmpl, std_tmpl, config.im_size, config.ClusterNo) image_dist.append(X_conv) filename = X[1] misc.imsave(filename[0].split('.orig')[0]+'.CN.png', np.squeeze(X_conv)) print(len(image_dist)) else: print('Invalid "mode" string!') return