2017-05-18 3 views
5

का उपयोग करने के बाद एक छवि को पुनर्निर्माण करना मेरे पास एक ऑटोकोडर है जो एक इनपुट के रूप में एक छवि लेता है और आउटपुट के रूप में एक नई छवि उत्पन्न करता है।extract_image_patches

इनपुट छवि (1x1024x1024x3) नेटवर्क पर खिलाए जाने से पहले पैच (1024x32x32x3) में विभाजित है।

एक बार मेरे पास आउटपुट होने के बाद, पैच आकार का एक बैच 1024x32x32x3 है, मैं 1024x1024x3 छवि का पुनर्निर्माण करने में सक्षम होना चाहता हूं। मैंने सोचा कि मैंने बस इसे फिर से बदलकर चिल्लाया था, लेकिन यह हुआ कि क्या हुआ।

पहले, छवि के रूप में Tensorflow द्वारा पढ़ा:

Patched input #168Patched input #169: Input image

मैं निम्नलिखित कोड

patch_size = [1, 32, 32, 1] 
patches = tf.extract_image_patches([image], 
    patch_size, patch_size, [1, 1, 1, 1], 'VALID') 
patches = tf.reshape(patches, [1024, 32, 32, 3]) 

यहाँ के साथ छवि समझौता इस छवि से पैच के एक जोड़े हैं

लेकिन जब मैं इस पैच डेटा को दोबारा बदलता हूं एन छवि है कि चीजें नाशपाती के आकार जाते हैं।

reconstructed = tf.reshape(patches, [1, 1024, 1024, 3]) 
converted = tf.image.convert_image_dtype(reconstructed, tf.uint8) 
encoded = tf.image.encode_png(converted) 

Reconstructed output

इस उदाहरण में, कोई प्रसंस्करण पैचिंग और पुनर्निर्माण के बीच किया गया है। मैंने version of the code बनाया है जिसका उपयोग आप इस व्यवहार का परीक्षण करने के लिए कर सकते हैं। इसका इस्तेमाल करने के लिए, चलाने के निम्नलिखित:

echo "/path/to/test-image.png" > inputs.txt 
mkdir images 
python3 image_test.py inputs.txt images 

कोड प्रत्येक इनपुट छवि में 1024 पैच से प्रत्येक के लिए एक इनपुट छवि, एक पैच छवि, और एक उत्पादन छवि हो जाएंगी, ताकि लाइनों है कि इनपुट बनाने बाहर टिप्पणी और आउटपुट छवियों यदि आप केवल सभी पैच को बचाने में चिंतित हैं।

किसी कृपया समझा क्या हुआ :(

+0

CJxD, मेरा उत्तर पर एक नजर है। आप अपने कोड में होने वाली गलती को देखने के लिए पैच प्रिंट कर सकते हैं और पहले भाग में पुनर्निर्मित कर सकते हैं। मुझे उम्मीद है कि मेरा जवाब आपकी मदद करेगा। – hars

+0

"वह चीजें नाशपाती के आकार में जाती हैं"। मैं उस अभिव्यक्ति को उधार लेने जा रहा हूँ! –

उत्तर

3

उपयोग अद्यतन # 2 - अपने काम के लिए एक छोटा सा उदाहरण: (TF 1,0)

आकार (4,4,1) की छवि को ध्यान में रखते करने के लिए परिवर्तित आकार (4,2,2,1) के धब्बे और उन्हें छवि के लिए वापस फिर से बनाया

import tensorflow as tf 
image = tf.constant([[[1], [2], [3], [4]], 
       [[5], [6], [7], [8]], 
       [[9], [10], [11], [12]], 
       [[13], [14], [15], [16]]]) 

patch_size = [1,2,2,1] 
patches = tf.extract_image_patches([image], 
    patch_size, patch_size, [1, 1, 1, 1], 'VALID') 
patches = tf.reshape(patches, [4, 2, 2, 1]) 
reconstructed = tf.reshape(patches, [1, 4, 4, 1]) 
rec_new = tf.space_to_depth(reconstructed,2) 
rec_new = tf.reshape(rec_new,[4,4,1]) 

sess = tf.Session() 
I,P,R_n = sess.run([image,patches,rec_new]) 
print(I) 
print(I.shape) 
print(P.shape) 
print(R_n) 
print(R_n.shape) 

आउटपुट:।

[[[ 1][ 2][ 3][ 4]] 
    [[ 5][ 6][ 7][ 8]] 
    [[ 9][10][11][12]] 
    [[13][14][15][16]]] 
(4, 4, 1) 
(4, 2, 2, 1) 
[[[ 1][ 2][ 3][ 4]] 
    [[ 5][ 6][ 7][ 8]] 
    [[ 9][10][11][12]] 
    [[13][14][15][16]]] 
(4,4,1) 

अपडेट - 3 चैनल (डिबगिंग ..)

केवल पी = sqrt (ज) के लिए काम

import tensorflow as tf 
import numpy as np 
c = 3 
h = 1024 
p = 32 

image = tf.random_normal([h,h,c]) 
patch_size = [1,p,p,1] 
patches = tf.extract_image_patches([image], 
    patch_size, patch_size, [1, 1, 1, 1], 'VALID') 
patches = tf.reshape(patches, [h, p, p, c]) 
reconstructed = tf.reshape(patches, [1, h, h, c]) 
rec_new = tf.space_to_depth(reconstructed,p) 
rec_new = tf.reshape(rec_new,[h,h,c]) 

sess = tf.Session() 
I,P,R_n = sess.run([image,patches,rec_new]) 
print(I.shape) 
print(P.shape) 
print(R_n.shape) 
err = np.sum((R_n-I)**2) 
print(err) 

आउटपुट के लिए:

(1024, 1024, 3) 
(1024, 32, 32, 3) 
(1024, 1024, 3) 
0.0 

अद्यतन 2

से पुनर्निर्माण extract_image_patches का आउटपुट मुश्किल लगता है। पैच निकालने के लिए प्रयुक्त अन्य कार्यों और पुनर्निर्माण के लिए प्रक्रिया को उलट देता है जो आसान लगता है।

import tensorflow as tf 
import numpy as np 
c = 3 
h = 1024 
p = 128 


image = tf.random_normal([1,h,h,c]) 

# Image to Patches Conversion 
pad = [[0,0],[0,0]] 
patches = tf.space_to_batch_nd(image,[p,p],pad) 
patches = tf.split(patches,p*p,0) 
patches = tf.stack(patches,3) 
patches = tf.reshape(patches,[(h/p)**2,p,p,c]) 

# Do processing on patches 
# Using patches here to reconstruct 
patches_proc = tf.reshape(patches,[1,h/p,h/p,p*p,c]) 
patches_proc = tf.split(patches_proc,p*p,3) 
patches_proc = tf.stack(patches_proc,axis=0) 
patches_proc = tf.reshape(patches_proc,[p*p,h/p,h/p,c]) 

reconstructed = tf.batch_to_space_nd(patches_proc,[p, p],pad) 

sess = tf.Session() 
I,P,R_n = sess.run([image,patches,reconstructed]) 
print(I.shape) 
print(P.shape) 
print(R_n.shape) 
err = np.sum((R_n-I)**2) 
print(err) 

आउटपुट:

(1, 1024, 1024, 3) 
(64, 128, 128, 3) 
(1, 1024, 1024, 3) 
0.0 

आप यहाँ अन्य शांत टेन्सर परिवर्तन कार्यों देख सकते हैं: https://www.tensorflow.org/api_guides/python/array_ops

+0

यदि मैं अलग पैच आकार सेट करता हूं तो आपका दूसरा उदाहरण क्रैश हो जाता है। पी = 64 उदाहरण के लिए – Temak

+0

पैच को दोबारा बदलने के दौरान आपको "एच" समायोजित करना होगा। इसे अभी अपडेट कर देगा। – hars

+0

यदि आप 'tf.reshape (पैच, [(एच/पी) ** 2, पी, पी, सी] सेट करते हैं तो यह क्रैश हो जाता है)' – Temak

2

tf.extract_image_patches शांत उपयोग करने के लिए मुश्किल है, के रूप में यह पृष्ठभूमि में सामान का एक बहुत है।

यदि आपको केवल गैर-ओवरलैपिंग की आवश्यकता है, तो इसे स्वयं लिखना बहुत आसान है। आप image_to_patches में सभी परिचालनों को परिवर्तित करके पूर्ण छवि का पुनर्निर्माण कर सकते हैं।

कोड का नमूना (भूखंडों मूल छवि और पैच):

import tensorflow as tf 
from skimage import io 
import matplotlib.pyplot as plt 


def image_to_patches(image, patch_height, patch_width): 
    # resize image so that it's dimensions are dividable by patch_height and patch_width 
    image_height = tf.cast(tf.shape(image)[0], dtype=tf.float32) 
    image_width = tf.cast(tf.shape(image)[1], dtype=tf.float32) 
    height = tf.cast(tf.ceil(image_height/patch_height) * patch_height, dtype=tf.int32) 
    width = tf.cast(tf.ceil(image_width/patch_width) * patch_width, dtype=tf.int32) 

    num_rows = height // patch_height 
    num_cols = width // patch_width 
    # make zero-padding 
    image = tf.squeeze(tf.image.resize_image_with_crop_or_pad(image, height, width)) 

    # get slices along the 0-th axis 
    image = tf.reshape(image, [num_rows, patch_height, width, -1]) 
    # h/patch_h, w, patch_h, c 
    image = tf.transpose(image, [0, 2, 1, 3]) 
    # get slices along the 1-st axis 
    # h/patch_h, w/patch_w, patch_w,patch_h, c 
    image = tf.reshape(image, [num_rows, num_cols, patch_width, patch_height, -1]) 
    # num_patches, patch_w, patch_h, c 
    image = tf.reshape(image, [num_rows * num_cols, patch_width, patch_height, -1]) 
    # num_patches, patch_h, patch_w, c 
    return tf.transpose(image, [0, 2, 1, 3]) 


image = io.imread('http://www.petful.com/wp-content/uploads/2011/09/slow-blinking-cat.jpg') 
print('Original image shape:', image.shape) 
tile_size = 200 
image = tf.constant(image) 
tiles = image_to_patches(image, tile_size, tile_size) 

sess = tf.Session() 
I, tiles = sess.run([image, tiles]) 
print(I.shape) 
print(tiles.shape) 


plt.figure(figsize=(1 * (4 + 1), 5)) 
plt.subplot(5, 1, 1) 
plt.imshow(I) 
plt.title('original') 
plt.axis('off') 
for i, tile in enumerate(tiles): 
    plt.subplot(5, 5, 5 + 1 + i) 
    plt.imshow(tile) 
    plt.title(str(i)) 
    plt.axis('off') 
plt.show()