USTC-JialunPeng / Diverse-Structure-Inpainting

CVPR 2021: "Generating Diverse Structure for Image Inpainting With Hierarchical VQ-VAE"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mask images and file lists

naoki7090624 opened this issue · comments

Thank you for sharing your great work!

I want to run test.py using your pretrained models. Do I need to prepare the mask images and file lists by myself? If you have the code to generate them, would you mind sharing it?

Thanks for your good suggestions.

Here is my code for generating random masks and the corresponding mask file list in the testing. This code generates 10,000 masks, where each even-numbered mask is composed of three random rectangles, and each odd-numbered mask is composed of five random free-form strokes.

import os
import cv2
import sys
import numpy as np
import tensorflow as tf

import net.nn as nn

save_dir = '/gdata/test_set/random_mask'

bbox1 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask1 = nn.bbox2mask(bbox1, 256, 256, 64, name='mask_r1')
bbox2 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask2 = nn.bbox2mask(bbox2, 256, 256, 64, name='mask_r2')
bbox3 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask3 = nn.bbox2mask(bbox3, 256, 256, 64, name='mask_r3')
irregular_mask1 = nn.brush_stroke_mask(256, 256, name='mask_i1')
irregular_mask2 = nn.brush_stroke_mask(256, 256, name='mask_i2')
irregular_mask3 = nn.brush_stroke_mask(256, 256, name='mask_i3')
irregular_mask4 = nn.brush_stroke_mask(256, 256, name='mask_i4')
irregular_mask5 = nn.brush_stroke_mask(256, 256, name='mask_i5')

mask1 = tf.cast(tf.cast(regular_mask1 + regular_mask2 + regular_mask3, tf.bool), tf.float32) * 255
mask1 = tf.cast(mask1, tf.uint8)
mask2 = tf.cast(tf.cast(irregular_mask1 + irregular_mask2 + irregular_mask3 + irregular_mask4+ irregular_mask5, tf.bool), tf.float32) *255
mask2 = tf.cast(mask2, tf.uint8)

# TF session
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
    mask_path_list = []
    for i in range(10000):
        if i % 2:
            mask_np = sess.run(mask1)[0,:,:,:]
        if not i % 2:
            mask_np = sess.run(mask2)[0,:,:,:]
        mask_path = save_dir+'/mask_%05d.png' % i
        mask_path_list.append(mask_path)
        cv2.imwrite(mask_path, mask_np)
        print("Processing %05d random mask..." % i)
        sys.stdout.flush()

    fo = open('/gdata/test_set/flist/random_mask.flist', "w")
    fo.write("\n".join(mask_path_list))
    fo.close()

    print("FINISHED!!!")

BTW, here is my code for generating Places2 validation images of 256x256 resolution and the corresponding image file list. This code generates 10,000 images given the file list of Places2 validation set.

import os
import cv2
import numpy as np

image_list = np.genfromtxt('/gdata1/places2_flist/places2_valid.flist', dtype=np.str)
save_dir = '/gdata/test_set/places2_img'
img_path_list = []

for i in range(10000):
    img = cv2.imread(image_list[i])[:,:,::-1]
    h, w = img.shape[:2]
    if h >= 256 and w >= 256:
        h_start = (h-256) // 2
        w_start = (w-256) // 2
        img = img[h_start: h_start+256, w_start: w_start+256, :]
    else:
        img = cv2.resize(img, (256, 256))
    img_path = save_dir+'/img_%05d.png' % i
    img_path_list.append(img_path)
    cv2.imwrite(img_path, img[:,:,::-1])
    print("Processing %05d image..." % i)
    sys.stdout.flush()

fo = open('/gdata/test_set/flist/places2_img.flist', "w")
fo.write("\n".join(img_path_list))
fo.close()

print("FINISHED!!!")

Certainly, you can modify the code to generate masks with 128x128 center holes or testing images of other datasets.

Best wishes,
Jialun

I appreciate your help.

I experimented with some images from the ADE20k dataset using your pretrained model(places2_random). However, artifacts appeared as shown in the following images. Are these results normal?

00002
00001

The first result seems acceptable. The second one is poor, probably because the scene is too complicated.

I appreciate your quick response!

Did you find any of these artifacts in your experiments?

When the scene is too complicated, these artifacts may happen. You can try to generate multiple results and select a plausible one from them.

Thank you for your kind support!
OK! I will try to generate multiple results.

Certainly, you can modify the code to generate masks with 128x128 center holes or testing

Thanks for your good suggestions.

Here is my code for generating random masks and the corresponding mask file list in the testing. This code generates 10,000 masks, where each even-numbered mask is composed of three random rectangles, and each odd-numbered mask is composed of five random free-form strokes.

import os
import cv2
import sys
import numpy as np
import tensorflow as tf

import net.nn as nn

save_dir = '/gdata/test_set/random_mask'

bbox1 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask1 = nn.bbox2mask(bbox1, 256, 256, 64, name='mask_r1')
bbox2 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask2 = nn.bbox2mask(bbox2, 256, 256, 64, name='mask_r2')
bbox3 = nn.random_bbox(256, 256, 0, 128, random_mask=True)
regular_mask3 = nn.bbox2mask(bbox3, 256, 256, 64, name='mask_r3')
irregular_mask1 = nn.brush_stroke_mask(256, 256, name='mask_i1')
irregular_mask2 = nn.brush_stroke_mask(256, 256, name='mask_i2')
irregular_mask3 = nn.brush_stroke_mask(256, 256, name='mask_i3')
irregular_mask4 = nn.brush_stroke_mask(256, 256, name='mask_i4')
irregular_mask5 = nn.brush_stroke_mask(256, 256, name='mask_i5')

mask1 = tf.cast(tf.cast(regular_mask1 + regular_mask2 + regular_mask3, tf.bool), tf.float32) * 255
mask1 = tf.cast(mask1, tf.uint8)
mask2 = tf.cast(tf.cast(irregular_mask1 + irregular_mask2 + irregular_mask3 + irregular_mask4+ irregular_mask5, tf.bool), tf.float32) *255
mask2 = tf.cast(mask2, tf.uint8)

# TF session
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
    mask_path_list = []
    for i in range(10000):
        if i % 2:
            mask_np = sess.run(mask1)[0,:,:,:]
        if not i % 2:
            mask_np = sess.run(mask2)[0,:,:,:]
        mask_path = save_dir+'/mask_%05d.png' % i
        mask_path_list.append(mask_path)
        cv2.imwrite(mask_path, mask_np)
        print("Processing %05d random mask..." % i)
        sys.stdout.flush()

    fo = open('/gdata/test_set/flist/random_mask.flist', "w")
    fo.write("\n".join(mask_path_list))
    fo.close()

    print("FINISHED!!!")

sry,why i used the code successfully,but cann't save mask image.
image

i holp y some support!

BTW, here is my code for generating Places2 validation images of 256x256 resolution and the corresponding image file list. This code generates 10,000 images given the file list of Places2 validation set.

import os
import cv2
import numpy as np

image_list = np.genfromtxt('/gdata1/places2_flist/places2_valid.flist', dtype=np.str)
save_dir = '/gdata/test_set/places2_img'
img_path_list = []

for i in range(10000):
    img = cv2.imread(image_list[i])[:,:,::-1]
    h, w = img.shape[:2]
    if h >= 256 and w >= 256:
        h_start = (h-256) // 2
        w_start = (w-256) // 2
        img = img[h_start: h_start+256, w_start: w_start+256, :]
    else:
        img = cv2.resize(img, (256, 256))
    img_path = save_dir+'/img_%05d.png' % i
    img_path_list.append(img_path)
    cv2.imwrite(img_path, img[:,:,::-1])
    print("Processing %05d image..." % i)
    sys.stdout.flush()

fo = open('/gdata/test_set/flist/places2_img.flist', "w")
fo.write("\n".join(img_path_list))
fo.close()

print("FINISHED!!!")

I only have one file of image, and I want to generate mask image.
which part should I change for the code?
I try :
image_list = np.genfromtxt('/mydata/image', dtype=np.str)
fo = open('/mydata/image', "w")
but error
so, what should i do? could you give me some advice?
thx

sry,why i used the code successfully,but cann't save mask image.
image

i holp y some support!

Did you create the random_mask/ folder? If the path doesn't exist, the images will not be saved without raising any error.

sry,why i used the code successfully,but cann't save mask image.
image
i holp y some support!

Did you create the random_mask/ folder? If the path doesn't exist, the images will not be saved without raising any error.
yes,i created
image

This folder is /.../.../random_mask, I think you should change save_dir = '/random_mask' to save_dir = '/.../.../random_mask'

I only have one file of image, and I want to generate mask image.
which part should I change for the code?
I try :
image_list = np.genfromtxt('/mydata/image', dtype=np.str)
fo = open('/mydata/image', "w")
but error
so, what should i do? could you give me some advice?
thx

You can make a file list with only one line for this image. Also, you need to change for i in range(10000): to for i in range(1):