尝试在Django得到值错误的情况下使用tensorflow运行神经样式传递

时间:2018-11-21 19:37:08

标签: python django tensorflow

Picture of error message

views.py

def neural_style_transfer(request):
    get_image_flag = True
    photo_content_number = request.session.get('photo_content', None)
    photo_style_number = request.session.get('photo_style', None)
    current_path = str(os.getcwd())

if photo_content_number:
    photo_content = Photo.objects.get(id=photo_content_number)
    photo_content_url = photo_content.picture.url
    photo_content_url = current_path + photo_content_url 

if photo_style_number:
    photo_style = Photo.objects.get(id=photo_style_number)
    photo_style_url = photo_style.picture.url
    photo_style_url = current_path + photo_style_url

if request.method == 'POST':  
    action = request.POST['action']
    if action == "Switch":
        temp = request.session.get('photo_content', None)
        request.session['photo_content'] = request.session.get('photo_style', None)
        request.session['photo_style'] = temp

        photo_content, photo_style = photo_style, photo_content
        photo_content_url, photo_style_url = photo_style_url, photo_content_url
        return redirect('/neural_style_transfer')
    elif action == "Reset":
        photo_content_number = 0
        photo_style_number = 0
        photo_content = None
        photo_content_url = None
        photo_style = None
        photo_style_url = None
        return redirect('/neural_style_transfer')
    elif action == "Process":
        load_model()
        photo_generated = Photo()
        photo_generated.save()
        photo_generated.uploader = request.user.username
        photo_generated_abs_url = current_path + "/media/pictures/" + str(photo_generated.id) + "_output.png"
        photo_generated_url = "/pictures/" + str(photo_generated.id) + "_output.png"        
        work_flag = True            
        nst.transfer(photo_content_url, photo_style_url, photo_generated_abs_url)
        photo_generated.picture = photo_generated_url
        photo_generated.save()
return render(request, 'neural_style_transfer.html', locals())

def load_vgg_model(path):
vgg = scipy.io.loadmat(path)

vgg_layers = vgg['layers']

def _weights(layer, expected_layer_name):

    wb = vgg_layers[0][layer][0][0][2]
    W = wb[0][0]
    b = wb[0][1]
    layer_name = vgg_layers[0][layer][0][0][0][0]
    assert layer_name == expected_layer_name
    return W, b

    return W, b

def _relu(conv2d_layer):
    return tf.nn.relu(conv2d_layer)

def _conv2d(prev_layer, layer, layer_name):
    W, b = _weights(layer, layer_name)
    W = tf.constant(W)
    b = tf.constant(np.reshape(b, (b.size)))
    return tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b

def _conv2d_relu(prev_layer, layer, layer_name):
    return _relu(_conv2d(prev_layer, layer, layer_name))

def _avgpool(prev_layer):
    return tf.nn.avg_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

graph = {}
graph['input']   = tf.Variable(np.zeros((1, CONFIG.IMAGE_HEIGHT, CONFIG.IMAGE_WIDTH, CONFIG.COLOR_CHANNELS)), dtype = 'float32')
graph['conv1_1']  = _conv2d_relu(graph['input'], 0, 'conv1_1')
graph['conv1_2']  = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
graph['avgpool1'] = _avgpool(graph['conv1_2'])
graph['conv2_1']  = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1')
graph['conv2_2']  = _conv2d_relu(graph['conv2_1'], 7, 'conv2_2')
graph['avgpool2'] = _avgpool(graph['conv2_2'])
graph['conv3_1']  = _conv2d_relu(graph['avgpool2'], 10, 'conv3_1')
graph['conv3_2']  = _conv2d_relu(graph['conv3_1'], 12, 'conv3_2')
graph['conv3_3']  = _conv2d_relu(graph['conv3_2'], 14, 'conv3_3')
graph['conv3_4']  = _conv2d_relu(graph['conv3_3'], 16, 'conv3_4')
graph['avgpool3'] = _avgpool(graph['conv3_4'])
graph['conv4_1']  = _conv2d_relu(graph['avgpool3'], 19, 'conv4_1')
graph['conv4_2']  = _conv2d_relu(graph['conv4_1'], 21, 'conv4_2')
graph['conv4_3']  = _conv2d_relu(graph['conv4_2'], 23, 'conv4_3')
graph['conv4_4']  = _conv2d_relu(graph['conv4_3'], 25, 'conv4_4')
graph['avgpool4'] = _avgpool(graph['conv4_4'])
graph['conv5_1']  = _conv2d_relu(graph['avgpool4'], 28, 'conv5_1')
graph['conv5_2']  = _conv2d_relu(graph['conv5_1'], 30, 'conv5_2')
graph['conv5_3']  = _conv2d_relu(graph['conv5_2'], 32, 'conv5_3')
graph['conv5_4']  = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4')
graph['avgpool5'] = _avgpool(graph['conv5_4'])

return graph

class CONFIG:
     IMAGE_WIDTH = 200
     IMAGE_HEIGHT = 150
     COLOR_CHANNELS = 3
     NOISE_RATIO = 0.6
     MEANS = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3)) 
     VGG_MODEL = 'pretrained-model/imagenet-vgg-verydeep-19.mat' 
     STYLE_IMAGE = 'images/stone_style.jpg' .
     CONTENT_IMAGE = 'images/content300.jpg' 
     OUTPUT_DIR = 'output/'

def load_model():
    model = load_vgg_model("/home/jerry/picturesque2/gallery/Neural_Style_Transfer/pretrained-model/imagenet-vgg-verydeep-19.mat")
    TF_CONFIG_ = tf.ConfigProto()
    TF_CONFIG_.gpu_options.allow_growth = True
    sess = tf.Session(config = TF_CONFIG_)

neura_style_transfer.py

def transfer(content_image_url, style_image_url, generated_image_url):

    content_image = imread(content_image_url)
    content_image = reshape_and_normalize_image(content_image)
    print("shape of content image", content_image.shape)
    content_image.resize((1, CONFIG.IMAGE_HEIGHT, CONFIG.IMAGE_WIDTH, CONFIG.COLOR_CHANNELS))

    style_image = imread(style_image_url)
    style_image = reshape_and_normalize_image(style_image)
    print("shape of style image", style_image.shape)
    style_image.resize((1, CONFIG.IMAGE_HEIGHT, CONFIG.IMAGE_WIDTH, CONFIG.COLOR_CHANNELS))

    generated_image = generate_noise_image(content_image)

    sess.run(model['input'].assign(content_image))
    out = model['conv4_2']
    a_C = sess.run(out)
    a_G = out
    J_content = compute_content_cost(a_C,a_G)
    sess.run(model['input'].assign(style_image)) 
    J_style = compute_style_cost(model,STYLE_LAYERS)
    J = total_cost(J_content, J_style, 10, 100000)

    optimizer = tf.train.AdamOptimizer(2.0)
    train_step = optimizer.minimize(J)
    generated_image = model_nn(train_step, J, J_content, J_style, generated_image)
    imageio.imwrite(generated_image_url, generated_image)
    sess.close()

问题是,neura_style_transfer.py算法可以独立运行。我可以在Django上进行图像处理,例如灰度。但是,当我尝试在Django上运行NST时,出现了值错误。我相信我已经将图片调整为适当的尺寸。任何帮助或猜测都会对我有很大帮助。如果有任何细节可以找到导致此问题的原因,我将尽快提供。谢谢您的宝贵时间。

硬件:  -作业系统:Ubuntu16.04 LTS  -CPU:Intel Core i7  -Ram:DDR4 16GB  -GPU:GTX 1080ti

0 个答案:

没有答案
相关问题