在做图像风格迁移实战中,需要使用VGG19网络进行计算输出,但是总是出现“服务似乎挂掉了,但是会立刻重启”的提示。
捣鼓了一天,也有按照网上出现的“8888端口被占用需要释放内存空间”的步骤去解决,但是程序一旦运行仍然出现问题,最终发现了原因:是输入图像过大导致了此问题。首先展示了一下问题
# import相关模块
import tensorflow as tf
import matplotlib.pyplot as plt
import os
# 加载VGG19网络
vgg = tf.keras.applications.VGG19(include_top=False,weights='imagenet')
# 加载style图像和content图像(省略了加载图像函数load_image)
style_img = load_image(STYLE_IMAGE_PATH)
content_img = load_image(CONTENT_IMAGE_PATH)
print(style_img.shape)
print(content_img.shape)
分别运行以下两个程序,将两幅图像分别送进VGG19网络计算输出。输入内容图像,运行正常;输入风格图像,开始出现“服务器似乎挂掉”的问题。如果使用tf.image.resize函数将风格图像缩小(450,300),就可以运行正常,因此使用已经训练好的网络模型必须将图像缩放到一个合理范围之内。至于为什么这样,由于手头有了新的任务需要做,就暂时记下待以后解决。如果有大佬知道原因,还望赐教。
inputs = tf.constant(content_img)*255
preprocessed_input = tf.keras.applications.vgg19.preprocess_input(inputs)
out = vgg(preprocessed_input)
inputs = tf.constant(style_img)*255
preprocessed_input = tf.keras.applications.vgg19.preprocess_input(inputs)
out = vgg(preprocessed_input)