def train(**kwargs): # train model opt.parse(kwargs) batch_size, validationRatio = opt.batch_size, opt.validationRatio LR, epoch, global_seed = opt.LR, opt.epoch, opt.global_seed vis.vis.env = opt.env fixSeed(opt.global_seed) min_loss = 10 model = models.SimpleNet() for i in range(epoch): print("Ensamble number:" + str(i)) #model = models.ResNetLike(BasicBlock, [1, 3, 3, 1], num_channels=2, num_classes=1) data, full_img = readSuffleData(opt.global_seed, opt.BASE_FOLDER) train_loader, val_loader, train_ds, val_ds = getTrainValLoaders( data, full_img, batch_size, num_workers, validationRatio) #train_loader, val_loader, train_ds, val_ds = getCustomTrainValLoaders(data,full_img,batch_size,num_workers,validationRatio,global_seed) model, val_result, train_result = generateSingleModel( model, train_loader, val_loader, train_ds, val_ds, LR, opt.global_epoches) # if min_loss<val_result: # LR = LR * 0.5 # else: # min_loss = val_result vis.plot('val_loss', val_result) vis.plot("train_loss", train_result) #print (model) df_pred = testModel(model) savePred(df_pred, val_result)
def get_feature_model(model_string, experiment_name, load_pretrained=False, opti=None, lr=None, mom=None, checkpoint_pretrained=None, learn_pos=False, force_on_cpu=False,cat=False): print('Loading feature model: {}, expname {}, pretrained? {}, checkpoint pretrained {}, learn pos? {} , cat? {}'.format(model_string, experiment_name,load_pretrained,checkpoint_pretrained,learn_pos,cat),flush=True) if model_string == 'vgg': vgg = models.vgg16(pretrained=False) vgg.load_state_dict(torch.load('/mnt/synology/breast/projects/lisa/koel/vgg16-397923af.pth')) model = m.ModifiedVGG(vgg, learn_pos=learn_pos) elif model_string == 'simple': model = m.SimpleNet(learn_pos=learn_pos, cat=cat) elif model_string == 'resnet' or model_string == 'resnet_pool': if learn_pos: num_classes = 3 elif cat: num_classes=5 else: num_classes = 1 model = res.get_resnet_classification(pretrained=True, num_classes=num_classes) # resnet = models.resnet18(pretrained=True) # model = m.ModifiedResNet(resnet, learn_pos=learn_pos) elif model_string == 'resnet_less': model = m.ModiefiedResNetLessFilter(learn_pos=learn_pos) elif model_string == 'fcresnet': if learn_pos: raise ValueError('Position learning not implemented for Fully Convolutional NN') resnet = models.resnet18(pretrained=True) normal_model = m.ModifiedResNet(resnet) if load_pretrained: normal_model = m.FCResNet(normal_model) optimizer = get_optimizer(normal_model.parameters(), opti, lr, mom) checkpoint_filename = os.path.join(checkpoint_pretrained, 'warmup_model_{}.pth.tar'.format(experiment_name)) if os.path.exists(checkpoint_filename): model, _, initial_epoch = load_checkpoint(normal_model, optimizer, filename=checkpoint_filename) else: raise ValueError('Checkpoint {} does not exist.'.format(os.path.abspath(checkpoint_filename))) if load_pretrained: model = m.FCResNet(model) elif model_string == 'auto': model = res.get_resnet_auto(given_model=None) if load_pretrained and not ( model_string == 'fcresnet' or model_string =='resnet_pool'): optimizer = get_optimizer(model.parameters(), opti, lr, mom) checkpoint_filename = os.path.join(checkpoint_pretrained, 'warmup_model_{}.pth.tar'.format(experiment_name)) if os.path.exists(checkpoint_filename): model, _, _ = load_checkpoint(model, optimizer, filename=checkpoint_filename) else: raise ValueError('Checkpoint {} does not exist.'.format(checkpoint_filename)) elif load_pretrained and model_string == 'resnet_pool': optimizer = get_optimizer(model.parameters(), opti, lr, mom) checkpoint_filename = os.path.join(checkpoint_pretrained, 'warmup_model_resnet.pth.tar') if os.path.exists(checkpoint_filename): model, _, _ = load_checkpoint(model, optimizer, filename=checkpoint_filename) else: raise ValueError('Checkpoint {} does not exist.'.format(checkpoint_filename)) if torch.cuda.is_available() and not force_on_cpu: print('Got model with cuda available', flush=True) model.cuda() return model
def main(): parser = argparse.ArgumentParser() parser.add_argument('env', type=str) parser.add_argument('--num_rollouts', type=int, default=20, help='Number of expert roll outs') parser.add_argument('--epochs', type=int, default=20, help='Number of epochs to train') parser.add_argument('--max_batches', type=int, default=10000, help='Max number of batches to train') args = parser.parse_args() print('training immitation...') data_path = os.path.join('data', args.env) file_name = 'train_{}.p'.format(args.num_rollouts) with open(os.path.join(data_path, file_name), 'rb') as f: expert_data = pickle.load(f) X = expert_data['observations'] Y = expert_data['actions'] assert len(Y.shape) == 2 print('number of training data:', len(X)) # for debug purpose # TODO: ask Alex about selu at output? env = gym.make(args.env) obs_shape = list(env.observation_space.shape) act_shape = list(env.action_space.shape) print(env.action_space.low, env.action_space.high) obs_ph = tf.placeholder(tf.float32, shape=[None] + obs_shape, name='obs_ph') act_ph = tf.placeholder(tf.float32, shape=[None] + act_shape, name='act_ph') net = models.SimpleNet() act_out, loss, train_step = net.CreateGraph(obs_ph, act_ph) tf.summary.scalar('losses/total_loss', loss) batch_size = 100 batch_index = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) tf_board = os.path.join('/tmp/gube/imitation', args.env) # [print(f) for f in glob.glob(os.path.join(tf_board, '*'))] # [os.remove(f) for f in glob.glob(os.path.join(tf_board, '*'))] writer = tf.summary.FileWriter( os.path.join(tf_board, str(int(time.time())))) writer.add_graph(sess.graph) merged_summary = tf.summary.merge_all() for i in range(args.epochs): x, y = shuffle(X, Y) print('Epoch {}'.format(i + 1)) offset = 0 while offset + batch_size <= len(x): start, end = offset, offset + batch_size sess.run(train_step, feed_dict={ obs_ph: x[start:end], act_ph: y[start:end] }) offset = end batch_index += 1 if batch_index >= args.max_batches: break s = sess.run(merged_summary, feed_dict={obs_ph: x, act_ph: y}) writer.add_summary(s, batch_index) print('Batch {}'.format(batch_index)) if batch_index >= args.max_batches: break saver = tf.train.Saver() saver.save(sess, os.path.join('policy/imitation', args.env, args.env))
use_cuda=True if torch.cuda.is_available() else False device=torch.device('cuda:0') if use_cuda else torch.device('cpu') classifier = models.ClassifierPro() classifier2 = models.ClassifierPro() #encoder = models.Encoder() encoder = tmodels.resnet50(pretrained=True) #encoder = tmodels.inception_v3(pretrained=True) #encoder.aux_logits=False encoder.fc = nn.Sequential() discriminator = models.DCDPro() #discriminator = models.DCDPro(input_features=128) ssnet = models.SimpleNet(2048, 64) classifier.to(device) encoder.to(device) #classifier2.to(device) #encoder2.to(device) discriminator.to(device) ssnet.to(device) loss_fn=torch.nn.CrossEntropyLoss() loss_fn2 = ContrastiveLoss() ##quitar loss_fn3 = SpecLoss() ##quitar # ----------------------------------------------------------------------------- ## etapa 1: entrenar g y h print("||||| Stage 1 |||||") optimizer=torch.optim.Adam(list(encoder.parameters())+list(classifier.parameters()), lr = 0.0001)
#datadir = '/home/villacis/Desktop/a4-code-v2-updated/minidigits' datadir = '/home/villacis/Desktop/villacis/datasets/plantclef_superminida_17_20_split' #datadir = '/home/villacis/Desktop/villacis/datasets/plantclef_minida_cropped' num_epochs1 = 40 num_epochs2 = 100 num_epochs3 = 2500000 use_cuda = True if torch.cuda.is_available() else False device = torch.device('cuda:0') if use_cuda else torch.device('cpu') classifier = models.ClassifierPro() #encoder = models.Encoder() encoder = tmodels.resnet18(pretrained=True) num_ftrs = encoder.fc.in_features ssnet = models.SimpleNet(num_ftrs, 4) #encoder = tmodels.inception_v3(pretrained=True) #encoder.aux_logits=False encoder.fc = nn.Sequential() discriminator = models.DCDPro() #discriminator = models.DCDPro(input_features=128) classifier.to(device) encoder.to(device) discriminator.to(device) ssnet = ssnet.to(device) loss_fn = torch.nn.CrossEntropyLoss() loss_fn2 = ContrastiveLoss() ##quitar loss_fn3 = SpecLoss() ##quitar # -----------------------------------------------------------------------------
def main(): parser = argparse.ArgumentParser() parser.add_argument('env', type=str) parser.add_argument('--num_rollouts', type=int, default=20, help='Number of roll outs') parser.add_argument('--max_steps', type=int, default=10000, help='Max number of steps per roll out') parser.add_argument('--num_experts', type=int, default=1, help='Number of expert roll outs') parser.add_argument('--epochs_per_rollout', type=int, default=5, help='Number of epochs to train for every roll out') parser.add_argument('--max_batches', type=int, default=10000, help='Max number of batches to train') args = parser.parse_args() print('training DAgger...') # set up env = gym.make(args.env) obs_shape = list(env.observation_space.shape) act_shape = list(env.action_space.shape) assert len(act_shape) == 1 print(env.action_space.low, env.action_space.high) max_steps = args.max_steps or env.spec.timestep_limit policy_module = importlib.import_module("policy.expert") expert = policy_module.get_policy(args.env) obs_ph = tf.placeholder(tf.float32, shape=[None] + obs_shape, name='obs_ph') act_ph = tf.placeholder(tf.float32, shape=[None] + act_shape, name='act_ph') net = models.SimpleNet() act_out, loss, train_step = net.CreateGraph(obs_ph, act_ph) tf.summary.scalar('losses/total_loss', loss) batch_size = 100 batch_index = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) tf_board = os.path.join('/tmp/gube/dagger', args.env) # [os.remove(f) for f in glob.glob(os.path.join(tf_board, '*'))] writer = tf.summary.FileWriter( os.path.join(tf_board, str(int(time.time())))) writer.add_graph(sess.graph) merged_summary = tf.summary.merge_all() X = np.empty([0] + obs_shape) # float Y = np.empty([0] + act_shape) # float returns = [] for i in range(args.num_rollouts): print('rollout', i) obs = env.reset() done = False totalr = 0. steps = 0 while not done: # obs[None, :] = [obs], adding 1 to the shape policy_action = sess.run(act_out, feed_dict={obs_ph: obs[None, :]})[0] expert_action = expert.act(obs) X = np.vstack((X, obs)) Y = np.vstack((Y, expert_action)) if i < args.num_experts: obs, r, done, _ = env.step(expert_action) else: obs, r, done, _ = env.step(policy_action) totalr += r steps += 1 if steps % 100 == 0: print("%i/%i" % (steps, max_steps)) if steps >= max_steps: break returns.append(totalr) for j in range(args.epochs_per_rollout): x, y = shuffle(X, Y) offset = 0 while offset + batch_size <= len(x): start, end = offset, offset + batch_size sess.run(train_step, feed_dict={ obs_ph: x[start:end], act_ph: y[start:end] }) offset = end batch_index += 1 if batch_index >= args.max_batches: break s = sess.run(merged_summary, feed_dict={obs_ph: x, act_ph: y}) writer.add_summary(s, batch_index) if batch_index >= args.max_batches: break print('batch', batch_index) print('returns', returns) print('mean return', np.mean(returns)) print('std of return', np.std(returns)) saver = tf.train.Saver() saver.save(sess, os.path.join('policy/dagger', args.env, args.env))