Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mask images and file lists #10

Closed
naoki7090624 opened this issue Jul 5, 2021 · 14 comments
Closed

Mask images and file lists #10

naoki7090624 opened this issue Jul 5, 2021 · 14 comments

Comments

@naoki7090624
Copy link

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?

@USTC-JialunPeng
Copy link
Owner

USTC-JialunPeng commented Jul 7, 2021

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!!!")

@USTC-JialunPeng
Copy link
Owner

USTC-JialunPeng commented Jul 7, 2021

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!!!")

@USTC-JialunPeng
Copy link
Owner

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

Best wishes,
Jialun

@naoki7090624
Copy link
Author

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

@USTC-JialunPeng
Copy link
Owner

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

@naoki7090624
Copy link
Author

I appreciate your quick response!

Did you find any of these artifacts in your experiments?

@USTC-JialunPeng
Copy link
Owner

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

@naoki7090624
Copy link
Author

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

@zouzouwei
Copy link

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!

@zouzouwei
Copy link

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

@USTC-JialunPeng
Copy link
Owner

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.

@zouzouwei
Copy link

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

@USTC-JialunPeng
Copy link
Owner

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

@USTC-JialunPeng
Copy link
Owner

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants