def train(self): tf.initialize_all_variables().run() self.saver = tf.train.Saver() load(self) self.g_sum = tf.summary.merge_all() self.writer = tf.summary.FileWriter("./logs", self.sess.graph) counter = 1 start_time = time.time() train_data = read_h5_data(self.train_data_path) for epoch in xrange(self.config.epoch): batch_idxs = len(train_data) // self.config.batch_size for idx in xrange(0, batch_idxs): batch = train_data[idx * self.config.batch_size:(idx + 1) * self.config.batch_size] _, errD, tmp = self.sess.run( [self.d_optim, self.d_loss, self.clip_d], feed_dict={self.images: batch}) ''' _,errD = self.sess.run([self.d_optim, self.d_loss,], feed_dict = {self.images: batch}) ''' _, summary_str, errG = self.sess.run( [self.g_optim, self.g_sum, self.g_content_loss], feed_dict={self.images: batch}) self.writer.add_summary(summary_str, counter) counter += 1 if counter % 10 == 0: print("Epoch: [%2d], step: [%2d], time: [%4.4f], gloss: [%.8f], dloss: [%.8f]" \ % ((epoch+1), counter, time.time()-start_time, errG, errD)) if np.mod(counter, 500) == 2: save(self, counter)
def test_rgb(self, img_path='Test/Set5/baby_GT.bmp'): #img_path = 'test.png' img = scipy.misc.imread(img_path) img_lr = scipy.ndimage.interpolation.zoom(img, 1.0 / self.config.scale, mode='constant') img_lr = cv2.resize(img, (171, 171)) img_lr = img_lr.astype(np.float32) img_lr = np.expand_dims(img_lr, axis=0) self.sess.run(tf.initialize_all_variables()) load(self) hr = self.generator(img_lr).eval() hr = hr.squeeze() self.imshow(hr, is_gray=False)
def test(self, img_path, config): import scipy.misc ycbcr = scipy.misc.imread(img_path, mode='YCbCr').astype(float) label = ycbcr[:, :, 0] input_ = scipy.ndimage.interpolation.zoom(label, 1.0 / 3.0) input_ = scipy.ndimage.interpolation.zoom(input_, 3.) batch_data, batch_label, nx, ny = self.input_setup(label, config) self.sess.run(tf.initialize_all_variables()) load(self) result = self.sess.run(self.pred, feed_dict={ self.images: batch_data, self.labels: batch_label }) result = merge(result, [nx, ny]) result = result.squeeze() print(result.shape) self.imshow(input_) self.imshow(result)
def test(self, img_path='Test/Set5/baby_GT.bmp'): img_color = scipy.misc.imread(img_path, mode='YCbCr') #img_color = cv2.imread(img_path) #img_color = cv2.cvtColor(img_color, cv2.COLOR_BGR2YCR_CB) img_color = img_color.transpose(2, 0, 1) img, Cb, Cr = img_color[0], img_color[1], img_color[2] img = img * np.float32(1.0 / 255.0) img = img.astype(np.float32) img_lr = scipy.ndimage.interpolation.zoom( img, 1.0 / self.config.scale, ) img_lr = np.expand_dims(np.expand_dims(img_lr, axis=2), axis=0) self.sess.run(tf.initialize_all_variables()) load(self) hr = self.generator(img_lr)[1].eval() hr = hr.squeeze() hr = fit_scale_crop(hr, img) hr = (hr * 255.0) self.imshow(hr, is_gray=True)
def generate_waveform(audio_path, sampling_rate, time_per_sample): """ Returns the audio sample as a NumPy array, along with the sampling rate. Uses function load from load_utils.py. Parameters: audio_path (str): Path to audio file. sampling_rate (float): Number of samples to take per second (discretizing time). Returns: tuple: Tuple of waveform as NumPy array and timesteps as NumPy array. """ waveform, _ = load(audio_path, sampling_rate, time_per_sample) # Converting from sample index to time in seconds, using the sampling rate. timesteps = np.linspace(0, len(waveform) / sampling_rate, len(waveform)) return waveform, timesteps
def train(self, config): if config.is_train: input_setup(self.sess, config) else: nx, ny = input_setup(self.sess, config) if config.is_train: data_dir = os.path.join('./{}'.format(config.checkpoint_dir), "train.h5") else: data_dir = os.path.join('./{}'.format(config.checkpoint_dir), "test.h5") train_data, train_label = read_data(data_dir) # Stochastic gradient descent with the standard backpropagation self.train_op = tf.train.GradientDescentOptimizer( config.learning_rate).minimize(self.loss) tf.initialize_all_variables().run() counter = 0 start_time = time.time() if load(self): print(" [*] Load SUCCESS") else: print(" [!] Load failed...") if config.is_train: print("Training...") for ep in xrange(config.epoch): # Run by batch images batch_idxs = len(train_data) // config.batch_size for idx in xrange(0, batch_idxs): batch_images = train_data[idx * config.batch_size:(idx + 1) * config.batch_size] batch_labels = train_label[idx * config.batch_size:(idx + 1) * config.batch_size] counter += 1 _, err = self.sess.run([self.train_op, self.loss], feed_dict={ self.images: batch_images, self.labels: batch_labels }) if counter % 10 == 0: print("Epoch: [%2d], step: [%2d], time: [%4.4f], loss: [%.8f]" \ % ((ep+1), counter, time.time()-start_time, err)) if counter % 500 == 0: self.save(config.checkpoint_dir, counter) else: print("Testing...") result = self.pred.eval({ self.images: train_data, self.labels: train_label }) result = merge(result, [nx, ny]) result = result.squeeze() image_path = os.path.join(os.getcwd(), config.sample_dir) image_path = os.path.join(image_path, "test_image.png") print(train_label.shape) self.imshow(result) imsave(result, image_path)