亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 tensorflow_datasets (tfds) 實現和理解預處理和數據擴充?

如何使用 tensorflow_datasets (tfds) 實現和理解預處理和數據擴充?

偶然的你 2022-10-18 17:22:41
我正在學習基于使用Oxford-IIIT Pets 的TF 2.0 教程的分割和數據增強。對于預處理/數據增強,它們為特定管道提供了一組功能:# Import datasetdataset, info = tfds.load('oxford_iiit_pet:3.*.*', with_info=True)def normalize(input_image, input_mask):  input_image = tf.cast(input_image, tf.float32) / 255.0  input_mask -= 1  return input_image, [email protected] load_image_train(datapoint):  input_image = tf.image.resize(datapoint['image'], (128, 128))  input_mask = tf.image.resize(datapoint['segmentation_mask'], (128, 128))  if tf.random.uniform(()) > 0.5:    input_image = tf.image.flip_left_right(input_image)    input_mask = tf.image.flip_left_right(input_mask)  input_image, input_mask = normalize(input_image, input_mask)  return input_image, input_maskTRAIN_LENGTH = info.splits['train'].num_examplesBATCH_SIZE = 64BUFFER_SIZE = 1000STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE鑒于 tf 語法,這段代碼給我帶來了幾個疑問。為了防止我只是做一個 ctrl C ctrl V 并真正理解 tensorflow 是如何工作的,我想問一些問題:1) 在normalize函數中,tf.cast(input_image, tf.float32) / 255.0可以通過tf.image.convert_image_dtype(input_image, tf.float32)?2) 在normalize函數中,可以在格式中更改我的 segmentation_mask 值tf.tensor而不更改為numpy?我想做的是只使用兩個可能的掩碼(0 和 1)而不是(0、1 和 2)。使用 numpy 我做了這樣的事情:segmentation_mask_numpy = segmentation_mask.numpy()segmentation_mask_numpy[(segmentation_mask_numpy == 2) | (segmentation_mask_numpy == 3)] = 0可以在沒有 numpy 轉換的情況下做到這一點嗎?3)在load_image_train函數中,他們說這個函數正在做數據增強,但是怎么做呢?在我看來,他們正在通過給定隨機數的翻轉來更改原始圖像,而不是根據原始圖像向數據集提供另一個圖像。那么,功能目標是更改圖像而不是向我的數據集添加保留原始圖像的 aug_image?如果我是正確的,如何更改此函數以提供 aug_image 并將原始圖像保留在數據集中?4) 在其他問題中,例如How to apply data augmentation in TensorFlow 2.0 after tfds.load()和TensorFlow 2.0 Keras: How to write image summary for TensorBoard他們使用了很多.map()順序調用或.map().map().cache().batch().repeat(). 我的問題是:有這個必要性嗎?是否存在更簡單的方法來做到這一點?我試圖閱讀 tf 文檔,但沒有成功。5)您建議使用此處ImageDataGenerator介紹的 keras或這種 tf 方法更好?
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

4 - 這些順序調用的事情是,它們簡化了我們操作數據集以應用轉換的工作,并且他們還聲稱這是一種加載和處理數據的更具性能的方式。關于模塊化/簡單性,我猜它完成了它的工作,因為您可以輕松加載、將其傳遞給整個預處理管道、隨機播放并使用幾行代碼迭代批量數據。


train_dataset =tf.data.TFRecordDataset(filenames=train_records_paths).map(parsing_fn)

train_dataset = train_dataset.shuffle(buffer_size=12000)

train_dataset = train_dataset.batch(batch_size)

train_dataset = train_dataset.repeat()

# Create a test dataset

test_dataset = tf.data.TFRecordDataset(filenames=test_records_paths).map(parsing_fn)

test_dataset = test_dataset.batch(batch_size)

test_dataset = test_dataset.repeat(1)

validation_steps = test_size / batch_size 

history = transferred_resnet50.fit(x=train_dataset,

                        epochs=epochs,

                        steps_per_epoch=steps_per_epoch,                        

                        validation_data=test_dataset,

                        validation_steps=validation_steps)

例如,為了加載我的數據集并為我的模型提供預處理數據,這就是我所要做的。


3 - 他們定義了一個預處理函數,他們的數據集被映射到,這意味著每次請求樣本時都會應用映射函數,就像在我的情況下,我使用解析函數來解析我的使用前 TFRecord 格式的數據:


def parsing_fn(serialized):

    features = \

        {

            'image': tf.io.FixedLenFeature([], tf.string),

            'label': tf.io.FixedLenFeature([], tf.int64)            

        }


    # Parse the serialized data so we get a dict with our data.

    parsed_example = tf.io.parse_single_example(serialized=serialized,

                                             features=features)


    # Get the image as raw bytes.

    image_raw = parsed_example['image']


    # Decode the raw bytes so it becomes a tensor with type.

    image = tf.io.decode_jpeg(image_raw)

    

    image = tf.image.resize(image,size=[224,224])

    

    # Get the label associated with the image.

    label = parsed_example['label']

    

    # The image and label are now correct TensorFlow types.

    return image, label

(另一個例子) - 從上面的解析函數,我可以使用下面的代碼來創建一個數據集,遍歷我的測試集圖像并繪制它們。


records_path = DATA_DIR+'/'+'TFRecords'+'/test/'+'test_0.tfrecord'

# Create a dataset

dataset = tf.data.TFRecordDataset(filenames=records_path)

# Parse the dataset using a parsing function 

parsed_dataset = dataset.map(parsing_fn)

# Gets a sample from the iterator

iterator = tf.compat.v1.data.make_one_shot_iterator(parsed_dataset) 


for i in range(100):

    image,label = iterator.get_next()

    img_array = image.numpy()

    img_array = img_array.astype(np.uint8)

    plt.imshow(img_array)

    plt.show()


查看完整回答
反對 回復 2022-10-18
  • 1 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號