def downsample_fn(x, scale=4): h, w = x.shape[0:2] hs, ws = h // scale, w // scale x = imresize(x, size=[hs, ws], interp='bicubic') x = imresize(x, size=[h, w], interp='bicubic') return x
def _imresize(self, x, w, h=0, range=[-1., 1.]): x = x.astype(np.float32) x = (x - range[0]) * 255. / (range[1] - range[0]) h = w if h == 0 else h if len(x.shape) == 2: x = np.expand_dims(x, axis=2) x = imresize(x, size=[w, h], interp='bicubic', mode=None) x = (x / 255.) * (range[1] - range[0]) + range[0] return x
def main(argv=None): keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image") pred_annotation, _ = inference(image, keep_probability) sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Model restored...") else: print("Couldn't restore model.") if FLAGS.mode == "train": print("train") cfg = config.TRAIN elif FLAGS.mode == "valid": print("valid") cfg = config.VALID if not os.path.exists(cfg.segment_preprocessed_path): os.makedirs(cfg.segment_preprocessed_path) img_list = sorted(tl.files.load_file_list(path=cfg.hr_img_path, regx='.*.png', printable=False)) rem = len(img_list) % FLAGS.batch_size for idx in range(0, len(img_list) - rem, FLAGS.batch_size): b_imgs_list = img_list[idx : idx + FLAGS.batch_size] load_image = lambda fn, path: downsample_preserve_aspect_ratio_fn( get_imgs_fn(fn, path), size=[256, 256], mean_center=False) b_imgs = tl.prepro.threading_data(b_imgs_list, fn=load_image, path=cfg.hr_img_path) pred = sess.run(pred_annotation, feed_dict = {image: b_imgs, keep_probability: 1.0}) pred = np.squeeze(pred, axis=3) # for i in range(FLAGS.batch_size): # plt.subplot(1, 2, 1) # plt.imshow(b_imgs[0]) # plt.subplot(1, 2, 2) # plt.imshow(pred[0].astype(np.uint8), cmap=cm.Paired, vmin=0, vmax=151) # plt.savefig('compare_{}.png'.format(i)) # plt.show() for i in range(FLAGS.batch_size): img = np.expand_dims(pred[i].astype(np.uint8), axis=2) img = imresize(img, size=[96, 96], interp='nearest', mode=None) img = np.squeeze(img, axis=2) one_hot = segment_helper.label_to_one_hot(img) segment_helper.save_one_hot(one_hot, cfg.segment_preprocessed_path, get_frame_key(b_imgs_list[i]) + '_seg.npy') print('saved %d from %s' % (FLAGS.batch_size + idx, cfg.hr_img_path))
def resize_fn(x, scaled_size=256): # We obtained the LR images by downsampling the HR images using bicubic kernel with downsampling factor r = 4. scale_ratio = max(scaled_size / x.shape[0], scaled_size / x.shape[1]) + 0.05 height = int(x.shape[0] * scale_ratio) width = int(x.shape[1] * scale_ratio) x = imresize(x, size=[width, height], interp='bicubic') return x
def upsample_fn(x, scale=4): h, w = x.shape[0:2] newh, neww = h * scale, w * scale x = imresize(x, size=(newh, neww), interp='bicubic') return x
def downsample_fn(x): # We obtained the LR images by downsampling the HR images using bicubic kernel with downsampling factor r = 4. x = imresize(x, size=[96, 96], interp='bicubic') x /= 255. / 2. x -= 1. return x