小编Tar*_*hra的帖子

Why in this code, await is not blocking ui in flutter

In the default example app whenever you create new fultter project I just added the following code.

  initState() {
    super.initState();
    loop();
  }

  loop() async {
    while (true) {
      await Future.delayed(Duration(milliseconds: 10));
      print("count now:$_counter");
    }
  }
Run Code Online (Sandbox Code Playgroud)
  1. Why is the app UI is not getting blocked? I am able to click + button and the counter increases smoothly. Even if I change the delay to 10 sec, the UI is resposive. Is the loop() runnning in different thread?but I know dart is …

multithreading async-await dart flutter

6
推荐指数
1
解决办法
1169
查看次数

如何使用 tf.data.Dataset.from_generator 进行批处理?我需要修改发电机吗

我正在使用该batch(8)函数,它修改形状并添加批次尺寸,但每批次仅获取一张图像。下面是我的代码:-

import cv2
import numpy as np
import os
import tensorflow as tf
import random

folder_path = "./real/"
files = os.listdir(folder_path)

def get_image():
    index = random.randint(0,len(files)-1)
    img = cv2.imread(folder_path+files[index])
    img = cv2.resize(img,(128,128))
    img = img/255.
    #More complex transformation
    yield img

dset = tf.data.Dataset.from_generator(get_image,(tf.float32)).batch(8)

for img in dset:
    print(img.shape)
    break
Run Code Online (Sandbox Code Playgroud)

即使使用batch(8),输出仍然是(1, 128, 128, 3)。我是否需要修改生成器来手动创建批次?另外,如何将其包装在tensorflow中的生成器中,使其运行得更快?

tensorflow tensorflow-datasets tensorflow2.0

6
推荐指数
1
解决办法
8993
查看次数