what’s the standard way of keras.model.predict to predict a single sample

I have a trained model,and load it in the follow way. Since I have custom loss.

Nx=512
Ny=512
Ntot=Nx*Ny
totsize=500
batchsize =5

def dice_coef_loss(y_true, y_pred):
    y_true_f = tf.reshape(y_true,[-1])
    y_pred_f = tf.reshape(y_pred,[-1])
    return tf.reduce_sum(tf.abs(y_true_f-y_pred_f))/(Ntot*batchsize)

model=tf.keras.models.load_model('path_to_saved_model.keras',custom_objects={'dice_coef_loss':dice_coef_loss})

And in the training process, my dataset is in the follow way.

def decode(x,y):
    image = tf.io.read_file(x)
    image = tf.io.decode_raw(image, out_type=tf.float32)
    image = tf.transpose(tf.reshape(image,[Ny,Nx]),[1,0])
    image = tf.expand_dims(image,2)

    label = tf.io.read_file(y)
    label = tf.io.decode_raw(label, out_type=tf.float32)
    label = tf.transpose(tf.reshape(label,(Ny,Nx)),[1,0])
    label = label/np.pi
    label = tf.expand_dims(label,2)

    return image,label

dataset= tf.data.Dataset.from_tensor_slices((files_in_train,files_in_label))
dataset=dataset.map(decode,num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset=dataset.batch(batchsize)

when I want to test the time of predict using the trained model, I first use

start=time.perf_counter()
ypre=model.predict(dataset.take(1))
end=time.perf_counter()
print('predict time: %s Seconds'%(end-start))

ypre.shape is (5, 512, 512, 1) and

predict time: 2.670394100015983 Seconds

so in princeple every sample casts time 2.6/5 ? but when I define another dataset2 with batch 1.

def decode2(x):
    image = tf.io.read_file(x)
    image = tf.io.decode_raw(image, out_type=tf.float32)
    image = tf.transpose(tf.reshape(image,[Ny,Nx]),[1,0])
    image = tf.expand_dims(image,2)

    return image

dataset2= tf.data.Dataset.from_tensor_slices((files_in_train))
dataset2=dataset2.map(decode2,num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset2=dataset2.batch(1)

and test the predict time similarly.

start=time.perf_counter()
ypre=model.predict(dataset2.take(1))
end=time.perf_counter()
print('predict time: %s Seconds'%(end-start))

I get

predict time: 34.888951200060546 Seconds

I don’t know why the time for predicting one sample is much slower than five sample. And I test other batchsize of the dataset2, it’s seem that only when batchsize is same as the batchsize of training , the time is correct.

So what’s the correct way when I want to predict only one sample?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật