未验证 提交 37bfe29f 编写于 作者: W wuzewu 提交者: GitHub

add component usage demo (#537)

add component usage demo
上级 9f2d7d09
import numpy as np
import wave
from visualdl import LogWriter
num_samples = 1
def read_audio_data(audio_path):
CHUNK = 4096
f = wave.open(audio_path, "rb")
wavdata = []
chunk = f.readframes(CHUNK)
while chunk:
data = np.fromstring(chunk, dtype='uint8')
wavdata.extend(data)
chunk = f.readframes(CHUNK)
# 8k sample rate, 16bit frame, 1 channel
shape = [8000, 2, 1]
return shape, wavdata
def record_audio(vdl_audio_comp):
sample_num = 0
record_times = num_samples * 3
audio_path = "test.wav"
for i in range(record_times):
if sample_num % num_samples == 0:
vdl_audio_comp.start_sampling()
idx = vdl_audio_comp.is_sample_taken()
if idx != -1:
audio_shape, audio_data = read_audio_data(audio_path)
vdl_audio_comp.set_sample(idx, audio_shape, audio_data)
sample_num += 1
if sample_num % num_samples == 0:
vdl_audio_comp.finish_sampling()
sample_num = 0
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_audio_comp = logger.audio(tag="test", num_samples=num_samples)
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_audio(vdl_audio_comp)
if __name__ == "__main__":
main()
import numpy as np
from visualdl import LogWriter
def record_embedding(vdl_embedding_comp):
hot_vectors = np.random.uniform(1, 2, size=(10, 3))
word_dict = {
"label1": 1,
"label2": 2,
"label3": 3,
}
vdl_embedding_comp.add_embeddings_with_word_dict(hot_vectors, word_dict)
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_embedding_comp = logger.embedding()
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_embedding(vdl_embedding_comp)
if __name__ == "__main__":
main()
import paddle.fluid as fluid
def lenet_5(img):
conv1 = fluid.nets.simple_img_conv_pool(
input=img,
filter_size=5,
num_filters=20,
pool_size=2,
pool_stride=2,
act="relu")
conv1_bn = fluid.layers.batch_norm(input=conv1)
conv2 = fluid.nets.simple_img_conv_pool(
input=conv1_bn,
filter_size=5,
num_filters=50,
pool_size=2,
pool_stride=2,
act="relu")
predition = fluid.layers.fc(input=conv2, size=10, act="softmax")
return predition
def main():
image = fluid.layers.data(name="img", shape=[1, 28, 28], dtype="float32")
predition = lenet_5(image)
place = fluid.CPUPlace()
exe = fluid.Executor(place=place)
exe.run(fluid.default_startup_program())
# after record, use visualdl --logdir [LOGDIR] --model_pb paddle_lenet_5_model to load the model,
# and view the data in webpage
fluid.io.save_inference_model(
"./paddle_lenet_5_model",
feeded_var_names=[image.name],
target_vars=[predition],
executor=exe)
if __name__ == "__main__":
main()
import numpy as np
from visualdl import LogWriter
def record_histogram(vdl_histogram_comp):
for i in range(1, 101):
f = 1 + 2 * i / 100
t = 6 - 2 * i / 100
data = np.random.uniform(f, t, size=(500))
vdl_histogram_comp.add_record(i, data)
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_histogram_comp = logger.histogram(tag="test", num_buckets=50)
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_histogram(vdl_histogram_comp)
if __name__ == "__main__":
main()
import numpy as np
from visualdl import LogWriter
from PIL import Image
num_samples = 1
def random_crop(img):
img = Image.open(img)
w, h = img.size
random_w = np.random.randint(0, w - 100)
random_h = np.random.randint(0, h - 100)
return img.crop((random_w, random_h, random_w + 100, random_h + 100))
def record_image(vdl_image_comp):
sample_num = 0
record_times = num_samples * 3
image_path = "test.jpg"
for i in range(record_times):
if sample_num % num_samples == 0:
vdl_image_comp.start_sampling()
idx = vdl_image_comp.is_sample_taken()
if idx != -1:
image_data = np.array(random_crop(image_path))
vdl_image_comp.set_sample(idx, image_data.shape,
image_data.flatten())
sample_num += 1
if sample_num % num_samples == 0:
vdl_image_comp.finish_sampling()
sample_num = 0
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_image_comp = logger.image(tag="test", num_samples=num_samples)
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_image(vdl_image_comp)
if __name__ == "__main__":
main()
from visualdl import LogWriter
def record_scalar(vdl_scalar_comp):
for i in range(1, 101):
vdl_scalar_comp.add_record(i, i**2)
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_scalar_comp = logger.scalar(tag="test")
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_scalar(vdl_scalar_comp)
if __name__ == "__main__":
main()
from visualdl import LogWriter
def record_text(vdl_text_comp):
for i in range(1, 6):
vdl_text_comp.add_record(i, "this is test text_%d" % i)
def main():
log_writter = LogWriter("./vdl_log", sync_cycle=10)
with log_writter.mode("train") as logger:
vdl_text_comp = logger.text(tag="test")
# after record, use visualdl --logdir vdl_log to load the log info,
# and view the data in webpage
record_text(vdl_text_comp)
if __name__ == "__main__":
main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册