3

I have StyleGAN2 code that I run in Google Colab. I use an API made with Flask to generate a face when it receives a request. The first time it receives an API request, it generates a good face. After the second and following requests, it generates no face anymore but a picture with colors in it like this: Color image

The problem I think lies with the Graph of Tensorflow. I already tried to reset and overwrite the Graph but nothing works. I always get an error like this if I try to reset or overwrite the Graph:

ERROR:__main__:Exception on / [POST]
Traceback (most recent call last):
  File "/content/stylegan2/dnnlib/tflib/tfutil.py", line 208, in set_vars
    setter = tf.get_default_graph().get_tensor_by_name(var.name.replace(":0", "/setter:0"))  # look for existing op
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py", line 3783, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py", line 3607, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py", line 3649, in _as_graph_element_locked
    "graph." % (repr(name), repr(op_name)))
KeyError: "The name 'G_synthesis_1/noise0/setter:0' refers to a Tensor which does not exist. The operation, 'G_synthesis_1/noise0/setter', does not exist in the graph."

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "<ipython-input-18-c7fad9bd1d9a>", line 32, in home
    person = person_from_mixed_seeds(json_msg, 1)
  File "<ipython-input-13-2d7a08c2c48b>", line 32, in person_from_mixed_seeds
    generate_images(network,seeds3,0.5,"result")
  File "<ipython-input-5-772f7ad0faf4>", line 39, in generate_images
    tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
  File "/content/stylegan2/dnnlib/tflib/tfutil.py", line 212, in set_vars
    setter = tf.assign(var, tf.placeholder(var.dtype, var.shape, "new_value"), name="setter")  # create new setter
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/state_ops.py", line 227, in assign
    validate_shape=validate_shape)
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/gen_state_ops.py", line 66, in assign
    use_locking=use_locking, name=name)
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/op_def_library.py", line 367, in _apply_op_helper
    g = ops._get_graph_from_inputs(_Flatten(keywords.values()))
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py", line 5979, in _get_graph_from_inputs
    _assert_same_graph(original_graph_element, graph_element)
  File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py", line 5914, in _assert_same_graph
    (item, original_item))
ValueError: Tensor("G_synthesis_1/noise0/new_value:0", shape=(1, 1, 4, 4), dtype=float32) must be from the same graph as Tensor("G_synthesis_1/noise0:0", shape=(1, 1, 4, 4), dtype=float32_ref).
 I hope that anybody can help me with the problem I have.

This error occurs in the function "generate_images" by tflib.set_vars().

My question is, how to reset and overwrite the Graph of Tensorflow without getting that error all the time and without getting wrong images. I hope one of you can solve the problem. Thanks!

My code:

# Run this for Google CoLab (use TensorFlow 1.x)
%tensorflow_version 1.x
import tensorflow
print(tensorflow.__version__)
from google.colab import drive
drive.mount('/content/drive', force_remount=True)

!git clone https://github.com/NVlabs/stylegan2.git
!ls /content/stylegan2/
import sys
sys.path.insert(0, "/content/stylegan2")

import dnnlib
# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, visit
# https://nvlabs.github.io/stylegan2/license.html

import argparse
import numpy as np
import PIL.Image
import dnnlib
import dnnlib.tflib as tflib
import re
import sys

import pretrained_networks

#----------------------------------------------------------------------------

# Take short seeds and expand into 512 number vectors (latent vectors)
def expand_seed(seeds, vector_size):
  result = []

  for seed in seeds:
    rnd = np.random.RandomState(seed)
    result.append( rnd.randn(1, vector_size) ) 
  return result


# Gs = neural network, seeds = vector of seeds, truncation_psi = make images clearer
def generate_images(Gs, seeds, truncation_psi, prefix):
    # Small details like hair, wrinkels, slight skin tone
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
    print(noise_vars)
    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi

    for seed_idx, seed in enumerate(seeds):
      print('Generating image for seed %d/%d ...' % (seed_idx, len(seeds)))
      # fix value to 1 for static details empty for changing details
      rnd = np.random.RandomState(1)
      ## ----Code fails in tflib.set_vars() when I reset and overwrite the Graph -> (noise_vars)----
      tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
      images = Gs.run(seed, None, **Gs_kwargs) # [minibatch, height, width, channel]
      # Save image to Google Drive
      path = f"/content/drive/My Drive/projects/stylegan2/code_based/{prefix}/img-{seed_idx+1}.png"
      # path = f"/content/{prefix}-{seed_idx+1}.png"
      print(images)
      PIL.Image.fromarray(images[0], 'RGB').save(path)
def load_pretrained_network():
  sc = dnnlib.SubmitConfig()
  sc.num_gpus = 1
  sc.submit_target = dnnlib.SubmitTarget.LOCAL
  sc.local.do_not_copy_source_files = True
  sc.run_dir_root = "/content/drive/My Drive/projects/stylegan2"
  sc.run_desc = 'generate-images'
  network_pkl = '/content/drive/My Drive/networks/stylegan2-ffhq-config-f.pkl'

  print('Loading networks from "%s"...' % network_pkl)
  _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
  print(Gs)
  return Gs
FEMALE = [0,4,5,6,7,9,10,17,18,19,21,22,24,26,27,28,32,34,35,38,42,43,45,46,47,52,53,54,57,58,59,60,61,62,63,72,74,77,78,80,82,83,84,85,86,
           92,94,95,97,98,100,101,102,103,104,105,106,107,108,109,110,113,114,115,118,119,120,121,129,131,133,138,140,142,145,147,148,
           150,156,157,161,164,165,166,170,171,172,173,175,178,181,182,184,187,190,192,195,196,197,198,199,200,201,202,203,205,206,207,
           208,210,212,215,218,221,222,225,226,227,228,229,233,235,236,237,241,243,244,246,247,248,249,250,252,255,257,258,259,260,262,
           263,264,266,272,273,276,277,278,279,280,283,284,285,287,288,290,291,292,293,294,297,298,299,301,302,303,304,305,306,307,308,
           309,310,311,312,313,316,318,319,320,321,325,326,327,329,331,333,334,335,336,337,338,340,342,344,345,347,348,349,350,352,354,
           355,356,357,358,359,361,362,363,365,367,368,369,370,371,373,378,379,380,381,382,383,385,387,389,391,392,394,395,397,398,399,
           405,407,408,409,410,413,417,418,419,421,422,423,427,428,429,430,433,435,437,438,440,441,442,444,447,448,449,450,451,453,456,
           457,458,459,460,462,463,465,466,467,468,469,472,474,475,476,477,478,479,481,482,483,484,485,486,487,488,489,492,494,497,498,
           499,501,504,508,510,511,513,514,515,516,517,519,520,521,522,523,524,525,526,527,532,533,534,535,536,538,539,540,541,542,543,
           544,547,549,552,553,555,556,557,558,560,561,563,565,566,567,570,571,575,576,577,578,580,581,582,583,584,585,587,588,596,597,
           598,600,601,602,603,604,606,610,613,614,615,616,617,618,621,622,624,625,627,629,630,632,633,636,637,638,641,642,643,645,646,
           648,650,655,657,659,661,662,665,667,670,671,372,374,376,378,389,380,381,685,687,688,689,692,693,695,696,698,701,702,705,706,
           713,715,717,719,720,721,722,723,724,726,729,732,735,737,739,742,750,752,753,754,755,756,757,758,759,760,761,762,768,771,772,
           773,774,775,776,781,782,783,784,786,787,788,789,790,791,794,795,796,797,800,801,803,806,808,809,810,811,814,816,817,818,819,
           821,823,825,828,829,832,833,834,835,838,841,843,846,847,848,849,850,855,856,857,858,861,863,865,867,868,869,870,872,875,876,
           877,878,880,882,883,885,886,889,890,892,897,898,899,902,904,906,907,911,912,913,917,920,921,922,924,925,926,928,960,932,934,
           936,937,938,939,940,941,942,944,945,949,950,951,952,954,955,956,958,960,962,963,964,965,966,967,968,969,970,971,972,973,977,
           979,980,981,982,983,985,986,987,989,990,991,992,994,996,997,999]

MALE = [x for x in range(1000) if x not in FEMALE]
print(len(FEMALE), len(MALE))

GLASSES = [2,3,17,19,26,40,42,50,55,65,68,70,85,88,90,96,112,130,139,140,146,147,152,155,164,183,194,198,209,214,215,241,244,245,
           254,260,267,296,300,312,313,322,324,331,333,346,361,366,386,401,412,422,425,443,448,472,487,494,501,516,544,545,551,552,565,568,
           591,594,595,597,607,610,639,640,669,670,671,377,703,710,719,730,764,765,767,777,780,792,798,799,802,808,812,916,823,857,866,870,
           881,887,893,894,895,899,915,916,934,936,941,973,979,988,991,993]

NO_GLASSES = [x for x in range(1000) if x not in GLASSES]

SOUTHERN =[13,18,37,66,79,81,89,93,100,115,116,133,138,149,158,172,180,187,193,222,256,257,278,284,307,332,334,340,351,369,371,
           372,387,396,410,421,470,483,485,490,495,514,548,557,577,586,588,598,598,599,600,601,608,609,614,625,635,638,647,649,
           700,708,711,712,714,720,728,735,740,750,762,763,786,793,796,799,800,803,814,822,832,838,839,849,858,859,871,874,879,
           903,905,911,922,923,924,928,930,939,944,948,950,955,961,965,973,978,983,984,992]

ASIAN = [2,3,35,38,40,56,75,86,90,92,151,155,157,162,190,192,198,201,203,225,263,266,321,325,345,361,394,400,434,441,444,450,
         463,464,513,516,553,556,559,565,594,618,626669,678,684,685,689,703,724,725,760,767,771,790,805,856,862,870,881,901,902,
         907,926,954,957,977,989]

AFRICAN = [28,137,147,171,355,405,417,489,491,533,536,723,785,818]

CAUCASIAN = [x for x in range(1000) if x not in SOUTHERN and x not in ASIAN and x not in AFRICAN]

print(len(SOUTHERN), len(ASIAN), len(AFRICAN),len(CAUCASIAN))

zero_to_five = [6,16,44,113,128,163,207,217,223,308,376,490,493,572,584,619,622,626,638,706,738,771,805,852,883,907,952]

six_to_eleven = [29,37,58,80,87,92,99,119,133,162,169,208,253,284,299,304,433,450,485,513,529,550,647,659,662,697,735,
                 824,910,913,920,929,958,974,977,982,989]

twelve_to_seventeen = [72,81,105,110,123,131,137,180,182,272,288,298,334,397,439,471,486,556,567,592,614,642,645,703,727,
                       742,762,781,835,865,871,872,885,928,946,987,990]

eighteen_to_twentyfive = [5,9,22,38,43,56,76,77,95,170,176,184,189,193,201,203,221,229,330,359,371,373,382,383,418,538,566,
                          589,635,656,675,690,696,717,776,782,833,854,867]

twentysix_to_thirtytwo = [0,3,10,14,20,21,27,28,31,34,35,47,59,61,66,78,82,83,86,89,100,106,107,108,114,118,127,129,134,138,
                          142,144,145,148,149,153,156,159,161,166,171,172,173,177,178,181,183,185,188,190,192,199,200,202,205,
                          210,212,214,218,222,228,233,234,235,239,243,247,248,257,258,259,260,262,266,268,275,277,278,279,285,
                          286,292,293,294,297,317,320,321,322,343,350,351,354,358,360,362,368,371,377,379,381,384,389,391,394,
                          406,409,412,413,414,419,420,442,444,445,451,455,464,474,478,479,484,481,497,499,504,505,521,522,523,
                          524,528,532,535,537,540,542,547,557,569,576,582,593,599,600,602,604,608,616,620,625,629,648,650,655,
                          658,660,661,672,673,674,679,680,684,689,691,695,705,713,720,721,723,724,737,741,744,746,747,748,753,
                          765,772,773,787,788,790,791,800,808,812,817,821,825,834,838,839,845,859,861,862,863,870,890,900,909,
                          911,926,927,935,939,945,971,994]

thirtythree_to_forty = [2,4,7,12,15,23,24,25,30,32,33,36,39,40,41,42,45,46,48,49,51,52,55,62,64,67,74,75,84,85,88,90,91,93,
                        94,98,102,103,104,109,112,115,117,120,121,122,125,126,136,147,150,151,157,160,165,167,175,194,196,197,
                        198,204,206,216,220,224,227,236,238,241,242,246,249,251,252,255,263,264,269,270,273,280,281,282,283,287,
                        289,291,296,303,305,307,310,316,318,319,324,325,329,331,336,337,338,339,344,349,352,353,367,369,370,372,
                        378,385,387,393,396,405,407,408,415,416,421,423,424,426,428,429,430,432,434,435,346,437,438,441,449,453,
                        457,458,459,461,462,463,466,468,469,470,473,475,476,480,482,483,488,489,495,496,498,502,503,508,510,512,
                        514,517,518,519,520,525,526,527,531,536,541,546,549,552,553,554,555,560,561,562,563,564,570,571,573,574,
                        575,577,578,579,581,583,585,586,587,588,596,598,601,606,609,612,615,617,618,621,624,627,631,633,637,644,
                        652,653,657,663,665,666,668,670,671,676,681,683,688,685,694,698,700,702,704,714,715,725,726,731,732,733,
                        734,739,743,745,750,752,754,755,758,760,761,767,769,770,774,775,778,779,783,784,785,789,793,794,795,796,
                        797,798,799,803,804,806,807,809,811,815,815,816,818,822,826,827,829,830,831,832,843,846,850,853,855,858,
                        860,860,864,868,869,873,875,877,878,881,882,884,886,888,889,891,894,896,897,898,901,902,904,906,908,912,
                        915,917,918,919,921,922,925,931,934,936,940,941,942,943,944,950,954,957,960,964,965,966,968,969,972,975,
                        976,978,980,981,984,985,986,992,993,995,996,997,998,1000]

fortyone_to_fifty = [1,11,13,17,18,19,26,50,53,60,63,65,68,69,71,73,96,97,101,116,124,130,132,135,139,140,141,143,146,152,154,
                     155,158,164,174,179,186,187,191,195,211,213,215,219,225,226,230,232,237,240,244,245,250,254,256,261,265,267,
                     271,274,276,290,295,301,302,306,309,311,313,314,323,328,332,333,335,340,342,345,346,347,348,355,356,357,361,
                     363,364,365,366,375,380,386,388,390,392,395,398,399,400,402,403,404,410,411,417,422,425,427,431,440,443,447,
                     448,452,454,456,460,465,467,472,477,481,492,494,500,501,506,507,509,511,515,516,530,533,534,539,543,544,545,
                     548,551,558,559,565,568,580,590,591,594,595,597,603,605,607,611,613,623,628,630,632,634,636,639,640,641,643,
                     646,649,651,654,667,669,677,678,682,686,687,692,699,701,707,708,709,710,711,712,718,719,722,728,729,730,736,
                     740,749,751,756,759,763,766,768,777,780,789,801,810,814,819,820,823,828,836,837,840,841,844,847,848,849,851,
                     856,857,866,874,876,879,880,892,893,895,899,914,916,923,924,930,932,933,937,938,947,948,949,951,953,955,956,
                     959,961,962,963,967,970,973,979,983,988,991,999]

fifthyplus = [8,54,57,70,79,111,168,209,231,300,312,315,326,327,341,374,401,446,487,610,664,693,716,757,764,792,802,887,903,905]
sets ={}
sets["FEMALE"] = FEMALE
sets["MALE"] = MALE

sets["GLASSES"] = GLASSES
sets["NO-GLASSES"] = NO_GLASSES

sets["CAUCASIAN"] = CAUCASIAN
sets["ASIAN"] = ASIAN
sets["AFRICAN"] = AFRICAN
sets["SOUTHERN"] = SOUTHERN

sets["zero-to-five"] = zero_to_five
sets["six-to-eleven"] = six_to_eleven
sets["twelve-to-seventeen"] = twelve_to_seventeen
sets["eighteen-to-twentyfive"] = eighteen_to_twentyfive
sets["twentysix-to-thirtytwo"] = twentysix_to_thirtytwo
sets["thirtythree-to-forty"] = thirtythree_to_forty
sets["fortyone-to-fifty"] = fortyone_to_fifty
sets["fifthyplus"] = fifthyplus
import json

def get_set_by_json(json):
  age = "26-32"
  if json["age"] in range(0,6):
    age = sets["zero-to-five"]
  elif json["age"] in range(6,12):
    age = sets["six-to-eleven"]
  elif json["age"] in range(12,18):
    age = sets["twelve-to-seventeen"]
  elif json["age"] in range(18,26):
    age = sets["eighteen-to-twentyfive"]
  elif json["age"] in range(26,33):
    age = sets["twentysix-to-thirtytwo"]
  elif json["age"] in range(33,41):
    age = sets["thirtythree-to-forty"]
  elif json["age"] in range(41,50):
    age = sets["fortyone-to-fifty"]
  elif json["age"] in range(50,101):
    age = sets["fifthyplus"]

  gender = json["gender"].upper()
  gender = sets[gender]

  glasses = json["glasses"]
  if glasses is True:
    glasses = sets["GLASSES"]
  else:
    glasses = sets["NO-GLASSES"]

  ethnicity = json["ethnicity"].upper()
  ethnicity = sets[ethnicity]

  dataset = [x for x in ethnicity if x in gender and x in glasses and x in age]

  return dataset
import random
import pandas as pd

def person_from_mixed_seeds(jsonstr, count):
  network = load_pretrained_network()
  vector_size = network.input_shape[1:][0]

  seeds3 = []

  # dataset = get_set_by_input()

  parsedjson = json.loads(jsonstr)
  dataset = get_set_by_json(parsedjson)

  for i in range(count):
    i1 = random.randint(0,len(dataset)-1)
    i2 = random.randint(0,len(dataset)-1)
    seeds1 = expand_seed([dataset[i1]],vector_size)
    seeds2 = expand_seed([dataset[i2]],vector_size)
    seeds = [seeds1[0][0], seeds2[0][0]]

    diff = seeds[1] - seeds[0]
    delta = (np.random.rand(512) * 0.6) + .2
    step = diff * delta
    current = [seeds[0].copy()]
    current += step
    current = np.round(current,5)
    print(current)
    seeds3.append(current)

  generate_images(network,seeds3,0.5,"result")
from IPython.display import Image
img = Image('/content/drive/My Drive/projects/stylegan2/code_based/result/img-0.png')

!pip install flask-ngrok
!pip install flask-cors
from flask_ngrok import run_with_ngrok
from flask import Flask
from flask_cors import CORS
from flask import request

app = Flask(__name__)
run_with_ngrok(app)
CORS(app)
global graph
graph = tensorflow.get_default_graph()

@app.route("/", methods=['GET','POST'])
def home():
    if request.method == 'GET':
      return "Post the type of person you want to create"
    elif request.method == 'POST':
      json_obj = request.json
      json_msg = json.dumps(json_obj)
      print(json_msg)
      print(graph)

      s = tensorflow.Session(graph=graph)

      print(s)

      with graph.as_default():
        with s as sess:
          sess.run(tensorflow.global_variables_initializer())
          sess.run(tensorflow.local_variables_initializer())
          print(sess)
          person = person_from_mixed_seeds(json_msg, 1)

      return json_msg

if __name__=='__main__':
    app.run()
MichaelDG
  • 39
  • 2
  • First of all thanks for putting your code and stack trace of the error. Not many do and leaves us clueless. I have abandoned working with `tf.Graph` for a long time now, so off the top of my head; `"The name 'G_synthesis_1/noise0/setter:0' refers to a Tensor which does not exist. The operation, 'G_synthesis_1/noise0/setter', does not exist in the graph."` is telling you that at first you somewhere lost your graph that you started with or the original graph that you started with has its data type changed from `dtype=float32` to something else. If u could coerce all float `dtype` would it help? – Sowmya May 12 '20 at 12:18
  • Thanks for the response! But I really don't know how to coerce dtype of the graph to float32. I already searched on Google and I found nothing to directly update the graph his dtype. – MichaelDG May 12 '20 at 16:42
  • In good old days, a variable could be declared as `random_weights = tf.Variable( tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), dtype=tf.float32, name="weights")` where `n=100` say. I used to make sure that all my `numpy` or `float` datatypes were first coerced to `dtype=tf.float32`. I also noticed that somewhere `uint8`. Now, what's `uint8` of `0.9999`? I guess, after the first time, your `float`s are somewhere getting converted to `int`. I don't know exactly where but your API is doing the correct job of rendering an image with all zeros. Sorry, if you find this just hand waving. – Sowmya May 13 '20 at 09:58
  • Could you also check the validity of your graph? For e.g. ` local_graph = tf.Graph() with local_graph.as_default(): x = tf.Variable(5) print(x.graph is local_graph, x.graph is tf.get_default_graph()) ` – Sowmya May 13 '20 at 10:02
  • Thank you very much! I will test it when I have the time for it. I will let you know if it works or not. – MichaelDG May 16 '20 at 10:05
  • I tried to solve my problem with your expectations. It did not solve the problem. I think it has nothing to do with a conversion of a float to an integer. Because the code of the face generator works correctly without the API. So I think the problem lies with the Graph, because I never reset the Graph after each request. And if I try to reset or overwrite the Graph, it gives that same error 'Tensor("G_synthesis_1/noise0/new_value:0", shape=(1, 1, 4, 4), dtype=float32) must be from the same graph as Tensor("G_synthesis_1/noise0:0", shape=(1, 1, 4, 4), dtype=float32_ref)'. Thanks anyway! – MichaelDG May 28 '20 at 10:54
  • For resetting graph you can try with `tf.compat.v1.reset_default_graph` .You can follow this post for details on the similar issue https://stackoverflow.com/questions/42616625/ –  Nov 30 '20 at 16:59

0 Answers0