用于将HSL颜色转换为RGB的神经网络模型

时间:2019-06-06 11:07:31

标签: python tensorflow machine-learning neural-network

我正在尝试实现一个神经网络,该网络获取HSL中的颜色代码,并尝试使用张量流将其转换为RGB,我不确定哪一部分是错误的,因为我正遭受巨大的损失并且准确性非常低

我是这个概念的新手,我尝试遵循针对mnist数据库实施的另一个代码的步骤

import tensorflow as tf
import random as rd
import numpy as np

# Parameters
learning_rate = 0.1
num_steps = 1000
#batch_size = 128
display_step = 100

# Network Parameters
n_hidden_1 = 4 # 1st layer number of neurons
n_hidden_2 = 4 # 2nd layer number of neurons
num_input = 3 # data input (HSL values)
num_classes = 3 # data output (RGB values)

# tf Graph input
X = tf.placeholder(tf.float32, [None, num_input])
Y = tf.placeholder(tf.float32, [None, num_classes])

# Store layers weight & bias
layers = {
    'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}


# Create model
def neural_net(x):
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.add(tf.matmul(x, layers['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)


    # Hidden fully connected layer with 256 neurons
    layer_2 = tf.add(tf.matmul(layer_1, layers['h2']), biases['b2'])
    layer_2 = tf.nn.sigmoid(layer_2)


    # Output fully connected layer with a neuron for each class
    out_layer = tf.matmul(layer_2, layers['out']) + biases['out']
    return out_layer



# Construct model
logits = neural_net(X)
prediction = tf.nn.softmax(logits)

# Define loss and optimizer
loss_op = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(
        logits = logits,
        labels = Y
    ))
#changed adam optimizer to gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train_op = optimizer.minimize(loss_op)

# Evaluate model
## changed prediction with logits
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()


def hsl_to_rgb(input):
    r=0
    g=0
    bb=0
    h = input[0]
    s = input[1]
    l = input[2]
    cc = (1 - abs(2 * l - 1)) * s
    x = cc * (1 - abs((h / 60) % 2 - 1))
    m = l - (cc / 2)
    if 0 <= h < 60:
        [r, g, bb] = [cc, x, 0]
    if 60 <= h < 120:
        [r, g, bb] = [x, cc, 0]
    if 120 <= h < 180:
        [r, g, bb] = [0, cc, x]
    if 180 <= h < 240:
        [r, g, bb] = [0, x, cc]
    if 240 <= h < 300:
        [r, g, bb] = [x, 0, cc]
    if 300 <= h < 360:
        [r, g, bb] = [cc, 0, x]
    return [int(round((r + m) * 255)), int(round((g + m) * 255)), 
        int(round((bb + m) * 255))]


#feeding the input and output
inList = []
actualList = []
for i in range (100):
    h = rd.randint(1, 361)
    s = rd.random()
    l = rd.random()
    inList.append([h, s, l])
    actualList.append(hsl_to_rgb([h, s, l]))



# Start training
with tf.Session() as sess:

    # Run the initializer
    sess.run(init)

    for step in range(1, num_steps+1):
        #batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        #sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
        sess.run(train_op, feed_dict={X:inList, Y:actualList})
        if step % display_step == 0 or step == 1:
            # Calculate batch loss and accuracy
            loss, acc = sess.run([loss_op, accuracy], 
            feed_dict{X:inList,Y:actualList})
            print("Step " + str(step) + ", Minibatch Loss= " +
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))

    print("Optimization Finished!")

    # Calculate accuracy
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={X:[[54,.26,.88]],
                                      Y:[[232, 231, 216]]}))

1 个答案:

答案 0 :(得分:0)

您需要规范化数据。

#feeding the input and output
inList = []
actualList = []
for i in range (100):
    h = rd.randint(1, 361)
    h /= 361
    s = rd.random()
    l = rd.random()
    inList.append([h, s, l])
    rgb = hsl_to_rgb([h, s, l])
    rgb[0] /= 255
    rgb[1] /= 255
    rgb[2] /= 255
    print(rgb)
    actualList.append(rgb)

您可以将网络输出乘以255,以获得实际的rgb值。

希望有帮助。