我是Tensorflow的新手。我使用预先训练的暹罗CNN模型拍摄图像并为每个图像返回偏好分数。我想弄清楚代码的哪些部分比较昂贵。有没有办法用tensorflow或tensorboard做到这一点?还是有人看到我的代码的哪些部分可以重新设计以提高效率?任何提示都将不胜感激。
代码:
# function to get preference scores for unseen items
def pref_score(Item_x,user_x):
from collections import defaultdict
from io import BytesIO
res_x = defaultdict(lambda: defaultdict(float))
for jjj in list(Item_x.keys()):
tstImg3=np.round(np.array(Image.open(BytesIO(Item_x[jjj][b'imgs'])).convert('RGB').resize((224,224)),dtype=np.float32))
rep_gan=tf.reshape(tstImg3, shape=[-1, 224, 224, 3])
with tf.device('/gpu:0'):
gan_image=rep_gan
image=tf.image.resize_nearest_neighbor(images=gan_image, size=[224,224], align_corners=None, name=None)
with tf.variable_scope("DVBPR") as scope:
scope.reuse_variables()
result = CNN(image,1.0)
user=tf.placeholder(dtype=tf.int32,shape=[1])
idx=tf.reduce_sum(tf.matmul(result,tf.transpose(tf.gather(thetau,user))),1)
res_x[jjj] = sess.run([gan_image,idx],feed_dict={user:[user_x]})[1]
print(user_x,"===>",jjj)
return res_x
# running function to get preference scores on images
test_res=pref_score(Item_x=test_item,user_x=1)