def train(model_conf:ModelConf, train_set, validation_set=None): """ train model for fashion mnist :param model_conf: :param train_set: :param validation_set: :return: """ model_conf.print_conf() images_placeholder = tf.placeholder(dtype=tf.float32, shape=[None, model_conf.HEIGHT*model_conf.WIDTH]) labels_placeholder = tf.placeholder(dtype=tf.int64, shape=[None]) model = DenseNet(model_conf, is_training=True, images=images_placeholder, labels=labels_placeholder) model.build_graph() summary_op = tf.summary.merge_all() saver = tf.train.Saver(tf.all_variables()) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) summary_writer = tf.summary.FileWriter(model_conf.SUMMARY_DIR, graph=sess.graph) for step in range(model_conf.NUM_STEPS): train_images, train_labels = train_set.next_batch() feed_dict = {images_placeholder: train_images, labels_placeholder: train_labels } start_time = time.time() [loss_value, acc_value, _, lr_value] = sess.run([model.loss, model.acc, model.train_op, model.learning_rate], feed_dict=feed_dict) duration = time.time() - start_time if step % 10 == 0: _print_log('training', step, loss_value, acc_value, lr_value, duration) if step % 100 == 0: summary_str = sess.run(summary_op, feed_dict=feed_dict) summary_writer.add_summary(summary_str, step) if step % 500 == 0: validation_images = validation_set.images validation_labels = validation_set.labels feed_dict = { images_placeholder: validation_images, labels_placeholder: validation_labels } [loss_value, acc_value] = sess.run([model.loss, model.acc], feed_dict=feed_dict) logger.info('(validation)loss: %f, acc: %f' % (loss_value, acc_value)) if (step != 0 and step % 10000 == 0) or step + 1 == model_conf.NUM_STEPS: checkpoint_path = os.path.join(model_conf.MODEL_SAVER_DIR, 'model.ckpt') saver.save(sess, checkpoint_path, step)
def test(model_conf: ModelConf, test_set): """ test the performance of trained model :param model_conf: :param test_set: :return: """ images_placeholder = tf.placeholder(dtype=tf.float32, shape=[None, model_conf.HEIGHT*model_conf.WIDTH]) labels_placeholder = tf.placeholder(dtype=tf.int64, shape=[model_conf.BATCH_SIZE]) model = DenseNet(model_conf, is_training=False, images=images_placeholder, labels=labels_placeholder) model.build_graph() saver = tf.train.Saver() sess = tf.Session() try: ckpt_state = tf.train.get_checkpoint_state(model_conf.MODEL_SAVER_DIR) except tf.errors.OutOfRangeError as e: logger.info('Can not restore checkpoint %s', e) return if not (ckpt_state and ckpt_state.model_checkpoint_path): logger.info('No model to eval yet at %s', model_conf.MODEL_SAVER_DIR) return logger.info('Loading checkpoint %s', ckpt_state.model_checkpoint_path) saver.restore(sess, ckpt_state.model_checkpoint_path) num_step = int(test_set.num_examples/model_conf.BATCH_SIZE)+1 num_correct = 0 for step in range(num_step): test_images, test_labels = test_set.next_batch() feed_dict = {images_placeholder: test_images, labels_placeholder:test_labels} [loss, prediction, acc] = sess.run([model.loss, model.prediction, model.acc], feed_dict=feed_dict) num_correct += np.sum(np.argmax(prediction, axis=1) == test_labels) logger.info("(Test)batch %d, loss=%f, acc=%f" % (step, loss, acc)) precision = num_correct*1.0/(num_step*model_conf.BATCH_SIZE) logger.info("Total precision: %f" % precision)
args.bc_mode = True model_params = vars(args) if not args.train and not args.test: print("You should train or test your network. Please check params.") exit() # some default params dataset/architecture related train_params = get_train_params_by_name(args.dataset) print("Params:") for k, v in model_params.items(): print("\t%s: %s" % (k, v)) print("Train params:") for k, v in train_params.items(): print("\t%s: %s" % (k, v)) print("Prepare training data...") data_provider = get_data_provider_by_name(args.dataset, train_params) print("Initialize the model..") model = DenseNet(data_provider=data_provider, **model_params) if args.train: print("Data provider train images: ", data_provider.train.num_examples) model.train_all_epochs(train_params) if args.test: if not args.train: model.load_model() print("Data provider test images: ", data_provider.test.num_examples) print("Testing...") loss, accuracy = model.test(data_provider.test, batch_size=200) print("mean cross_entropy: %f, mean accuracy: %f" % (loss, accuracy))
print('Run config:') for k, v in run_config.get_config().items(): print('\t%s: %s' % (k, v)) print('Network config:') for k, v in net_config.items(): print('\t%s: %s' % (k, v)) print('Prepare training data...') data_provider = get_data_provider_by_name(run_config.dataset, run_config.get_config()) # set net config net_config = DenseNetConfig().set_standard_dense_net( data_provider=data_provider, **net_config) print('Initialize the model...') model = DenseNet(args.path, data_provider, run_config, net_config) # save configs if args.save_config: model.save_config(args.path) if args.load_model: model.load_model() if args.test: # test print('Data provider test images: ', data_provider.test.num_examples) print('Testing...') loss, accuracy = model.test(data_provider.test, batch_size=200) print('mean cross_entropy: %f, mean accuracy: %f' % (loss, accuracy)) json.dump({ 'test_loss': '%s' % loss, 'test_acc': '%s' % accuracy
if not args.train and not args.test: print("You should train or test your network. Please check params.") exit() # some default params dataset/architecture related train_params = train_params_set print("Params:") for k, v in model_params.items(): print("\t%s: %s" % (k, v)) print("Train params:") for k, v in train_params.items(): print("\t%s: %s" % (k, v)) print("Prepare training data...") data_provider = DataProvider() print("Initialize the model..") model = DenseNet(data_provider=data_provider, **model_params) if args.train: # print("Data provider train images: ", np.shape(data_provider.train_imgs)) print("Data provider train images: ") model.train_all_epochs(train_params) if args.test: # model.load_model() # print("Data provider test images: ", np.shape(data_provider.test_imgs)) print("Testing...") model.test() # model.test(data_provider, batch_size=10) # loss, accuracy = model.test(data_provider, batch_size=200) # print("mean cross_entropy: %f, mean accuracy: %f" % (loss, accuracy))
def extract_feature_batch(): args = parse_opts() model_params = vars(args) batch_size = args.batch_size print("Initialize the model..") # fake data_provider DataProvider = collections.namedtuple('DataProvider', ['data_shape', 'n_classes']) data_provider = DataProvider(data_shape=(img_size, img_size, 1), n_classes=10) model = DenseNet(data_provider=data_provider, **model_params) end_points = model.end_points # for key, value in end_points.iteritems(): # print(key, value.get_shape().as_list()) # restore model model.saver.restore(model.sess, args.model_path) print("Successfully load model from model path: %s" % args.model_path) video_names = [x for x in os.listdir(args.face_dir)] video_names.sort() avg_num_imgs = 0 for vid, video_name in enumerate(video_names): video_dir = os.path.join(args.face_dir, video_name) img_paths = os.listdir(video_dir) if len(img_paths) == 0: continue output_subdir = os.path.join(args.outft_dir, video_name) if os.path.exists(output_subdir): continue else: os.makedirs(output_subdir) img_paths.sort(key=lambda x:int(x.split('.')[0])) avg_num_imgs += len(img_paths) imgs = [] for img_path in img_paths: img_path = os.path.join(video_dir, img_path) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.resize(img, (img_size, img_size)) imgs.append(img) imgs = (np.array(imgs, np.float32) - images_mean) / images_std imgs = np.expand_dims(imgs, 3) # pool4.shape=(batch_size, 4, 4, 256) # fc5.shape=fc6.shape=(batch_size, 1, 1, 512) # prob.shape=(batch_size, num_classes) fcs, probs = [], [] for i in xrange(0, imgs.shape[0], batch_size): feed_dict = { model.images: imgs[i: i + batch_size], model.is_training: False } fc, prob = model.sess.run( [end_points['fc'], end_points['preds']], feed_dict=feed_dict) # prev_last_pools.extend(prev_last_pool) fcs.extend(fc) probs.extend(prob) # prev_last_pools = np.array(prev_last_pools, np.float32) fcs = np.array(fcs, np.float32) probs = np.array(probs, np.float32) # with open(os.path.join(output_subdir, 'pool.npy'), 'wb') as f: # np.save(f, prev_last_pools) with open(os.path.join(output_subdir, 'fc.npy'), 'wb') as f: np.save(f, fcs) with open(os.path.join(output_subdir, 'prob.npy'), 'wb') as f: np.save(f, probs) print(vid, video_name, len(img_paths), fcs.shape, probs.shape) avg_num_imgs /= float(len(video_names)) print('average faces per video', avg_num_imgs)
torch_device = torch.device("cuda") # load images that you want to visualize log_path = save_path + '/TP.txt' # For example, TP.txt file contains name of image files manully selected from # the test set, which are classified as true positive images. # ex) 20080169_1_1-0-74-16.13539388 f = open(log_path, 'r') lines = f.readlines() lines = np.array(lines) lines = np.char.rstrip(lines) model = DenseNet(growthRate=12, depth=40, reduction=0.5, bottleneck=True, nClasses=2) model = nn.DataParallel(model).to(torch_device) model.load_state_dict(ckpoint['network']) # by changing 'hooks', you can choose the layer which you want to visualize # to check name of layers, use model.module to print out the whole structure of the neural net grad_cam = GradCam(model=model.module, hooks=["dense2"], device=torch_device) test_path = "/home/intern/osteo_classification/data/test/" test_loader = loader(test_path, 1, transform=None, sampler='',
print("You should train or test your network. Please check arguments.") exit() # Get model params (the arguments) and train params (depend on dataset). model_params = vars(args) train_params = get_train_params_by_name(args.dataset) print("\nModel parameters (specified as arguments):") for k, v in model_params.items(): print("\t%s: %s" % (k, v)) print("Train parameters (depend on specified dataset):") for k, v in train_params.items(): print("\t%s: %s" % (k, v)) # Train and/or test the specified model. print("\nPrepare training data...") data_provider = get_data_provider_by_name(args.dataset, train_params) print("Initialize the model...") model = DenseNet(data_provider=data_provider, **model_params) if args.train: print("Data provider train images: ", data_provider.train.num_examples) model.train_all_epochs(train_params) if args.test: if not args.train: model.load_model() print("Data provider test images: ", data_provider.test.num_examples) print("Testing...") loss, accuracy = model.test(data_provider.test, batch_size=200) model.print_pertinent_features(loss, accuracy, -1, True) print("mean cross_entropy: %f, mean accuracy: %f" % (loss[-1], accuracy))
print("You should train or test your network. Please check params.") exit() # some default params dataset/architecture related train_params = get_train_params_by_name(args.dataset) print("Params:") for k, v in model_params.items(): print("\t%s: %s" % (k, v)) print("Train params:") for k, v in train_params.items(): print("\t%s: %s" % (k, v)) print("Prepare training data...") data_provider = get_data_provider_by_name(args.dataset, train_params) print("Initialize the model..") model = DenseNet(data_provider=data_provider, **model_params) model_path = 'saves/%s_growth_rate=%d_depth=%d_dataset_%s/' % ( args.model_type, args.growth_rate, args.depth, args.dataset) print(model_path) if args.train: current_step = model.load_model(model_path) print("Data provider train images: ", data_provider.train.num_examples) model.train_all_epochs(train_params, args.epoch, current_step) if args.test: if not args.train: model.load_model(model_path) print("Data provider test images: ", data_provider.test.num_examples) print("Testing...") loss, accuracy = model.test(data_provider.test, batch_size=200) print("mean cross_entropy: %f, mean accuracy: %f" % (loss, accuracy))
def main(unused_argv): # Load training and eval data # export_cifar('/backups/datasets/cifar-10-python.tar.gz', '/backups/work/CIFAR10') imagenet = ImageNet('/backups/work/ILSVRC2017/ILSVRC', shuffle=True, normalize=True, augment=False, one_hot=False, batch_size=128) # imagenet = CIFAR('/backups/work/CIFAR10', # shuffle=True, normalize=True, augment=True, one_hot=False, batch_size=32) densenet = DenseNet(num_classes=imagenet.num_classes, growth_rate=12, bc_mode=True, block_config=(6, 12, 24, 16), dropout_rate=0.2, reduction=0.5, weight_decay=1e-4, nesterov_momentum=0.9) def train_input_fn(learning_rate): dataset = imagenet.train_set dataset = dataset.repeat(1) dataset = dataset.skip(imagenet.train_set_size % imagenet.batch_size) iterator = dataset.make_one_shot_iterator() features, labels = iterator.get_next() return {'images': features, 'learning_rate': learning_rate}, labels def eval_input_fn(): dataset = imagenet.val_set dataset = dataset.repeat(1) iterator = dataset.make_one_shot_iterator() features, labels = iterator.get_next() return {'images': features}, labels # Create the Estimator sess_config = tf.ConfigProto() sess_config.gpu_options.allow_growth = True config = tf.estimator.RunConfig().replace(session_config=sess_config) classifier = tf.estimator.Estimator( model_fn=densenet.imagenet_model_fn, model_dir="/backups/work/logs/imagenet_model1", # params={'image_shape': imagenet.image_shape}, config=config) # Set up logging # tensors_to_log = {"accuracy": "Accuracy/train_accuracy"} # logging_hook = tf.train.LoggingTensorHook( # tensors=tensors_to_log, every_n_iter=100) # debug_hook = tfdbg.LocalCLIDebugHook() # debug_hook.add_tensor_filter("has_inf_or_nan", tfdbg.has_inf_or_nan) # # Train the model for i in range(30): # Train the model classifier.train(input_fn=lambda: train_input_fn(learning_rate=0.1)) # Evaluate the model and print results eval_results = classifier.evaluate(input_fn=eval_input_fn) print(eval_results) for i in range(30): # Train the model classifier.train(input_fn=lambda: train_input_fn(learning_rate=0.01)) # Evaluate the model and print results eval_results = classifier.evaluate(input_fn=eval_input_fn) print(eval_results) for i in range(30): # Train the model classifier.train(input_fn=lambda: train_input_fn(learning_rate=0.001)) # Evaluate the model and print results eval_results = classifier.evaluate(input_fn=eval_input_fn) print(eval_results) for i in range(10): # Train the model classifier.train(input_fn=lambda: train_input_fn(learning_rate=0.0001)) # Evaluate the model and print results eval_results = classifier.evaluate(input_fn=eval_input_fn) print(eval_results)
# some default params dataset/architecture related train_params = get_train_params_by_name(args.dataset) print("Params:") for k, v in model_params.items(): print("\t%s: %s" % (k, v)) print("Train params:") for k, v in train_params.items(): print("\t%s: %s" % (k, v)) print("Prepare training data...") data_provider = get_data_provider_by_name(args.dataset, train_params) print("Initialize the model..") model = DenseNet(for_test_only=False, init_variables=None, init_global=None, bottleneck_output_size=None, first_output_features=None, data_provider=data_provider, **model_params) if args.train: print("Data provider train images: ", data_provider.train.num_examples) model.train_all_epochs(train_params) if args.comprese: if not args.train: model.load_model() old_varaible = model.get_trainable_variables_value() old_param_num = model.total_parameters print("Commpresing the network") comprese_model = CompreseDenseNet(model, args.clusster_num) all_new_comprese_kernels, all_new_bottleneck_kernels, all_new_batch_norm, all_new_transion_kernels, all_new_batch_norm_for_transion, new_W, new_transion_to_class_batch_norm = comprese_model.comprese( )
drop_last=True, late_fusion=arg.late_fuse) # the default loss function is the cross entropy loss class_loss = nn.CrossEntropyLoss() if arg.late_fuse: net = LateFuse_DenseNet(growthRate=arg.growthRate, depth=arg.depth, reduction=0.5, bottleneck=True, nClasses=2) net = nn.DataParallel(net).to(torch_device) model = LateFuseClassifyTrainer(arg, net, torch_device, class_loss=class_loss) else: net = DenseNet(growthRate=arg.growthRate, depth=arg.depth, reduction=0.5, bottleneck=True, nClasses=2) net = nn.DataParallel(net).to(torch_device) model = ClassifyTrainer(arg, ent, torch_device, class_loss=class_loss) if arg.test is False: model.train(train_loader, val_loader) model.test(test_loader, val_loader)