제너레이터에 매핑되는 증강 기능이 있습니다. 그러나 어떤 이유로 tfa.image.rotate
함수가 오류를 일으 킵니다.
def customGenerator(input_file_paths, dims, data_type):
for i, file_path in enumerate(input_file_paths):
if data_type.decode("utf-8") in ["png" or "tif"]:
img = plt.imread((file_path.decode("utf-8")))
elif data_type.decode("utf-8") == "npy":
img = np.load(file_path.decode("utf-8"))
x = resize(img[:,:,:3], dims)
yield x, x
def augment(image,label) :
print('image', image)
print('shape', image.shape)
print('type', type(image))
#angle = random.uniform(0, tf.constant(np.pi))
image = tfa.image.rotate(image, tf.constant(np.pi)
train_dataset = tf.data.Dataset.from_generator(generator=customGenerator,
output_types=(np.float32, np.float32),
output_shapes=(dims, dims),
args=[X_train_paths, dims, "png"])
train_dataset = train_dataset.map(augment, num_parallel_calls=AUTOTUNE)
나는 tfa.image.rotate
다른 사람들이 사용한 구현을 보았고 그들의 구현은 잘 작동했습니다. image
증가 함수에서 변수를 인쇄 해 보았습니다 . 그 결과 :
print('image', image) # these lines is in the augment function, result below
print('type', type(image))
image Tensor("args_0:0", shape=(256, 256, 3), dtype=float32)
type <class 'tensorflow.python.framework.ops.Tensor'>
반대로, 다른 사용자의 구현으로 이동하여 데이터 세트에 매핑되지 않은 이미지를 인쇄 할 때. 그들의 print(image)
그리고 print(type(image))
이것을 인쇄합니다 :
image tf.Tensor(
[[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
...
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
...
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(256, 256, 3), dtype=float32)
type <class 'tensorflow.python.framework.ops.EagerTensor'>
함수 image
에서 인쇄 할 때 이것이 인쇄 될 것으로 예상했습니다 augment
. 그래서 무슨 일이 일어나는지 잘 모르겠습니다. 몇 가지 질문이 있습니다. 또한 tf.executing_eagerly()
결과True
무슨 "args_0:0"
뜻인가요?
augment
함수 의 이미지 <class 'tensorflow.python.framework.ops.EagerTensor'>
가 일반 텐서 대신 유형이어야 합니까?
"args_0:0"
숫자 배열을 인쇄하는 위치에 예상했던 형식으로 만들 수있는 방법이 있습니까? 이것이 회전 기능을 고칠 것이라고 믿습니다.
마지막으로 그렇지 않은 경우 무작위 회전으로 이미지를 확대하는 더 좋은 방법이 있습니까?
시간과 도움에 감사드립니다.
코드가 작동하도록 변경했습니다.
암호:
import tensorflow_addons as tfa
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
def customGenerator(input_file_paths, dims, data_type):
for i, file_path in enumerate(input_file_paths):
image = tf.io.read_file(file_path)
image = tf.image.decode_png(image, channels = 3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [dims[0],dims[1]])
yield image, image
def augment(image,label) :
img = tfa.image.rotate(image, tf.constant(np.pi/8))
return (img, label)
X_train_paths = [os.path.join('data','img',name) for name in os.listdir('data/img')]
dims = (256,256,3)
train_dataset = tf.data.Dataset.from_generator(generator=customGenerator,
output_types=(tf.float32, tf.float32),
output_shapes=(dims, dims),
args=[X_train_paths, dims, "png"])
train_dataset = train_dataset.map(augment)
데이터 세트 반복 :
for images in train_dataset:
rotatedimg, normalimg= images[0],images[1]
break
산출:
plt.imshow(rotatedimg)
plt.imshow(normalimg)
기억해야 할 사항 :
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다