1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
4 - 這些順序調(diào)用的事情是,它們簡(jiǎn)化了我們操作數(shù)據(jù)集以應(yīng)用轉(zhuǎn)換的工作,并且他們還聲稱這是一種加載和處理數(shù)據(jù)的更具性能的方式。關(guān)于模塊化/簡(jiǎn)單性,我猜它完成了它的工作,因?yàn)槟梢暂p松加載、將其傳遞給整個(gè)預(yù)處理管道、隨機(jī)播放并使用幾行代碼迭代批量數(shù)據(jù)。
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)
例如,為了加載我的數(shù)據(jù)集并為我的模型提供預(yù)處理數(shù)據(jù),這就是我所要做的。
3 - 他們定義了一個(gè)預(yù)處理函數(shù),他們的數(shù)據(jù)集被映射到,這意味著每次請(qǐng)求樣本時(shí)都會(huì)應(yīng)用映射函數(shù),就像在我的情況下,我使用解析函數(shù)來(lái)解析我的使用前 TFRecord 格式的數(shù)據(jù):
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
(另一個(gè)例子) - 從上面的解析函數(shù),我可以使用下面的代碼來(lái)創(chuàng)建一個(gè)數(shù)據(jù)集,遍歷我的測(cè)試集圖像并繪制它們。
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()
添加回答
舉報(bào)