def test(img): inputs = tf.placeholder(tf.float32, [1, 448, 448, 3]) prediction = vgg16(inputs) pred_bboxes = prediction[:, :, :, :8]#[1, 7, 7, 8] pred_bboxes = tf.reshape(pred_bboxes, [1, 7, 7, 2, 4])#[1, 7, 7, 2, 4] pred_confidence = prediction[:, :, :, 8:10]#[1, 7, 7, 2] pred_class = prediction[:, :, :, 10:]#[1, 7, 7, 20] pred_bboxes = norm2bbox(pred_bboxes)#[1, 7, 7, 2, 4] class_confidences = tf.split(pred_class, num_or_size_splits=20, axis=-1) pred_bboxes = tf.reshape(pred_bboxes, [-1, 4]) indx_class = [] scores_class = [] for class_confidence in class_confidences: scores = pred_confidence * class_confidence#[1, 7, 7, 2] scores = tf.reshape(scores, [-1]) indx = tf.image.non_max_suppression(pred_bboxes, scores, 5) indx_class.append(indx[tf.newaxis, :]) scores_class.append(scores[tf.newaxis, :]) indx_class = tf.concat(indx_class, axis=0) scores_class = tf.concat(scores_class, axis=0) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, "./save_para/.\\model.ckpt") INDX_CLASS, PRED_BBOXES, SCORES_CLASS = sess.run([indx_class, pred_bboxes, scores_class], feed_dict={inputs: img[np.newaxis, :, :, :]}) bboxes, class_num = select_bbox_again(INDX_CLASS, SCORES_CLASS, PRED_BBOXES) for i in range(bboxes.shape[0]): try: img = draw_bbox(img, np.int32(bboxes[i]), OBJECT_NAMES[class_num[i]]) except: continue Image.fromarray(np.uint8(img)).show()
def __init__(self): self.clean_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) self.noised_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) self.train_phase = tf.placeholder(tf.bool) form_resnet = FormResNet("FormResNet") self.denoised_img, self.res = form_resnet(self.noised_img, self.train_phase) self.L_pix = tf.reduce_mean(tf.reduce_sum(tf.square(self.denoised_img - self.clean_img), [1, 2, 3])) # self.Phi = vgg16(tf.concat([self.denoised_img, self.denoised_img, self.denoised_img], 3)) # self.Phi_ = vgg16(tf.concat([self.clean_img, self.clean_img, self.clean_img], 3)) self.Phi = vgg16(self.denoised_img) self.Phi_ = vgg16(self.clean_img) self.L_feat = tf.reduce_mean(tf.square(self.Phi - self.Phi_)) self.L_grad = tf.reduce_mean(tf.reduce_sum(tf.abs(sobel(self.denoised_img)[0] - sobel(self.clean_img)[0]) +\ tf.abs(sobel(self.denoised_img)[1] - sobel(self.clean_img)[1]), [1, 2, 3])) self.L_cross = (1 - ALPHA - BETA) * self.L_pix + ALPHA * self.L_feat + BETA * self.L_grad self.Opt = tf.train.AdamOptimizer(1e-4).minimize(self.L_cross) self.sess = tf.Session() saver = tf.train.Saver() # saver.restore(self.sess, "./save_para_3_sigma25_2/FormResNet25.ckpt") saver.restore(self.sess, "./sigma50_6000/FormResNet50.ckpt")
def __init__(self): self.clean_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) self.noised_img = tf.placeholder(tf.float32, [None, None, None, IMG_C]) self.train_phase = tf.placeholder(tf.bool) form_resnet = FormResNet("FormResNet") self.denoised_img, self.res = form_resnet(self.noised_img, self.train_phase) self.L_pix = tf.reduce_mean( tf.reduce_sum(tf.square(self.denoised_img - self.clean_img), [1, 2, 3])) # self.Phi = vgg16(tf.concat([self.denoised_img, self.denoised_img, self.denoised_img], 3)) # self.Phi_ = vgg16(tf.concat([self.clean_img, self.clean_img, self.clean_img], 3)) self.Phi = vgg16(self.denoised_img) self.Phi_ = vgg16(self.clean_img) self.L_feat = tf.reduce_mean(tf.square(self.Phi - self.Phi_)) self.L_grad = tf.reduce_mean(tf.reduce_sum(tf.abs(sobel(self.denoised_img)[0] - sobel(self.clean_img)[0]) +\ tf.abs(sobel(self.denoised_img)[1] - sobel(self.clean_img)[1]), [1, 2, 3])) self.L_cross = ( 1 - ALPHA - BETA) * self.L_pix + ALPHA * self.L_feat + BETA * self.L_grad self.Opt = tf.train.AdamOptimizer(1e-4).minimize(self.L_cross) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def train(): inputs = tf.placeholder(tf.float32, [None, 448, 448, 3]) labels = tf.placeholder(tf.float32, [None, 7, 7, 25]) prediction = vgg16(inputs) loss, loss_bboxes, loss_confidence_obj, loss_confidence_noobj, loss_class = yolo_loss(prediction, labels) Opt = tf.train.MomentumOptimizer(1e-3, momentum=0.9).minimize(loss) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # saver.restore(sess, "./save_para/.\\model.ckpt") for i in range(10000): batch_img, batch_labels = read_batch(img_path, xml_path, BATCH_SIZE) sess.run(Opt, feed_dict={inputs: batch_img, labels: batch_labels}) [LOSS, b, o, n, c] = sess.run([loss, loss_bboxes, loss_confidence_obj, loss_confidence_noobj, loss_class], feed_dict={inputs: batch_img, labels: batch_labels}) print("Iteration: %d, Loss: %f, loss_bbox: %f, loss_confi_obj: %f, loss_confi_noobj: %f, loss_class: %f"%(i, LOSS, b, o, n, c)) if i % 500 == 0: saver.save(sess, "./save_para/model.ckpt")
def main(argv=None): ## Create the directory for training model if gfile.Exists(TRAIN_MODEL_DIR): gfile.DeleteRecursively(TRAIN_MODEL_DIR) gfile.MakeDirs(TRAIN_MODEL_DIR) # Using logging to output and record everything # set up logging to file util.set_logging(os.path.join(TRAIN_MODEL_DIR, 'myapp.log')) # Write down all the FLAGS logging.info('FLAG information') for key, value in tf.app.flags.FLAGS.__flags.iteritems(): logging.info( 'FLAG(%s) : %s'%(key, str(value))) # Select the dataset if FLAGS.dataset == 'cifar10': ds = cifar10() elif FLAGS.dataset == 'cifar100': ds = cifar100() else: raise ValueError('Wrong dataset name. Check FLAGS.dataset') # Download the dataset ds.maybe_download() # Read data train_data, train_labels = ds.read_data(True) TRAIN_SIZE = train_labels.shape[0] logging.info('Training Size = %d', TRAIN_SIZE) # This is where training samples and labels are fed to the graph. # These placeholder nodes will be fed a batch of training data at each # training step using the {feed_dict} argument to the Run() call below. # This part depends on the Dataset train_data_node = tf.placeholder( tf.float32, shape=(FLAGS.batch_size, ds.image_size(), ds.image_size(), ds.num_channel()), name='data_node') train_labels_node = tf.placeholder(tf.float32, shape=(FLAGS.batch_size, ds.num_label()), name='label_node') tf.image_summary('images', train_data_node, max_images=FLAGS.batch_size) # Training Model Architecture # Select the network if FLAGS.network == 'vgg16': network = vgg16() elif FLAGS.network == 'snn30k': network = snn30k() elif FLAGS.network == 'snn30k_wo_norm': network = snn30k_wo_norm() else: raise ValueError('Wrong dataset name. Check FLAGS.network') network_dict= network.model2(data=train_data_node, num_label=ds.num_label() , d1=FLAGS.d1, d2=FLAGS.d2, pair=FLAGS.pair, train=True) logits = network_dict['softmax_linear'] softmax = tf.nn.softmax(logits) tf.histogram_summary('logits', logits) tf.histogram_summary('softmax',softmax) # Define Objective Function cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( logits, train_labels_node), name='cross_entropy') tf.add_to_collection('losses', cross_entropy ) loss = tf.add_n(tf.get_collection('losses'), name='total_loss') tf.add_to_collection('show', cross_entropy) tf.add_to_collection('show', loss) tf.scalar_summary('loss/total_loss', loss) tf.scalar_summary('loss/entropy', cross_entropy) # Optimizer: set up a variable that's incremented once per batch and # controls the learning rate decay. batch = tf.Variable(0) # Decay once per epoch, using an exponential schedule starting at 0.01. learning_rate = tf.train.exponential_decay( FLAGS.learning_rate, # Base learning rate. batch * FLAGS.batch_size , # Current index into the dataset. TRAIN_SIZE * FLAGS.decay_step, # Decay step. FLAGS.decay_rate, # Decay rate. staircase=True, name='learning_rate' ) #learning_rate = tf.Variable(FLAGS.learning_rate, name='learning_rate') tf.scalar_summary("learning_rate", learning_rate) tf.add_to_collection('show', learning_rate) # Use simple momentum for the optimization. optimizer = tf.train.MomentumOptimizer(learning_rate, FLAGS.momentum) # optimizer = tf.train.AdamOptimizer(learning_rate) # Compute the gradients for a list of variables. grads_and_vars = optimizer.compute_gradients(loss, var_list=tf.all_variables()) # Let Batch normalization variables have higher learning rate clipped_grads_and_vars = [] """ for gv in grads_and_vars: if gv[0] is not None: if 'bn_weights' in gv[1].name: clipped_grads_and_vars.append([tf.mul(gv[0], tf.constant([1.00001]) ), gv[1]]) elif 'bn_biases' in gv[1].name: clipped_grads_and_vars.append([tf.mul(gv[0], tf.constant([1.00001]) ), gv[1]]) else: clipped_grads_and_vars.append([gv[0] , gv[1]]) train_op = optimizer.apply_gradients(clipped_grads_and_vars, global_step=batch) """ train_op = optimizer.apply_gradients(grads_and_vars, global_step=batch) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() train_loss=[] train_error= [] # Create a local session to run this computation. with tf.Session() as s: # Create a saver to store all the variables later saver = tf.train.Saver(tf.all_variables()) # Run all the initializers to prepare the trainable parameters. tf.initialize_all_variables().run() summary_writer = tf.train.SummaryWriter(TRAIN_MODEL_DIR, graph_def=s.graph_def) offset_old = 0 logging.info('Initialized!') ret = [] # Loop through training steps. num_steps = FLAGS.num_epochs * TRAIN_SIZE // FLAGS.batch_size session_start_time = time.time() for step in xrange(FLAGS.num_epochs * TRAIN_SIZE // FLAGS.batch_size): # Compute the offset of the current minibatch in the data. # Note that we could use better randomization across epochs. offset = (step * FLAGS.batch_size) % (TRAIN_SIZE - FLAGS.batch_size) batch_data = train_data[offset:(offset + FLAGS.batch_size), :, :, :] batch_labels = train_labels[offset:(offset + FLAGS.batch_size)] # This dictionary maps the batch data (as a np array) to the # node in the graph is should be fed to. feed_dict = {train_data_node: batch_data, train_labels_node: batch_labels} start_time = time.time() # Run the graph and fetch some of the nodes. # Remind : (train_op) is the most parameter here. Without it, Tensorflow will not do the backpropogation. ret.append( s.run( tf.get_collection('show')+[logits, train_op], feed_dict=feed_dict) ) duration = time.time() - start_time train_error.append( util.error_rate(ret[-1][-2], batch_labels )) if step % FLAGS.iter_print_train_info == (FLAGS.iter_print_train_info-1): # Print the training Information logging.info('Epoch %.2f, Step %d' % ((float(step) * FLAGS.batch_size / TRAIN_SIZE), step)) # Print the time information sec_per_batch = float(duration) remaining_sec = int(float((time.time() - session_start_time)/step) * float(num_steps - step)) remaining_time = str(datetime.timedelta(seconds=remaining_sec)) logging.info('%.3f sec/batch, remaining time = %s' %(sec_per_batch, remaining_time)) ret = np.array(ret) for idx, var in enumerate(tf.get_collection('show')): logging.info('Average (%s): %f' % (var.name, np.mean(ret[:, idx])) ) logging.info('Average Train error: %.2f%%' % np.mean(train_error)) train_error = [] ret = [] print('\n') sys.stdout.flush() if step % 100==99: # Save the summary information logging.info('Save the summary information') summary_str = s.run(summary_op, feed_dict) summary_writer.add_summary(summary_str, step) # Per Epoch if(offset < offset_old ): cur_epoch =np.round((float(step) * FLAGS.batch_size / TRAIN_SIZE)) cur_epoch = int(cur_epoch) logging.info('Epoch %d' % cur_epoch) # Randomize Data for Batch normalization logging.info('Reorder data order for Batch Normalization') rand_idx = np.random.permutation(len(train_labels)) train_data = train_data[rand_idx, :, :, :] train_labels = train_labels[rand_idx, :] # Horizontal mirroring logging.info('Randomly horizontal flip the Images') mir_idx = np.random.randint(2, size= len(train_labels)) mir_idx = np.nonzero(mir_idx > 0 )[0] for i in range(ds.num_channel()): train_data[mir_idx, :, :, i ] = train_data[mir_idx, :, ::-1, i] if((cur_epoch % 10) == 9): # Save Model checkpoint_path = os.path.join(TRAIN_MODEL_DIR, 'model.ckpt') logging.info('Save the model : %s'%(checkpoint_path)) saver.save(s, checkpoint_path, global_step=step) sys.stdout.flush() offset_old = offset # Save the last model logging.info('Save the model : %s'%(checkpoint_path)) saver.save(s, checkpoint_path, global_step=step)