def load_model(modelfile, config=None, args=None): if config.amp: from apex import amp if args.debug: print("Using apex amp") if modelfile == None: sys.stderr.write("No model file specified!") sys.exit(1) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if args.debug: print("Using device:", device) if args.arch is not None: model = network.network(config=config, arch=args.arch).to(device) else: model = network.network(config=config).to(device) if args.debug: print("Loading pretrained weights:", modelfile) state_dict = torch.load(modelfile)["state_dict"] if "state_dict" in state_dict: model.load_state_dict(convert_statedict(state_dict["state_dict"])) else: model.load_state_dict(torch.load(modelfile)["state_dict"]) if args.debug: print(model) model.eval() torch.set_grad_enabled(False) optimizer = torch.optim.Adam(model.parameters(), lr=config.lr) if config.amp: model, optimizer = amp.initialize(model, optimizer, opt_level="O1", verbosity=0) return model, device
def main(train_dir, batch_size, num_batches, log_dir, checkpoint_dir=None): if checkpoint_dir is None: checkpoint_dir = log_dir with tf.device('/cpu:0'): images, labels = build_input('cifar10', 100, 'test') images_cat_0, labels_cat_0 = build_input_cat_0('cifar10', 100, 'test') images_cat_1, labels_cat_1 = build_input_cat_1('cifar10', 100, 'test') predictions, total_loss, labels_cat2 = network(images, labels) predictions_cat_0, loss_0 = network(images_cat_0, labels_cat_0) predictions_cat_1, loss_1 = network(images_cat_1, labels_cat_1) tf.summary.scalar('loss', total_loss) tf.summary.scalar('loss_0', loss_0) tf.summary.scalar('loss_1', loss_1) predictions_cat_0 = tf.argmax(predictions_cat_0, axis=1) predictions_cat_1 = tf.argmax(predictions_cat_1, axis=1) predictions = tf.argmax(predictions, 1) tf.summary.scalar( 'accuracy_cat_0', slim.metrics.accuracy(predictions_cat_0, tf.to_int64(labels_cat_0))) tf.summary.scalar( 'accuracy_cat_1', slim.metrics.accuracy(predictions_cat_1, tf.to_int64(labels_cat_1))) tf.summary.scalar('accuracy_cat2', slim.metrics.accuracy(predictions, labels_cat2)) # These are streaming metrics which compute the "running" metric, # e.g running accuracy metrics_to_values, metrics_to_updates = slim.metrics.aggregate_metric_map( { 'accuracy_cat2': slim.metrics.streaming_accuracy(predictions, labels_cat2), 'accuracy_cat_0': slim.metrics.streaming_accuracy(predictions_cat_0, labels_cat_0), 'accuracy_cat_1': slim.metrics.streaming_accuracy(predictions_cat_1, labels_cat_1), }) # Define the streaming summaries to write: for metric_name, metric_value in metrics_to_values.items(): tf.summary.scalar(metric_name, metric_value) # Evaluate every 30 seconds slim.evaluation.evaluation_loop('', checkpoint_dir, log_dir, num_evals=num_batches, eval_op=list( metrics_to_updates.values()), summary_op=tf.summary.merge_all(), eval_interval_secs=60, max_number_of_evaluations=100000000)
def __init__(self, is_train=False): self.class_num = 5990 self.character_step = 8 self.character_height = 32 self.character_width = 32 #train self.train_tfrecords_name = "./make_tfrecords/data.tfrecords" self.summary_save_path = "./summary/" self.summary_steps = 10000 self.save_steps = 10 self.save_path = "./save/" #test: self.model_path = "./save/sliding_conv.ckpt-10" if is_train: self.batch_size = 16 self.with_clip = True self.network = network(batch_size=self.batch_size, class_num=self.class_num, character_height=self.character_height, character_width=self.character_width, character_step=self.character_step, is_train=True) else: current_path = os.path.dirname(os.path.abspath(__file__)) self.char_dict = self.create_char_dict( os.path.join(current_path, "char_std_5990.txt")) self.batch_size = 1 self.graph = tf.Graph() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0) self.session = tf.Session( config=tf.ConfigProto(gpu_options=gpu_options), graph=self.graph) with self.session.as_default(): with self.graph.as_default(): self.network = network( batch_size=self.batch_size, class_num=self.class_num, character_height=self.character_height, character_width=self.character_width, character_step=self.character_step, is_train=False) self.decoded, self.log_prob = tf.nn.ctc_greedy_decoder( self.network["outputs"], self.network["seq_len"], merge_repeated=True) init = tf.global_variables_initializer() self.session.run(init) saver = tf.train.Saver(tf.global_variables(), max_to_keep=100) saver.restore(self.session, self.model_path)
def __init__(self, state_size, action_size, lr=5e-4): self.action_size = action_size self.local_network = network(state_size, action_size) self.target_network = network(state_size, action_size) self.optimizer = opt.Adam(self.local_network.parameters(), lr=lr) self.memory = ReplayBuffer() self.eps = 1.0 self.decay = 0.99 self.eps_min = 0.01 self.update_every = 4 self.t_step = 1 self.gamma = 0.99 self.TAU = 1e-3
def train(model_name): #Get the lables from the .env file labels = env('LABELS') size = int(env('IMG_SIZE')) #create the train data (format the training images) train_data = create_train_data() tf.reset_default_graph() convnet = network(size, labels) model = tflearn.DNN(convnet, tensorboard_dir='log') #if the model already exists, load it so we are not training from scratch if os.path.exists('{}.meta'.format(model_name)): model.load(model_name) print('model loaded!') X = np.array([i[0] for i in train_data]).reshape(-1, size, size, 1) Y = [i[1] for i in train_data] model.fit(X, Y, n_epoch=50) #save the model in the models folder model.save('../models/' + model_name) print("here") print(type(model)) print("here") return model
def main(train_dir, batch_size, num_batches, log_dir, checkpoint_dir=None): if checkpoint_dir is None: checkpoint_dir = log_dir images, labels = build_input('cifar10', 100, 'test') predictions, total_loss = network(images, labels) tf.summary.scalar('loss', total_loss) predictions = tf.to_int32(tf.argmax(predictions, 1)) tf.summary.scalar('accuracy', slim.metrics.accuracy(predictions, labels)) # These are streaming metrics which compute the "running" metric, # e.g running accuracy metrics_to_values, metrics_to_updates = slim.metrics.aggregate_metric_map({ 'accuracy': slim.metrics.streaming_accuracy(predictions, labels), 'streaming_mse': slim.metrics.streaming_mean_squared_error(predictions, labels), }) # Define the streaming summaries to write: for metric_name, metric_value in metrics_to_values.items(): tf.summary.scalar(metric_name, metric_value) # Evaluate every 30 seconds slim.evaluation.evaluation_loop('', checkpoint_dir, log_dir, num_evals=num_batches, eval_op=list(metrics_to_updates.values()), summary_op=tf.summary.merge_all(), eval_interval_secs=20, max_number_of_evaluations=100000000)
def main(): data = CIFAR10(paths) data_loader = datautil.DataLoader(dataset=data, batch_size=512, num_workers=8, shuffle=True) epoch = 0 net = network().to(device) criterion = nn.CrossEntropyLoss().to(device) optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) running_loss = 0 for ep in tqdm(range(epoch, 50)): for i, (image, label) in tqdm(enumerate(data_loader)): optimizer.zero_grad() # print (repr(image)) output = net(image.to(device, dtype=torch.float32)) # print(label) loss = criterion(output, label.to(device)) loss.backward() optimizer.step() # running_loss += loss.item() if i % 2000 == 0: print(loss) pass torch.save(net.state_dict(), './model/model1.pt')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--epochs', default=100, type=int, help='epoch number') parser.add_argument('--start_epoch', default=0, type=int, help='start epoch number') parser.add_argument('-b', '--batch_size', default=4, type=int, help='mini-batch size') parser.add_argument('--lr', '--learning_rate', default=1e-4, type=float, help='initial learning rate') parser.add_argument('--weight-decay', default=0.0, type=float, help='weight decay') parser.add_argument('-c', '--continue', dest='continue_path', type=str, required=False) parser.add_argument('--exp_name', default=config.exp_name, type=str, required=False) parser.add_argument('--valid', action='store_true') parser.add_argument('--style_loss', action='store_true') args = parser.parse_args() print(args) config.exp_name = args.exp_name config.make_dir() save_args(args, config.log_dir) net = network() vgg = vgg_for_style_transfer() net = torch.nn.DataParallel(net).cuda() vgg = torch.nn.DataParallel(vgg).cuda() sess = Session(config, net=net) train_loader = get_dataloaders(os.path.join(config.data_dir, 'train.json'), batch_size=args.batch_size, shuffle=True) valid_loader = get_dataloaders(os.path.join(config.data_dir, 'val.json'), batch_size=args.batch_size, shuffle=True) if args.continue_path and os.path.exists(args.continue_path): sess.load_checkpoint(args.continue_path) clock = sess.clock tb_writer = sess.tb_writer criterion = nn.L1Loss().cuda() optimizer = optim.Adam(sess.net.parameters(), args.lr, weight_decay=args.weight_decay) scheduler = ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=10, verbose=True) for e in range(args.epochs): train_model(train_loader, sess.net, vgg, criterion, optimizer, clock.epoch, tb_writer) valid_out = valid_model(valid_loader, sess.net, vgg, criterion, optimizer, clock.epoch, tb_writer) tb_writer.add_scalar('train/learning_rate', optimizer.param_groups[-1]['lr'], clock.epoch) scheduler.step(valid_out['epoch_loss']) if valid_out['epoch_loss'] < sess.best_val_loss: sess.best_val_loss = valid_out['epoch_loss'] sess.save_checkpoint('best_model.pth.tar') if clock.epoch % 10 == 0: sess.save_checkpoint('epoch{}.pth.tar'.format(clock.epoch)) sess.save_checkpoint('latest.pth.tar') clock.tock()
def main(): tf.reset_default_graph() input_node = tf.placeholder(tf.float32, shape=(24, 24, 3)) #这个是你送入网络的图片大小,如果你是其他的大小自行修改 input_node = tf.expand_dims(input_node, 0) flow = network(input_node) flow = tf.cast(flow, tf.uint8, 'out') #设置输出类型以及输出的接口名字,为了之后的调用pb的时候使用 saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, model_path) #保存图 tf.train.write_graph(sess.graph_def, 'output_model/pb_model', 'model.pb') #把图和参数结构一起 freeze_graph.freeze_graph('output_model/pb_model/model.pb', '', False, model_path, 'out', 'save/restore_all', 'save/Const:0', 'output_model/pb_model/frozen_model.pb', False, "") print("done")
def test(model_name): labels = env('LABELS') size = int(env('IMG_SIZE')) #format the images that are uploaded test_upload = process_test_data(env('TEST_UPLOAD')) convnet = network(size, labels) model = tflearn.DNN(convnet, tensorboard_dir='log') #load the model for testing model.load('models/' + model_name) for num, data in enumerate(test_upload): img_num = data[1] img_data = data[0] orig = img_data data = img_data.reshape(size, size, 1) model_out = model.predict([data])[0] print(model_out) print(np.argmax(model_out)) str_label = labels[np.argmax(model_out)] #output as a text print(str_label)
def __init__(self, sess, s_size, a_size, scope, queues, trainer): self.queue = queues[0] self.param_queue = queues[1] self.replaymemory = ReplayMemory(100000) self.sess = sess self.learner_net = network(s_size, a_size, scope, 20) self.q = self.learner_net.q self.Q = self.learner_net.Q self.actions_q = tf.placeholder(shape=[None, a_size, N], dtype=tf.float32) self.q_target = tf.placeholder(shape=[None, N], dtype=tf.float32) self.ISWeights = tf.placeholder(shape=[None, N], dtype=tf.float32) self.q_actiona = tf.multiply(self.q, self.actions_q) self.q_action = tf.reduce_sum(self.q_actiona, axis=1) self.u = tf.abs(self.q_target - self.q_action) self.loss = tf.reduce_mean( tf.reduce_sum(tf.square(self.u) * self.ISWeights, axis=1)) self.local_vars = self.learner_net.local_vars #tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope) self.gradients = tf.gradients(self.loss, self.local_vars) #grads,self.grad_norms = tf.clip_by_norm(self.gradients,40.0) self.apply_grads = trainer.apply_gradients( zip(self.gradients, self.local_vars)) self.sess.run(tf.global_variables_initializer())
def test_input(self): batch_size = 1 # Mock input pipeline. mock_imgs = np.ones([batch_size, 32, 32, 1], dtype=np.float32) mock_edges = np.ones([batch_size, 32, 32, 1], dtype=np.float32) mock_interp = np.ones([batch_size, 32, 32, 1], dtype=np.float32) t_image = tf.placeholder(tf.float32, shape=(None, None, None, 1), name='t_input') t_interpolated = tf.placeholder(tf.float32, shape=(None, None, None, 1), name='t_input') t_edges = tf.placeholder(tf.float32, shape=(None, None, None, 1), name='t_input') d_flg = tf.placeholder(tf.bool, name='is_train') net_g = network(t_image, t_edges, is_train=d_flg) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) result = sess.run(net_g, feed_dict={ t_image: mock_imgs, t_interpolated: mock_interp, t_edges: mock_edges, d_flg: False }) print(result.shape)
def test(args, shared_model, inputs, labels, log): start_time = time.time() local_model = model.network(args, class_num=args.class_num, input_dim=inputs.shape[1]) local_model.load_state_dict(shared_model.state_dict()) if args.gpu: local_model = local_model.cuda() outputs = local_model(inputs) predict_labels = torch.argmax(outputs, dim=1) if args.gpu: predict_labels = predict_labels.cpu() predict_labels = predict_labels.numpy() cnt = 0 if args.gpu: labels = labels.cpu() labels = labels.numpy() for i in range(labels.shape[0]): if predict_labels[i] == labels[i]: cnt += 1 accuracy = cnt / labels.shape[0] f1 = f1_score(list(labels), list(predict_labels), average='macro') log.info('Test time ' + \ time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - start_time)) + \ ',f1: %0.4f\t accuracy: %0.4f' % (f1, 100* accuracy)) return accuracy, f1
def build_program(main_prog, startup_prog, args): image_shape = [int(m) for m in args.image_shape.split(",")] with fluid.program_guard(main_prog, startup_prog): with fluid.unique_name.guard(): image = fluid.data( name="image", shape=[None] + image_shape, dtype="float32") label = fluid.data(name="label", shape=[None, 1], dtype="int64") genotype = eval("genotypes.%s" % args.arch) drop_path_prob = '' drop_path_mask = '' do_drop_path = False logits, logits_aux = network( x=image, is_train=False, c_in=args.init_channels, num_classes=args.class_num, layers=args.layers, auxiliary=False, genotype=genotype, do_drop_path=do_drop_path, drop_prob=drop_path_prob, drop_path_mask=drop_path_mask, args=args, name='model') top1 = fluid.layers.accuracy(input=logits, label=label, k=1) top5 = fluid.layers.accuracy(input=logits, label=label, k=5) loss = fluid.layers.reduce_mean( fluid.layers.softmax_with_cross_entropy(logits, label)) outs = [loss, top1, top5] return outs
def load_check_point(train_id, path=None): """ """ if path is None: path = os.getcwd() param_fn = os.path.join(path, str(train_id) + '.param.npz') config_fn = os.path.join(path, str(train_id) + '.nnconfig.gz') params = joblib.load(config_fn) mdl, net = build(network(params), params) layers = L.get_all_layers(L.ConcatLayer(net.values(), axis=1)) if os.path.exists(param_fn): try: print('Loadong pre-trained weight...') with np.load(param_fn) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] L.set_all_param_values(layers, param_values) except Exception as e: print(e) print('Cannot load parameters!') else: print('Cannot find parameters!') return net, mdl, params
def main(): args = get_arguments() num_classes = dataset_settings[args.dataset]['num_classes'] input_size = dataset_settings[args.dataset]['input_size'] label = dataset_settings[args.dataset]['label'] model = network(num_classes=num_classes, pretrained=None).cuda() model = nn.DataParallel(model) state_dict = torch.load(args.restore_weight) model.load_state_dict(state_dict) model.eval() transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.406, 0.456, 0.485], std=[0.225, 0.224, 0.229]) ]) dataset = SCHPDataset(root=args.input, input_size=input_size, transform=transform) dataloader = DataLoader(dataset) if not os.path.exists(args.output): os.makedirs(args.output) palette = get_palette(num_classes) with torch.no_grad(): for idx, batch in enumerate(dataloader): image, meta = batch img_name = meta['name'][0] c = meta['center'].numpy()[0] s = meta['scale'].numpy()[0] w = meta['width'].numpy()[0] h = meta['height'].numpy()[0] output = model(image.cuda()) upsample = torch.nn.Upsample(size=input_size, mode='bilinear', align_corners=True) upsample_output = upsample(output) upsample_output = upsample_output.squeeze() upsample_output = upsample_output.permute(1, 2, 0) #CHW -> HWC logits_result = transform_logits(upsample_output.data.cpu().numpy(), c, s, w, h, input_size=input_size) parsing_result = np.argmax(logits_result, axis=2) parsing_result_path = os.path.join(args.output, img_name[:-4]+'.png') # plt.imshow(parsing_result) # plt.show() output_img = Image.fromarray(np.asarray(parsing_result, dtype=np.uint8)) output_img.putpalette(palette) output_img.save(parsing_result_path) if args.logits: #logits_result_path = os.path.join(args.output, img_name[:-4] + '.npy') np_result_path = os.path.join(args.output, img_name[:-4] + '.npy') #np.save(logits_result_path, logits_result) np.save(np_result_path, parsing_result) return
def main(_): config = tf.ConfigProto(device_count={'GPU': 0}) with tf.Session(config=config) as sess: model = network(sess, args) if args.phase == 'train': model.train(args) else: model.test(args)
def export(): tf.logging.set_verbosity(tf.logging.INFO) nn = network() inp = tf.placeholder(tf.float32, [None], name=INPUT_TENSOR_NAME) inp = tf.reshape(inp, [-1, CROPPED_IMAGE_SIZE, CROPPED_IMAGE_SIZE, 3]) td = dict(input=inp) nn.export_savedmodel( EXPORT_FOLDER, tf.estimator.export.build_raw_serving_input_receiver_fn(td))
def act(state, checkpoint): state = torch.from_numpy(state).float().unsqueeze(0) model = network(37, 4) model.load_state_dict(torch.load(checkpoint)) model.eval() with torch.no_grad(): action = model(state) action = np.argmax(action.numpy()) return action
def main(train_dir, batch_size, num_batches, log_dir): images, labels = build_input('cifar10', 100, 'train') predictions, total_loss = network(images, labels) tf.summary.scalar('loss', total_loss) optimizer = tf.train.GradientDescentOptimizer(0.1) train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) slim.learning.train(train_op, log_dir, save_summaries_secs=20, save_interval_secs=20)
def __init__(self, sess, env, name, s_size, a_size, queues): self.name = "worker_" + str(name) self.number = name self.sess = sess self.Actor_net = network(s_size, a_size, self.name) self.env = env self.queue = queues[0] self.param_queue = queues[1] self.sess.run(tf.global_variables_initializer())
def act(state, checkpoint, num_agents): actions = [] for i in range(num_agents): state_tensor = torch.from_numpy(state[i]).float() model = network(33, 4) model.load_state_dict(torch.load(checkpoint)) model.eval() with torch.no_grad(): action = model(state_tensor).data.numpy() actions.append(action) return actions
def save_weights_for_DeepSVDD(self, model, dataloader): """Initialize Deep SVDD weights using the encoder weights of the pretrained autoencoder.""" c = self.set_c(model, dataloader) net = network(self.args.latent_dim).to(self.device) state_dict = model.state_dict() net.load_state_dict(state_dict, strict=False) torch.save( { 'center': c.cpu().data.numpy().tolist(), 'net_dict': net.state_dict() }, 'weights/pretrained_parameters.pth')
def ex(net, learning_rate, split, epsilon, beta, dur, n_epochs, targets, batch_sz, shuffle, data_path, overlap_chunk=True, kernel_multiplier=1, train_id=None): """""" if train_id is None: train_id = uuid.uuid4() logger = tblog.Logger('runs/{}'.format(train_id)) params = { 'network': net, 'data_fn': os.path.join(data_path, 'train.h5'), 'scaler': SCALER_FN, 'split_fn': split, 'learning_rate': learning_rate, 'epsilon': epsilon, 'beta': beta, 'verbose': True, 'n_epochs': n_epochs, 'batch_sz': batch_sz, 'dur': dur, # frames 'overlap_chunk': True if overlap_chunk else False, 'kernel_multiplier': kernel_multiplier, 'report_every': 100, 'class_weight': False, 'prepare_submission': False, 'iter': 0 } params['targets'] = targets with h5py.File(params['data_fn']) as hf: # load split info split = joblib.load(params['split_fn']) params.update( {'split': {k: map(int, v) for k, v in split.iteritems()}}) # load class weight if needed if params['class_weight']: params.update( {'class_weight': get_class_weight(hf['y'][:])} ) mdl, net = build(network(params), params) train(mdl, hf, params, shuffle, logger) save_check_point(net, params, train_id, path='results/') f1, ll = evaluate(mdl, hf, params) if params['prepare_submission']: # predict test dataset and prepare submission test(mdl, hf, train_id, os.path.join(data_path, 'test.h5'), params) return train_id, f1, ll
def main(train_dir, batch_size, num_batches, log_dir): images, labels = build_input('cifar10', 100, 'train') images_cat_0, labels_cat_0 = build_input_cat_0('cifar10', 100, 'test') images_cat_1, labels_cat_1 = build_input_cat_1('cifar10', 100, 'test') predictions, total_loss, labels_cat2 = network(images, labels) predictions_cat_0, loss_0, labels_cat_0_ = network(images_cat_0, labels_cat_0) predictions_cat_1, loss_1, labels_cat_1_ = network(images_cat_1, labels_cat_1) report() tf.summary.scalar('loss', total_loss) tf.summary.scalar('loss_0', loss_0) tf.summary.scalar('loss_1', loss_1) predictions = tf.argmax(predictions, axis=1) predictions_cat_0 = tf.argmax(predictions_cat_0, axis=1) predictions_cat_1 = tf.argmax(predictions_cat_1, axis=1) tf.summary.scalar( 'accuracy', slim.metrics.accuracy(predictions, tf.to_int64(labels_cat2))) tf.summary.scalar( 'accuracy_cat_0', slim.metrics.accuracy(predictions_cat_0, tf.to_int64(labels_cat_0_))) tf.summary.scalar( 'accuracy_cat_1', slim.metrics.accuracy(predictions_cat_1, tf.to_int64(labels_cat_1_))) optimizer = tf.train.GradientDescentOptimizer(0.1) train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) slim.learning.train(train_op, log_dir, save_summaries_secs=20, save_interval_secs=20)
def build_program(main_prog, startup_prog, is_train, args): image_shape = [int(m) for m in args.image_shape.split(",")] with fluid.program_guard(main_prog, startup_prog): with fluid.unique_name.guard(): image = fluid.data(name="image", shape=[None] + image_shape, dtype="float32") label = fluid.data(name="label", shape=[None, 1], dtype="int64") data_loader = fluid.io.DataLoader.from_generator( feed_list=[image, label], capacity=64, use_double_buffer=True, iterable=True) genotype = eval("genotypes.%s" % args.arch) logits, logits_aux = network(x=image, is_train=is_train, c_in=args.init_channels, num_classes=args.class_num, layers=args.layers, auxiliary=args.auxiliary, genotype=genotype, name='model') top1 = fluid.layers.accuracy(input=logits, label=label, k=1) top5 = fluid.layers.accuracy(input=logits, label=label, k=5) loss = fluid.layers.reduce_mean( cross_entropy_label_smooth(logits, label, args.label_smooth)) if is_train: if args.auxiliary: loss_aux = fluid.layers.reduce_mean( cross_entropy_label_smooth(logits_aux, label, args.label_smooth)) loss = loss + args.auxiliary_weight * loss_aux step_per_epoch = int(args.trainset_num / args.batch_size) learning_rate = fluid.layers.exponential_decay( args.learning_rate, step_per_epoch, args.decay_rate, staircase=True) clip = fluid.clip.GradientClipByGlobalNorm( clip_norm=args.grad_clip) optimizer = fluid.optimizer.MomentumOptimizer( learning_rate, args.momentum, regularization=fluid.regularizer.L2DecayRegularizer( args.weight_decay), grad_clip=clip) optimizer.minimize(loss) outs = [loss, top1, top5, learning_rate] else: outs = [loss, top1, top5] return outs, data_loader
def test(test_dir, checkpoint_dir='./checkpoint_res50/'): # predict the result test_images = os.listdir(test_dir) features = tf.placeholder( "float32", shape=[None, CROP_SIZE, CROP_SIZE, IMAGE_CHANNEL], name="features") labels = tf.placeholder("float32", [None], name="labels") is_training = tf.placeholder("bool") one_hot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=80) net = model.network(NUM_CLASS, model_name, use_batch_norm=use_bn) logits = net.inference(inputs=features, is_training=is_training, reuse=False) values, indices = tf.nn.top_k(logits, 3) with tf.Session() as sess: saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: print('Restore the model from checkpoint %s' % ckpt.model_checkpoint_path) # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) start_step = int( ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) else: raise Exception('no checkpoint find') result = [] for test_image in test_images: temp_dict = {} x = scene_input.img_resize(os.path.join(test_dir, test_image), CROP_SIZE) indices_eval = sess.run(indices, feed_dict={ features: np.expand_dims(x, axis=0), is_training: False }) predictions = np.squeeze(indices_eval) temp_dict['image_id'] = test_image temp_dict['label_id'] = predictions.tolist() result.append(temp_dict) print('image %s is %d,%d,%d' % (test_image, predictions[0], predictions[1], predictions[2])) with open('submit.json', 'w') as f: json.dump(result, f) print('write result json, num is %d' % len(result))
def make_optimal_network(deep_load=False, load_black=True): _, routes = load_airport_and_route(deep_load=deep_load) netx = from_edgelist(routes) N = number_of_nodes(netx) net = network(N, graph=netx) optimal = optimal_distribution(deep_load=deep_load) R = B = [balls_per_node] * N if load_black: B = optimal else: R = optimal net.set_initial_distribution(R, B)
def show_results(network, features, labels, examples=4): figure, axis = plt.subplots(nrows=4, ncols=4, figsize=(15, 4 * examples)) dim1 = features.shape[0] for row in range(examples): img_idx = np.random.randint(dim1) image_array = network( mx.nd.array(features[img_idx:img_idx + 1], ctx=context).astype('float32')).squeeze(0).asnumpy() axis[row][0].imshow( np.transpose(features[img_idx], (1, 2, 0))[:, :, 0]) axis[row][1].imshow(np.transpose(image_array, (1, 2, 0))[:, :, 0]) axis[row][2].imshow(image_array.argmax(0)) axis[row][3].imshow(np.transpose(labels[img_idx], (1, 2, 0))[:, :, 0]) plt.show()
def test_build_graph(self): ###========================== DEFINE MODEL ============================### t_image = tf.placeholder(tf.float32, shape=(None, 32, 32, 1), name='t_input') t_interpolated = tf.placeholder(tf.float32, shape=(None, 32, 32, 1), name='t_input') t_edges = tf.placeholder(tf.float32, shape=(None, 32, 32, 1), name='t_input') d_flg = tf.placeholder(tf.bool, name='is_train') net_g = network(t_image, t_edges, t_interpolated, is_train=d_flg) print('out', net_g.shape)