def _handler(ir_path, vis_path, model_path, model_pre_path, ssim_weight, index, output_path=None): ir_img = get_train_images(ir_path, flag=False) vis_img = get_train_images(vis_path, flag=False) # ir_img = get_train_images_rgb(ir_path, flag=False) # vis_img = get_train_images_rgb(vis_path, flag=False) dimension = ir_img.shape ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) vis_img = vis_img.reshape([1, dimension[0], dimension[1], dimension[2]]) ir_img = np.transpose(ir_img, (0, 2, 1, 3)) vis_img = np.transpose(vis_img, (0, 2, 1, 3)) print('img shape final:', ir_img.shape) with tf.Graph().as_default(), tf.Session() as sess: infrared_field = tf.placeholder( tf.float32, shape=ir_img.shape, name='content') visible_field = tf.placeholder( tf.float32, shape=ir_img.shape, name='style') dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(infrared_field, visible_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output = sess.run(output_image, feed_dict={infrared_field: ir_img, visible_field: vis_img}) save_images(ir_path, output, output_path, prefix='fused' + str(index), suffix='_densefuse_addition_'+str(ssim_weight))
def _handler(ir_path, vi_path, model_path, model_pre_path, index, output_path=None): ir_img = read_test_image(ir_path) vi_img = read_test_image(vi_path) dimension = ir_img.shape ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) vi_img = vi_img.reshape([1, dimension[0], dimension[1], dimension[2]]) print('img shape final:', ir_img.shape) with tf.Graph().as_default(), tf.Session() as sess: infrared_field = tf.placeholder( tf.float32, shape=ir_img.shape, name='content') visible_field = tf.placeholder( tf.float32, shape=ir_img.shape, name='style') dfn = DenseFuseNet(model_pre_path) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output_image = dfn.transform_addition(infrared_field, visible_field) # output_image = dfn.transform_cbf(infrared_field, visible_field) output = sess.run(output_image, feed_dict={infrared_field: ir_img, visible_field: vi_img}) save_images(ir_path, output, output_path, prefix='Dense_fuse_' + str(index).zfill(2)) # def _handler_cbf(ir_path, vi_path, model_path, model_pre_path, index, output_path=None): # ir_img = read_test_image(ir_path) # vi_img = read_test_image(vi_path) # dimension = ir_img.shape # # ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) # vi_img = vi_img.reshape([1, dimension[0], dimension[1], dimension[2]]) # # print('img shape final:', ir_img.shape) # # with tf.compat.v1.Graph().as_default(), tf.compat.v1.Session() as sess: # infrared_field = tf.placeholder(tf.float32, shape=ir_img.shape, name='image_ir') # visible_field = tf.placeholder(tf.float32, shape=ir_img.shape, name='image_vi') # # dfn = DenseFuseNet(model_pre_path) # # # restore the trained model and run the style transferring # # saver = tf.train.Saver() # # saver.restore(sess, model_path) # # output_image = dfn.transform_cbf(infrared_field, visible_field) # # output = sess.run(output_image, feed_dict={infrared_field: ir_img, visible_field: vi_img}) # # save_images(ir_path, output, output_path, prefix='Dense_fuse_' + str(index).zfill(2))
def _handler_video(ir_path, vis_path, model_path, model_pre_path, ssim_weight, output_path=None): infrared = ir_path[0] img = get_train_images(infrared, flag=False) img = img.reshape([1, img.shape[0], img.shape[1], img.shape[2]]) img = np.transpose(img, (0, 2, 1, 3)) print('img shape final:', img.shape) num_imgs = len(ir_path) with tf.Graph().as_default(), tf.Session() as sess: # build the dataflow graph infrared_field = tf.placeholder( tf.float32, shape=img.shape, name='content') visible_field = tf.placeholder( tf.float32, shape=img.shape, name='style') dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(infrared_field, visible_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) ##################GET IMAGES################################################################################### start_time = datetime.now() for i in range(num_imgs): print('image number:', i) infrared = ir_path[i] visible = vis_path[i] ir_img = get_train_images(infrared, flag=False) vis_img = get_train_images(visible, flag=False) dimension = ir_img.shape ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) vis_img = vis_img.reshape([1, dimension[0], dimension[1], dimension[2]]) ir_img = np.transpose(ir_img, (0, 2, 1, 3)) vis_img = np.transpose(vis_img, (0, 2, 1, 3)) ################FEED######################################## output = sess.run(output_image, feed_dict={infrared_field: ir_img, visible_field: vis_img}) save_images(infrared, output, output_path, prefix='fused' + str(i), suffix='_addition_' + str(ssim_weight)) ###################################################################################################### elapsed_time = datetime.now() - start_time print('Dense block video==> elapsed time: %s' % (elapsed_time))
def _handler(ir_path, vis_path, model_path, model_pre_path, ssim_weight, index, output_path=None): ir_img = get_train_images(ir_path, flag=False) vis_img = get_train_images(vis_path, flag=False) # ir_img = get_train_images_rgb(ir_path, flag=False) # vis_img = get_train_images_rgb(vis_path, flag=False) dimension = ir_img.shape ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) vis_img = vis_img.reshape([1, dimension[0], dimension[1], dimension[2]]) ir_img = np.transpose(ir_img, (0, 2, 1, 3)) vis_img = np.transpose(vis_img, (0, 2, 1, 3)) print('img shape final:', ir_img.shape) with tf.Graph().as_default(), tf.Session() as sess: infrared_field = tf.placeholder(tf.float32, shape=ir_img.shape, name='content') visible_field = tf.placeholder(tf.float32, shape=ir_img.shape, name='style') dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(infrared_field, visible_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output = sess.run(output_image, feed_dict={ infrared_field: ir_img, visible_field: vis_img }) save_images(ir_path, output, output_path, prefix='3', suffix='') img333 = Image.open('/home/bingyang/wby/1/3.png') imgout = img333.transpose(Image.FLIP_LEFT_RIGHT) imgout = imgout.transpose(Image.ROTATE_90) imgout.save('/home/bingyang/wby/1/3.png')
def _handler(content_name, style_name, model_path, model_pre_path, ssim_weight, index, output_path=None): infrared_path = content_name visible_path = style_name content_img = get_train_images(infrared_path, flag=False) style_img = get_train_images(visible_path, flag=False) dimension = content_img.shape content_img = content_img.reshape([1, dimension[0], dimension[1], dimension[2]]) style_img = style_img.reshape([1, dimension[0], dimension[1], dimension[2]]) content_img = np.transpose(content_img, (0, 2, 1, 3)) style_img = np.transpose(style_img, (0, 2, 1, 3)) print('content_img shape final:', content_img.shape) with tf.Graph().as_default(), tf.Session() as sess: # build the dataflow graph content = tf.placeholder( tf.float32, shape=content_img.shape, name='content') style = tf.placeholder( tf.float32, shape=style_img.shape, name='style') dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(content,style) # output_image = dfn.transform_recons(style) # output_image = dfn.transform_recons(content) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output = sess.run(output_image, feed_dict={content: content_img, style: style_img}) save_images(infrared_path, output, output_path, prefix='fused' + str(index), suffix='_densefuse_addition_'+str(ssim_weight)) return output
def _handler_rgb(ir_path, vis_path, model_path, model_pre_path, ssim_weight, index, output_path=None): # ir_img = get_train_images(ir_path, flag=False) # vis_img = get_train_images(vis_path, flag=False) ir_img = get_test_image_rgb(ir_path, flag=False) vis_img = get_test_image_rgb(vis_path, flag=False) dimension = ir_img.shape ir_img = ir_img.reshape([1, dimension[0], dimension[1], dimension[2]]) vis_img = vis_img.reshape([1, dimension[0], dimension[1], dimension[2]]) #ir_img = np.transpose(ir_img, (0, 2, 1, 3)) #vis_img = np.transpose(vis_img, (0, 2, 1, 3)) ir_img1 = ir_img[:, :, :, 0] ir_img1 = ir_img1.reshape([1, dimension[0], dimension[1], 1]) ir_img2 = ir_img[:, :, :, 1] ir_img2 = ir_img2.reshape([1, dimension[0], dimension[1], 1]) ir_img3 = ir_img[:, :, :, 2] ir_img3 = ir_img3.reshape([1, dimension[0], dimension[1], 1]) vis_img1 = vis_img[:, :, :, 0] vis_img1 = vis_img1.reshape([1, dimension[0], dimension[1], 1]) vis_img2 = vis_img[:, :, :, 1] vis_img2 = vis_img2.reshape([1, dimension[0], dimension[1], 1]) vis_img3 = vis_img[:, :, :, 2] vis_img3 = vis_img3.reshape([1, dimension[0], dimension[1], 1]) print('img shape final:', ir_img1.shape) with tf.Graph().as_default(), tf.Session() as sess: infrared_field = tf.placeholder(tf.float32, shape=ir_img1.shape, name='content') visible_field = tf.placeholder(tf.float32, shape=ir_img1.shape, name='style') dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(infrared_field, visible_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output1 = sess.run(output_image, feed_dict={ infrared_field: ir_img1, visible_field: vis_img1 }) output2 = sess.run(output_image, feed_dict={ infrared_field: ir_img2, visible_field: vis_img2 }) output3 = sess.run(output_image, feed_dict={ infrared_field: ir_img3, visible_field: vis_img3 }) output1 = output1.reshape([1, dimension[0], dimension[1]]) output2 = output2.reshape([1, dimension[0], dimension[1]]) output3 = output3.reshape([1, dimension[0], dimension[1]]) output = np.stack((output1, output2, output3), axis=-1) #output = np.transpose(output, (0, 2, 1, 3)) save_images(ir_path, output, output_path, prefix='', suffix='' + str(ssim_weight))
def _handler_rgb_patch_based(images_path, model_path, model_pre_path, index, output_path=None): size = len(images_path) images = ["" for x in range(size)] ir_img1 = ["" for x in range(size)] ir_img2 = ["" for x in range(size)] ir_img3 = ["" for x in range(size)] for x in range(0, size): images[x] = get_train_images_rgb(images_path[x], flag=False) dimension = images[x].shape images[x] = images[x].reshape( [1, dimension[0], dimension[1], dimension[2]]) images[x] = np.transpose(images[x], (0, 2, 1, 3)) ir_img1[x] = images[x][:, :, :, 0] ir_img1[x] = ir_img1[x].reshape([1, dimension[0], dimension[1], 1]) ir_img2[x] = images[x][:, :, :, 1] ir_img2[x] = ir_img2[x].reshape([1, dimension[0], dimension[1], 1]) ir_img3[x] = images[x][:, :, :, 2] ir_img3[x] = ir_img3[x].reshape([1, dimension[0], dimension[1], 1]) print('img shape final:', ir_img1[0].shape) dimension = images[0].shape TRAIN_TAIL_SIZE_X = 32 TRAIN_TAIL_SIZE_Y = 32 TILES_X = (int)(dimension[1] / TRAIN_TAIL_SIZE_X) TILES_Y = (int)(dimension[2] / TRAIN_TAIL_SIZE_Y) INPUT_SHAPE_TILE = (1, TRAIN_TAIL_SIZE_X, TRAIN_TAIL_SIZE_Y, 1) with tf.Graph().as_default(), tf.Session() as sess: images_field = ["" for x in range(size)] for x in range(0, size): images_field[x] = tf.placeholder(tf.float32, shape=INPUT_SHAPE_TILE) dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(images_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) dimension = images[0].shape output1 = np.zeros([1, dimension[1], dimension[2]]) output2 = np.zeros([1, dimension[1], dimension[2]]) output3 = np.zeros([1, dimension[1], dimension[2]]) for tile_x in range(TILES_X): for tile_y in range(TILES_Y): x1 = tile_x * TRAIN_TAIL_SIZE_X y1 = tile_y * TRAIN_TAIL_SIZE_Y x2 = x1 + TRAIN_TAIL_SIZE_X y2 = y1 + TRAIN_TAIL_SIZE_Y tile_img1 = ["" for x in range(size)] tile_img2 = ["" for x in range(size)] tile_img3 = ["" for x in range(size)] for x in range(0, size): tile_img1[x] = ir_img1[x][:, x1:x2, y1:y2, :] tile_img2[x] = ir_img2[x][:, x1:x2, y1:y2, :] tile_img3[x] = ir_img3[x][:, x1:x2, y1:y2, :] output1_t = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, tile_img1)}) output2_t = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, tile_img2)}) output3_t = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, tile_img3)}) output1_t = output1_t.reshape( [1, TRAIN_TAIL_SIZE_X, TRAIN_TAIL_SIZE_Y]) output2_t = output2_t.reshape( [1, TRAIN_TAIL_SIZE_X, TRAIN_TAIL_SIZE_Y]) output3_t = output3_t.reshape( [1, TRAIN_TAIL_SIZE_X, TRAIN_TAIL_SIZE_Y]) output1[0, x1:x2, y1:y2] = output1_t output2[0, x1:x2, y1:y2] = output2_t output3[0, x1:x2, y1:y2] = output3_t output = np.stack((output1, output2, output3), axis=-1) output = np.transpose(output, (0, 2, 1, 3)) save_images(images_path, output, output_path, prefix='fused' + str(index), suffix='_densefuse_addition_patch_based')
def _handler_rgb(images_path, model_path, model_pre_path, index, output_path=None): size = len(images_path) images = ["" for x in range(size)] ir_img1 = ["" for x in range(size)] ir_img2 = ["" for x in range(size)] ir_img3 = ["" for x in range(size)] for x in range(0, size): images[x] = get_train_images_rgb(images_path[x], flag=False) dimension = images[x].shape images[x] = images[x].reshape( [1, dimension[0], dimension[1], dimension[2]]) images[x] = np.transpose(images[x], (0, 2, 1, 3)) ir_img1[x] = images[x][:, :, :, 0] ir_img1[x] = ir_img1[x].reshape([1, dimension[0], dimension[1], 1]) ir_img2[x] = images[x][:, :, :, 1] ir_img2[x] = ir_img2[x].reshape([1, dimension[0], dimension[1], 1]) ir_img3[x] = images[x][:, :, :, 2] ir_img3[x] = ir_img3[x].reshape([1, dimension[0], dimension[1], 1]) print('img shape final:', ir_img1[0].shape) with tf.Graph().as_default(), tf.Session() as sess: images_field = ["" for x in range(size)] for x in range(0, size): images_field[x] = tf.placeholder(tf.float32, shape=ir_img1[0].shape) dfn = DenseFuseNet(model_pre_path) output_image = dfn.transform_addition(images_field) # restore the trained model and run the style transferring saver = tf.train.Saver() saver.restore(sess, model_path) output1 = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, ir_img1)}) output2 = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, ir_img2)}) output3 = sess.run( output_image, feed_dict={i: d for i, d in zip(images_field, ir_img3)}) output1 = output1.reshape([1, dimension[0], dimension[1]]) output2 = output2.reshape([1, dimension[0], dimension[1]]) output3 = output3.reshape([1, dimension[0], dimension[1]]) output = np.stack((output1, output2, output3), axis=-1) output = np.transpose(output, (0, 2, 1, 3)) save_images(images_path, output, output_path, prefix='fused' + str(index), suffix='_densefuse_addition')