def testing(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: ssd_model = ssd300.SSD300(sess, False, class_size=class_size, graph_config=graph_config) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.trainable_variables()) if os.path.exists('./session_params/session.ckpt.index'): saver.restore(sess, './session_params/session.ckpt') image, actual, file_list = next(dataset.next_test()) pred_class, pred_class_val, pred_location = ssd_model.run( image, None, file_list) print('file_list:' + str(file_list)) # for index, act in zip(range(len(image)), actual): for a in act: print('【img-' + str(index) + ' actual】:' + str(a)) print('pred_class:' + str(pred_class[index])) print('pred_class_val:' + str(pred_class_val[index])) print('pred_location:' + str(pred_location[index])) else: print('No Data Exists!') sess.close()
def training(): batch_size = FLAGS.batch_size running_count = 0 config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: ssd_model = ssd300.SSD300(sess, True) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.trainable_variables()) if FLAGS.finetune: print('Finetune from %s' % FLAGS.finetune) ckpt = tf.train.get_checkpoint_state(FLAGS.finetune) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Failed to load model.') print('-------------------------') print('\nStart Training') min_loss_location = 100000. min_loss_class = 100000. while ((min_loss_location + min_loss_class) > 0.001 and running_count < FLAGS.max_steps): running_count += 1 train_data, actual_data, _ = get_traindata(batch_size) if len(train_data) > 0: loss_all, loss_class, loss_location, pred_class, pred_location = ssd_model.run( train_data, actual_data) l = np.sum(loss_location) c = np.sum(loss_class) if min_loss_location > l: min_loss_location = l if min_loss_class > c: min_loss_class = c if running_count % FLAGS.log_frequency == 0: # print('Step: %d | Loss All: %.4f(min) %.4f | Location: %.4f | Class: %.4f | pred_class: %.4f(all) %.4f(max) %.4f(min) | pred_location: %.4f(all) %.4f(max) %.4f(min)' # %(running_count, min_loss_location + min_loss_class, loss_all, np.sum(loss_location), np.sum(loss_class), np.sum(pred_class), np.amax(pred_class), np.min(pred_class), np.sum(pred_location), np.amax(pred_location), np.min(pred_location))) print( 'Step: %d | Loss All: %.4f(min) %.4f | Location: %.4f | Class: %.4f' % (running_count, min_loss_location + min_loss_class, loss_all, np.sum(loss_location), np.sum(loss_class))) # 定期保存ckpt if running_count % 1000 == 0: saver.save(sess, FLAGS.model_dir + 'session.ckpt') print('session.ckpt has been saved.') gc.collect() else: print('No Data Exists!') break saver.save(sess, FLAGS.model_dir + 'session.ckpt') sess.close() gc.collect() print('End Training')
def training(): batch_size = 15 running_count = 0 gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: ssd_model = ssd300.SSD300(sess, True) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.trainable_variables()) if os.path.exists('./session_params/session.ckpt.index'): print('\nStart Restore') saver.restore(sess, './session_params/session.ckpt') print('\nEnd Restore') print('\nStart Training') min_loss_location = 100000. min_loss_class = 100000. while ((min_loss_location + min_loss_class) > 0.001 and running_count < 100000): running_count += 1 train_data, actual_data, _ = get_traindata_voc2007(batch_size) if len(train_data) > 0: loss_all, loss_class, loss_location, pred_class, pred_location = ssd_model.run( train_data, actual_data) l = np.sum(loss_location) c = np.sum(loss_class) if min_loss_location > l: min_loss_location = l if min_loss_class > c: min_loss_class = c print('Running:【' + str(running_count) + '】|Loss All:【' + str(min_loss_location + min_loss_class) + '|' + str(loss_all) + '】|Location:【' + str(np.sum(loss_location)) + '】|Class:【' + str(np.sum(loss_class)) + '】|pred_class:【' + str(np.sum(pred_class)) + '|' + str(np.amax(pred_class)) + '|' + str(np.min(pred_class)) + '】|pred_location:【' + str(np.sum(pred_location)) + '|' + str(np.amax(pred_location)) + '|' + str(np.min(pred_location)) + '】') # 定期保存ckpt if running_count % 100 == 0: saver.save(sess, './session_params/session.ckpt') print('session.ckpt has been saved.') gc.collect() else: print('No Data Exists!') break saver.save(sess, './session_params/session.ckpt') sess.close() gc.collect() print('End Training')
def testing(): config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: ssd_model = ssd300.SSD300(sess, False) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.trainable_variables()) if os.path.exists(FLAGS.model_dir + 'session.ckpt.index'): saver.restore(sess, FLAGS.model_dir + 'session.ckpt') img_path, image_data, actual, file_list = get_traindata(1, False) pred_class, pred_class_val, pred_location = ssd_model.run( image_data, None) print('file_list:' + str(file_list)) image = cv2.imread(img_path) height = image.shape[0] width = image.shape[1] for index, act in zip(range(len(image_data)), actual): for a in act: print('【img-' + str(index) + ' actual】:' + str(a)) print('pred_class:' + str(pred_class[index])) print('pred_class_val:' + str(pred_class_val[index])) print('pred_location:' + str(pred_location[index])) for i in range(len(pred_location[index])): w = int(pred_location[index][i].tolist()[2] * width) h = int(pred_location[index][i].tolist()[3] * height) x = max( 0, int(pred_location[index][i].tolist()[0] * width - w / 2)) y = max( 0, int(pred_location[index][i].tolist()[1] * height - h / 2)) cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.putText(image, lable_arr[pred_class[index][i]], (x + 3, y + 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 2) else: print('No Data Exists!') sess.close() cv2.imshow("image", image) cv2.waitKey() cv2.destroyAllWindows()