... बदलेगी देश डेटासेट: फसल/आकार बदलने के प्रति छवियों के बैच के बाद डेटासेट.बैच()

0

सवाल

यह संभव है करने के लिए फसल/आकार छवियों प्रति बैच ?

मैं का उपयोग कर रहा हूँ ... बदलेगी देश डेटासेट एपीआई के रूप में नीचे:

dataset = dataset.shuffle().repeat().batch(batch_size, drop_remainder=True)

मैं करना चाहते हैं, बैच के भीतर सभी छवियों को करना चाहिए एक ही आकार है । हालांकि भर में बैचों यह अलग अलग आकार हो सकता.

उदाहरण के लिए, 1 बैच के सभी छवियों के आकार (batch_size, 300, 300, 3). अगले बैच कर सकते हैं छवियों के आकार (batch_size, 224, 224, 3). एक और बैच हो सकता है छवियों का आकार (batch_size, 400, 400, 3).

मूल रूप से मैं चाहता हूँ करने के लिए है dymanically आकार बैचों, लेकिन सभी के भीतर छवियों के बैच है, स्थिर आकार.

अगर हम का पालन के रूप में:

dataset = dataset.shuffle().repeat().batch(batch_size, drop_remainder=True).map(lambda x, y: map_fn(x, y))

करता है ऊपर .नक्शा() के लिए लागू होता है प्रत्येक बैच अलग-अलग या पूरे डाटासेट ?

अगर ऊपर .नक्शा() नहीं लागू करने के लिए प्रत्येक बैच अलग है, कैसे कर सकते हैं हम ऐसा ? हम कर सकते हैं किसी भी परिभाषित इटरेटर के बाद डेटासेट.बैच(), लागू tf.छवि । crop_and_resize (अधिक) प्रत्येक छवि के प्रति बैच और बाद में उपयोग डेटासेट.जुटना() गठबंधन करने के लिए सभी बदल बैचों ?

मैं पैदा कर रहा हूँ डाटासेट नीचे के रूप में:

# Dataset creation (read image data from files of COCO dataset)
dataset = tf.data.Dataset.list_files(self._file_pattern, shuffle=False)
dataset = dataset.shard(dataset_num_shards, dataset_shard_index)
dataset = dataset.shuffle(tf.cast(256 / dataset_num_shards, tf.int64))
dataset = dataset.interleave(map_func=tf.data.TFRecordDataset(filename).prefetch(1), cycle_length=32, block_length=1, num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.map(tf_example_decoder.TfExampleDecoder().decode, num_parallel_calls=64)
dataset = dataset.shuffle(64).repeat()
# Parse each image for preprocessing
dataset = dataset.map(lambda data, _: _parse_example(data), num_parallel_calls=64)
dataset = dataset.batch(batch_size=batch_size, drop_remainder=True)

# Below code suggested by you to resize images to fixed shape in each batch
def resize_data(images, labels):
    tf.print('Original shape -->', tf.shape(images))
    SIZE = (300, 300)
    return tf.image.resize(images, SIZE), labels
dataset = dataset.map(resize_data)
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)

tf.estimator.Estimator(...).train(
        input_fn=dataset,
        steps=steps,
        hooks=train_hooks)
python tensorflow tensorflow-datasets
2021-11-24 05:50:45
1

सबसे अच्छा जवाब

1

आम तौर पर, आप की कोशिश कर सकते हैं कुछ इस तरह है:

import tensorflow as tf
import numpy as np

dataset1 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 300, 300, 3)))
dataset2 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 224, 224, 3)))
dataset3 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 400, 400, 3)))
dataset = dataset1.concatenate(dataset2.concatenate(dataset3))
dataset = dataset.shuffle(1).repeat().batch(32, drop_remainder=True)

def resize_data(images):
  tf.print('Original shape -->', tf.shape(images))
  SIZE = (180, 180)

  return tf.image.resize(images, SIZE)

dataset = dataset.map(resize_data)

for images in dataset.take(3):
  tf.print('New shape -->', tf.shape(images))
Original shape --> [32 300 300 3]
New shape --> [32 180 180 3]
Original shape --> [32 224 224 3]
New shape --> [32 180 180 3]
Original shape --> [32 400 400 3]
New shape --> [32 180 180 3]

आप भी इस्तेमाल कर सकते tf.image.resize_with_crop_or_pad यदि आप चाहते हैं:

def resize_data(images):
  tf.print('Original shape -->', tf.shape(images))
  SIZE = (180, 180)
  return tf.image.resize_with_crop_or_pad(images, SIZE[0], SIZE[1])

dataset = dataset.map(resize_data)

for images in dataset.take(3):
  tf.print('New shape -->', tf.shape(images))

ध्यान दें कि का उपयोग कर repeat() जाएगा बनाने के लिए एक अनंत डेटासेट.

अद्यतन 1

यदि आप चाहते हैं एक यादृच्छिक आकार के प्रत्येक बैच के लिए, की कोशिश कुछ इस तरह है:

import tensorflow as tf
import numpy as np

dataset1 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 300, 300, 3)))
dataset2 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 224, 224, 3)))
dataset3 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 400, 400, 3)))
dataset = dataset1.concatenate(dataset2.concatenate(dataset3))
dataset = dataset.batch(32, drop_remainder=True).shuffle(96)


def resize_data(images):
  batch_size = tf.shape(images)[0]
  images_resized = tf.TensorArray(dtype=tf.float32, size = 0, dynamic_size=True)
  SIZE = tf.random.uniform((2,), minval=300, maxval=500, dtype=tf.int32)
  for i in range(batch_size):
    images_resized = images_resized.write(images_resized.size(), tf.image.resize(images[i], SIZE))
  return images_resized.stack()

dataset = dataset.map(resize_data)

for images in dataset:
  tf.print('New shape -->', tf.shape(images))
New shape --> [32 392 385 3]
New shape --> [32 468 459 3]
New shape --> [32 466 461 3]

अद्यतन 2

एक बहुत लचीला विकल्प है कि काम करता है के लिए किसी भी बैच का आकार इस तरह दिखेगा:

import tensorflow as tf
import numpy as np

dataset1 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 300, 300, 3)))
dataset2 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 224, 224, 3)))
dataset3 = tf.data.Dataset.from_tensor_slices(np.random.random((32, 400, 400, 3)))
dataset = dataset1.concatenate(dataset2.concatenate(dataset3))

def resize_and_batch(dataset, batch_size):
  final_dataset = None
  duration = len(dataset)//batch_size
  random_sizes = [tf.random.uniform((2,), minval=300, maxval=500, dtype=tf.int32) for _ in range(duration)]

  for i, size in zip(range(duration), random_sizes):
    idx = i * batch_size
    if i == 0:
      final_dataset = tf.data.Dataset.from_tensor_slices([tf.image.resize(x, size) for x in dataset.take(batch_size)])
    else:
      final_dataset = final_dataset.concatenate(tf.data.Dataset.from_tensor_slices([tf.image.resize(x, size) for x in dataset.skip(idx).take(batch_size)]))
  return final_dataset

batch_size = 10
ds = resize_and_batch(dataset, batch_size)
ds = ds.batch(batch_size).shuffle(len(ds))
for images in ds:
 tf.print('New shape -->', images.shape)
New shape --> TensorShape([10, 399, 348, 3])
New shape --> TensorShape([10, 356, 329, 3])
New shape --> TensorShape([10, 473, 373, 3])
New shape --> TensorShape([10, 489, 489, 3])
New shape --> TensorShape([10, 421, 335, 3])
New shape --> TensorShape([10, 447, 455, 3])
New shape --> TensorShape([10, 355, 382, 3])
New shape --> TensorShape([10, 310, 396, 3])
New shape --> TensorShape([10, 345, 356, 3])
2021-12-01 14:51:04

यह अच्छा लग रहा है । हालांकि यह अभी भी बाहर काम नहीं कर रहा मेरे लिए. जब मैं कोशिश करने के लिए ट्रेन के मॉडल के लिए, यह त्रुटि देता है नीचे की तरह: INVALID_ARGUMENT: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [640,426,3], [batch]: [480,640,3] हालांकि मैं दे दिया आकार = (300, 300) में tf.छवि । आकार(छवियों, आकार), बैच आकार = (480, 640). और अगले के रूप में छवि के विभिन्न आकार = (640, 426), यह विफल करने के लिए इसे जोड़ने के बैच के लिए. इसका मतलब है कि किसी भी तरह से यह करने में सक्षम नहीं लागू होते हैं । नक्शा() समारोह पर प्रत्येक व्यक्ति के बैच. किसी भी मदद/विचार है ?
Avid Learner

आप कोड जोड़ने पर कैसे आप कर रहे हैं बनाने के अपने डेटासेट के लिए अपने सवाल है? मुझे लगता है कि मैं एक सुराग नहीं है क्या समस्या हो सकती है.
AloneTogether

मैं अद्यतन के साथ सवाल कैसे मैं पैदा कर रहा हूँ डाटासेट. आपके जवाब का इंतजार.
Avid Learner

अद्यतन जवाब-
AloneTogether

batch_size=16. यह फेंकने के लिए एक ही त्रुटि के साथ batch_size > 1.
Avid Learner

अपनी मेहनत को समझने के लिए वास्तव में आप क्या कर रहे हैं के बिना किसी भी उपयोग करने के लिए डाटासेट का उपयोग कर रहे हैं और चर परिभाषित किया है कहीं और. समस्या यह है कि शायद प्रत्येक बैच नहीं है छवियों के सभी एक ही आकार की है ।
AloneTogether

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में