python - 无法从 Google Colab 外部的 Tensorflow 运行 CycleGan 示例

标签 python tensorflow machine-learning keras generative-adversarial-network

我正在关注 tutorial on tensorflows使用cyclegan的网页。通过 colab 运行代码效果很好但是当我下载jupiter code时并使用 jupyter nbconvert 对其进行转换:

jupyter nbconvert — to script cyclegan.ipynb --to python

我正在使用 python Cyclegan.py 运行代码,但收到错误:

File "C:\Users\myname\Desktop\PROJECT\GanTutorial\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 549, in rename_v2 compat.path_to_bytes(src), compat.path_to_bytes(dst), overwrite) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc5 in position 167: invalid continuation byte

我无法摆脱这个错误。有人在 Google Colab 之外成功运行该示例吗?

更新 在尝试在我自己的一些文件上使用经过训练的数据后,我收到以下错误消息:

' ' tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b'No files matched pattern:  

这是我的完整代码:

#!/usr/bin/env python
import subprocess
subprocess.run(["pip", "install", "git+https://github.com/tensorflow/examples.git"])


import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow_examples.models.pix2pix import pix2pix

import os
import time
import matplotlib.pyplot as plt
from IPython.display import clear_output

AUTOTUNE = tf.data.AUTOTUNE


GPUS = tf.config.experimental.list_physical_devices('GPU')
if GPUS:
    try:
        for GPU in GPUS:
            tf.config.experimental.set_memory_growth(GPU, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(GPUS), "Physical GPUs,", len(logical_gpus), "Logical GPUs") 
    except RuntimeError as  RE:
        print(RE)

dataset, metadata = tfds.load('cycle_gan/horse2zebra',
                              with_info=True, as_supervised=True)

train_horses, train_zebras = dataset['trainA'], dataset['trainB']
test_horses, test_zebras = dataset['testA'], dataset['testB']


BUFFER_SIZE = 256
BATCH_SIZE = 1
IMG_WIDTH = 256
IMG_HEIGHT = 256


def random_crop(image):
  cropped_image = tf.image.random_crop(
      image, size=[IMG_HEIGHT, IMG_WIDTH, 3])

  return cropped_image

# normalizing the images to [-1, 1]
def normalize(image):
  image = tf.cast(image, tf.float32)
  image = (image / 127.5) - 1
  return image

def random_jitter(image):
  # resizing to 286 x 286 x 3
  image = tf.image.resize(image, [286, 286],
                          method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

  # randomly cropping to 256 x 256 x 3
  image = random_crop(image)

  # random mirroring
  image = tf.image.random_flip_left_right(image)

  return image


def preprocess_image_train(image, label):
  image = random_jitter(image)
  image = normalize(image)
  return image


def preprocess_image_test(image, label):
  image = normalize(image)
  return image


train_horses = train_horses.map(
    preprocess_image_train, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)

train_zebras = train_zebras.map(
    preprocess_image_train, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)

test_horses = test_horses.map(
    preprocess_image_test, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)

test_zebras = test_zebras.map(
    preprocess_image_test, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)


sample_horse = next(iter(train_horses))
sample_zebra = next(iter(train_zebras))


plt.subplot(121)
plt.title('Horse')
plt.imshow(sample_horse[0] * 0.5 + 0.5)

plt.subplot(122)
plt.title('Horse with random jitter')
plt.imshow(random_jitter(sample_horse[0]) * 0.5 + 0.5)


plt.subplot(121)
plt.title('Zebra')
plt.imshow(sample_zebra[0] * 0.5 + 0.5)

plt.subplot(122)
plt.title('Zebra with random jitter')
plt.imshow(random_jitter(sample_zebra[0]) * 0.5 + 0.5)



OUTPUT_CHANNELS = 3

generator_g = pix2pix.unet_generator(OUTPUT_CHANNELS, norm_type='instancenorm')
generator_f = pix2pix.unet_generator(OUTPUT_CHANNELS, norm_type='instancenorm')

discriminator_x = pix2pix.discriminator(norm_type='instancenorm', target=False)
discriminator_y = pix2pix.discriminator(norm_type='instancenorm', target=False)


to_zebra = generator_g(sample_horse)
to_horse = generator_f(sample_zebra)
plt.figure(figsize=(8, 8))
contrast = 8

imgs = [sample_horse, to_zebra, sample_zebra, to_horse]
title = ['Horse', 'To Zebra', 'Zebra', 'To Horse']

for i in range(len(imgs)):
  plt.subplot(2, 2, i+1)
  plt.title(title[i])
  if i % 2 == 0:
    plt.imshow(imgs[i][0] * 0.5 + 0.5)
  else:
    plt.imshow(imgs[i][0] * 0.5 * contrast + 0.5)
plt.show()


plt.figure(figsize=(8, 8))

plt.subplot(121)
plt.title('Is a real zebra?')
plt.imshow(discriminator_y(sample_zebra)[0, ..., -1], cmap='RdBu_r')

plt.subplot(122)
plt.title('Is a real horse?')
plt.imshow(discriminator_x(sample_horse)[0, ..., -1], cmap='RdBu_r')

plt.show()


LAMBDA = 10

loss_obj = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real, generated):
  real_loss = loss_obj(tf.ones_like(real), real)

  generated_loss = loss_obj(tf.zeros_like(generated), generated)

  total_disc_loss = real_loss + generated_loss

  return total_disc_loss * 0.5

def generator_loss(generated):
  return loss_obj(tf.ones_like(generated), generated)

def calc_cycle_loss(real_image, cycled_image):
  loss1 = tf.reduce_mean(tf.abs(real_image - cycled_image))
  
  return LAMBDA * loss1

def identity_loss(real_image, same_image):
  loss = tf.reduce_mean(tf.abs(real_image - same_image))
  return LAMBDA * 0.5 * loss

generator_g_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
generator_f_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)

discriminator_x_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
discriminator_y_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)


checkpoint_path = "./checkpoints/train"

ckpt = tf.train.Checkpoint(generator_g=generator_g,
                           generator_f=generator_f,
                           discriminator_x=discriminator_x,
                           discriminator_y=discriminator_y,
                           generator_g_optimizer=generator_g_optimizer,
                           generator_f_optimizer=generator_f_optimizer,
                           discriminator_x_optimizer=discriminator_x_optimizer,
                           discriminator_y_optimizer=discriminator_y_optimizer)

ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)

# if a checkpoint exists, restore the latest checkpoint.
if ckpt_manager.latest_checkpoint:
  ckpt.restore(ckpt_manager.latest_checkpoint)
  print ('Latest checkpoint restored!!')


# ## Training
# 
EPOCHS = 4
def generate_images(model, test_input):
  prediction = model(test_input)
    
  plt.figure(figsize=(12, 12))

  display_list = [test_input[0], prediction[0]]
  title = ['Input Image', 'Predicted Image']

  for i in range(2):
    plt.subplot(1, 2, i+1)
    plt.title(title[i])
    # getting the pixel values between [0, 1] to plot it.
    plt.imshow(display_list[i] * 0.5 + 0.5)
    plt.axis('off')
  plt.show()

@tf.function
def train_step(real_x, real_y):
  # persistent is set to True because the tape is used more than
  # once to calculate the gradients.
  with tf.GradientTape(persistent=True) as tape:
    # Generator G translates X -> Y
    # Generator F translates Y -> X.
    
    fake_y = generator_g(real_x, training=True)
    cycled_x = generator_f(fake_y, training=True)

    fake_x = generator_f(real_y, training=True)
    cycled_y = generator_g(fake_x, training=True)

    # same_x and same_y are used for identity loss.
    same_x = generator_f(real_x, training=True)
    same_y = generator_g(real_y, training=True)

    disc_real_x = discriminator_x(real_x, training=True)
    disc_real_y = discriminator_y(real_y, training=True)

    disc_fake_x = discriminator_x(fake_x, training=True)
    disc_fake_y = discriminator_y(fake_y, training=True)

    # calculate the loss
    gen_g_loss = generator_loss(disc_fake_y)
    gen_f_loss = generator_loss(disc_fake_x)
    
    total_cycle_loss = calc_cycle_loss(real_x, cycled_x) + calc_cycle_loss(real_y, cycled_y)
    
    # Total generator loss = adversarial loss + cycle loss
    total_gen_g_loss = gen_g_loss + total_cycle_loss + identity_loss(real_y, same_y)
    total_gen_f_loss = gen_f_loss + total_cycle_loss + identity_loss(real_x, same_x)

    disc_x_loss = discriminator_loss(disc_real_x, disc_fake_x)
    disc_y_loss = discriminator_loss(disc_real_y, disc_fake_y)
  
  # Calculate the gradients for generator and discriminator
  generator_g_gradients = tape.gradient(total_gen_g_loss, 
                                        generator_g.trainable_variables)
  generator_f_gradients = tape.gradient(total_gen_f_loss, 
                                        generator_f.trainable_variables)
  
  discriminator_x_gradients = tape.gradient(disc_x_loss, 
                                            discriminator_x.trainable_variables)
  discriminator_y_gradients = tape.gradient(disc_y_loss, 
                                            discriminator_y.trainable_variables)
  
  # Apply the gradients to the optimizer
  generator_g_optimizer.apply_gradients(zip(generator_g_gradients, 
                                            generator_g.trainable_variables))

  generator_f_optimizer.apply_gradients(zip(generator_f_gradients, 
                                            generator_f.trainable_variables))
  
  discriminator_x_optimizer.apply_gradients(zip(discriminator_x_gradients,
                                                discriminator_x.trainable_variables))
  
  discriminator_y_optimizer.apply_gradients(zip(discriminator_y_gradients,
                                                discriminator_y.trainable_variables))


for epoch in range(EPOCHS):
  start = time.time()

  n = 0
  for image_x, image_y in tf.data.Dataset.zip((train_horses, train_zebras)):
    train_step(image_x, image_y)
    if n % 10 == 0:
      print ('.', end='')
    n += 1

  clear_output(wait=True)
  # Using a consistent image (sample_horse) so that the progress of the model
  # is clearly visible.
  generate_images(generator_g, sample_horse)

  if (epoch + 1) % 5 == 0:
    ckpt_save_path = ckpt_manager.save()
    print ('Saving checkpoint for epoch {} at {}'.format(epoch+1,
                                                         ckpt_save_path))

  print ('Time taken for epoch {} is {} sec\n'.format(epoch + 1,
                                                      time.time()-start))


# ## Generate using test dataset

# Run the trained model on the test dataset
# for inp in test_horses.take(5):
#   generate_images(generator_g, inp)



import subprocess
subprocess.run(["pip", "install", "git+https://github.com/tensorflow/examples.git"])

import tensorflow as tf


import tensorflow_datasets as tfds
from tensorflow_examples.models.pix2pix import pix2pix

from glob import glob
import os 

image_path_list = glob('/content/horse/*.jpg')
horse_img = tf.data.Dataset.list_files(image_path_list)

for i in horse_img:
    print(i)

tf.Tensor(b'/content/horse/horse4.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/horse3.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/horse2.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/horse1.jpg', shape=(), dtype=string)


def normalize(image):
  image = tf.cast(image, tf.float32)
  image = (image / 127.5) - 1
  return image

def load_images(path):
    image = tf.io.read_file(path)
    image = tf.io.decode_image(image, expand_animations = False)
    return image

def preprocess_image_test(image):
    image = tf.image.resize(image, [256, 256])
    image = normalize(image)
    return image

horse_img = horse_img.map(load_images)
horse_img = horse_img.map(
    preprocess_image_test, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)

for i in horse_img:
    print(i.shape)

(1, 256, 256, 3)
(1, 256, 256, 3)
(1, 256, 256, 3)
(1, 256, 256, 3)

for inp in horse_img.take(4):
  generate_images(generator_g, inp)

最佳答案

更新

正如我们在评论中提到的,here是工作文件,包括 notebookpython 脚本。笔记本文件在colab上测试,python文件在本地机器上测试。仅供引用,您遇到的错误主要是因为没有加载图像文件或其他文件。因此,请确保在处理之前正确加载文件。


您不需要任何转换工具。从该示例下载笔记本 page 。打开笔记本文件并将笔记本文件保存为 .py 格式:

File -> Download -> Python (.py)

通过这样做,将保存 .py。现在,打开它。您可能需要改变一些事情。一开始,您需要执行以下操作:

# no need, so comment this 
# get_ipython().system('pip install -q git+https://github.com/tensorflow/examples.git')

# need, add this
import subprocess
subprocess.run(["pip", "install", "git+https://github.com/tensorflow/examples.git"])

从现在开始,您可以像往常一样运行这个 .py 脚本。需要提醒您的一件事是,由于有一些 plot 函数(应该在笔记本中逐个单元地运行),您将看到弹出一个外部窗口。您需要退出这些才能继续运行代码。如果您不关闭这些窗口,程序将不会进一步运行。

enter image description here


但是这里有一些另外的事情我必须为我的本地机器进行更改。我在 Windows 上使用 TensorFlow 2.4.1,GPU RTX 2070 8GB。我将BUFFER_SIZE1000更改为256,不是逐渐而是直接设置这个大小。 并且,我还面临(虽然没有引起任何训练问题)以下问题(了解更多 Source 。)

CUBLAS_STATUS_ALLOC_FAILED

幸运的是程序没有崩溃,但为了安全起见,我在 .py 文件的最开头添加了以下代码。

GPUS = tf.config.experimental.list_physical_devices('GPU')
if GPUS:
    try:
        for GPU in GPUS:
            tf.config.experimental.set_memory_growth(GPU, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(GPUS), "Physical GPUs,", len(logical_gpus), "Logical GPUs") 
    except RuntimeError as  RE:
        print(RE)

开个玩笑,我第一次看到我的 CUDA 核心被持续使用了 96%,这是第一阶段的快乐,随后是获得更好的结果。 >^-^<

以下是 4 轮训练后的一些预测。

enter image description here enter image description here enter image description here enter image description here


推理/预测

让我们尝试一下 sample 。我们从 here 中抓取了一些马样本我们会将它们传递给生成模型。

from glob import glob
import os 

image_path_list = glob('/content/horse/*.jpg')
horse_img = tf.data.Dataset.list_files(image_path_list)

for i in horse_img:
    print(i)

tf.Tensor(b'/content/horse/2.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/3.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/4.jpg', shape=(), dtype=string)
tf.Tensor(b'/content/horse/1.jpg', shape=(), dtype=string)
def normalize(image):
  image = tf.cast(image, tf.float32)
  image = (image / 127.5) - 1
  return image

def load_images(path):
    image = tf.io.read_file(path)
    image = tf.io.decode_image(image, expand_animations = False)
    return image

def preprocess_image_test(image):
    image = tf.image.resize(image, [256, 256])
    image = normalize(image)
    return image

horse_img = horse_img.map(load_images)
horse_img = horse_img.map(
    preprocess_image_test, num_parallel_calls=AUTOTUNE).cache().shuffle(
    BUFFER_SIZE).batch(1)
for i in horse_img:
    print(i.shape)

(1, 256, 256, 3)
(1, 256, 256, 3)
(1, 256, 256, 3)
(1, 256, 256, 3)
for inp in horse_img.take(4):
  generate_images(generator_g, inp)

enter image description here enter image description here enter image description here enter image description here

关于python - 无法从 Google Colab 外部的 Tensorflow 运行 CycleGan 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66846582/

相关文章:

machine-learning - 如何在交叉验证中使用过采样数据?

从一组中选择 n 个向量同时最小化成本的算法

python - Big sur QT 不显示小部件

python - 谁使用 tf.estimator.train_and_evaluate 提前停止评估损失?

python - 在 Keras 中定义二进制掩码

python - 代码不收敛普通梯度下降

python - 插入表时处理 SQLAlchemy 中的触发器

python - 在本地集群上运行 dask 时超时 OSError

python - 为什么我系统安装的numpy没有matmul?

python - TensorFlow:如何编写多步衰减