How can I solve the AttributeError?

108 views Asked by At

I am running the code to evaluate some generated pictures and I keep getting issues. Here is the function :

# Code adapted from
# https://github.com/openai/improved-gan/blob/master/inception_score/model.py
# which was in turn derived from
# tensorflow/tensorflow/models/image/imagenet/classify_image.py
# ...
def _init_inception():
    global softmax
    if not os.path.exists(MODEL_DIR):
        os.makedirs(MODEL_DIR)
    filename = DATA_URL.split('/')[-1]
    filepath = os.path.join(MODEL_DIR, filename)
    if not os.path.exists(filepath):
        def _progress(count, block_size, total_size):
            sys.stdout.write('\r>> Downloading %s %.1f%%' % (
                filename, float(count * block_size) / float(total_size) * 100.0))
            sys.stdout.flush()

        filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
        print()
        statinfo = os.stat(filepath)
        print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
    tarfile.open(filepath, 'r:gz').extractall(MODEL_DIR)
    #with tf.gfile.FastGFile(os.path.join(
    with tf.io.gfile.GFile(os.path.join(
            MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')
    # Works with an arbitrary minibatch size.
    with tf.compat.v1.Session() as sess:
        pool3 = sess.graph.get_tensor_by_name('pool_3:0')
        ops = pool3.graph.get_operations()
        for op_idx, op in enumerate(ops):
            for o in op.outputs:
                shape = o.get_shape()
                shape = [s.value for s in shape]
                new_shape = []
                for j, s in enumerate(shape):
                    if s == 1 and j == 0:
                        new_shape.append(None)
                    else:
                        new_shape.append(s)
                o.set_shape(tf.TensorShape(new_shape))
        w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1]
        logits = tf.matmul(tf.squeeze(pool3, [1, 2]), w)
        softmax = tf.nn.softmax(logits)

The error appears to the part of '# Works with an arbitrary minibatch size.' Here is the error message:

Traceback (most recent call last):
  File "C:\Users\k\anaconda3\envs\tf\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\k\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\framework\ops.py", line 5385, in get_controller
    yield g
  File "D:/Git/Graduation/mytext2image-main/IS.py", line 182, in _init_inception
    shape = [s.value for s in shape]
  File "D:/Git/Graduation/mytext2image-main/IS.py", line 182, in <listcomp>
    shape = [s.value for s in shape]
AttributeError: 'NoneType' object has no attribute 'value'

Is this problem related to the tensorflow version?(I have replaced all 'tf' to 'tf.compat.v1'.). Any help will be appreciated.

0

There are 0 answers