def test_generator_run(self): tf.set_random_seed(1234) noise = tf.random_normal([100, 64]) image, _ = dcgan.generator(noise) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) image.eval()
def test_generator_invalid_input(self): wrong_dim_input = tf.zeros([5, 32, 32]) with self.assertRaises(ValueError): dcgan.generator(wrong_dim_input) correct_input = tf.zeros([3, 2]) with self.assertRaisesRegexp(ValueError, 'must be a power of 2'): dcgan.generator(correct_input, final_size=30) with self.assertRaisesRegexp(ValueError, 'must be greater than 8'): dcgan.generator(correct_input, final_size=4)
def test_generator_graph(self): tf.set_random_seed(1234) # Check graph construction for a number of image size/depths and batch # sizes. for i, batch_size in zip(xrange(3, 7), xrange(3, 8)): tf.reset_default_graph() final_size = 2 ** i noise = tf.random_normal([batch_size, 64]) image, end_points = dcgan.generator( noise, depth=32, final_size=final_size) self.assertAllEqual([batch_size, final_size, final_size, 3], image.shape.as_list()) expected_names = ['deconv%i' % j for j in xrange(1, i)] + ['logits'] self.assertSetEqual(set(expected_names), set(end_points.keys())) # Check layer depths. for j in range(1, i): layer = end_points['deconv%i' % j] self.assertEqual(32 * 2**(i-j-1), layer.get_shape().as_list()[-1])
(epoch_size + 1))) D_model.save_weights('logs/D_Epoch%d-GLoss%.4f-DLoss%.4f.h5' % ((epoch + 1), G_total_loss / (epoch_size + 1), D_total_loss / (epoch_size + 1))) print('Saving state, iter:', str(epoch + 1)) if __name__ == "__main__": image_shape = [64, 64, 3] # 数据集存放路径 annotation_path = "train_lines.txt" # 生成网络和评价网络 G_model = generator(64, image_shape) D_model = discriminator(64, image_shape) # G_model_path = "model_data/Generator_Flower.h5" # D_model_path = "model_data/Discriminator_Flower.h5" # G_model.load_weights(G_model_path, by_name=True, skip_mismatch=True) # D_model.load_weights(D_model_path, by_name=True, skip_mismatch=True) with open(annotation_path) as f: lines = f.readlines() num_train = len(lines) #------------------------------------------------------# # Init_Epoch为起始世代 # Epoch总训练世代 #------------------------------------------------------#
Epoch = 500 batch_size = 64 lr = 0.002 #------------------------------# # 每隔50个step保存一次图片 #------------------------------# save_interval = 50 #------------------------------------------# # 获得图片路径 #------------------------------------------# annotation_path = "train_lines.txt" #----------------------------# # 生成网络和评价网络 #----------------------------# G_model = generator(channel, input_shape) D_model = discriminator(channel, input_shape) #------------------------------------------# # 将训练好的模型重新载入 #------------------------------------------# if G_model_path != '': G_model.load_weights(G_model_path, by_name=True, skip_mismatch=True) if D_model_path != '': D_model.load_weights(D_model_path, by_name=True, skip_mismatch=True) with open(annotation_path) as f: lines = f.readlines() num_train = len(lines) #------------------------------------------------------#
def generate(self): self.net = generator(self.channel, self.image_shape) self.net.load_weights(self.model_path) print('{} model loaded.'.format(self.model_path))