# Problem loading image patches using pix2pix as a modle

**URL:** <https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282>\
**Category:** General Discussion\
**Tags:** tf-model, models, datasets\
**Created:** [October 31, 2023, 4:20am UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282 "2023-10-31T04:20:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/rafael_scatena/32/16875_2.png) [@Rafael\_Scatena](https://discuss.ai.google.dev/u/Rafael_Scatena)\
**Post date:** [October 31, 2023, 4:20am UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/1 "2023-10-31T04:20:28Z")

</div>

Hello there I would like to load image patches in my tensorflow model. I followed pix2pix loading example and adapted it to my model. But I cannot load the patches. Thanks for the assistance. Here is my code:

def load2(image\_file):  
image\_ts = tf.io.read\_file(image\_file)  
image\_ts = tf.image.decode\_png(image\_ts)

```
# Calculate the center of the image
w_center = 358

# Split the image into two parts
input_image = image_ts[:, w_center:, :]
real_image = image_ts[:, :w_center, :]

input_image = tf.cast(input_image, tf.float32)
real_image = tf.cast(real_image, tf.float32)

input_image, real_image = resize(input_image, real_image, IMG_HEIGHT, IMG_WIDTH)

input_patches = []
gt_patches = []

# Extract patches using tf.image.extract_patches
input_patches = tf.image.extract_patches(input_image[tf.newaxis, ...],sizes=[1, patch_size[0], patch_size[1], 1],
strides=[1, stride[0], stride[1], 1],rates = [1, 1, 1, 1])

gt_patches = tf.image.extract_patches(real_image[tf.newaxis, ...],sizes=[1, patch_size[0], patch_size[1], 1],
strides=[1, stride[0], stride[1], 1],rates = [1, 1, 1, 1])

# Reshape patches to get a list of individual patches

```

# input\_patches = tf.reshape(input\_patches, [-1, patch\_size[0], patch\_size[1], 3])

# gt\_patches = tf.reshape(gt\_patches, [-1, patch\_size[0], patch\_size[1], 3])

```
return input_patches, gt_patches, image_file

```

# LOAD TRAIN

def load\_image\_train(image\_file):  
input\_image=  
real\_image=  
input\_image, real\_image, image\_file = load2(image\_file)

# input\_image, real\_image = random\_jitter(input\_image, real\_image)

input\_image, real\_image = normalize\_images(input\_image, real\_image)

return input\_image, real\_image, image\_file

input\_directory\_train = “/home/rafael/Área de Trabalho/Dataset-PNG”  
input\_directory\_test = “/home/rafael/Área de Trabalho/Dataset-PNG/Test”  
input\_directory\_val = “/home/rafael/Área de Trabalho/Dataset-PNG/Val”

# Use glob to get a list of image file paths in the input directory

image\_files = glob.glob(input\_directory\_train + “/_.png") # Change the file extension as needed  
image\_files\_test = glob.glob(input\_directory\_test + "/_.png”) # Change the file extension as needed  
image\_files\_val = glob.glob(input\_directory\_val + “/\*.png”) # Change the file extension as needed

# Create a dataset from the list of image files

dataset = tf.data.Dataset.from\_tensor\_slices(image\_files)  
dataset = dataset.map(load\_image\_train,num\_parallel\_calls=tf.data.AUTOTUNE)

# Define batch size and other data pipeline operations (e.g., shuffle, repeat, etc.) batch\_size = 32

dataset = dataset.shuffle(buffer\_size=1000) # Shuffle the data  
dataset = dataset.batch(BATCH\_SIZE) # Batch the data  
dataset = dataset.prefetch(buffer\_size=tf.data.experimental.AUTOTUNE) # Prefetch for better performanc

# Skip the first N elements and take the next M elements

N = 0  
M = 200  
dataset = dataset.skip(N).take(M)

---

<div class="post-metadata">

**Author:** ![tagoma](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/tagoma/32/19434_2.png) [@tagoma](https://discuss.ai.google.dev/u/tagoma)\
**Post date:** [October 31, 2023, 12:23pm UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/3 "2023-10-31T12:23:04Z")

</div>

Hi @Rafael_Scatena & welcome to the Tensorflow forum.  
Can you please be more specific about the issue you are facing? Can you also provide with details about your dataset and any other piece of relevant information? What are message do you get?  
Thank you.

---

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/rafael_scatena/32/16875_2.png) [@Rafael\_Scatena](https://discuss.ai.google.dev/u/Rafael_Scatena)\
**Post date:** [October 31, 2023, 3:09pm UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/5 "2023-10-31T15:09:38Z")

</div>

I get the message that the input shape is (1,0,16,16,1) instead of (1,16,16,1). It should be (1,16,16,1).

---

<div class="post-metadata">

**Author:** ![Kiran\_Sai\_Ramineni](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/kiran_sai_ramineni/32/8859_2.png) [@Kiran\_Sai\_Ramineni](https://discuss.ai.google.dev/u/Kiran_Sai_Ramineni)\
**Post date:** [November 1, 2023, 9:08am UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/7 "2023-11-01T09:08:37Z")

</div>

> [@Rafael\_Scatena](#):
>
> the input shape is (1,0,16,16,1) instead of (1,16,16,1). It should be (1,16,16,1).

Hi @Rafael_Scatena, From the above error i can say that the error is due to the passing of the incorrect input shape. It may happen during the processing of the data.

Could you please share the stand alone code in colab gist to reproduce the issue. Also if possible please share the sample data so that it would be easy to reproduce the issue and find out the cause of the error. Thank You.

---

<div class="post-metadata">

**Author:** ![Alizaa2](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Alizaa2](https://discuss.ai.google.dev/u/Alizaa2)\
**Post date:** [November 3, 2023, 4:11am UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/9 "2023-11-03T04:11:34Z")

</div>

It looks like you’re trying to load and process image patches for a TensorFlow model. I see a few issues in your code. Let’s address them:

1. Imports: Make sure you have the necessary imports at the beginning of your code, such as importing TensorFlow and any other required libraries.

2. Function Definitions: You provided code snippets, but it’s not clear if you have defined the functions like `resize`, `random_jitter`, and `normalize_images`. Ensure that these functions are correctly defined.

3. String Quotes: In your code, you are using smart quotes (e.g., “ and ”) instead of regular quotes ("). Please replace them with regular double quotes.

4. Incorrect File Extensions: You are using incorrect file extensions for image file filtering. The correct extensions should be “.png,” not “/.png” or "\*.png”.

5. `load_image_train` Function: This function seems to return multiple values, but your code doesn’t capture those returned values correctly. Make sure to unpack the values returned by the `load2` function correctly.

6. `dataset` Configuration: Ensure that you have defined variables like `IMG_HEIGHT`, `IMG_WIDTH`, `patch_size`, and `stride` with appropriate values. Also, the variable `BATCH_SIZE` should be defined.

7. File Paths: Verify that the file paths in `input_directory_train`, `input_directory_test`, and `input_directory_val` are correct.

8. File Loading: The code uses `glob` to get a list of image file paths, but you should ensure that the file paths are correctly obtained, and the file extensions match the actual extensions in your dataset directory.

9. Data Pipeline Operations: You have defined data pipeline operations like shuffle, batch, and prefetch correctly. However, make sure that `N` and `M` are set to appropriate values.

Once you address these issues [cursed wikihow images](https://cursedimages.us/cursed-furby-images/) and provide additional context or error messages if you encounter any problems, I can help you further with specific questions or issues you’re facing.

---

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/rafael_scatena/32/16875_2.png) [@Rafael\_Scatena](https://discuss.ai.google.dev/u/Rafael_Scatena)\
**Post date:** [November 6, 2023, 4:01pm UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/14 "2023-11-06T16:01:33Z")

</div>

gist code: [MTANN3.ipynb · GitHub](https://gist.github.com/RafaelOScatena/64bd9b87add1e98e1d6e5cf88f29855d)

---

<div class="post-metadata">

**Author:** ![Rafael\_Scatena](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/rafael_scatena/32/16875_2.png) [@Rafael\_Scatena](https://discuss.ai.google.dev/u/Rafael_Scatena)\
**Post date:** [November 6, 2023, 4:12pm UTC](https://discuss.ai.google.dev/t/problem-loading-image-patches-using-pix2pix-as-a-modle/29282/15 "2023-11-06T16:12:52Z")

</div>

The dataset with .png is this one: [https://drive.google.com/drive/folders/1O\_Ls-FHlHOE-ej-3kNGFqS3Ov4Kq2AB2?usp=sharing](https://drive.google.com/drive/folders/1O_Ls-FHlHOE-ej-3kNGFqS3Ov4Kq2AB2?usp=sharing)
