克服Graphdef在Tensorflow中不能大于2GB-使用tf进行图像转换 [英] overcome Graphdef cannot be larger than 2GB in tensorflow -- Image transformations with tf

查看:342
本文介绍了克服Graphdef在Tensorflow中不能大于2GB-使用tf进行图像转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提到了这个问题,但我不太理解Mr.mrry提供的
第二种方法。

I've referred to this question, but I don't quite understand the second method provided by Mr.mrry.

克服Graphdef的张量流不能大于2GB

基本上,我正在尝试在图像上使用tf内置的图像转换方法。我遇到标题中提供的错误。
另外,我是否需要继续为每次迭代创建一个新会话?
当前,此过程有点慢,并且不确定如何加快速度。

Basically, I'm trying to use tf's built in image transformation methods on images. I'm running into the error provided in the title. Also, do I need to keep creating a new session for each iteration? Currently, this process is a little slow and am not sure how to speed it up.

import tensorflow as tf
import os
from scipy.ndimage import imread
from scipy.misc import imresize, imshow
import matplotlib.pyplot as plt


for fish in Fishes:
    fish_images = os.listdir(os.path.join('C:\\Users\\Moondra\\Desktop\\Fishes', fish))  # get the image files
    os.makedirs(SAVE_DIR + fish, exist_ok = True)
    for num, fish_image in enumerate(fish_images):
        image =imread(os.path.join('C:\\Users\\Moondra\\Desktop\\Fishes', fish, fish_image))
        new_img =tf.image.adjust_brightness(image, .4)  #image transformation
        with tf.Session() as sess:

            new_image =sess.run(new_img)
            imsave(os.path.join(SAVE_DIR, fish, fish +str(num)+'.jpg'), new_image)


推荐答案

这不是应该使用TF的方式。

This is not how TF should be used.


  1. 您应该创建图形一次

  2. 您应该创建会话一次

  1. You should create graph once.
  2. You should create session once.

您当前的代码在循环中同时执行两项操作,从而导致运行缓慢和内存问题。问题在于TF不是强制性语言,所以

Your current code does both things in a loop, thus causing slowness and memory issues. The problem lies in the fact that TF is not imperative language, so

new_img =tf.image.adjust_brightness(image, .4)  #image transformation

不是在图像上应用函数,而是在图形中创建操作,并将对此操作的引用存储在new_img中。因此,每次调用此函数时,图形都会增长。

is not application of a function on the image.This creates an operation in a graph, and stores reference to this operation in new_img. So each time you call this function, your graph grows.

因此,在伪代码中,它应该是:

So in pseudo code it should be:

create placeholder for image name
create transformed image op - new_img
create session
for each image
  call in a session new_img op, providing path to the placehodler using feed_dict

这篇关于克服Graphdef在Tensorflow中不能大于2GB-使用tf进行图像转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆