1
0

More than 1 year has passed since last update.

tecoGANをtensorflow 2系に書き換えた

Last updated at Posted at 2023-01-05

Colaboratoryでtensorflow 1系へのサポートが終わっていたので、2系に書き換えた。

TecoGAN

TecoGANについてはこちらから
https://qiita.com/kannkyo/items/4d515ca988f536fadf44

Tensorflow 2系への書き換え

自動書き換え

まずは1系から2系への自動アップデートスクリプトを行った。
https://www.tensorflow.org/guide/migrate?hl=ja

lib/ops.pymain.pyが対象となる

!tf_upgrade_v2 \
  --intree ./ \
  --outtree ./ \
  --reportfile report.txt


!tf_upgrade_v2 \
  --intree ./lib/ \
  --outtree ./lib/ \
  --reportfile report_lib.txt

ただ、これだけだと動かないので色々書き換える

手動書き換え

上記で直りきらなかった部分を治していった結果を覚えている限り書く

tf_slim

tensorflow.contrib.slimが無くなっていた。調べたところ新たにtf_slimに置き換わったみたいなので、こちらに書き換えた。該当コードはmain.pyの21行目あたりとlib/ops.pyの2行目

+ import tf_slim as slim
- import tensorflow.contrib.slim as slim

もちろんtf_slimはインストール

!pip install tf_slim

tensorflow_addons

tensorflow.contribが2系から無くなったみたいなので、どこに移行されたのか調べていたらここに行きついた。該当コードは216行目当たり
https://www.tensorflow.org/addons/api_docs/python/tfa/image/dense_image_warp

+ import tensorflow_addons as tfa
+ pre_warp_hi = tfa.image.dense_image_warp(pre_gen, gen_flow)
- pre_warp_hi = tf.contrib.image.dense_image_warp(pre_gen, gen_flow)

eager execution を無効化

eager executionの意味を調べたところ切実な実行らしい。このままだと

inputs_raw = tf.compat.v1.placeholder(tf.float32, shape=input_shape, name='inputs_raw')

のところでエラーが生じる。tf.compat.v1は2系で無理矢理1系を使う形だから切実に実行しようとするとダメ?

import tensorflow_addons as tfa
+ tf.compat.v1.disable_eager_execution()

これを追加してeager execution を無効化する。

結果

coraboratoryでtecoGANを動かせた!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0