在Tensorflow中训练CNN

时间:2018-10-23 21:45:02

标签: tensorflow

我正在训练CNN以在Tensorflow中进行回归。但是,我的CNN不能正常运行。当我将训练后的数据提供给CNN时,它将得到一个大小为100 * 100且值为零的矩阵。我的输入是大小为200 * 100的图像,而我的目标或CNN的输出是大小为100 * 100的图像。如果有人在这方面能帮助我,我将非常感激。这是我的代码:

import tensorflow as tf 
import numpy as np 
#from Dataset_Preprocessing import Create_Train_Validation_Test


batch_size = 64
imag_size = 100;
num_channels = 1;
Learning_rate1 = 1e-3
TRAIN_DIR = 'Image/'
TEST_DIR = 'Label/'
MODEL_NAME = 'Image_Reconstruction_CNN-{}-{}-model'.format('5_convolutional_layer', 'HOJAT')


# Loading Data 
Train_X = np.load('Train_X.npy')
Train_Y = np.load('Train_Y.npy')
Validation_X = np.load('Validation_X.npy')
Validation_Y = np.load('Validation_Y.npy')
Test_X = np.load('Test_X.npy')
Test_Y = np.load('Test_Y.npy')

x = tf.placeholder(tf.float32, shape=[None, 2*imag_size,imag_size,1], name='x')
y = tf.placeholder(tf.float32, shape=[None, imag_size,imag_size,1], name='y')

""" 
defined functions
>>>
>>>
>>>
"""

def create_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

def create_biases(shape):
    return tf.Variable(tf.truncated_normal([shape], stddev=0.05))

def create_convolutional_layer_ReLU(data, num_input_channels, conv_filter_size, num_filters):  

    weights = create_weights(shape = [conv_filter_size, conv_filter_size, num_input_channels, num_filters])
    print(weights)

    biases = create_biases(shape = num_filters)

    layer = tf.nn.conv2d(data, weights, strides = [1, 1, 1, 1], padding = 'SAME')

    layer += biases

    layer = tf.nn.relu(layer)

    return layer


def create_convolutional_layer_BN_ReLU(data, num_input_channels, conv_filter_size, num_filters):  

    weights = create_weights(shape = [conv_filter_size, conv_filter_size, num_input_channels, num_filters])
    print(weights)

    biases = create_biases(shape = num_filters)

    layer = tf.nn.conv2d(data, weights, strides = [1, 1, 1, 1], padding = 'SAME')

    layer += biases

    layer = tf.layers.batch_normalization(layer)

    layer = tf.nn.relu(layer)

    return layer


"""
>>>
>>>
>>>
end of defined functions 
>>>
>>>
>>>
"""

def Image_Reconstruction_CNN(x):

    layer_1 = create_convolutional_layer_ReLU(x,num_input_channels = 1, conv_filter_size = 3, num_filters= 64)

    layer_2 = create_convolutional_layer_BN_ReLU(layer_1,num_input_channels = 64, conv_filter_size = 3, num_filters= 64)

    layer_2 = tf.nn.dropout(layer_2,0.7)

    layer_3 = create_convolutional_layer_BN_ReLU(layer_2,num_input_channels = 64, conv_filter_size = 3, num_filters= 64)

    layer_3 = tf.nn.dropout(layer_3,0.7)

    layer_3 = tf.nn.max_pool(layer_3, ksize = [1,100,1,1], strides = [1,2,1,1], padding = 'SAME')

    layer_4 = create_convolutional_layer_BN_ReLU(layer_3,num_input_channels = 64, conv_filter_size = 3, num_filters= 64)

    layer_5 = create_convolutional_layer_BN_ReLU(layer_4,num_input_channels = 64, conv_filter_size = 3, num_filters= 1)

    return layer_5

#Helper Function For Showing Progress of The Network
#def show_progress(epoch, feed_dict_train, feed_dict_validate, val_loss):
#  acc = session.run(accuracy, feed_dict=feed_dict_train)
#  val_acc = session.run(accuracy, feed_dict=feed_dict_validate)
#  msg = "Training Epoch {0} --- Training Accuracy: {1:>6.1%}, Validation Accuracy: {2:>6.1%},  Validation Loss: {3:.3f}"
#  print(msg.format(epoch + 1, acc, val_acc, val_loss))

Save_Dir = 'MODEL_SAVER_2/'
from scipy.io import savemat


prediction = Image_Reconstruction_CNN(x)

Loss = tf.losses.mean_squared_error(y,prediction)
cost = tf.reduce_mean(Loss)
optimizer = tf.train.AdamOptimizer(learning_rate= Learning_rate1).minimize(cost)

accuracy = tf.metrics.mean_absolute_error(y,prediction)
init_g = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()
hm_epochs = 1

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables()) 
    sess.run(init_g)
    sess.run(init_l)

    saver = tf.train.Saver()

    for epoch in range(hm_epochs):
        epoch_loss = 0

        k=0
        batch_id=1
        while k< 2605:
            start=k
            end=k+batch_size
            batch_x = np.array(Train_X[start:end])
            batch_y = np.array(Train_Y[start:end])
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
            epoch_loss += c
            k += batch_size

            print('Epoch {}. Batch {} out of 41. Epoch {}, Batch {} loss is {}'.format(
                    epoch+1,batch_id,epoch+1,batch_id,epoch_loss))

            with open("RESULT.txt","a") as text:
                print('Epoch {}. Batch {} out of {}. Epoch {}, Batch {} loss is {}'.format(
                        epoch+1, int(Train_X.shape[0]/batch_size)+1, batch_id, epoch+1, batch_id,epoch_loss), file = text) 


            batch_id += 1

        saver.save(sess,Save_Dir)


        result1 = sess.run(prediction, feed_dict={x: Train_X[0:50], y: Train_Y[0:50]})
        savemat('result1.mat',mdict={'result1':result1})

        print('')
        print('Epoch {} completed out of {} Epochs. Epoch {}, TOTAL Loss is {}'.format(epoch+1,hm_epochs,epoch+1,epoch_loss))

        with open("RESULT.txt","a") as text:
            print('',file= text)
            print('Epoch {} completed out of {} Epochs. Epoch {}, TOTAL Loss is {}'.format(
                    epoch+1,hm_epochs,epoch+1,epoch_loss),file= text)

        acc = sess.run(accuracy, feed_dict={x: Validation_X, y: Validation_Y})

        print('Accuracy:', acc)
        print('')

        with open("RESULT.txt","a") as text:
            print('Accuracy:', acc, file=text)
            print('',file=text)

0 个答案:

没有答案
相关问题