def train_upscale_cnn_gpu(datasets, batch_size, input_shape, output_shape, channel_size, sess): iterator = tf.compat.v1.data.make_one_shot_iterator(datasets) dataset = iterator.get_next() parsed_dataset = tf.io.parse_example(dataset, features={ 'filename': tf.io.FixedLenFeature([], tf.string), "x_image": tf.io.FixedLenFeature([], tf.string), "y_image": tf.io.FixedLenFeature([], tf.string)}) x_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['x_image'][index]) for index in range(0, batch_size)], tf.float32) y_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['y_image'][index]) for index in range(0, batch_size)], tf.float32) # 七層CNN 同尺寸 低解 -> 高解 # with tf.device('/job:localhost/replica:0/task:0/device:XLA_GPU:0'): # stage 1 y1 = cnn.add_deconv_layer(x_s, [3, 3, 128, 3], [batch_size, output_shape[0], output_shape[1], 128]) y2 = cnn.add_cnn_layer(y1, [1, 1, 128, 32]) y3 = cnn.add_cnn_layer(y2, [3, 3, 32, 3]) loss = tf.reduce_mean(tf.square(y3 - y_s)) optimizer = tf.compat.v1.train.AdamOptimizer(1e-4) train_step = optimizer.minimize(loss) if os.path.isfile("trained_parameters/upscale_128_3_layer_" + str(batch_size) + "p.index"): saver = tf.compat.v1.train.Saver() saver.restore(sess=sess, save_path="trained_parameters/upscale_128_3_layer_" + str(batch_size) + "p") else: saver = tf.compat.v1.train.Saver() sess.run(tf.compat.v1.global_variables_initializer()) while True: _, loss_val = sess.run([train_step, loss]) saver.save(sess=sess, save_path="trained_parameters/upscale_128_3_layer_" + str(batch_size) + "p") print(loss_val)
def two_times_srcnn_model(self): with self.g.as_default(): iterator = tf.compat.v1.data.make_one_shot_iterator(self.datasets) dataset = iterator.get_next() parsed_dataset = tf.io.parse_example(dataset, features={ 'filename': tf.io.FixedLenFeature([], tf.string), "x_image": tf.io.FixedLenFeature([], tf.string), "y_image": tf.io.FixedLenFeature([], tf.string)}) x_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['x_image'][index]) for index in range(0, self.batch_size)], tf.float32) y_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['y_image'][index]) for index in range(0, self.batch_size)], tf.float32) y1 = cnn.add_deconv_layer(x_s, [3, 3, 128, 3], [self.batch_size, self.output_shape[0], self.output_shape[1], 128]) y2 = cnn.add_cnn_layer(y1, [1, 1, 128, 32]) self.y3 = cnn.add_cnn_layer(y2, [3, 3, 32, 3]) self.loss = tf.reduce_mean(tf.square(self.y3 - y_s)) optimizer = tf.compat.v1.train.AdamOptimizer(1e-4) self.train_step = optimizer.minimize(self.loss) self.saver = tf.compat.v1.train.Saver() self.sess = tf.Session(graph=self.g) if os.path.isfile("trained_parameters/srcnn_2x.index"): self.saver.restore(sess=self.sess, save_path="trained_parameters/srcnn_2x") else: self.sess.run(tf.compat.v1.global_variables_initializer())
def cnn_with_batch_norm(x_s, k_size, input_features, output_features, strides): y1 = cnn.add_cnn_layer(x_s, [k_size[0], k_size[1], input_features, output_features], strides=strides) mean, variance = tf.nn.moments(y1, [0,1,2] if input_features > 1 else [0]) A_1_bn = tf.nn.batch_normalization(y1, mean=mean, variance=variance, offset=None, scale=None, variance_epsilon=1e-4) y1_act = tf.nn.relu(A_1_bn) A_1_pool = tf.nn.max_pool(y1_act, ksize=(1, 3, 3, 1), strides=(1, 1, 1, 1), padding='SAME') return A_1_pool
def two_times_srcnn_predit(self, image, input_shape, channel_size): x_s = tf.placeholder(tf.float32, [None, input_shape[0], input_shape[1], channel_size], "x_test") y1 = cnn.add_deconv_layer(x_s, [3, 3, 128, 3], [batch_size, input_shape[0]*2, output_shape[1]*2, 128]) y2 = cnn.add_cnn_layer(y1, [1, 1, 128, 32]) y3 = cnn.add_cnn_layer(y2, [3, 3, 32, 3]) pred = tf.cast(y3, tf.uint8) saver = tf.compat.v1.train.Saver() sess = tf.Session() if os.path.isfile("trained_parameters/srcnn_2x.index"): saver.restore(sess=sess, save_path="trained_parameters/srcnn_2x") else: raise '請先完成訓練,或將指定權重放在正確的資料夾下' out_put = sess.run(pred, feed_dict={x_s: image}) return out_put[0]
[np.shape(test_dataset)[0], shape_size]) test_labels = im_creator.test_labels() # define placeholder for inputs to network x_s = tf.placeholder(tf.float32, [None, 2352]) # 28x28 y_s = tf.placeholder(tf.float32, [None, 10]) keep_prob = tf.placeholder(tf.float32) x_image = tf.reshape(x_s, [-1, 28, 28, 3]) # print(x_image.shape) # [n_samples, 28,28,1] ## conv1 layer ## # W_conv1 = weight_variable([5,5, 3,32]) # patch 5x5, in size 1, out size 32 # b_conv1 = bias_variable([32]) # h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32 # h_pool1 = max_pool_2x2(h_conv1) # output size 14x14x32 y1 = cnn.add_cnn_layer(x_image, tf.shape(x_image)[0], [5, 5, 3, 32]) h_pool1 = cnn.add_pooling_layer(y1) ## conv2 layer ## # W_conv2 = weight_variable([5,5, 32, 64]) # patch 5x5, in size 32, out size 64 # b_conv2 = bias_variable([64]) # h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 14x14x64 # h_pool2 = max_pool_2x2(h_conv2) # output size 7x7x64 ## fc1 layer ## # W_fc1 = weight_variable([7*7*64, 1024]) # b_fc1 = bias_variable([1024]) # # [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64] # h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) # h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
def train_srcnn_gpu(datasets, batch_size, input_shape, output_shape, channel_size, sess): iterator = tf.compat.v1.data.make_one_shot_iterator(datasets) dataset = iterator.get_next() parsed_dataset = tf.io.parse_example(dataset, features={ 'filename': tf.io.FixedLenFeature([], tf.string), "x_image": tf.io.FixedLenFeature([], tf.string), "y_image": tf.io.FixedLenFeature([], tf.string)}) x_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['x_image'][index]) for index in range(0, batch_size)], tf.float32) y_s = tf.cast([tf.image.decode_jpeg(parsed_dataset['y_image'][index]) for index in range(0, batch_size)], tf.float32) # deconv_3 = add_deconv_layer(x_s, [3, 3, 32, 3], [batch_size, input_shape[0] * 2, input_shape[1] * 2, 32], stride=2) # 七層CNN 同尺寸 低解 -> 高解 # with tf.device('/job:localhost/replica:0/task:0/device:XLA_GPU:0'): # stage 1 y1 = cnn.add_cnn_layer(x_s, [3, 3, 3, 64]) y2 = cnn.add_cnn_layer(y1, [1, 1, 64, 64]) y3 = cnn.add_cnn_layer(y2, [3, 3, 64, 64]) y4 = cnn.add_cnn_layer(y3, [3, 3, 64, 64]) y5 = cnn.add_cnn_layer(y4, [3, 3, 64, 128]) x_ = cnn.add_cnn_layer(x_s, [3, 3, 3, 128]) y5_act = tf.nn.relu(tf.add(y5, x_)) y6 = cnn.add_cnn_layer(y5_act, [3, 3, 128, 3]) # y1 = cnn_with_batch_norm(x_s, [3, 3], 3, 64, 1) # y2 = cnn_with_batch_norm(y1, [1, 1], 64, 64, 1) # y3 = cnn_with_batch_norm(y2, [3, 3], 64, 64, 1) # y4 = cnn_with_batch_norm(y3, [3, 3], 64, 64, 1) # y5 = cnn_with_batch_norm(y4, [3, 3], 64, 128, 1) # stage 1 x # x_ = cnn_with_batch_norm(x_s, [1,1], 3, 128, 1) # mean, variance = tf.nn.moments(x_, [0,1,2]) # x_bn = tf.nn.batch_normalization(x_, mean=mean, variance=variance, offset=None, scale=None, variance_epsilon=1e-4) # y5_act = tf.nn.relu(tf.add(y5, x_bn)) # y6 = cnn_with_batch_norm(y5_act, [3,3], 128, 3, 1) # pool = tf.nn.avg_pool(y6, ksize=(1, 3, 3, 1), strides=(1, 1, 1, 1), padding='SAME') loss = tf.reduce_mean(tf.square(y6 - y_s)) optimizer = tf.compat.v1.train.AdamOptimizer(1e-4) train_step = optimizer.minimize(loss) if os.path.isfile("trained_parameters/srcnn_f33" + str(batch_size) + "p.index"): saver = tf.compat.v1.train.Saver() saver.restore(sess=sess, save_path="trained_parameters/srcnn_f33" + str(batch_size) + "p") else: saver = tf.compat.v1.train.Saver() sess.run(tf.compat.v1.global_variables_initializer()) while True: _, loss_val = sess.run([train_step, loss]) saver.save(sess=sess, save_path="trained_parameters/srcnn_f33" + str(batch_size) + "p") print(loss_val)