0

从出现的通知中我可以看出,Microsoft Visual Studio 无法将数字 3221226505 转换为 int32。但是,我不明白在代码中的哪一点被要求这样做。我正在尝试复制 TensorFlow 的 Nueral Style Transfer,如下所示:

from matplotlib import gridspec
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub

def load_image(image_path, image_size=(256, 256), preserve_aspect_ratio=True):
    img = tf.io.decode_image(
      tf.io.read_file(image_path),
      channels=3, dtype=tf.float32)[tf.newaxis, ...]
    img = crop_center(img)
    img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)
    return img

def crop_center(image):
    shape = image.shape
    new_shape = min(shape[1], shape[2])
    offset_y = max(shape[1] - shape[2], 0) // 2
    offset_x = max(shape[2] - shape[1], 0) // 2
    cropped_image = tf.image.crop_to_bounding_box(image, offset_y, offset_x, new_shape, new_shape)
    return cropped_image

def visualize(images, titles=('',)):
    n = len(images)
    image_sizes = [image.shape[1] for image in images]
    w = (image_sizes[0] * 6) // 320
    plt.figure(figsize=(w  * n, w))
    gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)
    for i in range(n):
        plt.subplot(gs[i])
        plt.imshow(images[i][0], aspect='equal')
        plt.axis('off')
        plt.title(titles[i] if len(titles) > i else '')
        plt.savefig("final.jpg")
    plt.show()
content_image = "InsertFilePathHere"
style_image = "InsertFilePathHere"
output_img_size = 512
content_img_size = (output_img_size, output_img_size)
style_img_size = (256, 256)
content_image = load_image(content_image, content_img_size)
style_image = load_image(style_image, style_img_size)

style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')
#visualize([content_image, style_image], ['Content Image', 'Style Image'])

hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
hub_module = hub.load(hub_handle)

results = hub_module(tf.constant(content_image), tf.constant(style_image))

stylized_image = results[0]

visualize([content_image, style_image, stylized_image], titles=['Original Image', 'Style Image', 'Stylized Image'])

根据我所做的故障排除,代码运行到以下行:

hub_module = hub.load(hub_handle)

然后停止运行,并选择“按任意键继续”,然后弹出错误消息。

4

0 回答 0